mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Sort tests.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256 b;
|
||||
}
|
||||
|
||||
function f(S calldata s) external pure returns (uint256 a, uint256 b) {
|
||||
a = s.a;
|
||||
b = s.b;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f((uint256,uint256)): 42, 23 -> 42, 23
|
||||
@@ -0,0 +1,20 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256 b;
|
||||
}
|
||||
|
||||
function f(uint256 a, S calldata s, uint256 b)
|
||||
external
|
||||
pure
|
||||
returns (uint256, uint256, uint256, uint256)
|
||||
{
|
||||
return (a, s.a, s.b, b);
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f(uint256,(uint256,uint256),uint256): 1, 2, 3, 4 -> 1, 2, 3, 4
|
||||
@@ -0,0 +1,24 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256[2] b;
|
||||
uint256 c;
|
||||
}
|
||||
|
||||
function f(S calldata s)
|
||||
external
|
||||
pure
|
||||
returns (uint256 a, uint256 b0, uint256 b1, uint256 c)
|
||||
{
|
||||
a = s.a;
|
||||
b0 = s.b[0];
|
||||
b1 = s.b[1];
|
||||
c = s.c;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f((uint256,uint256[2],uint256)): 42, 1, 2, 23 -> 42, 1, 2, 23
|
||||
@@ -0,0 +1,17 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256 b;
|
||||
}
|
||||
|
||||
function f(S calldata s) external pure returns (uint256, uint256) {
|
||||
S memory m = s;
|
||||
return (m.a, m.b);
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f((uint256,uint256)): 42, 23 -> 42, 23
|
||||
@@ -0,0 +1,27 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S1 {
|
||||
uint256 a;
|
||||
uint256 b;
|
||||
}
|
||||
struct S2 {
|
||||
uint256 a;
|
||||
}
|
||||
|
||||
function f(S1 calldata s1, S2 calldata s2, S1 calldata s3)
|
||||
external
|
||||
pure
|
||||
returns (uint256 a, uint256 b, uint256 c, uint256 d, uint256 e)
|
||||
{
|
||||
a = s1.a;
|
||||
b = s1.b;
|
||||
c = s2.a;
|
||||
d = s3.a;
|
||||
e = s3.b;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f((uint256,uint256),(uint256),(uint256,uint256)): 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5
|
||||
@@ -0,0 +1,16 @@
|
||||
contract C {
|
||||
struct s {
|
||||
uint256 a;
|
||||
uint256 b;
|
||||
}
|
||||
|
||||
function f() public returns (uint256) {
|
||||
s[7][]; // This is only the type, should not have any effect
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 3
|
||||
@@ -0,0 +1,32 @@
|
||||
contract Test {
|
||||
struct S {
|
||||
uint8 x;
|
||||
uint16 y;
|
||||
uint256 z;
|
||||
}
|
||||
|
||||
function test() public returns (uint256 x, uint256 y, uint256 z) {
|
||||
S memory data = combine(1, 2, 3);
|
||||
x = extract(data, 0);
|
||||
y = extract(data, 1);
|
||||
z = extract(data, 2);
|
||||
}
|
||||
|
||||
function extract(S memory s, uint256 which) internal returns (uint256 x) {
|
||||
if (which == 0) return s.x;
|
||||
else if (which == 1) return s.y;
|
||||
else return s.z;
|
||||
}
|
||||
|
||||
function combine(uint8 x, uint16 y, uint256 z)
|
||||
internal
|
||||
returns (S memory s)
|
||||
{
|
||||
s.x = x;
|
||||
s.y = y;
|
||||
s.z = z;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// test() -> 1, 2, 3
|
||||
@@ -0,0 +1,42 @@
|
||||
contract Test {
|
||||
struct S {
|
||||
uint8 x;
|
||||
uint16 y;
|
||||
uint256 z;
|
||||
}
|
||||
struct X {
|
||||
uint8 x;
|
||||
S s;
|
||||
}
|
||||
|
||||
function test()
|
||||
public
|
||||
returns (uint256 a, uint256 x, uint256 y, uint256 z)
|
||||
{
|
||||
X memory d = combine(1, 2, 3, 4);
|
||||
a = extract(d, 0);
|
||||
x = extract(d, 1);
|
||||
y = extract(d, 2);
|
||||
z = extract(d, 3);
|
||||
}
|
||||
|
||||
function extract(X memory s, uint256 which) internal returns (uint256 x) {
|
||||
if (which == 0) return s.x;
|
||||
else if (which == 1) return s.s.x;
|
||||
else if (which == 2) return s.s.y;
|
||||
else return s.s.z;
|
||||
}
|
||||
|
||||
function combine(uint8 a, uint8 x, uint16 y, uint256 z)
|
||||
internal
|
||||
returns (X memory s)
|
||||
{
|
||||
s.x = a;
|
||||
s.s.x = x;
|
||||
s.s.y = y;
|
||||
s.s.z = z;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// test() -> 1, 2, 3, 4
|
||||
@@ -0,0 +1,56 @@
|
||||
contract Test {
|
||||
struct S {
|
||||
uint8 x;
|
||||
uint16 y;
|
||||
uint256 z;
|
||||
uint8[2] a;
|
||||
}
|
||||
S[5] data;
|
||||
|
||||
function testInit()
|
||||
public
|
||||
returns (uint8 x, uint16 y, uint256 z, uint8 a, bool flag)
|
||||
{
|
||||
S[2] memory d;
|
||||
x = d[0].x;
|
||||
y = d[0].y;
|
||||
z = d[0].z;
|
||||
a = d[0].a[1];
|
||||
flag = true;
|
||||
}
|
||||
|
||||
function testCopyRead()
|
||||
public
|
||||
returns (uint8 x, uint16 y, uint256 z, uint8 a)
|
||||
{
|
||||
data[2].x = 1;
|
||||
data[2].y = 2;
|
||||
data[2].z = 3;
|
||||
data[2].a[1] = 4;
|
||||
S memory s = data[2];
|
||||
x = s.x;
|
||||
y = s.y;
|
||||
z = s.z;
|
||||
a = s.a[1];
|
||||
}
|
||||
|
||||
function testAssign()
|
||||
public
|
||||
returns (uint8 x, uint16 y, uint256 z, uint8 a)
|
||||
{
|
||||
S memory s;
|
||||
s.x = 1;
|
||||
s.y = 2;
|
||||
s.z = 3;
|
||||
s.a[1] = 4;
|
||||
x = s.x;
|
||||
y = s.y;
|
||||
z = s.z;
|
||||
a = s.a[1];
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// testInit() -> 0, 0, 0, 0, true
|
||||
// testCopyRead() -> 1, 2, 3, 4
|
||||
// testAssign() -> 1, 2, 3, 4
|
||||
@@ -0,0 +1,24 @@
|
||||
contract Test {
|
||||
struct S {
|
||||
uint8 a;
|
||||
mapping(uint256 => uint256) b;
|
||||
uint8 c;
|
||||
}
|
||||
S s;
|
||||
|
||||
function f() public returns (uint256) {
|
||||
S memory x;
|
||||
if (x.a != 0 || x.c != 0) return 1;
|
||||
x.a = 4;
|
||||
x.c = 5;
|
||||
s = x;
|
||||
if (s.a != 4 || s.c != 5) return 2;
|
||||
x = S(2, 3);
|
||||
if (x.a != 2 || x.c != 3) return 3;
|
||||
x = s;
|
||||
if (s.a != 4 || s.c != 5) return 4;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> 0
|
||||
@@ -0,0 +1,19 @@
|
||||
contract C {
|
||||
struct S {
|
||||
S[] x;
|
||||
}
|
||||
S sstorage;
|
||||
|
||||
function f() public returns (uint256) {
|
||||
S memory s;
|
||||
s.x = new S[](10);
|
||||
delete s;
|
||||
// TODO Uncomment after implemented.
|
||||
// sstorage.x.push();
|
||||
delete sstorage;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> 1
|
||||
@@ -0,0 +1,36 @@
|
||||
contract test {
|
||||
struct testStruct {
|
||||
uint256 m_value;
|
||||
}
|
||||
testStruct data1;
|
||||
testStruct data2;
|
||||
testStruct data3;
|
||||
|
||||
constructor() public {
|
||||
data1.m_value = 2;
|
||||
}
|
||||
|
||||
function assign()
|
||||
public
|
||||
returns (
|
||||
uint256 ret_local,
|
||||
uint256 ret_global,
|
||||
uint256 ret_global3,
|
||||
uint256 ret_global1
|
||||
)
|
||||
{
|
||||
testStruct storage x = data1; //x is a reference data1.m_value == 2 as well as x.m_value = 2
|
||||
data2 = data1; // should copy data. data2.m_value == 2
|
||||
|
||||
ret_local = x.m_value; // = 2
|
||||
ret_global = data2.m_value; // = 2
|
||||
|
||||
x.m_value = 3;
|
||||
data3 = x; //should copy the data. data3.m_value == 3
|
||||
ret_global3 = data3.m_value; // = 3
|
||||
ret_global1 = data1.m_value; // = 3. Changed due to the assignment to x.m_value
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// assign() -> 2, 2, 3, 3
|
||||
@@ -0,0 +1,48 @@
|
||||
contract c {
|
||||
struct Nested {
|
||||
uint256 x;
|
||||
uint256 y;
|
||||
}
|
||||
struct Struct {
|
||||
uint256 a;
|
||||
mapping(uint256 => Struct) b;
|
||||
Nested nested;
|
||||
uint256 c;
|
||||
}
|
||||
mapping(uint256 => Struct) data;
|
||||
|
||||
function set(uint256 k) public returns (bool) {
|
||||
data[k].a = 1;
|
||||
data[k].nested.x = 3;
|
||||
data[k].nested.y = 4;
|
||||
data[k].c = 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
function copy(uint256 from, uint256 to) public returns (bool) {
|
||||
data[to] = data[from];
|
||||
return true;
|
||||
}
|
||||
|
||||
function retrieve(uint256 k)
|
||||
public
|
||||
returns (uint256 a, uint256 x, uint256 y, uint256 c)
|
||||
{
|
||||
a = data[k].a;
|
||||
x = data[k].nested.x;
|
||||
y = data[k].nested.y;
|
||||
c = data[k].c;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// set(uint256): 7 -> true
|
||||
// retrieve(uint256): 7 -> 1, 3, 4, 2
|
||||
// copy(uint256,uint256): 7, 8 -> true
|
||||
// retrieve(uint256): 7 -> 1, 3, 4, 2
|
||||
// retrieve(uint256): 8 -> 1, 3, 4, 2
|
||||
// copy(uint256,uint256): 0, 7 -> true
|
||||
// retrieve(uint256): 7 -> 0, 0, 0, 0
|
||||
// retrieve(uint256): 8 -> 1, 3, 4, 2
|
||||
// copy(uint256,uint256): 7, 8 -> true
|
||||
// retrieve(uint256): 8 -> 0, 0, 0, 0
|
||||
@@ -0,0 +1,19 @@
|
||||
contract c {
|
||||
struct Struct {
|
||||
uint256 a;
|
||||
uint256 b;
|
||||
}
|
||||
Struct data1;
|
||||
Struct data2;
|
||||
|
||||
function test() public returns (bool) {
|
||||
data1.a = 1;
|
||||
data1.b = 2;
|
||||
Struct memory x = data1;
|
||||
data2 = x;
|
||||
return data2.a == data1.a && data2.b == data1.b;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// test() -> true
|
||||
@@ -0,0 +1,20 @@
|
||||
contract test {
|
||||
struct testStruct {
|
||||
uint256 m_value;
|
||||
}
|
||||
testStruct data1;
|
||||
|
||||
constructor() public {
|
||||
data1.m_value = 2;
|
||||
}
|
||||
|
||||
function deleteMember() public returns (uint256 ret_value) {
|
||||
testStruct storage x = data1; //should not copy the data. data1.m_value == 2 but x.m_value = 0
|
||||
x.m_value = 4;
|
||||
delete x.m_value;
|
||||
ret_value = data1.m_value;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// deleteMember() -> 0
|
||||
@@ -0,0 +1,18 @@
|
||||
contract test {
|
||||
struct testStruct {
|
||||
uint256 m_value;
|
||||
}
|
||||
mapping(uint256 => testStruct) campaigns;
|
||||
|
||||
constructor() public {
|
||||
campaigns[0].m_value = 2;
|
||||
}
|
||||
|
||||
function deleteIt() public returns (uint256) {
|
||||
delete campaigns[0];
|
||||
return campaigns[0].m_value;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// deleteIt() -> 0
|
||||
@@ -0,0 +1,14 @@
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
bool x;
|
||||
}
|
||||
S public s;
|
||||
|
||||
constructor() public {
|
||||
s = S({a: 1, x: true});
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// s() -> 1, true
|
||||
Reference in New Issue
Block a user