From 46c5982038819ecf15736fad5a914cef4c68672b Mon Sep 17 00:00:00 2001 From: Rigidity Date: Tue, 25 Jun 2024 20:05:50 -0400 Subject: [PATCH] More tests --- tests.toml | 71 +++++++++++++++++++++-- tests/errors/implicit_return_if_stmt.rue | 26 +++++++++ tests/errors/recursive_type_alias.rue | 11 ++++ tests/functions/birecursive_functions.rue | 23 ++++++++ tests/functions/nested_scopes.rue | 19 ++++++ tests/guards/and_guard.rue | 8 +++ tests/guards/type_guards.rue | 57 ++++++++++++++++-- tests/types/list_types.rue | 15 +++++ tests/types/pair_types.rue | 6 ++ 9 files changed, 227 insertions(+), 9 deletions(-) create mode 100644 tests/errors/implicit_return_if_stmt.rue create mode 100644 tests/errors/recursive_type_alias.rue create mode 100644 tests/functions/birecursive_functions.rue create mode 100644 tests/functions/nested_scopes.rue create mode 100644 tests/guards/and_guard.rue create mode 100644 tests/types/list_types.rue create mode 100644 tests/types/pair_types.rue diff --git a/tests.toml b/tests.toml index 0a501b1..9dc9ee0 100644 --- a/tests.toml +++ b/tests.toml @@ -139,11 +139,14 @@ output = "42" hash = "837525fb91b88dbf02d9ef5a0b322f6943f93424b6e8fe6d26dd1be6d3311871" [type_guards] -bytes = 63 -cost = 1332 -input = "()" -output = "5" -hash = "32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1" +parser_errors = [] +compiler_errors = [ + "unused function `bytes_guard` at 15:5", + "unused function `bytes32_guard` at 23:5", + "unused function `public_key_guard` at 33:5", + "unused function `inverted_guard` at 43:5", + "unused function `pair_guard` at 51:5", +] [if_stmt] bytes = 107 @@ -381,3 +384,61 @@ cost = 20323 input = "(((0x42840c6aebec47ce2e01629ce381b461c19695264281a7b1aab5d4ff54506775 1) (0x917b0e1ee2c8ad5755e1f97c4642ea653288acf816eee1b0d537dd8e01106711 2)) 3 100000)" output = "((60 36) (73 0x0186a0) (g1_negate 0x42840c6aebec47ce2e01629ce381b461c19695264281a7b1aab5d4ff54506775 0x008235 (0x42840c6aebec47ce2e01629ce381b461c19695264281a7b1aab5d4ff54506775)) (g1_negate 0x917b0e1ee2c8ad5755e1f97c4642ea653288acf816eee1b0d537dd8e01106711 0x01046a (0x917b0e1ee2c8ad5755e1f97c4642ea653288acf816eee1b0d537dd8e01106711)))" hash = "83edaa46ef48ec8bbc8be46b4dd0485ee870fed36f7e51fad3f714cda94bd14c" + +[list_types] +parser_errors = [] +compiler_errors = [ + "unused let binding `empty` at 2:9", + "unused let binding `empty_hinted` at 3:9", +] + +[pair_types] +bytes = 57 +cost = 3035 +input = "()" +output = "80" +hash = "91cecc8bd72dfaf559214ff324f9df7f45c652a96b8f5ae5e3b7fae0dd851014" + +[nested_scopes] +bytes = 421 +cost = 0 +input = "()" +output = "()" +hash = "8d496dc7cdbc417db2132eda6894b29a91511f176e93a1ea943d210cd27822b2" +error = "()" + +[birecursive_functions] +bytes = 195 +cost = 12822 +input = "()" +output = "()" +hash = "770bb2c0b2582924adf403e62374f8424a2ed510eef70b5f450eccab238a4911" + +[implicit_return_if_stmt] +parser_errors = [] +compiler_errors = [ + "implicit return is not allowed in if statements, use an explicit return statement at 2:19", + "block missing return value at 10:19", + "implicit return is not allowed in if statements, use an explicit return statement at 10:19", + "block missing return value at 9:22", + "block missing return value at 16:19", + "implicit return is not allowed in if statements, use an explicit return statement at 16:19", + "unused function `another` at 9:5", + "unused function `yet_another` at 15:5", + "unused function `raise_ok` at 24:5", +] + +[recursive_type_alias] +parser_errors = [] +compiler_errors = [ + "type aliases cannot reference themselves at 4:1", + "type aliases cannot reference themselves at 7:1", + "unused type alias `E` at 6:6", +] + +[and_guard] +bytes = 63 +cost = 1332 +input = "()" +output = "5" +hash = "32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1" diff --git a/tests/errors/implicit_return_if_stmt.rue b/tests/errors/implicit_return_if_stmt.rue new file mode 100644 index 0000000..efd60e6 --- /dev/null +++ b/tests/errors/implicit_return_if_stmt.rue @@ -0,0 +1,26 @@ +fun main() -> Int { + if 1000 > 100 { + 42 + } + + 100 +} + +fun another() -> Int { + if 100 > 1000 { + assert true; + } +} + +fun yet_another() -> Int { + if 100 > 1000 { + assert true; + assert false; + assert false; + } + 100 +} + +fun raise_ok() -> Int { + raise; +} diff --git a/tests/errors/recursive_type_alias.rue b/tests/errors/recursive_type_alias.rue new file mode 100644 index 0000000..c1c0018 --- /dev/null +++ b/tests/errors/recursive_type_alias.rue @@ -0,0 +1,11 @@ +type A = B; +type B = C; +type C = D; +type D = A; + +type E = F; +type F = E; + +fun main() -> A { + true +} diff --git a/tests/functions/birecursive_functions.rue b/tests/functions/birecursive_functions.rue new file mode 100644 index 0000000..7122e20 --- /dev/null +++ b/tests/functions/birecursive_functions.rue @@ -0,0 +1,23 @@ +fun main() -> Int { + a(5) +} + +fun a(num: Int) -> Int { + if num > 0 { + b(num - 1) + } else { + c() + } +} + +fun b(num: Int) -> Int { + if num > 0 { + a(num - 1) + } else { + 0 + } +} + +fun c() -> Int { + 52 +} diff --git a/tests/functions/nested_scopes.rue b/tests/functions/nested_scopes.rue new file mode 100644 index 0000000..ac3846b --- /dev/null +++ b/tests/functions/nested_scopes.rue @@ -0,0 +1,19 @@ +fun main(value: Int) -> Int { + fun inner_1(num: Int) -> Int { + outer_1() * value + num + } + + fun inner_2(num: Int) -> Int { + outer_2(num, inner_1) + } + + outer_1() + inner_1(inner_2(value)) * outer_2(value, inner_2) +} + +fun outer_1() -> Int { + 96 +} + +fun outer_2(num: Int, closure: fun(num: Int) -> Int) -> Int { + num * closure(num) +} diff --git a/tests/guards/and_guard.rue b/tests/guards/and_guard.rue new file mode 100644 index 0000000..7a55b21 --- /dev/null +++ b/tests/guards/and_guard.rue @@ -0,0 +1,8 @@ +fun main() -> Int { + let a = [1, 2, 3]; + let b = [4, 5, 6]; + + assume !(a is Nil) && !(b is Nil); + + a.first + b.first +} diff --git a/tests/guards/type_guards.rue b/tests/guards/type_guards.rue index 7a55b21..5868b36 100644 --- a/tests/guards/type_guards.rue +++ b/tests/guards/type_guards.rue @@ -1,8 +1,57 @@ fun main() -> Int { - let a = [1, 2, 3]; - let b = [4, 5, 6]; + let items = [1, 2, 3]; + let bytes = "Hello, world!"; + sum(...items) + (bytes is Bytes32) as Int +} + +fun sum(...nums: Int[]) -> Int { + if nums is (Int, Int[]) { + nums.first + sum(...nums.rest) + } else { + 0 + } +} + +fun bytes_guard(value: Any) -> Bool { + if value is Bytes { + value.length > 1 + } else { + false + } +} - assume !(a is Nil) && !(b is Nil); +fun bytes32_guard(value: Any) -> Bool { + assert value is Bytes; + + if value is Bytes32 { + value.length > 1 + } else { + false + } +} + +fun public_key_guard(value: Any) -> Bool { + assert value is Bytes; + + if value is PublicKey { + value.length > 1 + } else { + false + } +} + +fun inverted_guard(value: Any) -> Bool { + if !(value is (Any, Any)) { + value.length > 1 + } else { + false + } +} - a.first + b.first +fun pair_guard(value: Any) -> Int { + if value is (Any, Any) { + pair_guard(value.first) + pair_guard(value.rest) + } else { + value as Int + } } diff --git a/tests/types/list_types.rue b/tests/types/list_types.rue new file mode 100644 index 0000000..538f330 --- /dev/null +++ b/tests/types/list_types.rue @@ -0,0 +1,15 @@ +fun main() -> Int { + let empty = []; + let empty_hinted: Int[] = empty; + + let items = [1, 2, 3]; + let items_hinted: Int[] = items; + + let spread = [...items]; + let spread_hinted: Int[] = [...items]; + + let prepend = [0, ...items]; + let prepend_hinted: Int[] = [0, ...items]; + + items[0] + items_hinted[1] + spread[2] + spread_hinted[0] + prepend[1] + prepend_hinted[2] +} diff --git a/tests/types/pair_types.rue b/tests/types/pair_types.rue new file mode 100644 index 0000000..5553ae2 --- /dev/null +++ b/tests/types/pair_types.rue @@ -0,0 +1,6 @@ +fun main() -> Int { + let pair = (20, 24); + let pair_hinted: (Int, Int) = pair; + + pair.first + pair.rest + pair_hinted.first + pair_hinted.rest +}