From 2c7ab0cf48b497a24ebeb264aca968c4d5e80047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 21 Mar 2023 12:22:51 +0100 Subject: [PATCH] Tests for suffixes + operators --- .../operators_on_suffixed_booleans.sol | 26 ++++++++++++++ .../operators_on_suffixed_integers.sol | 26 ++++++++++++++ .../suffix_vs_operator_precedence.sol | 36 +++++++++++++++++++ ...nchecked_counter_with_literal_suffixes.sol | 34 ++++++++++++++++++ .../application/invalid_suffix_in_delete.sol | 9 +++++ ...invalid_suffixed_parenthesized_literal.sol | 9 +++++ 6 files changed, 140 insertions(+) create mode 100644 test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_booleans.sol create mode 100644 test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_integers.sol create mode 100644 test/libsolidity/semanticTests/literalSuffixes/suffix_vs_operator_precedence.sol create mode 100644 test/libsolidity/semanticTests/literalSuffixes/unchecked_counter_with_literal_suffixes.sol create mode 100644 test/libsolidity/syntaxTests/literalSuffixes/application/invalid_suffix_in_delete.sol create mode 100644 test/libsolidity/syntaxTests/literalSuffixes/suffixableLiterals/invalid_suffixed_parenthesized_literal.sol diff --git a/test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_booleans.sol b/test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_booleans.sol new file mode 100644 index 000000000..03865c012 --- /dev/null +++ b/test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_booleans.sol @@ -0,0 +1,26 @@ +type Bool is bool; +using {bitor as |, bitand as &, bitnot as ~} for Bool global; + +function bitor(Bool x, Bool y) pure returns (Bool) { + return Bool.wrap(Bool.unwrap(x) || Bool.unwrap(y)); +} + +function bitand(Bool x, Bool y) pure returns (Bool) { + return Bool.wrap(Bool.unwrap(x) && Bool.unwrap(y)); +} + +function bitnot(Bool x) pure returns (Bool) { + return Bool.wrap(!Bool.unwrap(x)); +} + +function b(bool x) pure suffix returns (Bool) { + return Bool.wrap(x); +} + +contract C { + function test() public pure returns (Bool) { + return ~ ~ ~(false b | true b & ~false b) & true b; + } +} +// ---- +// test() -> false diff --git a/test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_integers.sol b/test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_integers.sol new file mode 100644 index 000000000..efe1ea765 --- /dev/null +++ b/test/libsolidity/semanticTests/literalSuffixes/operators_on_suffixed_integers.sol @@ -0,0 +1,26 @@ +type Int is int; +using {add as +, mul as *, unsub as -} for Int global; + +function add(Int a, Int b) pure returns (Int) { + return Int.wrap(Int.unwrap(a) + Int.unwrap(b)); +} + +function mul(Int a, Int b) pure returns (Int) { + return Int.wrap(Int.unwrap(a) * Int.unwrap(b)); +} + +function unsub(Int a) pure returns (Int) { + return Int.wrap(-Int.unwrap(a)); +} + +function i(int x) pure suffix returns (Int) { + return Int.wrap(x); +} + +contract C { + function test() public pure returns (Int) { + return - - -(4 i + 4 i * -3 i) * 2 i; + } +} +// ---- +// test() -> 16 diff --git a/test/libsolidity/semanticTests/literalSuffixes/suffix_vs_operator_precedence.sol b/test/libsolidity/semanticTests/literalSuffixes/suffix_vs_operator_precedence.sol new file mode 100644 index 000000000..7dd75e83b --- /dev/null +++ b/test/libsolidity/semanticTests/literalSuffixes/suffix_vs_operator_precedence.sol @@ -0,0 +1,36 @@ +type Int is int; +using {sub as -, unsub as -} for Int global; + +function sub(Int a, Int b) pure returns (Int) { + return Int.wrap((Int.unwrap(a) - Int.unwrap(b)) * 100); +} + +function unsub(Int a) pure returns (Int) { + return Int.wrap(Int.unwrap(a) + 10); +} + +function u(int x) pure suffix returns (Int) { + return Int.wrap(x + 1); +} + +function u(Int x) pure suffix returns (Int) { + return Int.wrap(Int.unwrap(x) + 1); +} + +contract C { + function testUnary() public pure returns (Int) { + return -2 u; + } + + function testUnaryWithParens() public pure returns (Int) { + return -(2 u); + } + + function testBinary() public pure returns (Int) { + return 4 u - 2 u; + } +} +// ---- +// testUnary() -> 13 +// testUnaryWithParens() -> 13 +// testBinary() -> 200 diff --git a/test/libsolidity/semanticTests/literalSuffixes/unchecked_counter_with_literal_suffixes.sol b/test/libsolidity/semanticTests/literalSuffixes/unchecked_counter_with_literal_suffixes.sol new file mode 100644 index 000000000..e3806909a --- /dev/null +++ b/test/libsolidity/semanticTests/literalSuffixes/unchecked_counter_with_literal_suffixes.sol @@ -0,0 +1,34 @@ +type UncheckedCounter is uint; + +using { + add as +, + lt as < +} for UncheckedCounter global; + +function add(UncheckedCounter x, UncheckedCounter y) pure returns (UncheckedCounter) { + unchecked { + return UncheckedCounter.wrap(UncheckedCounter.unwrap(x) + UncheckedCounter.unwrap(y)); + } +} + +function lt(UncheckedCounter x, UncheckedCounter y) pure returns (bool) { + return UncheckedCounter.unwrap(x) < UncheckedCounter.unwrap(y); +} + +function cycles(uint c) pure suffix returns (UncheckedCounter) { + return UncheckedCounter.wrap(c); +} + +contract C { + uint public total = 0; + + function testCounter() public returns (uint) { + for (UncheckedCounter i = 20 cycles; i < 23 cycles; i = i + 1 cycles) + total += UncheckedCounter.unwrap(i); + + return total; + } +} +// ---- +// testCounter() -> 63 +// total() -> 63 diff --git a/test/libsolidity/syntaxTests/literalSuffixes/application/invalid_suffix_in_delete.sol b/test/libsolidity/syntaxTests/literalSuffixes/application/invalid_suffix_in_delete.sol new file mode 100644 index 000000000..d511742f5 --- /dev/null +++ b/test/libsolidity/syntaxTests/literalSuffixes/application/invalid_suffix_in_delete.sol @@ -0,0 +1,9 @@ +function suffix(string memory s) pure suffix returns (string memory) { return s; } + +contract C { + function f() public pure { + delete "abc" suffix; + } +} +// ---- +// TypeError 4247: (143-155): Expression has to be an lvalue. diff --git a/test/libsolidity/syntaxTests/literalSuffixes/suffixableLiterals/invalid_suffixed_parenthesized_literal.sol b/test/libsolidity/syntaxTests/literalSuffixes/suffixableLiterals/invalid_suffixed_parenthesized_literal.sol new file mode 100644 index 000000000..c60bc8642 --- /dev/null +++ b/test/libsolidity/syntaxTests/literalSuffixes/suffixableLiterals/invalid_suffixed_parenthesized_literal.sol @@ -0,0 +1,9 @@ +function suffix(uint) pure suffix returns (uint) {} + +contract C { + function f() public { + (1) suffix; + } +} +// ---- +// ParserError 2314: (104-110): Expected ';' but got identifier