From 9250da605bf73a5a092e7936a00a8577834bb2a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 24 Jan 2023 23:39:38 +0100 Subject: [PATCH] fixup! User-defined operators: Tests --- .../userDefined/calling_all_operators.sol | 224 +++++++++--------- .../calling_all_operators_precendence.sol | 35 ++- 2 files changed, 123 insertions(+), 136 deletions(-) diff --git a/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators.sol b/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators.sol index b61edd168..2ae468f9f 100644 --- a/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators.sol +++ b/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators.sol @@ -1,125 +1,115 @@ -type Int is int128; +type Int is int8; using { - bitor as |, bitand as &, bitxor as ^, bitnot as ~, - add as +, sub as -, unsub as -, mul as *, div as /, mod as %, + bitor as |, bitand as &, bitxor as ^, bitnot as ~, shl as <<, sar as >>, + add as +, sub as -, unsub as -, mul as *, div as /, mod as %, exp as **, eq as ==, noteq as !=, lt as <, gt as >, leq as <=, geq as >=, - shl as <<, sar as >>, exp as **, not as ! + not as ! } for Int; -function uw(Int x) pure returns (int128) { - return Int.unwrap(x); -} -function w(int128 x) pure returns (Int) { - return Int.wrap(x); -} -function bitor(Int, Int) pure returns (Int) { - return w(10); -} -function bitand(Int, Int) pure returns (Int) { - return w(11); -} -function bitxor(Int, Int) pure returns (Int) { - return w(12); -} -function bitnot(Int) pure returns (Int) { - return w(13); -} -function add(Int x, Int) pure returns (Int) { - return w(uw(x) + 10); -} -function sub(Int, Int) pure returns (Int) { - return w(15); -} -function unsub(Int) pure returns (Int) { - return w(16); -} -function mul(Int, Int) pure returns (Int) { - return w(17); -} -function div(Int, Int) pure returns (Int) { - return w(18); -} -function mod(Int, Int) pure returns (Int) { - return w(19); -} -function eq(Int x, Int) pure returns (bool) { - return uw(x) == 1; -} -function noteq(Int x, Int) pure returns (bool) { - return uw(x) == 2; -} -function lt(Int x, Int) pure returns (bool) { - return uw(x) < 10; -} -function gt(Int x, Int) pure returns (bool) { - return uw(x) > 10; -} -function leq(Int x, Int) pure returns (bool) { - return uw(x) <= 10; -} -function geq(Int x, Int) pure returns (bool) { - return uw(x) >= 10; -} -function shl(Int, Int) pure returns (Int) { - return w(20); -} -function sar(Int, Int) pure returns (Int) { - return w(21); -} -function exp(Int, Int) pure returns (Int) { - return w(22); -} -function not(Int) pure returns (Int) { - return w(23); -} +function bitor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) | Int.unwrap(y)); } +function bitand(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) & Int.unwrap(y)); } +function bitxor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) ^ Int.unwrap(y)); } +function bitnot(Int x) pure returns (Int) { return Int.wrap(~Int.unwrap(x)); } +function shl(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) << uint8(Int.unwrap(y))); } +function sar(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) >> uint8(Int.unwrap(y))); } + +function add(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) + Int.unwrap(y)); } +function sub(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) - Int.unwrap(y)); } +function unsub(Int x) pure returns (Int) { return Int.wrap(-Int.unwrap(x)); } +function mul(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) * Int.unwrap(y)); } +function div(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) / Int.unwrap(y)); } +function mod(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) % Int.unwrap(y)); } +function exp(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) ** uint8(Int.unwrap(y))); } + +function eq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) == Int.unwrap(y); } +function noteq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) != Int.unwrap(y); } +function lt(Int x, Int y) pure returns (bool) { return Int.unwrap(x) < Int.unwrap(y); } +function gt(Int x, Int y) pure returns (bool) { return Int.unwrap(x) > Int.unwrap(y); } +function leq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) <= Int.unwrap(y); } +function geq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) >= Int.unwrap(y); } + +function not(Int x) pure returns (Int) { return Int.unwrap(x) == 0 ? Int.wrap(1) : Int.wrap(0); } contract C { - function test_bitor() public pure returns (Int) { return w(1) | w(2); } - function test_bitand() public pure returns (Int) { return w(1) & w(2); } - function test_bitxor() public pure returns (Int) { return w(1) ^ w(2); } - function test_bitnot() public pure returns (Int) { return ~w(1); } - function test_add(int128 x) public pure returns (Int) { return w(x) + w(2); } - function test_sub() public pure returns (Int) { return w(1) - w(2); } - function test_unsub() public pure returns (Int) { return -w(1); } - function test_mul() public pure returns (Int) { return w(1) * w(2); } - function test_div() public pure returns (Int) { return w(1) / w(2); } - function test_mod() public pure returns (Int) { return w(1) % w(2); } - function test_eq(int128 x) public pure returns (bool) { return w(x) == w(2); } - function test_neq(int128 x) public pure returns (bool) { return w(x) != w(2); } - function test_lt(int128 x) public pure returns (bool) { return w(x) < w(2); } - function test_gt(int128 x) public pure returns (bool) { return w(x) > w(2); } - function test_leq(int128 x) public pure returns (bool) { return w(x) <= w(2); } - function test_geq(int128 x) public pure returns (bool) { return w(x) >= w(2); } - function test_shl() public pure returns (Int) { return w(1) << w(2); } - function test_sar() public pure returns (Int) { return w(1) >> w(2); } - function test_exp() public pure returns (Int) { return w(1) ** w(2); } - function test_not() public pure returns (Int) { return !w(1); } + Int constant ZERO = Int.wrap(0); + Int constant ONE = Int.wrap(1); + Int constant TWO = Int.wrap(2); + Int constant THREE = Int.wrap(3); + Int constant FOUR = Int.wrap(4); + Int constant SIX = Int.wrap(6); + + function test_bitwise() public pure { + assert(Int.unwrap(ONE | TWO) == 3); + assert(Int.unwrap(ONE | ZERO) == 1); + + assert(Int.unwrap(ONE & THREE) == 1); + assert(Int.unwrap(ONE & ONE) == 1); + + assert(Int.unwrap(TWO ^ TWO) == 0); + assert(Int.unwrap(TWO ^ ONE) == 3); + + assert(Int.unwrap(~ZERO) == -1); + assert(Int.unwrap(~ONE) == -2); + assert(Int.unwrap(~TWO) == -3); + + assert(Int.unwrap(ONE << ONE) == 2); + assert(Int.unwrap(ONE << TWO) == 4); + + assert(Int.unwrap(TWO >> ONE) == 1); + assert(Int.unwrap(FOUR >> TWO) == 1); + } + + function test_arithmetic() public pure { + assert(Int.unwrap(ONE + TWO) == 3); + assert(Int.unwrap(ONE + ZERO) == 1); + + assert(Int.unwrap(TWO - ONE) == 1); + assert(Int.unwrap(THREE - THREE) == 0); + + assert(Int.unwrap(-TWO) == -2); + assert(Int.unwrap(-ZERO) == 0); + + assert(Int.unwrap(ONE * ONE) == 1); + assert(Int.unwrap(THREE * TWO) == 6); + + assert(Int.unwrap(SIX / TWO) == 3); + assert(Int.unwrap(THREE / TWO) == 1); + + assert(Int.unwrap(SIX % TWO) == 0); + assert(Int.unwrap(THREE % TWO) == 1); + + assert(Int.unwrap(ONE ** SIX) == 1); + assert(Int.unwrap(TWO ** TWO) == 4); + } + + function test_comparison() public pure { + assert((ONE == ONE) == true); + assert((ONE == TWO) == false); + + assert((ONE != ONE) == false); + assert((ONE != TWO) == true); + + assert((ONE < TWO) == true); + assert((TWO < ONE) == false); + + assert((ONE <= TWO) == true); + assert((TWO <= ONE) == false); + + assert((ONE > TWO) == false); + assert((TWO > ONE) == true); + + assert((ONE >= TWO) == false); + assert((TWO >= ONE) == true); + } + + function test_boolean() public pure { + assert(Int.unwrap(!ZERO) == 1); + assert(Int.unwrap(!ONE) == 0); + assert(Int.unwrap(!TWO) == 0); + } } // ---- -// test_bitor() -> 10 -// test_bitand() -> 11 -// test_bitxor() -> 12 -// test_bitnot() -> 13 -// test_add(int128): 4 -> 14 -// test_add(int128): 104 -> 114 -// test_sub() -> 15 -// test_unsub() -> 16 -// test_mul() -> 17 -// test_div() -> 18 -// test_mod() -> 19 -// test_eq(int128): 1 -> true -// test_eq(int128): 2 -> false -// test_neq(int128): 2 -> true -// test_neq(int128): 1 -> false -// test_lt(int128): 9 -> true -// test_lt(int128): 10 -> false -// test_gt(int128): 11 -> true -// test_gt(int128): 10 -> false -// test_leq(int128): 10 -> true -// test_leq(int128): 11 -> false -// test_geq(int128): 10 -> true -// test_geq(int128): 9 -> false -// test_shl() -> 20 -// test_sar() -> 21 -// test_exp() -> 22 -// test_not() -> 23 +// test_bitwise() -> +// test_arithmetic() -> +// test_comparison() -> +// test_boolean() -> diff --git a/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators_precendence.sol b/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators_precendence.sol index c4a5a9b07..90e1620e9 100644 --- a/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators_precendence.sol +++ b/test/libsolidity/semanticTests/operators/userDefined/calling_all_operators_precendence.sol @@ -6,28 +6,25 @@ using { not as ! } for Int; -function uw(Int x) pure returns (int64) { return Int.unwrap(x); } -function w(int64 x) pure returns (Int) { return Int.wrap(x); } +function bitor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) | Int.unwrap(y)); } +function bitand(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) & Int.unwrap(y)); } +function bitxor(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) ^ Int.unwrap(y)); } +function bitnot(Int x) pure returns (Int) { return Int.wrap(~Int.unwrap(x)); } +function shl(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) << uint64(Int.unwrap(y))); } +function sar(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) >> uint64(Int.unwrap(y))); } -function bitor(Int x, Int y) pure returns (Int) { return w(uw(x) | uw(y)); } -function bitand(Int x, Int y) pure returns (Int) { return w(uw(x) & uw(y)); } -function bitxor(Int x, Int y) pure returns (Int) { return w(uw(x) ^ uw(y)); } -function bitnot(Int x) pure returns (Int) { return w(~uw(x)); } -function shl(Int x, Int y) pure returns (Int) { return w(uw(x) << uint64(uw(y))); } -function sar(Int x, Int y) pure returns (Int) { return w(uw(x) >> uint64(uw(y))); } +function add(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) + Int.unwrap(y)); } +function sub(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) - Int.unwrap(y)); } +function unsub(Int x) pure returns (Int) { return Int.wrap(-Int.unwrap(x)); } +function mul(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) * Int.unwrap(y)); } +function div(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) / Int.unwrap(y)); } +function mod(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) % Int.unwrap(y)); } +function exp(Int x, Int y) pure returns (Int) { return Int.wrap(Int.unwrap(x) ** uint64(Int.unwrap(y))); } -function add(Int x, Int y) pure returns (Int) { return w(uw(x) + uw(y)); } -function sub(Int x, Int y) pure returns (Int) { return w(uw(x) - uw(y)); } -function unsub(Int x) pure returns (Int) { return w(-uw(x)); } -function mul(Int x, Int y) pure returns (Int) { return w(uw(x) * uw(y)); } -function div(Int x, Int y) pure returns (Int) { return w(uw(x) / uw(y)); } -function mod(Int x, Int y) pure returns (Int) { return w(uw(x) % uw(y)); } -function exp(Int x, Int y) pure returns (Int) { return w(uw(x) ** uint64(uw(y))); } +function eq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) == Int.unwrap(y); } +function noteq(Int x, Int y) pure returns (bool) { return Int.unwrap(x) != Int.unwrap(y); } -function eq(Int x, Int y) pure returns (bool) { return uw(x) == uw(y); } -function noteq(Int x, Int y) pure returns (bool) { return uw(x) != uw(y); } - -function not(Int x) pure returns (Int) { return w((uw(x) == 0 ? int64(1) : int64(0))); } +function not(Int x) pure returns (Int) { return Int.unwrap(x) == 0 ? Int.wrap(1) : Int.wrap(0); } contract C { Int constant I0 = Int.wrap(0);