From 451d9d84932996a452a686c5a0fc5e3b909ef90a Mon Sep 17 00:00:00 2001 From: KtorZ Date: Sun, 26 Feb 2023 11:26:52 +0100 Subject: [PATCH] Add new acceptance test scenario: 068 Stack overflow happening during code generation. --- examples/acceptance_tests/068/aiken.lock | 13 +++ examples/acceptance_tests/068/aiken.toml | 8 ++ examples/acceptance_tests/068/lib/tests.ak | 110 +++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 examples/acceptance_tests/068/aiken.lock create mode 100644 examples/acceptance_tests/068/aiken.toml create mode 100644 examples/acceptance_tests/068/lib/tests.ak diff --git a/examples/acceptance_tests/068/aiken.lock b/examples/acceptance_tests/068/aiken.lock new file mode 100644 index 00000000..0423f31b --- /dev/null +++ b/examples/acceptance_tests/068/aiken.lock @@ -0,0 +1,13 @@ +# This file was generated by Aiken +# You typically do not need to edit this file + +[[requirements]] +name = "aiken-lang/stdlib" +version = "main" +source = "github" + +[[packages]] +name = "aiken-lang/stdlib" +version = "main" +requirements = [] +source = "github" diff --git a/examples/acceptance_tests/068/aiken.toml b/examples/acceptance_tests/068/aiken.toml new file mode 100644 index 00000000..10e139d3 --- /dev/null +++ b/examples/acceptance_tests/068/aiken.toml @@ -0,0 +1,8 @@ +name = "aiken-lang/acceptance_test_068" +version = '0.0.0' +description = '' + +[[dependencies]] +name = 'aiken-lang/stdlib' +version = 'main' +source = 'github' diff --git a/examples/acceptance_tests/068/lib/tests.ak b/examples/acceptance_tests/068/lib/tests.ak new file mode 100644 index 00000000..9a6548e7 --- /dev/null +++ b/examples/acceptance_tests/068/lib/tests.ak @@ -0,0 +1,110 @@ +use aiken/bytearray +use aiken/dict.{Dict} +use aiken/hash.{Blake2b_224, Hash} +use aiken/transaction/credential.{Script} + +pub type PolicyId = + Hash + +pub type AssetName = + ByteArray + +pub opaque type Value { + inner: Dict>, +} + +pub fn zero() -> Value { + Value { inner: dict.new() } +} + +pub fn from_asset( + policy_id: PolicyId, + asset_name: AssetName, + quantity: Int, +) -> Value { + let asset = + dict.new() + |> dict.insert(asset_name, quantity, bytearray.compare) + dict.new() + |> dict.insert(policy_id, asset, bytearray.compare) + |> Value +} + +pub fn add(left v0: Value, right v1: Value) -> Value { + dict.union_with( + v0.inner, + v1.inner, + fn(_, a0, a1) { + let result = + dict.union_with( + a0, + a1, + fn(_, q0, q1) { + let q = q0 + q1 + if q == 0 { + None + } else { + Some(q) + } + }, + bytearray.compare, + ) + + if dict.is_empty(result) { + None + } else { + Some(result) + } + }, + bytearray.compare, + ) + |> Value +} + +/// Flatten a value as a list of results, possibly discarding some along the way. +/// +/// When the `transform` function returns `None`, the result is discarded altogether. +pub fn flatten_with( + self: Value, + transform: fn(PolicyId, AssetName, Int) -> Option, +) -> List { + dict.fold( + self.inner, + fn(policy_id, asset, assets) { + dict.fold( + asset, + fn(asset_name, quantity, xs) { + when transform(policy_id, asset_name, quantity) is { + None -> xs + Some(x) -> [x, ..xs] + } + }, + assets, + ) + }, + [], + ) +} + +test flatten_with_1() { + flatten_with(zero(), fn(p, a, q) { Some((p, a, q)) }) == [] +} + +test flatten_with_2() { + let v = + zero() + |> add(from_asset("a", "1", 14)) + |> add(from_asset("b", "", 42)) + |> add(from_asset("a", "2", 42)) + + flatten_with( + v, + fn(p, a, q) { + if q == 42 { + Some((p, a)) + } else { + None + } + }, + ) == [("b", ""), ("a", "2")] +}