diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index e2772c15d..a1df29049 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -75,32 +75,6 @@ BOOST_AUTO_TEST_CASE(exp_operator) testContractAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return u256(1 << a.convert_to()); }, 0, 16); } -BOOST_AUTO_TEST_CASE(exp_operator_const) -{ - char const* sourceCode = R"( - contract test { - function f() public returns(uint d) { return 2 ** 3; } - } - )"; - ALSO_VIA_YUL( - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(8))); - ) -} - -BOOST_AUTO_TEST_CASE(exp_operator_const_signed) -{ - char const* sourceCode = R"( - contract test { - function f() public returns(int d) { return (-2) ** 3; } - } - )"; - ALSO_VIA_YUL( - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(-8))); - ) -} - BOOST_AUTO_TEST_CASE(exp_zero) { char const* sourceCode = R"( @@ -112,162 +86,6 @@ BOOST_AUTO_TEST_CASE(exp_zero) testContractAgainstCppOnRange("f(uint256)", [](u256 const&) -> u256 { return u256(1); }, 0, 16); } -BOOST_AUTO_TEST_CASE(exp_zero_literal) -{ - char const* sourceCode = R"( - contract test { - function f() public returns(uint d) { return 0 ** 0; } - } - )"; - ALSO_VIA_YUL( - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(1))); - ) -} - - -BOOST_AUTO_TEST_CASE(conditional_expression_true_literal) -{ - char const* sourceCode = R"( - contract test { - function f() public returns(uint d) { - return true ? 5 : 10; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(5))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_false_literal) -{ - char const* sourceCode = R"( - contract test { - function f() public returns(uint d) { - return false ? 5 : 10; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(10))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_multiple) -{ - char const* sourceCode = R"( - contract test { - function f(uint x) public returns(uint d) { - return x > 100 ? - x > 1000 ? 1000 : 100 - : - x > 50 ? 50 : 10; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(uint256)", u256(1001)), toBigEndian(u256(1000))); - ABI_CHECK(callContractFunction("f(uint256)", u256(500)), toBigEndian(u256(100))); - ABI_CHECK(callContractFunction("f(uint256)", u256(80)), toBigEndian(u256(50))); - ABI_CHECK(callContractFunction("f(uint256)", u256(40)), toBigEndian(u256(10))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_with_return_values) -{ - char const* sourceCode = R"( - contract test { - function f(bool cond, uint v) public returns (uint a, uint b) { - cond ? a = v : b = v; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(bool,uint256)", true, u256(20)), encodeArgs(u256(20), u256(0))); - ABI_CHECK(callContractFunction("f(bool,uint256)", false, u256(20)), encodeArgs(u256(0), u256(20))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_storage_memory_1) -{ - char const* sourceCode = R"( - contract test { - bytes2[2] data1; - function f(bool cond) public returns (uint) { - bytes2[2] memory x; - x[0] = "aa"; - bytes2[2] memory y; - y[0] = "bb"; - - data1 = cond ? x : y; - - uint ret = 0; - if (data1[0] == "aa") - { - ret = 1; - } - - if (data1[0] == "bb") - { - ret = 2; - } - - return ret; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1))); - ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(2))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_storage_memory_2) -{ - char const* sourceCode = R"( - contract test { - bytes2[2] data1; - function f(bool cond) public returns (uint) { - data1[0] = "cc"; - - bytes2[2] memory x; - bytes2[2] memory y; - y[0] = "bb"; - - x = cond ? y : data1; - - uint ret = 0; - if (x[0] == "bb") - { - ret = 1; - } - - if (x[0] == "cc") - { - ret = 2; - } - - return ret; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1))); - ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(2))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_different_types) -{ - char const* sourceCode = R"( - contract test { - function f(bool cond) public returns (uint) { - uint8 x = 0xcd; - uint16 y = 0xabab; - return cond ? x : y; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(0xcd))); - ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(0xabab))); -} - /* let's add this back when I figure out the correct type conversion. BOOST_AUTO_TEST_CASE(conditional_expression_string_literal) { @@ -284,87 +102,6 @@ BOOST_AUTO_TEST_CASE(conditional_expression_string_literal) } */ -BOOST_AUTO_TEST_CASE(conditional_expression_tuples) -{ - char const* sourceCode = R"( - contract test { - function f(bool cond) public returns (uint, uint) { - return cond ? (1, 2) : (3, 4); - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1), u256(2))); - ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(3), u256(4))); -} - -BOOST_AUTO_TEST_CASE(conditional_expression_functions) -{ - char const* sourceCode = R"( - contract test { - function x() public returns (uint) { return 1; } - function y() public returns (uint) { return 2; } - - function f(bool cond) public returns (uint) { - function () returns (uint) z = cond ? x : y; - return z(); - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1))); - ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(2))); -} - -BOOST_AUTO_TEST_CASE(c99_scoping_activation) -{ - char const* sourceCode = R"( - contract test { - function f() pure public returns (uint) { - uint x = 7; - { - x = 3; // This should still assign to the outer variable - uint x; - x = 4; // This should assign to the new one - } - return x; - } - function g() pure public returns (uint x) { - x = 7; - { - x = 3; - uint x; - return x; // This returns the new variable, i.e. 0 - } - } - function h() pure public returns (uint x, uint a, uint b) { - x = 7; - { - x = 3; - a = x; // This should read from the outer - uint x = 4; - b = x; - } - } - function i() pure public returns (uint x, uint a) { - x = 7; - { - x = 3; - uint x = x; // This should read from the outer and assign to the inner - a = x; - } - } - } - )"; - ALSO_VIA_YUL( - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()"), encodeArgs(3)); - ABI_CHECK(callContractFunction("g()"), encodeArgs(0)); - ABI_CHECK(callContractFunction("h()"), encodeArgs(3, 3, 4)); - ABI_CHECK(callContractFunction("i()"), encodeArgs(3, 3)); - ) -} - BOOST_AUTO_TEST_CASE(recursive_calls) { char const* sourceCode = R"( @@ -409,20 +146,6 @@ BOOST_AUTO_TEST_CASE(multiple_functions) ) } -BOOST_AUTO_TEST_CASE(named_args) -{ - char const* sourceCode = R"( - contract test { - function a(uint a, uint b, uint c) public returns (uint r) { r = a * 100 + b * 10 + c * 1; } - function b() public returns (uint r) { r = a({a: 1, b: 2, c: 3}); } - } - )"; - ALSO_VIA_YUL( - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("b()", bytes()), toBigEndian(u256(123))); - ) -} - BOOST_AUTO_TEST_CASE(disorder_named_args) { char const* sourceCode = R"( diff --git a/test/libsolidity/semanticTests/c99_scoping_activation.sol b/test/libsolidity/semanticTests/c99_scoping_activation.sol new file mode 100644 index 000000000..1201c2c83 --- /dev/null +++ b/test/libsolidity/semanticTests/c99_scoping_activation.sol @@ -0,0 +1,43 @@ +contract test { + function f() pure public returns (uint) { + uint x = 7; + { + x = 3; // This should still assign to the outer variable + uint x; + x = 4; // This should assign to the new one + } + return x; + } + function g() pure public returns (uint x) { + x = 7; + { + x = 3; + uint x; + return x; // This returns the new variable, i.e. 0 + } + } + function h() pure public returns (uint x, uint a, uint b) { + x = 7; + { + x = 3; + a = x; // This should read from the outer + uint x = 4; + b = x; + } + } + function i() pure public returns (uint x, uint a) { + x = 7; + { + x = 3; + uint x = x; // This should read from the outer and assign to the inner + a = x; + } + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 3 +// g() -> 0 +// h() -> 3, 3, 4 +// i() -> 3, 3 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_different_types.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_different_types.sol new file mode 100644 index 000000000..df07364b3 --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_different_types.sol @@ -0,0 +1,10 @@ +contract test { + function f(bool cond) public returns (uint) { + uint8 x = 0xcd; + uint16 y = 0xabab; + return cond ? x : y; + } +} +// ---- +// f(bool): true -> 0xcd +// f(bool): false -> 0xabab diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_false_literal.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_false_literal.sol new file mode 100644 index 000000000..3915c7b8d --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_false_literal.sol @@ -0,0 +1,7 @@ +contract test { + function f() public returns(uint d) { + return false ? 5 : 10; + } +} +// ---- +// f() -> 10 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_functions.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_functions.sol new file mode 100644 index 000000000..bff9122b1 --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_functions.sol @@ -0,0 +1,12 @@ +contract test { + function x() public returns (uint) { return 1; } + function y() public returns (uint) { return 2; } + + function f(bool cond) public returns (uint) { + function () returns (uint) z = cond ? x : y; + return z(); + } +} +// ---- +// f(bool): true -> 1 +// f(bool): false -> 2 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_multiple.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_multiple.sol new file mode 100644 index 000000000..2a9d6de80 --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_multiple.sol @@ -0,0 +1,13 @@ +contract test { + function f(uint x) public returns(uint d) { + return x > 100 ? + x > 1000 ? 1000 : 100 + : + x > 50 ? 50 : 10; + } +} +// ---- +// f(uint256): 1001 -> 1000 +// f(uint256): 500 -> 100 +// f(uint256): 80 -> 50 +// f(uint256): 40 -> 10 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_storage_memory_1.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_storage_memory_1.sol new file mode 100644 index 000000000..3434cca01 --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_storage_memory_1.sol @@ -0,0 +1,27 @@ +contract test { + bytes2[2] data1; + function f(bool cond) public returns (uint) { + bytes2[2] memory x; + x[0] = "aa"; + bytes2[2] memory y; + y[0] = "bb"; + + data1 = cond ? x : y; + + uint ret = 0; + if (data1[0] == "aa") + { + ret = 1; + } + + if (data1[0] == "bb") + { + ret = 2; + } + + return ret; + } +} +// ---- +// f(bool): true -> 1 +// f(bool): false -> 2 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_storage_memory_2.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_storage_memory_2.sol new file mode 100644 index 000000000..8633d2bfa --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_storage_memory_2.sol @@ -0,0 +1,28 @@ +contract test { + bytes2[2] data1; + function f(bool cond) public returns (uint) { + data1[0] = "cc"; + + bytes2[2] memory x; + bytes2[2] memory y; + y[0] = "bb"; + + x = cond ? y : data1; + + uint ret = 0; + if (x[0] == "bb") + { + ret = 1; + } + + if (x[0] == "cc") + { + ret = 2; + } + + return ret; + } +} +// ---- +// f(bool): true -> 1 +// f(bool): false -> 2 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_true_literal.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_true_literal.sol new file mode 100644 index 000000000..06247f0dc --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_true_literal.sol @@ -0,0 +1,7 @@ +contract test { + function f() public returns(uint d) { + return true ? 5 : 10; + } +} +// ---- +// f() -> 5 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_tuples.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_tuples.sol new file mode 100644 index 000000000..d27d06bac --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_tuples.sol @@ -0,0 +1,8 @@ +contract test { + function f(bool cond) public returns (uint, uint) { + return cond ? (1, 2) : (3, 4); + } +} +// ---- +// f(bool): true -> 1, 2 +// f(bool): false -> 3, 4 diff --git a/test/libsolidity/semanticTests/expressions/conditional_expression_with_return_values.sol b/test/libsolidity/semanticTests/expressions/conditional_expression_with_return_values.sol new file mode 100644 index 000000000..bbaf051bd --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/conditional_expression_with_return_values.sol @@ -0,0 +1,8 @@ +contract test { + function f(bool cond, uint v) public returns (uint a, uint b) { + cond ? a = v : b = v; + } +} +// ---- +// f(bool,uint256): true, 20 -> 20, 0 +// f(bool,uint256): false, 20 -> 0, 20 diff --git a/test/libsolidity/semanticTests/expressions/exp_operator_const.sol b/test/libsolidity/semanticTests/expressions/exp_operator_const.sol new file mode 100644 index 000000000..8d282fb8f --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/exp_operator_const.sol @@ -0,0 +1,7 @@ +contract test { + function f() public returns(uint d) { return 2 ** 3; } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 8 diff --git a/test/libsolidity/semanticTests/expressions/exp_operator_const_signed.sol b/test/libsolidity/semanticTests/expressions/exp_operator_const_signed.sol new file mode 100644 index 000000000..9e3c3b4c9 --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/exp_operator_const_signed.sol @@ -0,0 +1,7 @@ +contract test { + function f() public returns(int d) { return (-2) ** 3; } +} +// ==== +// compileViaYul: also +// ---- +// f() -> -8 diff --git a/test/libsolidity/semanticTests/expressions/exp_zero_literal.sol b/test/libsolidity/semanticTests/expressions/exp_zero_literal.sol new file mode 100644 index 000000000..1dfa0a796 --- /dev/null +++ b/test/libsolidity/semanticTests/expressions/exp_zero_literal.sol @@ -0,0 +1,7 @@ +contract test { + function f() public returns(uint d) { return 0 ** 0; } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 1 diff --git a/test/libsolidity/semanticTests/functionCall/multiple_functions.sol b/test/libsolidity/semanticTests/functionCall/multiple_functions.sol new file mode 100644 index 000000000..981608d89 --- /dev/null +++ b/test/libsolidity/semanticTests/functionCall/multiple_functions.sol @@ -0,0 +1,12 @@ +contract test { + function a() public returns(uint n) { return 0; } + function b() public returns(uint n) { return 1; } + function c() public returns(uint n) { return 2; } + function f() public returns(uint n) { return 3; } +} +// ---- +// a() -> 0 +// b() -> 1 +// c() -> 2 +// f() -> 3 +// i_am_not_there() -> FAILURE diff --git a/test/libsolidity/semanticTests/functionCall/named_args.sol b/test/libsolidity/semanticTests/functionCall/named_args.sol new file mode 100644 index 000000000..132139b99 --- /dev/null +++ b/test/libsolidity/semanticTests/functionCall/named_args.sol @@ -0,0 +1,6 @@ +contract test { + function a(uint a, uint b, uint c) public returns (uint r) { r = a * 100 + b * 10 + c * 1; } + function b() public returns (uint r) { r = a({a: 1, b: 2, c: 3}); } +} +// ---- +// b() -> 123