From c41f996c7bd9c4563250b4574bb838bdde9903c0 Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Thu, 24 Dec 2020 15:20:40 +0100 Subject: [PATCH] Adding more tests for array copying. Co-authored-by: Harikrishnan Mulackal --- .../array_copy_storage_to_memory_nested.sol | 19 ++++++++++++++++ .../calldata_dyn_2d_bytes_to_memory.sol | 11 ++++++++++ .../calldata_to_storage_different_base.sol | 13 +++++++++++ .../memory_dyn_2d_bytes_to_storage.sol | 22 +++++++++++++++++++ .../memory_to_storage_different_base.sol | 17 ++++++++++++++ .../calldata_to_memory_different_base.sol | 10 +++++++++ .../storage_to_memory_different_base.sol | 14 ++++++++++++ 7 files changed, 106 insertions(+) create mode 100644 test/libsolidity/semanticTests/array/copying/array_copy_storage_to_memory_nested.sol create mode 100644 test/libsolidity/semanticTests/array/copying/calldata_dyn_2d_bytes_to_memory.sol create mode 100644 test/libsolidity/semanticTests/array/copying/calldata_to_storage_different_base.sol create mode 100644 test/libsolidity/semanticTests/array/copying/memory_dyn_2d_bytes_to_storage.sol create mode 100644 test/libsolidity/semanticTests/array/copying/memory_to_storage_different_base.sol create mode 100644 test/libsolidity/syntaxTests/array/invalidCopy/calldata_to_memory_different_base.sol create mode 100644 test/libsolidity/syntaxTests/array/invalidCopy/storage_to_memory_different_base.sol diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_storage_to_memory_nested.sol b/test/libsolidity/semanticTests/array/copying/array_copy_storage_to_memory_nested.sol new file mode 100644 index 000000000..0fb56593c --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/array_copy_storage_to_memory_nested.sol @@ -0,0 +1,19 @@ +pragma abicoder v2; +contract C { + uint[][] a; + + function f() public returns (uint[][] memory) { + a.push(); + a.push(); + a[0].push(0); + a[0].push(1); + a[1].push(2); + a[1].push(3); + uint[][] memory m = a; + return m; + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 0x20, 2, 0x40, 0xa0, 2, 0, 1, 2, 2, 3 diff --git a/test/libsolidity/semanticTests/array/copying/calldata_dyn_2d_bytes_to_memory.sol b/test/libsolidity/semanticTests/array/copying/calldata_dyn_2d_bytes_to_memory.sol new file mode 100644 index 000000000..07296aac0 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/calldata_dyn_2d_bytes_to_memory.sol @@ -0,0 +1,11 @@ +pragma abicoder v2; + +contract C { + function f(bytes[] calldata c) external returns (bytes[] memory) { + return c; + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes[]): 0x20, 2, 0x60, 0x60, 0x20, 2, "ab" -> 0x20, 2, 0x40, 0x80, 2, "ab", 2, "ab" diff --git a/test/libsolidity/semanticTests/array/copying/calldata_to_storage_different_base.sol b/test/libsolidity/semanticTests/array/copying/calldata_to_storage_different_base.sol new file mode 100644 index 000000000..d9771bf33 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/calldata_to_storage_different_base.sol @@ -0,0 +1,13 @@ +pragma abicoder v2; + +contract C { + bytes10[] s; + function f(bytes8[] calldata c) external returns (uint256, bytes10, bytes10, bytes10) { + s = c; + return (s.length, s[0], s[1], s[2]); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes8[]): 0x20, 3, "abcd", "bcde", "cdef" -> 3, "abcd", "bcde", "cdef" diff --git a/test/libsolidity/semanticTests/array/copying/memory_dyn_2d_bytes_to_storage.sol b/test/libsolidity/semanticTests/array/copying/memory_dyn_2d_bytes_to_storage.sol new file mode 100644 index 000000000..1b22b4d85 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/memory_dyn_2d_bytes_to_storage.sol @@ -0,0 +1,22 @@ +pragma abicoder v2; + +contract C { + bytes[] s; + function f() external returns (uint256) { + bytes[] memory m = new bytes[](3); + m[0] = "ab"; m[1] = "cde"; m[2] = "fghij"; + s = m; + assert(s.length == m.length); + for (uint i = 0; i < s.length; ++i) { + assert(s[i].length == m[i].length); + for (uint j = 0; j < s[i].length; ++j) { + assert(s[i][j] == m[i][j]); + } + } + return s.length; + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 3 diff --git a/test/libsolidity/semanticTests/array/copying/memory_to_storage_different_base.sol b/test/libsolidity/semanticTests/array/copying/memory_to_storage_different_base.sol new file mode 100644 index 000000000..24ac2e202 --- /dev/null +++ b/test/libsolidity/semanticTests/array/copying/memory_to_storage_different_base.sol @@ -0,0 +1,17 @@ +pragma abicoder v2; + +contract C { + bytes10[] s; + function f() external returns (uint256, bytes10, bytes10, bytes10) { + bytes4[] memory m = new bytes4[](3); + m[0] = "abcd"; + m[1] = "bcde"; + m[2] = "cdef"; + s = m; + return (s.length, s[0], s[1], s[2]); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 3, "abcd", "bcde", "cdef" diff --git a/test/libsolidity/syntaxTests/array/invalidCopy/calldata_to_memory_different_base.sol b/test/libsolidity/syntaxTests/array/invalidCopy/calldata_to_memory_different_base.sol new file mode 100644 index 000000000..b08fd0df5 --- /dev/null +++ b/test/libsolidity/syntaxTests/array/invalidCopy/calldata_to_memory_different_base.sol @@ -0,0 +1,10 @@ +pragma abicoder v2; + +contract C { + function f(bytes8[] calldata c) external returns (uint256, bytes10, bytes10, bytes10) { + bytes10[] memory m = c; + return (m.length, m[0], m[1], m[2]); + } +} +// ---- +// TypeError 9574: (134-156): Type bytes8[] calldata is not implicitly convertible to expected type bytes10[] memory. diff --git a/test/libsolidity/syntaxTests/array/invalidCopy/storage_to_memory_different_base.sol b/test/libsolidity/syntaxTests/array/invalidCopy/storage_to_memory_different_base.sol new file mode 100644 index 000000000..af4a593cc --- /dev/null +++ b/test/libsolidity/syntaxTests/array/invalidCopy/storage_to_memory_different_base.sol @@ -0,0 +1,14 @@ +pragma abicoder v2; + +contract C { + bytes8[] s; + function f() external returns (uint256, bytes10, bytes10, bytes10) { + s.push("abcd"); + s.push("bcde"); + s.push("cdef"); + bytes10[] memory m = s; + return (m.length, m[0], m[1], m[2]); + } +} +// ---- +// TypeError 9574: (203-225): Type bytes8[] storage ref is not implicitly convertible to expected type bytes10[] memory.