From 0eae9e70ff02d9f5b6d6ffc63615a6f7ff572196 Mon Sep 17 00:00:00 2001 From: wechman Date: Fri, 9 Sep 2022 06:57:27 +0200 Subject: [PATCH] Tests for copying structs between data locations --- ...h_nested_array_from_calldata_to_memory.sol | 16 +++++++++++ ..._nested_array_from_calldata_to_storage.sol | 22 +++++++++++++++ ...ith_nested_array_from_memory_to_memory.sol | 16 +++++++++++ ...h_nested_array_from_storage_to_storage.sol | 27 +++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_memory.sol create mode 100644 test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_storage.sol create mode 100644 test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_memory_to_memory.sol create mode 100644 test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_storage_to_storage.sol diff --git a/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_memory.sol b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_memory.sol new file mode 100644 index 000000000..8ccc30936 --- /dev/null +++ b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_memory.sol @@ -0,0 +1,16 @@ +pragma abicoder v2; + +contract C { + struct S { + uint8[1] x; + uint8[] y; + } + + function test(S calldata s) public returns (S memory) { + return s; + } +} + +// ---- +// test((uint8[1],uint8[])): 0x20, 3, 0x40, 2, 7, 11 -> 0x20, 3, 0x40, 2, 7, 11 +// test((uint8[1],uint8[])): 0x20, 3, 0x40, 3, 17, 19, 23 -> 0x20, 3, 0x40, 3, 17, 19, 23 diff --git a/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_storage.sol b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_storage.sol new file mode 100644 index 000000000..fa67deb2c --- /dev/null +++ b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_calldata_to_storage.sol @@ -0,0 +1,22 @@ +pragma abicoder v2; + +contract C { + struct S { + uint8[1] x; + uint8[] y; + } + + S s; + + function test(S calldata src) public { + s = src; + + require(s.x[0] == 3); + require(s.y.length == 2); + require(s.y[0] == 7); + require(s.y[1] == 11); + } +} + +// ---- +// test((uint8[1],uint8[])): 0x20, 3, 0x40, 2, 7, 11 diff --git a/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_memory_to_memory.sol b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_memory_to_memory.sol new file mode 100644 index 000000000..c340289ad --- /dev/null +++ b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_memory_to_memory.sol @@ -0,0 +1,16 @@ +pragma abicoder v2; + +contract C { + struct S { + uint8[1] x; + uint8[] y; + } + + function test(S memory s) public returns (S memory r) { + return r; + } +} + +// ---- +// test((uint8[1],uint8[])): 0x20, 3, 0x40, 2, 7, 11 -> 0x20, 0, 0x40, 0 +// test((uint8[1],uint8[])): 0x20, 3, 0x40, 3, 17, 19, 23 -> 0x20, 0, 0x40, 0 diff --git a/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_storage_to_storage.sol b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_storage_to_storage.sol new file mode 100644 index 000000000..3f29790d8 --- /dev/null +++ b/test/libsolidity/semanticTests/structs/copy_struct_with_nested_array_from_storage_to_storage.sol @@ -0,0 +1,27 @@ +contract C { + struct S { + uint8[1] x; + uint8[] y; + } + + S src; + S dst; + + constructor() { + src.x = [3]; + src.y.push(7); + src.y.push(11); + } + + function test() public { + dst = src; + + require(dst.x[0] == 3); + require(dst.y.length == 2); + require(dst.y[0] == 7); + require(dst.y[1] == 11); + } +} + +// ---- +// test()