diff options
author | chriseth <c@ethdev.com> | 2015-11-26 22:28:28 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-11-26 22:28:28 +0800 |
commit | c498dcce22b2921ee57f280da9117e491c021e1b (patch) | |
tree | 1eb3bec6140d9b2e1ee57f62343dbc9c70d84c78 /test/libsolidity | |
parent | cd94aa978a77ace1296f9978bfae6d8735b5c91d (diff) | |
parent | e06768e8b580d009b9a9905f70ae2d8814699115 (diff) | |
download | dexon-solidity-c498dcce22b2921ee57f280da9117e491c021e1b.tar.gz dexon-solidity-c498dcce22b2921ee57f280da9117e491c021e1b.tar.zst dexon-solidity-c498dcce22b2921ee57f280da9117e491c021e1b.zip |
Merge pull request #222 from chriseth/newArrays
Dynamically create memory arrays.
Diffstat (limited to 'test/libsolidity')
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 46 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 54 |
2 files changed, 99 insertions, 1 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 93b42c51..0b356145 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1955,7 +1955,7 @@ BOOST_AUTO_TEST_CASE(value_for_constructor) contract Main { Helper h; function Main() { - h = new Helper.value(10)("abc", true); + h = (new Helper).value(10)("abc", true); } function getFlag() returns (bool ret) { return h.getFlag(); } function getName() returns (bytes3 ret) { return h.getName(); } @@ -5816,6 +5816,50 @@ BOOST_AUTO_TEST_CASE(lone_struct_array_type) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(3))); } +BOOST_AUTO_TEST_CASE(create_memory_array) +{ + char const* sourceCode = R"( + contract C { + struct S { uint[2] a; bytes b; } + function f() returns (byte, uint, uint, byte) { + var x = new bytes(200); + x[199] = 'A'; + var y = new uint[2][](300); + y[203][1] = 8; + var z = new S[](180); + z[170].a[1] = 4; + z[170].b = new bytes(102); + z[170].b[99] = 'B'; + return (x[199], y[203][1], z[170].a[1], z[170].b[99]); + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(string("A"), u256(8), u256(4), string("B"))); +} + +BOOST_AUTO_TEST_CASE(memory_arrays_of_various_sizes) +{ + // Computes binomial coefficients the chinese way + char const* sourceCode = R"( + contract C { + function f(uint n, uint k) returns (uint) { + uint[][] memory rows = new uint[][](n + 1); + for (uint i = 1; i <= n; i++) { + rows[i] = new uint[](i); + rows[i][0] = rows[i][rows[i].length - 1] = 1; + for (uint j = 1; j < i - 1; j++) + rows[i][j] = rows[i - 1][j - 1] + rows[i - 1][j]; + } + return rows[n][k - 1]; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f(uint256,uint256)", encodeArgs(u256(3), u256(1))) == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("f(uint256,uint256)", encodeArgs(u256(9), u256(5))) == encodeArgs(u256(70))); +} + BOOST_AUTO_TEST_CASE(memory_overwrite) { char const* sourceCode = R"( diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index e87e47a8..4f26fa4d 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2529,6 +2529,60 @@ BOOST_AUTO_TEST_CASE(member_access_parser_ambiguity) BOOST_CHECK(success(text)); } +BOOST_AUTO_TEST_CASE(create_memory_arrays) +{ + char const* text = R"( + library L { + struct R { uint[10][10] y; } + struct S { uint a; uint b; uint[20][20][20] c; R d; } + } + contract C { + function f(uint size) { + L.S[][] memory x = new L.S[][](10); + var y = new uint[](20); + var z = new bytes(size); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(mapping_in_memory_array) +{ + char const* text = R"( + contract C { + function f(uint size) { + var x = new mapping(uint => uint)[](4); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(new_for_non_array) +{ + char const* text = R"( + contract C { + function f(uint size) { + var x = new uint(7); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(invalid_args_creating_memory_array) +{ + char const* text = R"( + contract C { + function f(uint size) { + var x = new uint[](); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + BOOST_AUTO_TEST_SUITE_END() } |