From bdf05bf8a0d980af299dc508cbe721b38a02a503 Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Fri, 6 Nov 2020 14:29:43 +0100 Subject: [PATCH 1/5] Moving try catch test to semantic tests. --- test/libsolidity/SolidityEndToEndTest.cpp | 53 ------------------- .../tryCatch/try_catch_library_call.sol | 45 ++++++++++++++++ 2 files changed, 45 insertions(+), 53 deletions(-) create mode 100644 test/libsolidity/semanticTests/tryCatch/try_catch_library_call.sol diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index c497c46b2..3072d2038 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -6690,59 +6690,6 @@ BOOST_AUTO_TEST_CASE(dirty_scratch_space_prior_to_constant_optimiser) ); } -BOOST_AUTO_TEST_CASE(try_catch_library_call) -{ - char const* sourceCode = R"( - library L { - struct S { uint x; } - function integer(uint t, bool b) public view returns (uint) { - if (b) { - return t; - } else { - revert("failure"); - } - } - function stru(S storage t, bool b) public view returns (uint) { - if (b) { - return t.x; - } else { - revert("failure"); - } - } - } - contract C { - using L for L.S; - L.S t; - function f(bool b) public returns (uint, string memory) { - uint x = 8; - try L.integer(x, b) returns (uint _x) { - return (_x, ""); - } catch Error(string memory message) { - return (18, message); - } - } - function g(bool b) public returns (uint, string memory) { - t.x = 9; - try t.stru(b) returns (uint x) { - return (x, ""); - } catch Error(string memory message) { - return (19, message); - } - } - } - )"; - if (solidity::test::CommonOptions::get().evmVersion().supportsReturndata()) - { - compileAndRun(sourceCode, 0, "L", bytes()); - compileAndRun(sourceCode, 0, "C", bytes(), map{{"L", m_contractAddress}}); - - ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(8, 0x40, 0)); - ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(18, 0x40, 7, "failure")); - ABI_CHECK(callContractFunction("g(bool)", true), encodeArgs(9, 0x40, 0)); - ABI_CHECK(callContractFunction("g(bool)", false), encodeArgs(19, 0x40, 7, "failure")); - } -} - BOOST_AUTO_TEST_CASE(strip_reason_strings) { char const* sourceCode = R"( diff --git a/test/libsolidity/semanticTests/tryCatch/try_catch_library_call.sol b/test/libsolidity/semanticTests/tryCatch/try_catch_library_call.sol new file mode 100644 index 000000000..e7bad76b0 --- /dev/null +++ b/test/libsolidity/semanticTests/tryCatch/try_catch_library_call.sol @@ -0,0 +1,45 @@ +library L { + struct S { uint x; } + function integer(uint t, bool b) public view returns (uint) { + if (b) { + return t; + } else { + revert("failure"); + } + } + function stru(S storage t, bool b) public view returns (uint) { + if (b) { + return t.x; + } else { + revert("failure"); + } + } +} +contract C { + using L for L.S; + L.S t; + function f(bool b) public returns (uint, string memory) { + uint x = 8; + try L.integer(x, b) returns (uint _x) { + return (_x, ""); + } catch Error(string memory message) { + return (18, message); + } + } + function g(bool b) public returns (uint, string memory) { + t.x = 9; + try t.stru(b) returns (uint x) { + return (x, ""); + } catch Error(string memory message) { + return (19, message); + } + } +} +// ==== +// EVMVersion: >=byzantium +// ---- +// library: L +// f(bool): true -> 8, 0x40, 0 +// f(bool): false -> 18, 0x40, 7, "failure" +// g(bool): true -> 9, 0x40, 0 +// g(bool): false -> 19, 0x40, 7, "failure" From 11033c95361216c0e8ce4bb153465e18c3c55ecc Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Thu, 12 Nov 2020 15:29:08 +0100 Subject: [PATCH 2/5] Moving some bytes and array tests to semanticTests --- test/libsolidity/SolidityEndToEndTest.cpp | 398 ------------------ .../array/array_pop_array_transition.sol | 26 ++ .../array/array_pop_storage_empty.sol | 12 + .../array/array_pop_uint16_transition.sol | 23 + .../array/array_pop_uint24_transition.sol | 23 + .../array_copy_storage_storage_dyn_dyn.sol | 22 + .../array_copy_storage_storage_struct.sol | 20 + .../copying/array_copy_target_leftover.sol | 19 + .../array/copying/bytes_inside_mappings.sol | 15 + .../array/copying/copy_removes_bytes_data.sol | 12 + .../array/delete_removes_bytes_data.sol | 12 + .../array/dynamic_array_cleanup.sol | 23 + .../array/dynamic_multi_array_cleanup.sol | 23 + .../array/fixed_array_cleanup.sol | 17 + .../array/short_fixed_array_cleanup.sol | 17 + ...truct_containing_bytes_copy_and_delete.sol | 33 ++ 16 files changed, 297 insertions(+), 398 deletions(-) create mode 100644 test/libsolidity/semanticTests/array/array_pop_array_transition.sol create mode 100644 test/libsolidity/semanticTests/array/array_pop_storage_empty.sol create mode 100644 test/libsolidity/semanticTests/array/array_pop_uint16_transition.sol create mode 100644 test/libsolidity/semanticTests/array/array_pop_uint24_transition.sol create mode 100644 test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol create mode 100644 test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_struct.sol create mode 100644 test/libsolidity/semanticTests/array/copying/array_copy_target_leftover.sol create mode 100644 test/libsolidity/semanticTests/array/copying/bytes_inside_mappings.sol create mode 100644 test/libsolidity/semanticTests/array/copying/copy_removes_bytes_data.sol create mode 100644 test/libsolidity/semanticTests/array/delete_removes_bytes_data.sol create mode 100644 test/libsolidity/semanticTests/array/dynamic_array_cleanup.sol create mode 100644 test/libsolidity/semanticTests/array/dynamic_multi_array_cleanup.sol create mode 100644 test/libsolidity/semanticTests/array/fixed_array_cleanup.sol create mode 100644 test/libsolidity/semanticTests/array/short_fixed_array_cleanup.sol create mode 100644 test/libsolidity/semanticTests/structs/struct_containing_bytes_copy_and_delete.sol diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 3072d2038..f8472d191 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -2593,25 +2593,6 @@ BOOST_AUTO_TEST_CASE(copying_bytes_multiassign) ABI_CHECK(callContractFunction("val()"), encodeArgs(0x80)); } -BOOST_AUTO_TEST_CASE(delete_removes_bytes_data) -{ - char const* sourceCode = R"( - contract c { - fallback() external { data = msg.data; } - function del() public returns (bool) { delete data; return true; } - bytes data; - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("---", 7), bytes()); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("del()", 7), encodeArgs(true)); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data) { char const* sourceCode = R"( @@ -2633,83 +2614,6 @@ BOOST_AUTO_TEST_CASE(copy_from_calldata_removes_bytes_data) ); } -BOOST_AUTO_TEST_CASE(copy_removes_bytes_data) -{ - char const* sourceCode = R"( - contract c { - function set() public returns (bool) { data1 = msg.data; return true; } - function reset() public returns (bool) { data1 = data2; return true; } - bytes data1; - bytes data2; - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("set()", 1, 2, 3, 4, 5), encodeArgs(true)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("reset()"), encodeArgs(true)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - -BOOST_AUTO_TEST_CASE(bytes_inside_mappings) -{ - char const* sourceCode = R"( - contract c { - function set(uint key) public returns (bool) { data[key] = msg.data; return true; } - function copy(uint from, uint to) public returns (bool) { data[to] = data[from]; return true; } - mapping(uint => bytes) data; - } - )"; - compileAndRun(sourceCode); - // store a short byte array at 1 and a longer one at 2 - ABI_CHECK(callContractFunction("set(uint256)", 1, 2), encodeArgs(true)); - ABI_CHECK(callContractFunction("set(uint256)", 2, 2, 3, 4, 5), encodeArgs(true)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - // copy shorter to longer - ABI_CHECK(callContractFunction("copy(uint256,uint256)", 1, 2), encodeArgs(true)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - // copy empty to both - ABI_CHECK(callContractFunction("copy(uint256,uint256)", 99, 1), encodeArgs(true)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("copy(uint256,uint256)", 99, 2), encodeArgs(true)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - -BOOST_AUTO_TEST_CASE(struct_containing_bytes_copy_and_delete) -{ - char const* sourceCode = R"( - contract c { - struct Struct { uint a; bytes data; uint b; } - Struct data1; - Struct data2; - function set(uint _a, bytes calldata _data, uint _b) external returns (bool) { - data1.a = _a; - data1.b = _b; - data1.data = _data; - return true; - } - function copy() public returns (bool) { - data1 = data2; - return true; - } - function del() public returns (bool) { - delete data1; - return true; - } - } - )"; - compileAndRun(sourceCode); - string data = "123456789012345678901234567890123"; - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("set(uint256,bytes,uint256)", 12, 0x60, 13, u256(data.length()), data), encodeArgs(true)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("copy()"), encodeArgs(true)); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("set(uint256,bytes,uint256)", 12, 0x60, 13, u256(data.length()), data), encodeArgs(true)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("del()"), encodeArgs(true)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - BOOST_AUTO_TEST_CASE(storing_invalid_boolean) { char const* sourceCode = R"( @@ -2894,197 +2798,6 @@ BOOST_AUTO_TEST_CASE(bytes_in_arguments) ); } -BOOST_AUTO_TEST_CASE(fixed_array_cleanup) -{ - char const* sourceCode = R"( - contract c { - uint spacer1; - uint spacer2; - uint[20] data; - function fill() public { - for (uint i = 0; i < data.length; ++i) data[i] = i+1; - } - function clear() public { delete data; } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - - compileAndRun(sourceCode); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("fill()"), bytes()); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("clear()"), bytes()); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(short_fixed_array_cleanup) -{ - char const* sourceCode = R"( - contract c { - uint spacer1; - uint spacer2; - uint[3] data; - function fill() public { - for (uint i = 0; i < data.length; ++i) data[i] = i+1; - } - function clear() public { delete data; } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - - compileAndRun(sourceCode); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("fill()"), bytes()); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("clear()"), bytes()); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(dynamic_array_cleanup) -{ - char const* sourceCode = R"( - contract c { - uint[20] spacer; - uint[] dynamic; - function fill() public { - for (uint i = 0; i < 21; ++i) - dynamic.push(i + 1); - } - function halfClear() public { - while (dynamic.length > 5) - dynamic.pop(); - } - function fullClear() public { delete dynamic; } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - - compileAndRun(sourceCode); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("fill()"), bytes()); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("halfClear()"), bytes()); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("fullClear()"), bytes()); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(dynamic_multi_array_cleanup) -{ - char const* sourceCode = R"( - contract c { - struct s { uint[][] d; } - s[] data; - function fill() public returns (uint) { - while (data.length < 3) - data.push(); - while (data[2].d.length < 4) - data[2].d.push(); - while (data[2].d[3].length < 5) - data[2].d[3].push(); - data[2].d[3][4] = 8; - return data[2].d[3][4]; - } - function clear() public { delete data; } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - - compileAndRun(sourceCode); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("fill()"), encodeArgs(8)); - BOOST_CHECK(!storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("clear()"), bytes()); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(array_copy_storage_storage_dyn_dyn) -{ - char const* sourceCode = R"( - contract c { - uint[] data1; - uint[] data2; - function setData1(uint length, uint index, uint value) public { - data1 = new uint[](length); - if (index < length) - data1[index] = value; - } - function copyStorageStorage() public { data2 = data1; } - function getData2(uint index) public returns (uint len, uint val) { - len = data2.length; if (index < len) val = data2[index]; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("setData1(uint256,uint256,uint256)", 10, 5, 4), bytes()); - ABI_CHECK(callContractFunction("copyStorageStorage()"), bytes()); - ABI_CHECK(callContractFunction("getData2(uint256)", 5), encodeArgs(10, 4)); - ABI_CHECK(callContractFunction("setData1(uint256,uint256,uint256)", 0, 0, 0), bytes()); - ABI_CHECK(callContractFunction("copyStorageStorage()"), bytes()); - ABI_CHECK(callContractFunction("getData2(uint256)", 0), encodeArgs(0, 0)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - -BOOST_AUTO_TEST_CASE(array_copy_target_leftover) -{ - // test that leftover elements in the last slot of target are correctly cleared during assignment - char const* sourceCode = R"( - contract c { - byte[10] data1; - bytes2[32] data2; - function test() public returns (uint check, uint res1, uint res2) { - uint i; - for (i = 0; i < data2.length; ++i) - data2[i] = 0xffff; - check = uint(uint16(data2[31])) * 0x10000 | uint(uint16(data2[14])); - for (i = 0; i < data1.length; ++i) - data1[i] = byte(uint8(1 + i)); - data2 = data1; - for (i = 0; i < 16; ++i) - res1 |= uint(uint16(data2[i])) * 0x10000**i; - for (i = 0; i < 16; ++i) - res2 |= uint(uint16(data2[16 + i])) * 0x10000**i; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(u256("0xffffffff"), asString(fromHex("0000000000000000000000000a00090008000700060005000400030002000100")), asString(fromHex("0000000000000000000000000000000000000000000000000000000000000000")))); -} - -BOOST_AUTO_TEST_CASE(array_copy_storage_storage_struct) -{ - char const* sourceCode = R"( - contract c { - struct Data { uint x; uint y; } - Data[] data1; - Data[] data2; - function test() public returns (uint x, uint y) { - while (data1.length < 9) - data1.push(); - data1[8].x = 4; - data1[8].y = 5; - data2 = data1; - x = data2[8].x; - y = data2[8].y; - while (data1.length > 0) - data1.pop(); - data2 = data1; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(4, 5)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - BOOST_AUTO_TEST_CASE(array_copy_storage_abi) { // NOTE: This does not really test copying from storage to ABI directly, @@ -3141,117 +2854,6 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi) ); } -BOOST_AUTO_TEST_CASE(array_pop_uint16_transition) -{ - char const* sourceCode = R"( - contract c { - uint16[] data; - function test() public returns (uint16 x, uint16 y, uint16 z) { - for (uint i = 1; i <= 48; i++) - data.push(uint16(i)); - for (uint j = 1; j <= 10; j++) - data.pop(); - x = data[data.length - 1]; - for (uint k = 1; k <= 10; k++) - data.pop(); - y = data[data.length - 1]; - for (uint l = 1; l <= 10; l++) - data.pop(); - z = data[data.length - 1]; - for (uint m = 1; m <= 18; m++) - data.pop(); - } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(38, 28, 18)); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(array_pop_uint24_transition) -{ - char const* sourceCode = R"( - contract c { - uint256 a; - uint256 b; - uint256 c; - uint24[] data; - function test() public returns (uint24 x, uint24 y) { - for (uint i = 1; i <= 30; i++) - data.push(uint24(i)); - for (uint j = 1; j <= 10; j++) - data.pop(); - x = data[data.length - 1]; - for (uint k = 1; k <= 10; k++) - data.pop(); - y = data[data.length - 1]; - for (uint l = 1; l <= 10; l++) - data.pop(); - } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(20, 10)); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(array_pop_array_transition) -{ - char const* sourceCode = R"( - contract c { - uint256 a; - uint256 b; - uint256 c; - uint16[] inner = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; - uint16[][] data; - function test() public returns (uint x, uint y, uint z) { - for (uint i = 1; i <= 48; i++) - data.push(inner); - for (uint j = 1; j <= 10; j++) - data.pop(); - x = data[data.length - 1][0]; - for (uint k = 1; k <= 10; k++) - data.pop(); - y = data[data.length - 1][1]; - for (uint l = 1; l <= 10; l++) - data.pop(); - z = data[data.length - 1][2]; - for (uint m = 1; m <= 18; m++) - data.pop(); - delete inner; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(1, 2, 3)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - -BOOST_AUTO_TEST_CASE(array_pop_storage_empty) -{ - char const* sourceCode = R"( - contract c { - uint[] data; - function test() public { - data.push(7); - data.pop(); - } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs()); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - BOOST_AUTO_TEST_CASE(byte_array_pop_storage_empty) { char const* sourceCode = R"( diff --git a/test/libsolidity/semanticTests/array/array_pop_array_transition.sol b/test/libsolidity/semanticTests/array/array_pop_array_transition.sol new file mode 100644 index 000000000..d826dea30 --- /dev/null +++ b/test/libsolidity/semanticTests/array/array_pop_array_transition.sol @@ -0,0 +1,26 @@ +contract c { + uint256 a; + uint256 b; + uint256 c; + uint16[] inner = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + uint16[][] data; + function test() public returns (uint x, uint y, uint z) { + for (uint i = 1; i <= 48; i++) + data.push(inner); + for (uint j = 1; j <= 10; j++) + data.pop(); + x = data[data.length - 1][0]; + for (uint k = 1; k <= 10; k++) + data.pop(); + y = data[data.length - 1][1]; + for (uint l = 1; l <= 10; l++) + data.pop(); + z = data[data.length - 1][2]; + for (uint m = 1; m <= 18; m++) + data.pop(); + delete inner; + } +} +// ---- +// test() -> 1, 2, 3 +// storage: empty diff --git a/test/libsolidity/semanticTests/array/array_pop_storage_empty.sol b/test/libsolidity/semanticTests/array/array_pop_storage_empty.sol new file mode 100644 index 000000000..24f210177 --- /dev/null +++ b/test/libsolidity/semanticTests/array/array_pop_storage_empty.sol @@ -0,0 +1,12 @@ +contract c { + uint[] data; + function test() public { + data.push(7); + data.pop(); + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> +// storage: empty diff --git a/test/libsolidity/semanticTests/array/array_pop_uint16_transition.sol b/test/libsolidity/semanticTests/array/array_pop_uint16_transition.sol new file mode 100644 index 000000000..8cecac615 --- /dev/null +++ b/test/libsolidity/semanticTests/array/array_pop_uint16_transition.sol @@ -0,0 +1,23 @@ +contract c { + uint16[] data; + function test() public returns (uint16 x, uint16 y, uint16 z) { + for (uint i = 1; i <= 48; i++) + data.push(uint16(i)); + for (uint j = 1; j <= 10; j++) + data.pop(); + x = data[data.length - 1]; + for (uint k = 1; k <= 10; k++) + data.pop(); + y = data[data.length - 1]; + for (uint l = 1; l <= 10; l++) + data.pop(); + z = data[data.length - 1]; + for (uint m = 1; m <= 18; m++) + data.pop(); + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> 38, 28, 18 +// storage: empty diff --git a/test/libsolidity/semanticTests/array/array_pop_uint24_transition.sol b/test/libsolidity/semanticTests/array/array_pop_uint24_transition.sol new file mode 100644 index 000000000..467774cc2 --- /dev/null +++ b/test/libsolidity/semanticTests/array/array_pop_uint24_transition.sol @@ -0,0 +1,23 @@ +contract c { + uint256 a; + uint256 b; + uint256 c; + uint24[] data; + function test() public returns (uint24 x, uint24 y) { + for (uint i = 1; i <= 30; i++) + data.push(uint24(i)); + for (uint j = 1; j <= 10; j++) + data.pop(); + x = data[data.length - 1]; + for (uint k = 1; k <= 10; k++) + data.pop(); + y = data[data.length - 1]; + for (uint l = 1; l <= 10; l++) + data.pop(); + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> 20, 10 +// storage: empty diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol b/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol new file mode 100644 index 000000000..c183d8a82 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol @@ -0,0 +1,22 @@ + +contract c { + uint[] data1; + uint[] data2; + function setData1(uint length, uint index, uint value) public { + data1 = new uint[](length); + if (index < length) + data1[index] = value; + } + function copyStorageStorage() public { data2 = data1; } + function getData2(uint index) public returns (uint len, uint val) { + len = data2.length; if (index < len) val = data2[index]; + } +} +// ---- +// setData1(uint256,uint256,uint256): 10, 5, 4 -> +// copyStorageStorage() -> +// getData2(uint256): 5 -> 10, 4 +// setData1(uint256,uint256,uint256): 0, 0, 0 -> +// copyStorageStorage() -> +// getData2(uint256): 0 -> 0, 0 +// storage: empty diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_struct.sol b/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_struct.sol new file mode 100644 index 000000000..5c53eaa77 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_struct.sol @@ -0,0 +1,20 @@ +contract c { + struct Data { uint x; uint y; } + Data[] data1; + Data[] data2; + function test() public returns (uint x, uint y) { + while (data1.length < 9) + data1.push(); + data1[8].x = 4; + data1[8].y = 5; + data2 = data1; + x = data2[8].x; + y = data2[8].y; + while (data1.length > 0) + data1.pop(); + data2 = data1; + } +} +// ---- +// test() -> 4, 5 +// storage: empty diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_target_leftover.sol b/test/libsolidity/semanticTests/array/copying/array_copy_target_leftover.sol new file mode 100644 index 000000000..88d741654 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/array_copy_target_leftover.sol @@ -0,0 +1,19 @@ +contract c { + byte[10] data1; + bytes2[32] data2; + function test() public returns (uint check, uint res1, uint res2) { + uint i; + for (i = 0; i < data2.length; ++i) + data2[i] = 0xffff; + check = uint(uint16(data2[31])) * 0x10000 | uint(uint16(data2[14])); + for (i = 0; i < data1.length; ++i) + data1[i] = byte(uint8(1 + i)); + data2 = data1; + for (i = 0; i < 16; ++i) + res1 |= uint(uint16(data2[i])) * 0x10000**i; + for (i = 0; i < 16; ++i) + res2 |= uint(uint16(data2[16 + i])) * 0x10000**i; + } +} +// ---- +// test() -> 0xffffffff, 0x0000000000000000000000000a00090008000700060005000400030002000100, 0x0000000000000000000000000000000000000000000000000000000000000000 diff --git a/test/libsolidity/semanticTests/array/copying/bytes_inside_mappings.sol b/test/libsolidity/semanticTests/array/copying/bytes_inside_mappings.sol new file mode 100644 index 000000000..21b5e9c5d --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/bytes_inside_mappings.sol @@ -0,0 +1,15 @@ +contract c { + function set(uint key) public returns (bool) { data[key] = msg.data; return true; } + function copy(uint from, uint to) public returns (bool) { data[to] = data[from]; return true; } + mapping(uint => bytes) data; +} +// ---- +// set(uint256): 1, 2 -> true +// set(uint256): 2, 2, 3, 4, 5 -> true +// storage: nonempty +// copy(uint256,uint256): 1, 2 -> true +// storage: nonempty +// copy(uint256,uint256): 99, 1 -> true +// storage: nonempty +// copy(uint256,uint256): 99, 2 -> true +// storage: empty diff --git a/test/libsolidity/semanticTests/array/copying/copy_removes_bytes_data.sol b/test/libsolidity/semanticTests/array/copying/copy_removes_bytes_data.sol new file mode 100644 index 000000000..bb97ae2c1 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/copy_removes_bytes_data.sol @@ -0,0 +1,12 @@ + +contract c { + function set() public returns (bool) { data1 = msg.data; return true; } + function reset() public returns (bool) { data1 = data2; return true; } + bytes data1; + bytes data2; +} +// ---- +// set(): 1, 2, 3, 4, 5 -> true +// storage: nonempty +// reset() -> true +// storage: empty diff --git a/test/libsolidity/semanticTests/array/delete_removes_bytes_data.sol b/test/libsolidity/semanticTests/array/delete_removes_bytes_data.sol new file mode 100644 index 000000000..28d6e5d8f --- /dev/null +++ b/test/libsolidity/semanticTests/array/delete_removes_bytes_data.sol @@ -0,0 +1,12 @@ +contract c { + fallback() external { data = msg.data; } + function del() public returns (bool) { delete data; return true; } + bytes data; +} +// ==== +// compileViaYul: also +// ---- +// (): 7 -> +// storage: nonempty +// del(): 7 -> true +// storage: empty diff --git a/test/libsolidity/semanticTests/array/dynamic_array_cleanup.sol b/test/libsolidity/semanticTests/array/dynamic_array_cleanup.sol new file mode 100644 index 000000000..07c3f63aa --- /dev/null +++ b/test/libsolidity/semanticTests/array/dynamic_array_cleanup.sol @@ -0,0 +1,23 @@ +contract c { + uint[20] spacer; + uint[] dynamic; + function fill() public { + for (uint i = 0; i < 21; ++i) + dynamic.push(i + 1); + } + function halfClear() public { + while (dynamic.length > 5) + dynamic.pop(); + } + function fullClear() public { delete dynamic; } +} +// ==== +// compileViaYul: also +// ---- +// storage: empty +// fill() -> +// storage: nonempty +// halfClear() -> +// storage: nonempty +// fullClear() -> +// storage: empty diff --git a/test/libsolidity/semanticTests/array/dynamic_multi_array_cleanup.sol b/test/libsolidity/semanticTests/array/dynamic_multi_array_cleanup.sol new file mode 100644 index 000000000..c58abad52 --- /dev/null +++ b/test/libsolidity/semanticTests/array/dynamic_multi_array_cleanup.sol @@ -0,0 +1,23 @@ +contract c { + struct s { uint[][] d; } + s[] data; + function fill() public returns (uint) { + while (data.length < 3) + data.push(); + while (data[2].d.length < 4) + data[2].d.push(); + while (data[2].d[3].length < 5) + data[2].d[3].push(); + data[2].d[3][4] = 8; + return data[2].d[3][4]; + } + function clear() public { delete data; } +} +// ==== +// compileViaYul: also +// ---- +// storage: empty +// fill() -> 8 +// storage: nonempty +// clear() -> +// storage: empty diff --git a/test/libsolidity/semanticTests/array/fixed_array_cleanup.sol b/test/libsolidity/semanticTests/array/fixed_array_cleanup.sol new file mode 100644 index 000000000..9a6f35452 --- /dev/null +++ b/test/libsolidity/semanticTests/array/fixed_array_cleanup.sol @@ -0,0 +1,17 @@ +contract c { + uint spacer1; + uint spacer2; + uint[20] data; + function fill() public { + for (uint i = 0; i < data.length; ++i) data[i] = i+1; + } + function clear() public { delete data; } +} +// ==== +// compileViaYul: also +// ---- +// storage: empty +// fill() -> +// storage: nonempty +// clear() -> +// storage: empty diff --git a/test/libsolidity/semanticTests/array/short_fixed_array_cleanup.sol b/test/libsolidity/semanticTests/array/short_fixed_array_cleanup.sol new file mode 100644 index 000000000..8909ed48e --- /dev/null +++ b/test/libsolidity/semanticTests/array/short_fixed_array_cleanup.sol @@ -0,0 +1,17 @@ +contract c { + uint spacer1; + uint spacer2; + uint[3] data; + function fill() public { + for (uint i = 0; i < data.length; ++i) data[i] = i+1; + } + function clear() public { delete data; } +} +// ==== +// compileViaYul: also +// ---- +// storage: empty +// fill() -> +// storage: nonempty +// clear() -> +// storage: empty diff --git a/test/libsolidity/semanticTests/structs/struct_containing_bytes_copy_and_delete.sol b/test/libsolidity/semanticTests/structs/struct_containing_bytes_copy_and_delete.sol new file mode 100644 index 000000000..c6e25f9d5 --- /dev/null +++ b/test/libsolidity/semanticTests/structs/struct_containing_bytes_copy_and_delete.sol @@ -0,0 +1,33 @@ +contract c { + struct Struct { uint a; bytes data; uint b; } + Struct data1; + Struct data2; + function set(uint _a, bytes calldata _data, uint _b) external returns (bool) { + data1.a = _a; + data1.b = _b; + data1.data = _data; + return true; + } + function copy() public returns (bool) { + data1 = data2; + return true; + } + function del() public returns (bool) { + delete data1; + return true; + } + function test(uint256 i) public returns (byte) { + return data1.data[i]; + } +} +// ---- +// storage: empty +// set(uint256,bytes,uint256): 12, 0x60, 13, 33, "12345678901234567890123456789012", "3" -> true +// test(uint256): 32 -> "3" +// storage: nonempty +// copy() -> true +// storage: empty +// set(uint256,bytes,uint256): 12, 0x60, 13, 33, "12345678901234567890123456789012", "3" -> true +// storage: nonempty +// del() -> true +// storage: empty From 31981bad120c2f8dc4c7c1c78665c3f7b19f747e Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Fri, 13 Nov 2020 09:38:11 +0100 Subject: [PATCH 3/5] Organizing array tests into more directories. --- .../array/{ => copying}/array_copy_cleanup_uint128.sol | 0 .../array/{ => copying}/array_copy_cleanup_uint40.sol | 0 .../semanticTests/array/{ => delete}/bytes_delete_element.sol | 0 .../semanticTests/array/{ => delete}/delete_bytes_array.sol | 0 .../semanticTests/array/{ => delete}/delete_memory_array.sol | 0 .../array/{ => delete}/delete_on_array_of_structs.sol | 0 .../array/{ => delete}/delete_removes_bytes_data.sol | 0 .../semanticTests/array/{ => delete}/delete_storage_array.sol | 0 .../array/{ => delete}/delete_storage_array_packed.sol | 0 .../{ => indexAccess}/arrays_complex_memory_index_access.sol | 0 .../array/{ => indexAccess}/bytes_memory_index_access.sol | 0 .../array/{ => indexAccess}/fixed_bytes_index_access.sol | 0 .../semanticTests/array/{ => indexAccess}/index_access.sol | 0 .../array/{ => indexAccess}/inline_array_index_access_ints.sol | 0 .../array/{ => indexAccess}/inline_array_index_access_strings.sol | 0 .../memory_arrays_dynamic_index_access_write.sol | 0 .../array/{ => indexAccess}/memory_arrays_index_access_write.sol | 0 test/libsolidity/semanticTests/array/{ => pop}/array_pop.sol | 0 .../semanticTests/array/{ => pop}/array_pop_array_transition.sol | 0 .../semanticTests/array/{ => pop}/array_pop_empty_exception.sol | 0 .../semanticTests/array/{ => pop}/array_pop_isolated.sol | 0 .../semanticTests/array/{ => pop}/array_pop_storage_empty.sol | 0 .../semanticTests/array/{ => pop}/array_pop_uint16_transition.sol | 0 .../semanticTests/array/{ => pop}/array_pop_uint24_transition.sol | 0 test/libsolidity/semanticTests/array/{ => pop}/byte_array_pop.sol | 0 .../semanticTests/array/{ => pop}/byte_array_pop_copy_long.sol | 0 .../array/{ => pop}/byte_array_pop_empty_exception.sol | 0 .../semanticTests/array/{ => pop}/byte_array_pop_isolated.sol | 0 .../semanticTests/array/{ => pop}/byte_array_pop_masking_long.sol | 0 test/libsolidity/semanticTests/array/{ => push}/array_push.sol | 0 .../semanticTests/array/{ => push}/array_push_nested.sol | 0 .../semanticTests/array/{ => push}/array_push_packed_array.sol | 0 .../semanticTests/array/{ => push}/array_push_struct.sol | 0 .../semanticTests/array/{ => push}/byte_array_push.sol | 0 .../semanticTests/array/{ => push}/byte_array_push_transition.sol | 0 .../semanticTests/array/{ => push}/push_no_args_1d.sol | 0 .../semanticTests/array/{ => push}/push_no_args_2d.sol | 0 .../semanticTests/array/{ => push}/push_no_args_bytes.sol | 0 .../semanticTests/array/{ => push}/push_no_args_struct.sol | 0 39 files changed, 0 insertions(+), 0 deletions(-) rename test/libsolidity/semanticTests/array/{ => copying}/array_copy_cleanup_uint128.sol (100%) rename test/libsolidity/semanticTests/array/{ => copying}/array_copy_cleanup_uint40.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/bytes_delete_element.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/delete_bytes_array.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/delete_memory_array.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/delete_on_array_of_structs.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/delete_removes_bytes_data.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/delete_storage_array.sol (100%) rename test/libsolidity/semanticTests/array/{ => delete}/delete_storage_array_packed.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/arrays_complex_memory_index_access.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/bytes_memory_index_access.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/fixed_bytes_index_access.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/index_access.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/inline_array_index_access_ints.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/inline_array_index_access_strings.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/memory_arrays_dynamic_index_access_write.sol (100%) rename test/libsolidity/semanticTests/array/{ => indexAccess}/memory_arrays_index_access_write.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop_array_transition.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop_empty_exception.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop_isolated.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop_storage_empty.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop_uint16_transition.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/array_pop_uint24_transition.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/byte_array_pop.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/byte_array_pop_copy_long.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/byte_array_pop_empty_exception.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/byte_array_pop_isolated.sol (100%) rename test/libsolidity/semanticTests/array/{ => pop}/byte_array_pop_masking_long.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/array_push.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/array_push_nested.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/array_push_packed_array.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/array_push_struct.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/byte_array_push.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/byte_array_push_transition.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/push_no_args_1d.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/push_no_args_2d.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/push_no_args_bytes.sol (100%) rename test/libsolidity/semanticTests/array/{ => push}/push_no_args_struct.sol (100%) diff --git a/test/libsolidity/semanticTests/array/array_copy_cleanup_uint128.sol b/test/libsolidity/semanticTests/array/copying/array_copy_cleanup_uint128.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_copy_cleanup_uint128.sol rename to test/libsolidity/semanticTests/array/copying/array_copy_cleanup_uint128.sol diff --git a/test/libsolidity/semanticTests/array/array_copy_cleanup_uint40.sol b/test/libsolidity/semanticTests/array/copying/array_copy_cleanup_uint40.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_copy_cleanup_uint40.sol rename to test/libsolidity/semanticTests/array/copying/array_copy_cleanup_uint40.sol diff --git a/test/libsolidity/semanticTests/array/bytes_delete_element.sol b/test/libsolidity/semanticTests/array/delete/bytes_delete_element.sol similarity index 100% rename from test/libsolidity/semanticTests/array/bytes_delete_element.sol rename to test/libsolidity/semanticTests/array/delete/bytes_delete_element.sol diff --git a/test/libsolidity/semanticTests/array/delete_bytes_array.sol b/test/libsolidity/semanticTests/array/delete/delete_bytes_array.sol similarity index 100% rename from test/libsolidity/semanticTests/array/delete_bytes_array.sol rename to test/libsolidity/semanticTests/array/delete/delete_bytes_array.sol diff --git a/test/libsolidity/semanticTests/array/delete_memory_array.sol b/test/libsolidity/semanticTests/array/delete/delete_memory_array.sol similarity index 100% rename from test/libsolidity/semanticTests/array/delete_memory_array.sol rename to test/libsolidity/semanticTests/array/delete/delete_memory_array.sol diff --git a/test/libsolidity/semanticTests/array/delete_on_array_of_structs.sol b/test/libsolidity/semanticTests/array/delete/delete_on_array_of_structs.sol similarity index 100% rename from test/libsolidity/semanticTests/array/delete_on_array_of_structs.sol rename to test/libsolidity/semanticTests/array/delete/delete_on_array_of_structs.sol diff --git a/test/libsolidity/semanticTests/array/delete_removes_bytes_data.sol b/test/libsolidity/semanticTests/array/delete/delete_removes_bytes_data.sol similarity index 100% rename from test/libsolidity/semanticTests/array/delete_removes_bytes_data.sol rename to test/libsolidity/semanticTests/array/delete/delete_removes_bytes_data.sol diff --git a/test/libsolidity/semanticTests/array/delete_storage_array.sol b/test/libsolidity/semanticTests/array/delete/delete_storage_array.sol similarity index 100% rename from test/libsolidity/semanticTests/array/delete_storage_array.sol rename to test/libsolidity/semanticTests/array/delete/delete_storage_array.sol diff --git a/test/libsolidity/semanticTests/array/delete_storage_array_packed.sol b/test/libsolidity/semanticTests/array/delete/delete_storage_array_packed.sol similarity index 100% rename from test/libsolidity/semanticTests/array/delete_storage_array_packed.sol rename to test/libsolidity/semanticTests/array/delete/delete_storage_array_packed.sol diff --git a/test/libsolidity/semanticTests/array/arrays_complex_memory_index_access.sol b/test/libsolidity/semanticTests/array/indexAccess/arrays_complex_memory_index_access.sol similarity index 100% rename from test/libsolidity/semanticTests/array/arrays_complex_memory_index_access.sol rename to test/libsolidity/semanticTests/array/indexAccess/arrays_complex_memory_index_access.sol diff --git a/test/libsolidity/semanticTests/array/bytes_memory_index_access.sol b/test/libsolidity/semanticTests/array/indexAccess/bytes_memory_index_access.sol similarity index 100% rename from test/libsolidity/semanticTests/array/bytes_memory_index_access.sol rename to test/libsolidity/semanticTests/array/indexAccess/bytes_memory_index_access.sol diff --git a/test/libsolidity/semanticTests/array/fixed_bytes_index_access.sol b/test/libsolidity/semanticTests/array/indexAccess/fixed_bytes_index_access.sol similarity index 100% rename from test/libsolidity/semanticTests/array/fixed_bytes_index_access.sol rename to test/libsolidity/semanticTests/array/indexAccess/fixed_bytes_index_access.sol diff --git a/test/libsolidity/semanticTests/array/index_access.sol b/test/libsolidity/semanticTests/array/indexAccess/index_access.sol similarity index 100% rename from test/libsolidity/semanticTests/array/index_access.sol rename to test/libsolidity/semanticTests/array/indexAccess/index_access.sol diff --git a/test/libsolidity/semanticTests/array/inline_array_index_access_ints.sol b/test/libsolidity/semanticTests/array/indexAccess/inline_array_index_access_ints.sol similarity index 100% rename from test/libsolidity/semanticTests/array/inline_array_index_access_ints.sol rename to test/libsolidity/semanticTests/array/indexAccess/inline_array_index_access_ints.sol diff --git a/test/libsolidity/semanticTests/array/inline_array_index_access_strings.sol b/test/libsolidity/semanticTests/array/indexAccess/inline_array_index_access_strings.sol similarity index 100% rename from test/libsolidity/semanticTests/array/inline_array_index_access_strings.sol rename to test/libsolidity/semanticTests/array/indexAccess/inline_array_index_access_strings.sol diff --git a/test/libsolidity/semanticTests/array/memory_arrays_dynamic_index_access_write.sol b/test/libsolidity/semanticTests/array/indexAccess/memory_arrays_dynamic_index_access_write.sol similarity index 100% rename from test/libsolidity/semanticTests/array/memory_arrays_dynamic_index_access_write.sol rename to test/libsolidity/semanticTests/array/indexAccess/memory_arrays_dynamic_index_access_write.sol diff --git a/test/libsolidity/semanticTests/array/memory_arrays_index_access_write.sol b/test/libsolidity/semanticTests/array/indexAccess/memory_arrays_index_access_write.sol similarity index 100% rename from test/libsolidity/semanticTests/array/memory_arrays_index_access_write.sol rename to test/libsolidity/semanticTests/array/indexAccess/memory_arrays_index_access_write.sol diff --git a/test/libsolidity/semanticTests/array/array_pop.sol b/test/libsolidity/semanticTests/array/pop/array_pop.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop.sol rename to test/libsolidity/semanticTests/array/pop/array_pop.sol diff --git a/test/libsolidity/semanticTests/array/array_pop_array_transition.sol b/test/libsolidity/semanticTests/array/pop/array_pop_array_transition.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop_array_transition.sol rename to test/libsolidity/semanticTests/array/pop/array_pop_array_transition.sol diff --git a/test/libsolidity/semanticTests/array/array_pop_empty_exception.sol b/test/libsolidity/semanticTests/array/pop/array_pop_empty_exception.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop_empty_exception.sol rename to test/libsolidity/semanticTests/array/pop/array_pop_empty_exception.sol diff --git a/test/libsolidity/semanticTests/array/array_pop_isolated.sol b/test/libsolidity/semanticTests/array/pop/array_pop_isolated.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop_isolated.sol rename to test/libsolidity/semanticTests/array/pop/array_pop_isolated.sol diff --git a/test/libsolidity/semanticTests/array/array_pop_storage_empty.sol b/test/libsolidity/semanticTests/array/pop/array_pop_storage_empty.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop_storage_empty.sol rename to test/libsolidity/semanticTests/array/pop/array_pop_storage_empty.sol diff --git a/test/libsolidity/semanticTests/array/array_pop_uint16_transition.sol b/test/libsolidity/semanticTests/array/pop/array_pop_uint16_transition.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop_uint16_transition.sol rename to test/libsolidity/semanticTests/array/pop/array_pop_uint16_transition.sol diff --git a/test/libsolidity/semanticTests/array/array_pop_uint24_transition.sol b/test/libsolidity/semanticTests/array/pop/array_pop_uint24_transition.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_pop_uint24_transition.sol rename to test/libsolidity/semanticTests/array/pop/array_pop_uint24_transition.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_pop.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_pop.sol rename to test/libsolidity/semanticTests/array/pop/byte_array_pop.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_pop_copy_long.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_copy_long.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_pop_copy_long.sol rename to test/libsolidity/semanticTests/array/pop/byte_array_pop_copy_long.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_pop_empty_exception.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_empty_exception.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_pop_empty_exception.sol rename to test/libsolidity/semanticTests/array/pop/byte_array_pop_empty_exception.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_pop_isolated.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_isolated.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_pop_isolated.sol rename to test/libsolidity/semanticTests/array/pop/byte_array_pop_isolated.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_pop_masking_long.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_masking_long.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_pop_masking_long.sol rename to test/libsolidity/semanticTests/array/pop/byte_array_pop_masking_long.sol diff --git a/test/libsolidity/semanticTests/array/array_push.sol b/test/libsolidity/semanticTests/array/push/array_push.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_push.sol rename to test/libsolidity/semanticTests/array/push/array_push.sol diff --git a/test/libsolidity/semanticTests/array/array_push_nested.sol b/test/libsolidity/semanticTests/array/push/array_push_nested.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_push_nested.sol rename to test/libsolidity/semanticTests/array/push/array_push_nested.sol diff --git a/test/libsolidity/semanticTests/array/array_push_packed_array.sol b/test/libsolidity/semanticTests/array/push/array_push_packed_array.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_push_packed_array.sol rename to test/libsolidity/semanticTests/array/push/array_push_packed_array.sol diff --git a/test/libsolidity/semanticTests/array/array_push_struct.sol b/test/libsolidity/semanticTests/array/push/array_push_struct.sol similarity index 100% rename from test/libsolidity/semanticTests/array/array_push_struct.sol rename to test/libsolidity/semanticTests/array/push/array_push_struct.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_push.sol b/test/libsolidity/semanticTests/array/push/byte_array_push.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_push.sol rename to test/libsolidity/semanticTests/array/push/byte_array_push.sol diff --git a/test/libsolidity/semanticTests/array/byte_array_push_transition.sol b/test/libsolidity/semanticTests/array/push/byte_array_push_transition.sol similarity index 100% rename from test/libsolidity/semanticTests/array/byte_array_push_transition.sol rename to test/libsolidity/semanticTests/array/push/byte_array_push_transition.sol diff --git a/test/libsolidity/semanticTests/array/push_no_args_1d.sol b/test/libsolidity/semanticTests/array/push/push_no_args_1d.sol similarity index 100% rename from test/libsolidity/semanticTests/array/push_no_args_1d.sol rename to test/libsolidity/semanticTests/array/push/push_no_args_1d.sol diff --git a/test/libsolidity/semanticTests/array/push_no_args_2d.sol b/test/libsolidity/semanticTests/array/push/push_no_args_2d.sol similarity index 100% rename from test/libsolidity/semanticTests/array/push_no_args_2d.sol rename to test/libsolidity/semanticTests/array/push/push_no_args_2d.sol diff --git a/test/libsolidity/semanticTests/array/push_no_args_bytes.sol b/test/libsolidity/semanticTests/array/push/push_no_args_bytes.sol similarity index 100% rename from test/libsolidity/semanticTests/array/push_no_args_bytes.sol rename to test/libsolidity/semanticTests/array/push/push_no_args_bytes.sol diff --git a/test/libsolidity/semanticTests/array/push_no_args_struct.sol b/test/libsolidity/semanticTests/array/push/push_no_args_struct.sol similarity index 100% rename from test/libsolidity/semanticTests/array/push_no_args_struct.sol rename to test/libsolidity/semanticTests/array/push/push_no_args_struct.sol From 29d480d0edbd5d9e3b7a63f310cb4b160cbae882 Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Fri, 13 Nov 2020 09:44:43 +0100 Subject: [PATCH 4/5] Moving some byte array pop tests to semanticTests. --- test/libsolidity/SolidityEndToEndTest.cpp | 75 ------------------- .../pop/byte_array_pop_long_storage_empty.sol | 21 ++++++ ...ray_pop_long_storage_empty_garbage_ref.sol | 20 +++++ .../pop/byte_array_pop_storage_empty.sol | 16 ++++ 4 files changed, 57 insertions(+), 75 deletions(-) create mode 100644 test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty.sol create mode 100644 test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol create mode 100644 test/libsolidity/semanticTests/array/pop/byte_array_pop_storage_empty.sol diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index f8472d191..e610f239f 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -2854,81 +2854,6 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi) ); } -BOOST_AUTO_TEST_CASE(byte_array_pop_storage_empty) -{ - char const* sourceCode = R"( - contract c { - bytes data; - function test() public { - data.push(0x07); - data.push(0x05); - data.push(0x03); - data.pop(); - data.pop(); - data.pop(); - } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs()); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(byte_array_pop_long_storage_empty) -{ - char const* sourceCode = R"( - contract c { - uint256 a; - uint256 b; - uint256 c; - bytes data; - function test() public returns (bool) { - for (uint8 i = 0; i <= 40; i++) - data.push(byte(i+1)); - for (int8 j = 40; j >= 0; j--) { - require(data[uint8(j)] == byte(j+1)); - require(data.length == uint8(j+1)); - data.pop(); - } - return true; - } - } - )"; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(true)); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ); -} - -BOOST_AUTO_TEST_CASE(byte_array_pop_long_storage_empty_garbage_ref) -{ - char const* sourceCode = R"( - contract c { - uint256 a; - uint256 b; - bytes data; - function test() public { - for (uint8 i = 0; i <= 40; i++) - data.push(0x03); - for (uint8 j = 0; j <= 40; j++) { - assembly { - mstore(0, "garbage") - } - data.pop(); - } - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs()); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - BOOST_AUTO_TEST_CASE(external_array_args) { char const* sourceCode = R"( diff --git a/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty.sol new file mode 100644 index 000000000..fb61f8c74 --- /dev/null +++ b/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty.sol @@ -0,0 +1,21 @@ +contract c { + uint256 a; + uint256 b; + uint256 c; + bytes data; + function test() public returns (bool) { + for (uint8 i = 0; i <= 40; i++) + data.push(byte(i+1)); + for (int8 j = 40; j >= 0; j--) { + require(data[uint8(j)] == byte(j+1)); + require(data.length == uint8(j+1)); + data.pop(); + } + return true; + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> true +// storage: empty diff --git a/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol new file mode 100644 index 000000000..a230eb033 --- /dev/null +++ b/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol @@ -0,0 +1,20 @@ +contract c { + uint256 a; + uint256 b; + bytes data; + function test() public { + for (uint8 i = 0; i <= 40; i++) + data.push(0x03); + for (uint8 j = 0; j <= 40; j++) { + assembly { + mstore(0, "garbage") + } + data.pop(); + } + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> +// storage: empty diff --git a/test/libsolidity/semanticTests/array/pop/byte_array_pop_storage_empty.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_storage_empty.sol new file mode 100644 index 000000000..db8899388 --- /dev/null +++ b/test/libsolidity/semanticTests/array/pop/byte_array_pop_storage_empty.sol @@ -0,0 +1,16 @@ +contract c { + bytes data; + function test() public { + data.push(0x07); + data.push(0x05); + data.push(0x03); + data.pop(); + data.pop(); + data.pop(); + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> +// storage: empty From 1428a939a667b7dd4cc1adc11f40939ee6ceb0ef Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Fri, 13 Nov 2020 12:14:30 +0100 Subject: [PATCH 5/5] Moving some more array tests to semanticTests. --- test/libsolidity/SolidityEndToEndTest.cpp | 401 ------------------ .../array/calldata_array_two_dimensional.sol | 38 ++ .../calldata_array_two_dimensional_1.sol | 38 ++ .../array/calldata_bytes_array_bounds.sol | 12 + .../copying/array_copy_calldata_storage.sol | 25 ++ .../copying/array_copy_including_array.sol | 40 ++ .../copying/arrays_from_and_to_storage.sol | 18 + .../array/delete/memory_arrays_delete.sol | 15 + .../array/external_array_args.sol | 12 + .../array/indexAccess/bytes_index_access.sol | 27 ++ .../indexAccess/bytes_index_access_memory.sol | 17 + .../constructor/arrays_in_constructors.sol | 28 ++ .../bytes_in_constructors_packer.sol | 28 ++ .../bytes_in_constructors_unpacker.sol | 12 + 14 files changed, 310 insertions(+), 401 deletions(-) create mode 100644 test/libsolidity/semanticTests/array/calldata_array_two_dimensional.sol create mode 100644 test/libsolidity/semanticTests/array/calldata_array_two_dimensional_1.sol create mode 100644 test/libsolidity/semanticTests/array/calldata_bytes_array_bounds.sol create mode 100644 test/libsolidity/semanticTests/array/copying/array_copy_calldata_storage.sol create mode 100644 test/libsolidity/semanticTests/array/copying/array_copy_including_array.sol create mode 100644 test/libsolidity/semanticTests/array/copying/arrays_from_and_to_storage.sol create mode 100644 test/libsolidity/semanticTests/array/delete/memory_arrays_delete.sol create mode 100644 test/libsolidity/semanticTests/array/external_array_args.sol create mode 100644 test/libsolidity/semanticTests/array/indexAccess/bytes_index_access.sol create mode 100644 test/libsolidity/semanticTests/array/indexAccess/bytes_index_access_memory.sol create mode 100644 test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol create mode 100644 test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol create mode 100644 test/libsolidity/semanticTests/constructor/bytes_in_constructors_unpacker.sol diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index e610f239f..b650db5b5 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -2854,145 +2854,6 @@ BOOST_AUTO_TEST_CASE(array_copy_storage_abi) ); } -BOOST_AUTO_TEST_CASE(external_array_args) -{ - char const* sourceCode = R"( - contract c { - function test(uint[8] calldata a, uint[] calldata b, uint[5] calldata c, uint a_index, uint b_index, uint c_index) - external returns (uint av, uint bv, uint cv) { - av = a[a_index]; - bv = b[b_index]; - cv = c[c_index]; - } - } - )"; - compileAndRun(sourceCode); - bytes params = encodeArgs( - 1, 2, 3, 4, 5, 6, 7, 8, // a - 32 * (8 + 1 + 5 + 1 + 1 + 1), // offset to b - 21, 22, 23, 24, 25, // c - 0, 1, 2, // (a,b,c)_index - 3, // b.length - 11, 12, 13 // b - ); - ABI_CHECK(callContractFunction("test(uint256[8],uint256[],uint256[5],uint256,uint256,uint256)", params), encodeArgs(1, 12, 23)); -} - -BOOST_AUTO_TEST_CASE(bytes_index_access) -{ - char const* sourceCode = R"( - contract c { - bytes data; - function direct(bytes calldata arg, uint index) external returns (uint) { - return uint(uint8(arg[index])); - } - function storageCopyRead(bytes calldata arg, uint index) external returns (uint) { - data = arg; - return uint(uint8(data[index])); - } - function storageWrite() external returns (uint) { - data = new bytes(35); - data[31] = 0x77; - data[32] = 0x14; - - data[31] = 0x01; - data[31] |= 0x08; - data[30] = 0x01; - data[32] = 0x03; - return uint(uint8(data[30])) * 0x100 | uint(uint8(data[31])) * 0x10 | uint(uint8(data[32])); - } - } - )"; - string array{ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33}; - ALSO_VIA_YUL( - DISABLE_EWASM_TESTRUN() - - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("direct(bytes,uint256)", 64, 33, u256(array.length()), array), encodeArgs(33)); - ABI_CHECK(callContractFunction("storageCopyRead(bytes,uint256)", 64, 33, u256(array.length()), array), encodeArgs(33)); - ABI_CHECK(callContractFunction("storageWrite()"), encodeArgs(0x193)); - ); -} - -BOOST_AUTO_TEST_CASE(array_copy_calldata_storage) -{ - char const* sourceCode = R"( - contract c { - uint[9] m_data; - uint[] m_data_dyn; - uint8[][] m_byte_data; - function store(uint[9] calldata a, uint8[3][] calldata b) external returns (uint8) { - m_data = a; - m_data_dyn = a; - m_byte_data = b; - return b[3][1]; // note that access and declaration are reversed to each other - } - function retrieve() public returns (uint a, uint b, uint c, uint d, uint e, uint f, uint g) { - a = m_data.length; - b = m_data[7]; - c = m_data_dyn.length; - d = m_data_dyn[7]; - e = m_byte_data.length; - f = m_byte_data[3].length; - g = m_byte_data[3][1]; - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("store(uint256[9],uint8[3][])", encodeArgs(21, 22, 23, 24, 25, 26, 27, 28, 29, u256(32 * (9 + 1)), 4, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33 )), encodeArgs(32)); - ABI_CHECK(callContractFunction("retrieve()"), encodeArgs(9, 28, 9, 28, 4, 3, 32)); -} - -BOOST_AUTO_TEST_CASE(array_copy_including_array) -{ - char const* sourceCode = R"( - contract c { - uint[3][90][] large; - uint[3][3][] small; - function test() public returns (uint r) { - for (uint i = 0; i < 7; i++) { - large.push(); - small.push(); - } - large[3][2][0] = 2; - large[1] = large[3]; - small[3][2][0] = 2; - small[1] = small[2]; - r = (( - small[3][2][0] * 0x100 | - small[1][2][0]) * 0x100 | - large[3][2][0]) * 0x100 | - large[1][2][0]; - delete small; - delete large; - - } - function clear() public returns (uint, uint) { - for (uint i = 0; i < 7; i++) { - large.push(); - small.push(); - } - small[3][2][0] = 0; - large[3][2][0] = 0; - while (small.length > 0) - small.pop(); - while (large.length > 0) - large.pop(); - return (small.length, large.length); - } - } - )"; - compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("test()"), encodeArgs(0x02000202)); - BOOST_CHECK(storageEmpty(m_contractAddress)); - ABI_CHECK(callContractFunction("clear()"), encodeArgs(0, 0)); - BOOST_CHECK(storageEmpty(m_contractAddress)); -} - //BOOST_AUTO_TEST_CASE(assignment_to_const_array_vars) //{ // char const* sourceCode = R"( @@ -3305,159 +3166,6 @@ BOOST_AUTO_TEST_CASE(return_bytes_internal) } } -BOOST_AUTO_TEST_CASE(bytes_index_access_memory) -{ - char const* sourceCode = R"( - contract Main { - function f(bytes memory _s1, uint i1, uint i2, uint i3) public returns (byte c1, byte c2, byte c3) { - c1 = _s1[i1]; - c2 = intern(_s1, i2); - c3 = internIndirect(_s1)[i3]; - } - function intern(bytes memory _s1, uint i) public returns (byte c) { - return _s1[i]; - } - function internIndirect(bytes memory _s1) public returns (bytes memory) { - return _s1; - } - } - )"; - compileAndRun(sourceCode, 0, "Main"); - string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); - bytes dyn1 = encodeArgs(u256(s1.length()), s1); - bytes args1 = encodeArgs(u256(0x80), u256(3), u256(4), u256(5)) + dyn1; - BOOST_REQUIRE( - callContractFunction("f(bytes,uint256,uint256,uint256)", asString(args1)) == - encodeArgs(string{s1[3]}, string{s1[4]}, string{s1[5]}) - ); -} - -BOOST_AUTO_TEST_CASE(bytes_in_constructors_unpacker) -{ - char const* sourceCode = R"( - contract Test { - uint public m_x; - bytes public m_s; - constructor(uint x, bytes memory s) { - m_x = x; - m_s = s; - } - } - )"; - string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); - bytes dyn1 = encodeArgs(u256(s1.length()), s1); - u256 x = 7; - bytes args1 = encodeArgs(x, u256(0x40)) + dyn1; - compileAndRun(sourceCode, 0, "Test", args1); - BOOST_REQUIRE(callContractFunction("m_x()") == encodeArgs(x)); - BOOST_REQUIRE(callContractFunction("m_s()") == encodeArgs(u256(0x20)) + dyn1); -} - -BOOST_AUTO_TEST_CASE(bytes_in_constructors_packer) -{ - char const* sourceCode = R"( - contract Base { - uint public m_x; - bytes m_s; - constructor(uint x, bytes memory s) { - m_x = x; - m_s = s; - } - function part(uint i) public returns (byte) { - return m_s[i]; - } - } - contract Main is Base { - constructor(bytes memory s, uint x) Base(x, f(s)) {} - function f(bytes memory s) public returns (bytes memory) { - return s; - } - } - contract Creator { - function f(uint x, bytes memory s) public returns (uint r, byte ch) { - Main c = new Main(s, x); - r = c.m_x(); - ch = c.part(x); - } - } - )"; - compileAndRun(sourceCode, 0, "Creator"); - string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); - bytes dyn1 = encodeArgs(u256(s1.length()), s1); - u256 x = 7; - bytes args1 = encodeArgs(x, u256(0x40)) + dyn1; - BOOST_REQUIRE( - callContractFunction("f(uint256,bytes)", asString(args1)) == - encodeArgs(x, string{s1[unsigned(x)]}) - ); -} - -BOOST_AUTO_TEST_CASE(arrays_in_constructors) -{ - char const* sourceCode = R"( - contract Base { - uint public m_x; - address[] m_s; - constructor(uint x, address[] memory s) { - m_x = x; - m_s = s; - } - function part(uint i) public returns (address) { - return m_s[i]; - } - } - contract Main is Base { - constructor(address[] memory s, uint x) Base(x, f(s)) {} - function f(address[] memory s) public returns (address[] memory) { - return s; - } - } - contract Creator { - function f(uint x, address[] memory s) public returns (uint r, address ch) { - Main c = new Main(s, x); - r = c.m_x(); - ch = c.part(x); - } - } - )"; - compileAndRun(sourceCode, 0, "Creator"); - vector s1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - bytes dyn1 = encodeArgs(u256(s1.size()), s1); - u256 x = 7; - bytes args1 = encodeArgs(x, u256(0x40)) + dyn1; - BOOST_REQUIRE( - callContractFunction("f(uint256,address[])", asString(args1)) == - encodeArgs(x, s1[unsigned(x)]) - ); -} - -BOOST_AUTO_TEST_CASE(arrays_from_and_to_storage) -{ - char const* sourceCode = R"( - contract Test { - uint24[] public data; - function set(uint24[] memory _data) public returns (uint) { - data = _data; - return data.length; - } - function get() public returns (uint24[] memory) { - return data; - } - } - )"; - compileAndRun(sourceCode, 0, "Test"); - - vector data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}; - BOOST_REQUIRE( - callContractFunction("set(uint24[])", u256(0x20), u256(data.size()), data) == - encodeArgs(u256(data.size())) - ); - ABI_CHECK(callContractFunction("data(uint256)", u256(7)), encodeArgs(u256(8))); - ABI_CHECK(callContractFunction("data(uint256)", u256(15)), encodeArgs(u256(16))); - ABI_CHECK(callContractFunction("data(uint256)", u256(18)), encodeArgs()); - ABI_CHECK(callContractFunction("get()"), encodeArgs(u256(0x20), u256(data.size()), data)); -} - BOOST_AUTO_TEST_CASE(memory_types_initialisation) { char const* sourceCode = R"( @@ -3480,35 +3188,6 @@ BOOST_AUTO_TEST_CASE(memory_types_initialisation) ABI_CHECK(callContractFunction("nestedStat()"), encodeArgs(vector(3 * 7))); } -BOOST_AUTO_TEST_CASE(memory_arrays_delete) -{ - char const* sourceCode = R"( - contract Test { - function del() public returns (uint24[3][4] memory) { - uint24[3][4] memory x; - for (uint24 i = 0; i < x.length; i ++) - for (uint24 j = 0; j < x[i].length; j ++) - x[i][j] = i * 0x10 + j; - delete x[1]; - delete x[3][2]; - return x; - } - } - )"; - compileAndRun(sourceCode, 0, "Test"); - - vector data(3 * 4); - for (unsigned i = 0; i < 4; i++) - for (unsigned j = 0; j < 3; j++) - { - u256 v = 0; - if (!(i == 1 || (i == 3 && j == 2))) - v = i * 0x10 + j; - data[i * 3 + j] = v; - } - ABI_CHECK(callContractFunction("del()"), encodeArgs(data)); -} - BOOST_AUTO_TEST_CASE(calldata_struct_short) { char const* sourceCode = R"( @@ -3557,86 +3236,6 @@ BOOST_AUTO_TEST_CASE(calldata_struct_function_type) ABI_CHECK(callContractFunctionNoEncoding("f((function))", fn_C_h), encodeArgs(23)); } -BOOST_AUTO_TEST_CASE(calldata_bytes_array_bounds) -{ - char const* sourceCode = R"( - pragma experimental ABIEncoderV2; - contract C { - function f(bytes[] calldata a, uint256 i) external returns (uint) { - return uint8(a[0][i]); - } - } - )"; - compileAndRun(sourceCode, 0, "C"); - - ABI_CHECK( - callContractFunction("f(bytes[],uint256)", 0x40, 0, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), - encodeArgs('a') - ); - ABI_CHECK( - callContractFunction("f(bytes[],uint256)", 0x40, 1, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), - encodeArgs('b') - ); - ABI_CHECK( - callContractFunction("f(bytes[],uint256)", 0x40, 2, 1, 0x20, 2, bytes{'a', 'b'} + bytes(30, 0)), - encodeArgs() - ); -} - -BOOST_AUTO_TEST_CASE(calldata_array_two_dimensional) -{ - vector> data { - { 0x0A01, 0x0A02, 0x0A03 }, - { 0x0B01, 0x0B02, 0x0B03, 0x0B04 } - }; - - for (bool outerDynamicallySized: { true, false }) - { - string arrayType = outerDynamicallySized ? "uint256[][]" : "uint256[][2]"; - string sourceCode = R"( - pragma experimental ABIEncoderV2; - contract C { - function test()" + arrayType + R"( calldata a) external returns (uint256) { - return a.length; - } - function test()" + arrayType + R"( calldata a, uint256 i) external returns (uint256) { - return a[i].length; - } - function test()" + arrayType + R"( calldata a, uint256 i, uint256 j) external returns (uint256) { - return a[i][j]; - } - function reenc()" + arrayType + R"( calldata a, uint256 i, uint256 j) external returns (uint256) { - return this.test(a, i, j); - } - } - )"; - compileAndRun(sourceCode, 0, "C"); - - bytes encoding = encodeArray( - outerDynamicallySized, - true, - data | boost::adaptors::transformed([&](vector const& _values) { - return encodeArray(true, false, _values); - }) - ); - - ABI_CHECK(callContractFunction("test(" + arrayType + ")", 0x20, encoding), encodeArgs(data.size())); - for (size_t i = 0; i < data.size(); i++) - { - ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, i, encoding), encodeArgs(data[i].size())); - for (size_t j = 0; j < data[i].size(); j++) - { - ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, j, encoding), encodeArgs(data[i][j])); - ABI_CHECK(callContractFunction("reenc(" + arrayType + ",uint256,uint256)", 0x60, i, j, encoding), encodeArgs(data[i][j])); - } - // out of bounds access - ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256,uint256)", 0x60, i, data[i].size(), encoding), encodeArgs()); - } - // out of bounds access - ABI_CHECK(callContractFunction("test(" + arrayType + ",uint256)", 0x40, data.size(), encoding), encodeArgs()); - } -} - BOOST_AUTO_TEST_CASE(calldata_array_dynamic_three_dimensional) { vector>> data { diff --git a/test/libsolidity/semanticTests/array/calldata_array_two_dimensional.sol b/test/libsolidity/semanticTests/array/calldata_array_two_dimensional.sol new file mode 100644 index 000000000..4c20627e7 --- /dev/null +++ b/test/libsolidity/semanticTests/array/calldata_array_two_dimensional.sol @@ -0,0 +1,38 @@ +pragma experimental ABIEncoderV2; +contract C { + function test(uint256[][2] calldata a) external returns (uint256) { + return a.length; + } + function test(uint256[][2] calldata a, uint256 i) external returns (uint256) { + return a[i].length; + } + function test(uint256[][2] calldata a, uint256 i, uint256 j) external returns (uint256) { + return a[i][j]; + } + function reenc(uint256[][2] calldata a, uint256 i, uint256 j) external returns (uint256) { + return this.test(a, i, j); + } +} +// ==== +// compileViaYul: also +// ---- +// test(uint256[][2]): 0x20, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 2 +// test(uint256[][2],uint256): 0x40, 0, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 3 +// test(uint256[][2],uint256): 0x40, 1, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 4 +// test(uint256[][2],uint256,uint256): 0x60, 0, 0, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A01 +// reenc(uint256[][2],uint256,uint256): 0x60, 0, 0, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A01 +// test(uint256[][2],uint256,uint256): 0x60, 0, 1, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A02 +// reenc(uint256[][2],uint256,uint256): 0x60, 0, 1, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A02 +// test(uint256[][2],uint256,uint256): 0x60, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A03 +// reenc(uint256[][2],uint256,uint256): 0x60, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A03 +// test(uint256[][2],uint256,uint256): 0x60, 1, 0, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B01 +// reenc(uint256[][2],uint256,uint256): 0x60, 1, 0, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B01 +// test(uint256[][2],uint256,uint256): 0x60, 1, 1, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B02 +// reenc(uint256[][2],uint256,uint256): 0x60, 1, 1, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B02 +// test(uint256[][2],uint256,uint256): 0x60, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B03 +// reenc(uint256[][2],uint256,uint256): 0x60, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B03 +// test(uint256[][2],uint256,uint256): 0x60, 1, 3, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B04 +// reenc(uint256[][2],uint256,uint256): 0x60, 1, 3, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B04 +// test(uint256[][2],uint256,uint256): 0x60, 0, 3, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> FAILURE +// test(uint256[][2],uint256,uint256): 0x60, 1, 4, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> FAILURE +// test(uint256[][2],uint256): 0x40, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> FAILURE diff --git a/test/libsolidity/semanticTests/array/calldata_array_two_dimensional_1.sol b/test/libsolidity/semanticTests/array/calldata_array_two_dimensional_1.sol new file mode 100644 index 000000000..9d40a356d --- /dev/null +++ b/test/libsolidity/semanticTests/array/calldata_array_two_dimensional_1.sol @@ -0,0 +1,38 @@ +pragma experimental ABIEncoderV2; +contract C { + function test(uint256[][] calldata a) external returns (uint256) { + return a.length; + } + function test(uint256[][] calldata a, uint256 i) external returns (uint256) { + return a[i].length; + } + function test(uint256[][] calldata a, uint256 i, uint256 j) external returns (uint256) { + return a[i][j]; + } + function reenc(uint256[][] calldata a, uint256 i, uint256 j) external returns (uint256) { + return this.test(a, i, j); + } +} +// ==== +// compileViaYul: also +// ---- +// test(uint256[][]): 0x20, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 2 +// test(uint256[][],uint256): 0x40, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 3 +// test(uint256[][],uint256): 0x40, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 4 +// test(uint256[][],uint256,uint256): 0x60, 0, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A01 +// reenc(uint256[][],uint256,uint256): 0x60, 0, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A01 +// test(uint256[][],uint256,uint256): 0x60, 0, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A02 +// reenc(uint256[][],uint256,uint256): 0x60, 0, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A02 +// test(uint256[][],uint256,uint256): 0x60, 0, 2, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A03 +// reenc(uint256[][],uint256,uint256): 0x60, 0, 2, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0A03 +// test(uint256[][],uint256,uint256): 0x60, 1, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B01 +// reenc(uint256[][],uint256,uint256): 0x60, 1, 0, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B01 +// test(uint256[][],uint256,uint256): 0x60, 1, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B02 +// reenc(uint256[][],uint256,uint256): 0x60, 1, 1, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B02 +// test(uint256[][],uint256,uint256): 0x60, 1, 2, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B03 +// reenc(uint256[][],uint256,uint256): 0x60, 1, 2, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B03 +// test(uint256[][],uint256,uint256): 0x60, 1, 3, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B04 +// reenc(uint256[][],uint256,uint256): 0x60, 1, 3, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> 0x0B04 +// test(uint256[][],uint256,uint256): 0x60, 0, 3, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> FAILURE +// test(uint256[][],uint256,uint256): 0x60, 1, 4, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> FAILURE +// test(uint256[][],uint256): 0x40, 2, 2, 0x40, 0xC0, 3, 0x0A01, 0x0A02, 0x0A03, 4, 0x0B01, 0x0B02, 0x0B03, 0x0B04 -> FAILURE diff --git a/test/libsolidity/semanticTests/array/calldata_bytes_array_bounds.sol b/test/libsolidity/semanticTests/array/calldata_bytes_array_bounds.sol new file mode 100644 index 000000000..ed120d501 --- /dev/null +++ b/test/libsolidity/semanticTests/array/calldata_bytes_array_bounds.sol @@ -0,0 +1,12 @@ +pragma experimental ABIEncoderV2; +contract C { + function f(bytes[] calldata a, uint256 i) external returns (uint) { + return uint8(a[0][i]); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes[],uint256): 0x40, 0, 1, 0x20, 2, 0x6162000000000000000000000000000000000000000000000000000000000000 -> 0x61 +// f(bytes[],uint256): 0x40, 1, 1, 0x20, 2, 0x6162000000000000000000000000000000000000000000000000000000000000 -> 0x62 +// f(bytes[],uint256): 0x40, 2, 1, 0x20, 2, 0x6162000000000000000000000000000000000000000000000000000000000000 -> FAILURE diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_calldata_storage.sol b/test/libsolidity/semanticTests/array/copying/array_copy_calldata_storage.sol new file mode 100644 index 000000000..20c934bc4 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/array_copy_calldata_storage.sol @@ -0,0 +1,25 @@ +contract c { + uint[9] m_data; + uint[] m_data_dyn; + uint8[][] m_byte_data; + function store(uint[9] calldata a, uint8[3][] calldata b) external returns (uint8) { + m_data = a; + m_data_dyn = a; + m_byte_data = b; + return b[3][1]; // note that access and declaration are reversed to each other + } + function retrieve() public returns (uint a, uint b, uint c, uint d, uint e, uint f, uint g) { + a = m_data.length; + b = m_data[7]; + c = m_data_dyn.length; + d = m_data_dyn[7]; + e = m_byte_data.length; + f = m_byte_data[3].length; + g = m_byte_data[3][1]; + } +} +// ==== +// compileViaYul: also +// ---- +// store(uint256[9],uint8[3][]): 21, 22, 23, 24, 25, 26, 27, 28, 29, 0x140, 4, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33 -> 32 +// retrieve() -> 9, 28, 9, 28, 4, 3, 32 diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_including_array.sol b/test/libsolidity/semanticTests/array/copying/array_copy_including_array.sol new file mode 100644 index 000000000..04564b748 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/array_copy_including_array.sol @@ -0,0 +1,40 @@ +contract c { + uint[3][90][] large; + uint[3][3][] small; + function test() public returns (uint r) { + for (uint i = 0; i < 7; i++) { + large.push(); + small.push(); + } + large[3][2][0] = 2; + large[1] = large[3]; + small[3][2][0] = 2; + small[1] = small[2]; + r = (( + small[3][2][0] * 0x100 | + small[1][2][0]) * 0x100 | + large[3][2][0]) * 0x100 | + large[1][2][0]; + delete small; + delete large; + + } + function clear() public returns (uint, uint) { + for (uint i = 0; i < 7; i++) { + large.push(); + small.push(); + } + small[3][2][0] = 0; + large[3][2][0] = 0; + while (small.length > 0) + small.pop(); + while (large.length > 0) + large.pop(); + return (small.length, large.length); + } +} +// ---- +// test() -> 0x02000202 +// storage: empty +// clear() -> 0, 0 +// storage: empty diff --git a/test/libsolidity/semanticTests/array/copying/arrays_from_and_to_storage.sol b/test/libsolidity/semanticTests/array/copying/arrays_from_and_to_storage.sol new file mode 100644 index 000000000..8b99bf14c --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/arrays_from_and_to_storage.sol @@ -0,0 +1,18 @@ +contract Test { + uint24[] public data; + function set(uint24[] memory _data) public returns (uint) { + data = _data; + return data.length; + } + function get() public returns (uint24[] memory) { + return data; + } +} +// ==== +// compileViaYul: also +// ---- +// set(uint24[]): 0x20, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 -> 18 +// data(uint256): 7 -> 8 +// data(uint256): 15 -> 16 +// data(uint256): 18 -> FAILURE +// get() -> 0x20, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 diff --git a/test/libsolidity/semanticTests/array/delete/memory_arrays_delete.sol b/test/libsolidity/semanticTests/array/delete/memory_arrays_delete.sol new file mode 100644 index 000000000..31c86b353 --- /dev/null +++ b/test/libsolidity/semanticTests/array/delete/memory_arrays_delete.sol @@ -0,0 +1,15 @@ +contract Test { + function del() public returns (uint24[3][4] memory) { + uint24[3][4] memory x; + for (uint24 i = 0; i < x.length; i ++) + for (uint24 j = 0; j < x[i].length; j ++) + x[i][j] = i * 0x10 + j; + delete x[1]; + delete x[3][2]; + return x; + } +} +// ==== +// compileViaYul: also +// ---- +// del() -> 0, 1, 2, 0, 0, 0, 0x20, 0x21, 0x22, 0x30, 0x31, 0 diff --git a/test/libsolidity/semanticTests/array/external_array_args.sol b/test/libsolidity/semanticTests/array/external_array_args.sol new file mode 100644 index 000000000..38205ea95 --- /dev/null +++ b/test/libsolidity/semanticTests/array/external_array_args.sol @@ -0,0 +1,12 @@ +contract c { + function test(uint[8] calldata a, uint[] calldata b, uint[5] calldata c, uint a_index, uint b_index, uint c_index) + external returns (uint av, uint bv, uint cv) { + av = a[a_index]; + bv = b[b_index]; + cv = c[c_index]; + } +} +// ==== +// compileViaYul: also +// ---- +// test(uint256[8],uint256[],uint256[5],uint256,uint256,uint256): 1, 2, 3, 4, 5, 6, 7, 8, 0x220, 21, 22, 23, 24, 25, 0, 1, 2, 3, 11, 12, 13 -> 1, 12, 23 diff --git a/test/libsolidity/semanticTests/array/indexAccess/bytes_index_access.sol b/test/libsolidity/semanticTests/array/indexAccess/bytes_index_access.sol new file mode 100644 index 000000000..9a0c85ac7 --- /dev/null +++ b/test/libsolidity/semanticTests/array/indexAccess/bytes_index_access.sol @@ -0,0 +1,27 @@ +contract c { + bytes data; + function direct(bytes calldata arg, uint index) external returns (uint) { + return uint(uint8(arg[index])); + } + function storageCopyRead(bytes calldata arg, uint index) external returns (uint) { + data = arg; + return uint(uint8(data[index])); + } + function storageWrite() external returns (uint) { + data = new bytes(35); + data[31] = 0x77; + data[32] = 0x14; + + data[31] = 0x01; + data[31] |= 0x08; + data[30] = 0x01; + data[32] = 0x03; + return uint(uint8(data[30])) * 0x100 | uint(uint8(data[31])) * 0x10 | uint(uint8(data[32])); + } +} +// ==== +// compileViaYul: also +// ---- +// direct(bytes,uint256): 0x40, 33, 34, 0x000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F, left(0x2021) -> 0x21 +// storageCopyRead(bytes,uint256): 0x40, 33, 34, 0x000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F, left(0x2021) -> 0x21 +// storageWrite() -> 0x193 diff --git a/test/libsolidity/semanticTests/array/indexAccess/bytes_index_access_memory.sol b/test/libsolidity/semanticTests/array/indexAccess/bytes_index_access_memory.sol new file mode 100644 index 000000000..1c2824d7c --- /dev/null +++ b/test/libsolidity/semanticTests/array/indexAccess/bytes_index_access_memory.sol @@ -0,0 +1,17 @@ +contract Main { + function f(bytes memory _s1, uint i1, uint i2, uint i3) public returns (byte c1, byte c2, byte c3) { + c1 = _s1[i1]; + c2 = intern(_s1, i2); + c3 = internIndirect(_s1)[i3]; + } + function intern(bytes memory _s1, uint i) public returns (byte c) { + return _s1[i]; + } + function internIndirect(bytes memory _s1) public returns (bytes memory) { + return _s1; + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes,uint256,uint256,uint256): 0x80, 3, 4, 5, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> "d", "e", "f" diff --git a/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol b/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol new file mode 100644 index 000000000..ae380af8e --- /dev/null +++ b/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol @@ -0,0 +1,28 @@ +contract Base { + uint public m_x; + address[] m_s; + constructor(uint x, address[] memory s) { + m_x = x; + m_s = s; + } + function part(uint i) public returns (address) { + return m_s[i]; + } +} +contract Main is Base { + constructor(address[] memory s, uint x) Base(x, f(s)) {} + function f(address[] memory s) public returns (address[] memory) { + return s; + } +} +contract Creator { + function f(uint x, address[] memory s) public returns (uint r, address ch) { + Main c = new Main(s, x); + r = c.m_x(); + ch = c.part(x); + } +} +// ==== +// compileViaYul: also +// ---- +// f(uint256,address[]): 7, 0x40, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 -> 7, 8 diff --git a/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol new file mode 100644 index 000000000..35e69bf4e --- /dev/null +++ b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol @@ -0,0 +1,28 @@ +contract Base { + uint public m_x; + bytes m_s; + constructor(uint x, bytes memory s) { + m_x = x; + m_s = s; + } + function part(uint i) public returns (byte) { + return m_s[i]; + } +} +contract Main is Base { + constructor(bytes memory s, uint x) Base(x, f(s)) {} + function f(bytes memory s) public returns (bytes memory) { + return s; + } +} +contract Creator { + function f(uint x, bytes memory s) public returns (uint r, byte ch) { + Main c = new Main(s, x); + r = c.m_x(); + ch = c.part(x); + } +} +// ==== +// compileViaYul: also +// ---- +// f(uint256,bytes): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> 7, "h" diff --git a/test/libsolidity/semanticTests/constructor/bytes_in_constructors_unpacker.sol b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_unpacker.sol new file mode 100644 index 000000000..1d163d82e --- /dev/null +++ b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_unpacker.sol @@ -0,0 +1,12 @@ +contract Test { + uint public m_x; + bytes public m_s; + constructor(uint x, bytes memory s) { + m_x = x; + m_s = s; + } +} +// ---- +// constructor(): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> +// m_x() -> 7 +// m_s() -> 0x20, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz"