Memory structs.

This commit is contained in:
chriseth 2015-06-30 11:54:51 +02:00
parent 6308d8d0f7
commit fc8f83b84e

View File

@ -4806,6 +4806,149 @@ BOOST_AUTO_TEST_CASE(memory_arrays_dynamic_index_access_write)
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0x20), u256(4), data)); BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0x20), u256(4), data));
} }
BOOST_AUTO_TEST_CASE(memory_structs_read_write)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; uint8[2] a; }
S[5] data;
function testInit() returns (uint8 x, uint16 y, uint 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() returns (uint8 x, uint16 y, uint 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() returns (uint8 x, uint16 y, uint 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];
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("testInit()") == encodeArgs(u256(0), u256(0), u256(0), u256(0), true));
BOOST_CHECK(callContractFunction("testCopyRead()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
BOOST_CHECK(callContractFunction("testAssign()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
}
BOOST_AUTO_TEST_CASE(memory_structs_as_function_args)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; }
function test() returns (uint x, uint y, uint z) {
S memory data = combine(1, 2, 3);
x = extract(data, 0);
y = extract(data, 1);
z = extract(data, 2);
}
function extract(S s, uint which) internal returns (uint x) {
if (which == 0) return s.x;
else if (which == 1) return s.y;
else return s.z;
}
function combine(uint8 x, uint16 y, uint z) internal returns (S s) {
s.x = x;
s.y = y;
s.z = z;
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(1), u256(2), u256(3)));
}
BOOST_AUTO_TEST_CASE(memory_structs_wrapped)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; }
struct X { uint8 x; S s; }
function test() returns (uint a, uint x, uint y, uint 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 s, uint which) internal returns (uint 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, uint z) internal returns (X s) {
s.x = a;
s.s.x = x;
s.s.y = y;
s.s.z = z;
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
}
BOOST_AUTO_TEST_CASE(memory_structs_wrapped_load)
{
char const* sourceCode = R"(
contract Test {
struct S { uint8 x; uint16 y; uint z; }
struct X { uint8 x; S s; }
X m_x;
function load() returns (uint a, uint x, uint y, uint z) {
m_x.x = 1;
m_x.s.x = 2;
m_x.s.y = 3;
m_x.s.z = 4;
X memory d = m_x;
a = d.x;
x = d.s.x;
y = d.s.y;
z = d.s.z;
}
function store() returns (uint a, uint x, uint y, uint z) {
X memory d = m_x;
d.x = 1;
d.s.x = 2;
d.s.y = 3;
d.s.z = 4;
m_x = d;
a = m_x.x;
x = m_x.s.x;
y = m_x.s.y;
z = m_x.s.z;
}
}
)";
compileAndRun(sourceCode, 0, "Test");
BOOST_CHECK(callContractFunction("load()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
BOOST_CHECK(callContractFunction("store()") == encodeArgs(u256(1), u256(2), u256(3), u256(4)));
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }