Skip to content

Commit dfe8fce

Browse files
committed
Tests for copying nested array of structs between data locations
1 parent c4d9712 commit dfe8fce

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pragma abicoder v2;
2+
3+
contract C {
4+
struct S {
5+
uint8 x;
6+
uint8 y;
7+
}
8+
9+
function test1(S[1][2] calldata a) public returns (S[1][2] memory) {
10+
return a;
11+
}
12+
13+
function test2(S[1][] calldata a) public returns (S[1][] memory) {
14+
return a;
15+
}
16+
17+
function test3(S[][2] calldata a) public returns (S[][2] memory) {
18+
return a;
19+
}
20+
}
21+
22+
// ----
23+
// test1((uint8,uint8)[1][2]): 1, 2, 3, 4 -> 1, 2, 3, 4
24+
// test2((uint8,uint8)[1][]): 0x20, 3, 7, 11, 13, 17, 19, 23 -> 0x20, 3, 7, 11, 13, 17, 19, 23
25+
// test3((uint8,uint8)[][2]): 0x20, 0x40, 0xa0, 1, 3, 7, 2, 11, 13, 17, 19 -> 0x20, 0x40, 0xa0, 1, 3, 7, 2, 11, 13, 17, 19
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pragma abicoder v2;
2+
3+
contract C {
4+
struct S {
5+
uint8 x;
6+
uint8 y;
7+
}
8+
9+
function test1(S[1][2] memory a) public returns (S[1][2] memory r) {
10+
r = a;
11+
}
12+
13+
function test2(S[1][] memory a) public returns (S[1][] memory r) {
14+
r = a;
15+
}
16+
17+
function test3(S[][2] memory a) public returns (S[][2] memory r) {
18+
r = a;
19+
}
20+
}
21+
22+
// ----
23+
// test1((uint8,uint8)[1][2]): 1, 2, 3, 4 -> 1, 2, 3, 4
24+
// test2((uint8,uint8)[1][]): 0x20, 3, 7, 11, 13, 17, 19, 23 -> 0x20, 3, 7, 11, 13, 17, 19, 23
25+
// test3((uint8,uint8)[][2]): 0x20, 0x40, 0xa0, 1, 3, 7, 2, 11, 13, 17, 19 -> 0x20, 0x40, 0xa0, 1, 3, 7, 2, 11, 13, 17, 19
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
pragma abicoder v2;
2+
3+
contract C {
4+
struct S {
5+
uint8 x;
6+
uint8 y;
7+
}
8+
9+
S[][] src1;
10+
S[][1] src2;
11+
S[1][] src3;
12+
13+
S[][] dst1;
14+
S[][1] dst2;
15+
S[1][] dst3;
16+
17+
constructor() {
18+
src1 = new S[][](1);
19+
src1[0].push(S({x: 3, y: 7}));
20+
src1[0].push(S({x: 11, y: 13}));
21+
22+
src2[0].push(S({x: 3, y: 7}));
23+
src2[0].push(S({x: 11, y: 13}));
24+
src2[0].push(S({x: 17, y: 19}));
25+
26+
src3.push([S({x: 3, y: 7})]);
27+
src3.push([S({x: 11, y: 13})]);
28+
}
29+
30+
function test1() public {
31+
dst1 = src1;
32+
33+
require(dst1.length == 1);
34+
require(dst1[0][0].x == src1[0][0].x);
35+
require(dst1[0][0].y == src1[0][0].y);
36+
require(dst1[0][1].x == src1[0][1].x);
37+
require(dst1[0][1].y == src1[0][1].y);
38+
}
39+
40+
function test2() public {
41+
dst2 = src2;
42+
43+
require(dst2[0].length == 3);
44+
require(dst2[0][0].x == src2[0][0].x);
45+
require(dst2[0][0].y == src2[0][0].y);
46+
require(dst2[0][1].x == src2[0][1].x);
47+
require(dst2[0][1].y == src2[0][1].y);
48+
require(dst2[0][2].x == src2[0][2].x);
49+
require(dst2[0][2].y == src2[0][2].y);
50+
}
51+
52+
function test3() public {
53+
dst3 = src3;
54+
55+
require(dst3.length == 2);
56+
require(dst3[0][0].x == src3[0][0].x);
57+
require(dst3[0][0].y == src3[0][0].y);
58+
require(dst3[1][0].x == src3[1][0].x);
59+
require(dst3[1][0].y == src3[1][0].y);
60+
}
61+
}
62+
63+
// ====
64+
// compileViaYul: true
65+
// ----
66+
// test1()
67+
// gas irOptimized: 123279
68+
// test2()
69+
// gas irOptimized: 123073
70+
// test3()

0 commit comments

Comments
 (0)