From e7906ba1beecf9fd04d9e3476da7a66d61714629 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 15 Jun 2015 12:10:41 +0200 Subject: Copying between memory and memory. Also fixed some encoding and padding issues with older copying code. --- libsolidity/SolidityEndToEndTest.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index f12abd48..f4d875e7 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -2420,7 +2420,7 @@ BOOST_AUTO_TEST_CASE(event_really_lots_of_data_from_storage) callContractFunction("deposit()"); BOOST_REQUIRE_EQUAL(m_logs.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress); - BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 3) + asBytes("ABC")); + BOOST_CHECK(m_logs[0].data == encodeArgs(10, 0x60, 15, 3, string("ABC"))); BOOST_REQUIRE_EQUAL(m_logs[0].topics.size(), 1); BOOST_CHECK_EQUAL(m_logs[0].topics[0], dev::sha3(string("Deposit(uint256,bytes,uint256)"))); } @@ -4232,6 +4232,31 @@ BOOST_AUTO_TEST_CASE(reusing_memory) BOOST_REQUIRE(callContractFunction("f(uint256)", 0x34) == encodeArgs(dev::sha3(dev::toBigEndian(u256(0x34))))); } +BOOST_AUTO_TEST_CASE(return_string) +{ + char const* sourceCode = R"( + contract Main { + string public s; + function set(string _s) external { + s = _s; + } + function get1() returns (string r) { + return s; + } +// function get2() returns (string r) { +// r = s; +// } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s("Julia"); + bytes args = encodeArgs(u256(0x20), u256(s.length()), s); + BOOST_REQUIRE(callContractFunction("set(string)", asString(args)) == encodeArgs()); + BOOST_CHECK(callContractFunction("get1()") == args); +// BOOST_CHECK(callContractFunction("get2()") == args); +// BOOST_CHECK(callContractFunction("s()") == args); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 6e9f58622846e2ce24ce5caa3c24d12f44dfc96d Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 16 Jun 2015 17:20:41 +0200 Subject: Type conversion specialities for storage references. --- libsolidity/SolidityEndToEndTest.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index f4d875e7..109481ed 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4243,9 +4243,9 @@ BOOST_AUTO_TEST_CASE(return_string) function get1() returns (string r) { return s; } -// function get2() returns (string r) { -// r = s; -// } + function get2() returns (string r) { + r = s; + } } )"; compileAndRun(sourceCode, 0, "Main"); @@ -4253,8 +4253,8 @@ BOOST_AUTO_TEST_CASE(return_string) bytes args = encodeArgs(u256(0x20), u256(s.length()), s); BOOST_REQUIRE(callContractFunction("set(string)", asString(args)) == encodeArgs()); BOOST_CHECK(callContractFunction("get1()") == args); -// BOOST_CHECK(callContractFunction("get2()") == args); -// BOOST_CHECK(callContractFunction("s()") == args); + BOOST_CHECK(callContractFunction("get2()") == args); + BOOST_CHECK(callContractFunction("s()") == args); } BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3 From db120699688a1a76089a372753bc310472099648 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 17 Jun 2015 12:01:39 +0200 Subject: Accessors for strings. --- libsolidity/SolidityEndToEndTest.cpp | 78 ++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 109481ed..10624281 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4257,6 +4257,84 @@ BOOST_AUTO_TEST_CASE(return_string) BOOST_CHECK(callContractFunction("s()") == args); } +BOOST_AUTO_TEST_CASE(return_multiple_strings_of_various_sizes) +{ + char const* sourceCode = R"( + contract Main { + string public s1; + string public s2; + function set(string _s1, uint x, string _s2) external returns (uint) { + s1 = _s1; + s2 = _s2; + return x; + } + function get() returns (string r1, string r2) { + r1 = s1; + r2 = s2; + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s1( + "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + ); + string s2( + "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ" + "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ" + "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ" + "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ" + "ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ" + ); + vector lengthes{0, 30, 32, 63, 64, 65, 210, 300}; + for (auto l1: lengthes) + for (auto l2: lengthes) + { + bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1)); + bytes dyn2 = encodeArgs(u256(l2), s2.substr(0, l2)); + bytes args = encodeArgs(u256(0x60), u256(l1), u256(0x60 + dyn1.size())) + dyn1 + dyn2; + BOOST_REQUIRE( + callContractFunction("set(string,uint256,string)", asString(args)) == + encodeArgs(u256(l1)) + ); + bytes result = encodeArgs(u256(0x40), u256(0x40 + dyn1.size())) + dyn1 + dyn2; + BOOST_CHECK(callContractFunction("get()") == result); + BOOST_CHECK(callContractFunction("s1()") == encodeArgs(0x20) + dyn1); + BOOST_CHECK(callContractFunction("s2()") == encodeArgs(0x20) + dyn2); + } +} + +BOOST_AUTO_TEST_CASE(accessor_involving_strings) +{ + char const* sourceCode = R"( + contract Main { + struct stringData { string a; uint b; string c; } + mapping(uint => stringData[]) public data; + function set(uint x, uint y, string a, uint b, string c) external returns (bool) { + data[x].length = y + 1; + data[x][y].a = a; + data[x][y].b = b; + data[x][y].c = c; + return true; + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + string s2("ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"); + bytes s1Data = encodeArgs(u256(s1.length()), s1); + bytes s2Data = encodeArgs(u256(s2.length()), s2); + u256 b = 765; + u256 x = 7; + u256 y = 123; + bytes args = encodeArgs(x, y, u256(0xa0), b, u256(0xa0 + s1Data.size()), s1Data, s2Data); + bytes result = encodeArgs(u256(0x60), b, u256(0x60 + s1Data.size()), s1Data, s2Data); + BOOST_REQUIRE(callContractFunction("set(uint256,uint256,string,uint256,string)", asString(args)) == encodeArgs(true)); + BOOST_REQUIRE(callContractFunction("data(uint256,uint256)", x, y) == result); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 366e25b8d1db849bbfe11c9c645fd37cbae1a477 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 17 Jun 2015 18:51:29 +0200 Subject: Storage array reference test. --- libsolidity/SolidityEndToEndTest.cpp | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index f4d875e7..66a8f882 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4257,6 +4257,59 @@ BOOST_AUTO_TEST_CASE(return_string) // BOOST_CHECK(callContractFunction("s()") == args); } +BOOST_AUTO_TEST_CASE(storage_array_ref) +{ + char const* sourceCode = R"( + contract BinarySearch { + /// Finds the position of _value in the sorted list _data. + /// Note that "internal" is important here, because storage references only work for internal or private functions + function find(uint[] storage _data, uint _value) internal returns (uint o_position) { + return find(_data, 0, _data.length, _value); + } + function find(uint[] storage _data, uint _begin, uint _len, uint _value) private returns (uint o_position) { + if (_len == 0 || (_len == 1 && _data[_begin] != _value)) + return uint(-1); // failure + uint halfLen = _len / 2; + uint v = _data[_begin + halfLen]; + if (_value < v) + return find(_data, _begin, halfLen, _value); + else if (_value > v) + return find(_data, _begin + halfLen + 1, halfLen - 1, _value); + else + return _begin + halfLen; + } + } + + contract Store is BinarySearch { + uint[] data; + function add(uint v) { + data.length++; + data[data.length - 1] = v; + } + function find(uint v) returns (uint) { + return find(data, v); + } + } + )"; + compileAndRun(sourceCode, 0, "Store"); + BOOST_REQUIRE(callContractFunction("find(uint256)", u256(7)) == encodeArgs(u256(-1))); + BOOST_REQUIRE(callContractFunction("add(uint256)", u256(7)) == encodeArgs()); + BOOST_REQUIRE(callContractFunction("find(uint256)", u256(7)) == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("add(uint256)", u256(11)) == encodeArgs()); + BOOST_CHECK(callContractFunction("add(uint256)", u256(17)) == encodeArgs()); + BOOST_CHECK(callContractFunction("add(uint256)", u256(27)) == encodeArgs()); + BOOST_CHECK(callContractFunction("add(uint256)", u256(31)) == encodeArgs()); + BOOST_CHECK(callContractFunction("add(uint256)", u256(32)) == encodeArgs()); + BOOST_CHECK(callContractFunction("add(uint256)", u256(66)) == encodeArgs()); + BOOST_CHECK(callContractFunction("add(uint256)", u256(177)) == encodeArgs()); + BOOST_CHECK(callContractFunction("find(uint256)", u256(7)) == encodeArgs(u256(0))); + BOOST_CHECK(callContractFunction("find(uint256)", u256(27)) == encodeArgs(u256(3))); + BOOST_CHECK(callContractFunction("find(uint256)", u256(32)) == encodeArgs(u256(5))); + BOOST_CHECK(callContractFunction("find(uint256)", u256(176)) == encodeArgs(u256(-1))); + BOOST_CHECK(callContractFunction("find(uint256)", u256(0)) == encodeArgs(u256(-1))); + BOOST_CHECK(callContractFunction("find(uint256)", u256(400)) == encodeArgs(u256(-1))); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 6d2f2f668ba2529aa2ed0bae88afc55b2869cd97 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 18 Jun 2015 14:41:06 +0200 Subject: Tests for constructor arguments "from outside". --- libsolidity/SolidityEndToEndTest.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 66a8f882..7ee259ab 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -1726,7 +1726,7 @@ BOOST_AUTO_TEST_CASE(fixed_bytes_in_calls) BOOST_CHECK(callContractFunction("callHelper(bytes2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5))); } -BOOST_AUTO_TEST_CASE(constructor_arguments) +BOOST_AUTO_TEST_CASE(constructor_arguments_internal) { char const* sourceCode = R"( contract Helper { @@ -1749,8 +1749,28 @@ BOOST_AUTO_TEST_CASE(constructor_arguments) function getName() returns (bytes3 ret) { return h.getName(); } })"; compileAndRun(sourceCode, 0, "Main"); - BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true)); - BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); + BOOST_CHECK(callContractFunction("getFlag()") == encodeArgs(true)); + BOOST_CHECK(callContractFunction("getName()") == encodeArgs("abc")); +} + +BOOST_AUTO_TEST_CASE(constructor_arguments_external) +{ + char const* sourceCode = R"( + contract Main { + bytes3 name; + bool flag; + + function Main(bytes3 x, bool f) { + name = x; + flag = f; + } + function getName() returns (bytes3 ret) { return name; } + function getFlag() returns (bool ret) { return flag; } + } + )"; + compileAndRun(sourceCode, 0, "Main", encodeArgs("abc", true)); + BOOST_CHECK(callContractFunction("getFlag()") == encodeArgs(true)); + BOOST_CHECK(callContractFunction("getName()") == encodeArgs("abc")); } BOOST_AUTO_TEST_CASE(functions_called_by_constructor) @@ -4166,7 +4186,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund) } } )"; - BOOST_CHECK(compileAndRunWthoutCheck(sourceCode, 0, "A").empty()); + BOOST_CHECK(compileAndRunWithoutCheck(sourceCode, 0, "A").empty()); } BOOST_AUTO_TEST_CASE(positive_integers_to_signed) -- cgit v1.2.3 From 128fbccd42c4f480a488e92cd14aaac4744b80d4 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 22 Jun 2015 12:50:09 +0200 Subject: Memory-storage copy. --- libsolidity/SolidityEndToEndTest.cpp | 168 +++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index a839aa02..16ef8e49 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4355,6 +4355,174 @@ BOOST_AUTO_TEST_CASE(accessor_involving_strings) BOOST_REQUIRE(callContractFunction("data(uint256,uint256)", x, y) == result); } +BOOST_AUTO_TEST_CASE(bytes_in_function_calls) +{ + char const* sourceCode = R"( + contract Main { + string public s1; + string public s2; + function set(string _s1, uint x, string _s2) returns (uint) { + s1 = _s1; + s2 = _s2; + return x; + } + function setIndirectFromMemory(string _s1, uint x, string _s2) returns (uint) { + return this.set(_s1, x, _s2); + } + function setIndirectFromCalldata(string _s1, uint x, string _s2) external returns (uint) { + return this.set(_s1, x, _s2); + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + string s2("ABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZABCDEFGHIJKLMNOPQRSTUVXYZ"); + vector lengthes{0, 31, 64, 65}; + for (auto l1: lengthes) + for (auto l2: lengthes) + { + bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1)); + bytes dyn2 = encodeArgs(u256(l2), s2.substr(0, l2)); + bytes args1 = encodeArgs(u256(0x60), u256(l1), u256(0x60 + dyn1.size())) + dyn1 + dyn2; + BOOST_REQUIRE( + callContractFunction("setIndirectFromMemory(string,uint256,string)", asString(args1)) == + encodeArgs(u256(l1)) + ); + BOOST_CHECK(callContractFunction("s1()") == encodeArgs(0x20) + dyn1); + BOOST_CHECK(callContractFunction("s2()") == encodeArgs(0x20) + dyn2); + // swapped + bytes args2 = encodeArgs(u256(0x60), u256(l1), u256(0x60 + dyn2.size())) + dyn2 + dyn1; + BOOST_REQUIRE( + callContractFunction("setIndirectFromCalldata(string,uint256,string)", asString(args2)) == + encodeArgs(u256(l1)) + ); + BOOST_CHECK(callContractFunction("s1()") == encodeArgs(0x20) + dyn2); + BOOST_CHECK(callContractFunction("s2()") == encodeArgs(0x20) + dyn1); + } +} + +BOOST_AUTO_TEST_CASE(return_bytes_external) +{ + char const* sourceCode = R"( + contract Main { + bytes s1; + function set(bytes _s1) external returns (uint _r, bytes _r1) { + _r = _s1.length; + s1 = this.setStageTwo(_s1); + _r1 = s1; + } + function setStageTwo(bytes _s1) returns (bytes) { + return this.setStageThree(_s1); + } + function setStageThree(bytes _s1) external returns (bytes) { + return _s1; + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + vector lengthes{0, 31, 64, 65}; + for (auto l1: lengthes) + { + bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1)); + bytes args1 = encodeArgs(u256(0x20)) + dyn1; + BOOST_REQUIRE( + callContractFunction("set(bytes)", asString(args1)) == + encodeArgs(u256(l1), u256(0x40)) + dyn1 + ); + } +} + +BOOST_AUTO_TEST_CASE(return_bytes_internal) +{ + char const* sourceCode = R"( + contract Main { + bytes s1; + function doSet(bytes _s1) returns (bytes _r1) { + s1 = _s1; + _r1 = s1; + } + function set(bytes _s1) external returns (uint _r, bytes _r1) { + _r1 = doSet(_s1); + _r = _r1.length; + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + vector lengthes{0, 31, 64, 65}; + for (auto l1: lengthes) + { + bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1)); + bytes args1 = encodeArgs(u256(0x20)) + dyn1; + BOOST_REQUIRE( + callContractFunction("set(bytes)", asString(args1)) == + encodeArgs(u256(l1), u256(0x40)) + dyn1 + ); + } +} + +BOOST_AUTO_TEST_CASE(bytes_index_access_memory) +{ + char const* sourceCode = R"( + contract Main { + function f(bytes _s1, uint i1, uint i2, uint i3) returns (byte c1, byte c2, byte c3) { + c1 = _s1[i1]; + c2 = intern(_s1, i2); + c3 = internIndirect(_s1)[i3]; + } + function intern(bytes _s1, uint i) returns (byte c) { + return _s1[i]; + } + function internIndirect(bytes _s1) returns (bytes) { + return _s1; + } + } + )"; + compileAndRun(sourceCode, 0, "Main"); + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + bytes dyn1 = encodeArgs(u256(s1.length()), s1); + bytes args1 = encodeArgs(u256(0x80), u256(3), u256(4), u256(5)) + dyn1; + BOOST_REQUIRE( + callContractFunction("f(bytes,uint256,uint256,uint256)", asString(args1)) == + encodeArgs(string{s1[3]}, string{s1[4]}, string{s1[5]}) + ); +} + +BOOST_AUTO_TEST_CASE(bytes_in_constructors) +{ + char const* sourceCode = R"( + contract Base { + uint public m_x; + bytes public m_s; + function Base(uint x, bytes s) { + m_x = x; + m_s = s; + } + } + contract Main is Base { + function Main(bytes s, uint x) Base(x, f(s)) {} + function f(bytes s) returns (bytes) { return s; } + } + contract Creator { + function f(uint x, bytes s) returns (uint r, bytes b) { + var c = new Main(s, x); + r = c.m_x(); + b = c.m_s(); + } + } + )"; + compileAndRun(sourceCode, 0, "Creator"); + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + bytes dyn1 = encodeArgs(u256(s1.length()), s1); + u256 x = 7; + bytes args1 = encodeArgs(x, u256(0x40)) + dyn1; + BOOST_REQUIRE( + callContractFunction("f(uint256,bytes)", asString(args1)) == + encodeArgs(x, u256(0x40), dyn1) + ); +} + BOOST_AUTO_TEST_CASE(storage_array_ref) { char const* sourceCode = R"( -- cgit v1.2.3 From 90ee186044bfa881d944836be9c6f396e610e219 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 22 Jun 2015 18:05:13 +0200 Subject: Remove dynamic return types. --- libsolidity/SolidityEndToEndTest.cpp | 32 -------------------------------- 1 file changed, 32 deletions(-) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index 16ef8e49..b0275b1b 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4401,38 +4401,6 @@ BOOST_AUTO_TEST_CASE(bytes_in_function_calls) } } -BOOST_AUTO_TEST_CASE(return_bytes_external) -{ - char const* sourceCode = R"( - contract Main { - bytes s1; - function set(bytes _s1) external returns (uint _r, bytes _r1) { - _r = _s1.length; - s1 = this.setStageTwo(_s1); - _r1 = s1; - } - function setStageTwo(bytes _s1) returns (bytes) { - return this.setStageThree(_s1); - } - function setStageThree(bytes _s1) external returns (bytes) { - return _s1; - } - } - )"; - compileAndRun(sourceCode, 0, "Main"); - string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); - vector lengthes{0, 31, 64, 65}; - for (auto l1: lengthes) - { - bytes dyn1 = encodeArgs(u256(l1), s1.substr(0, l1)); - bytes args1 = encodeArgs(u256(0x20)) + dyn1; - BOOST_REQUIRE( - callContractFunction("set(bytes)", asString(args1)) == - encodeArgs(u256(l1), u256(0x40)) + dyn1 - ); - } -} - BOOST_AUTO_TEST_CASE(return_bytes_internal) { char const* sourceCode = R"( -- cgit v1.2.3 From 1e8c26421be9c577ece43c2d1602afe70288c674 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 22 Jun 2015 20:50:29 +0200 Subject: Decoding for constructor. --- libsolidity/SolidityEndToEndTest.cpp | 40 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'libsolidity/SolidityEndToEndTest.cpp') diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index b0275b1b..d397dc59 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -4457,26 +4457,52 @@ BOOST_AUTO_TEST_CASE(bytes_index_access_memory) ); } -BOOST_AUTO_TEST_CASE(bytes_in_constructors) +BOOST_AUTO_TEST_CASE(bytes_in_constructors_unpacker) { char const* sourceCode = R"( - contract Base { + contract Test { uint public m_x; bytes public m_s; + function Test(uint x, bytes s) { + m_x = x; + m_s = s; + } + } + )"; + string s1("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); + bytes dyn1 = encodeArgs(u256(s1.length()), s1); + u256 x = 7; + bytes args1 = encodeArgs(x, u256(0x40)) + dyn1; + compileAndRun(sourceCode, 0, "Test", args1); + BOOST_REQUIRE(callContractFunction("m_x()") == encodeArgs(x)); + BOOST_REQUIRE(callContractFunction("m_s()") == encodeArgs(u256(0x20)) + dyn1); +} + +BOOST_AUTO_TEST_CASE(bytes_in_constructors_packer) +{ + char const* sourceCode = R"( + contract Base { + uint public m_x; + bytes m_s; function Base(uint x, bytes s) { m_x = x; m_s = s; } + function part(uint i) returns (byte) { + return m_s[i]; + } } contract Main is Base { - function Main(bytes s, uint x) Base(x, f(s)) {} - function f(bytes s) returns (bytes) { return s; } + function Main(bytes s, uint x) Base(x, s){}//f(s)) {} + function f(bytes s) returns (bytes) { + return s; + } } contract Creator { - function f(uint x, bytes s) returns (uint r, bytes b) { + function f(uint x, bytes s) returns (uint r, byte ch) { var c = new Main(s, x); r = c.m_x(); - b = c.m_s(); + ch = c.part(x); } } )"; @@ -4487,7 +4513,7 @@ BOOST_AUTO_TEST_CASE(bytes_in_constructors) bytes args1 = encodeArgs(x, u256(0x40)) + dyn1; BOOST_REQUIRE( callContractFunction("f(uint256,bytes)", asString(args1)) == - encodeArgs(x, u256(0x40), dyn1) + encodeArgs(x, string{s1[unsigned(x)]}) ); } -- cgit v1.2.3