From 7ebd536e79215f06f5ce7e14591aa494d06032b6 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 12 Oct 2015 23:02:35 +0200 Subject: Tuple expressions. --- test/libsolidity/SolidityEndToEndTest.cpp | 55 ++++++++++++++++++++++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 27 +++++++++++ test/libsolidity/SolidityParser.cpp | 17 ++++++- 3 files changed, 98 insertions(+), 1 deletion(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index bee19010..1d9a403a 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5677,6 +5677,61 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration) BOOST_CHECK(callContractFunction("f()", encodeArgs()) == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(tuples) +{ + char const* sourceCode = R"( + contract C { + uint[] data; + function g() internal returns (uint a, uint b, uint[] storage c) { + return (1, 2, data); + } + function h() external returns (uint a, uint b) { + return (5, 6); + } + function f() returns (uint) { + data.length = 1; + data[0] = 3; + uint a; uint b; + (a, b) = this.h(); + if (a != 1 || b != 2) return 1; + uint[] storage c; + (a, b, c) = g(); + if (a != 5 || b != 6 || c[0] != 3) return 2; + (a, b) = (b, a); + if (a != 6 || b != 5) return 3; + (a, , b, ) = (8, 9, 10, 11, 12); + if (a != 8 || b != 10) return 3; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); +} + +BOOST_AUTO_TEST_CASE(destructuring_assignment_wildcard) +{ + char const* sourceCode = R"( + contract C { + function f() returns (uint) { + uint a; + uint b; + uint c; + (a,) = (1,); + if (a != 1) return 1; + (,b) = (2,3,4); + if (b != 4) return 2; + (, c,) = (5,6,7); + if (c != 6) return 3; + (a, b,) = (11, 12, 13); + if (a != 11 || b != 12) return 4; + (, a, b) = (11, 12, 13); + if (a != 12 || b != 13) return 5; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); +} BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 2558ba97..0b5a86d7 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2460,6 +2460,33 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_4) BOOST_CHECK(expectError(text) == Error::Type::TypeError); } +BOOST_AUTO_TEST_CASE(tuples) +{ + char const* text = R"( + contract C { + function f() { + uint a = (1); + var (b,) = (1,); + var (c,d) = (1, 2 + a); + var (e,) = (1, 2, b); + } + } + )"; + BOOST_CHECK_NO_THROW(parseAndAnalyse(text)); +} + +BOOST_AUTO_TEST_CASE(tuples_empty_components) +{ + char const* text = R"( + contract C { + function f() { + (1,,2); + } + } + )"; + SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); +} + BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_5) { char const* text = R"( diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 35393811..9aa4a8ef 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -983,7 +983,7 @@ BOOST_AUTO_TEST_CASE(local_const_variable) BOOST_AUTO_TEST_CASE(multi_variable_declaration) { char const* text = R"( - library Lib { + contract C { function f() { var (a,b,c) = g(); var (d) = 2; @@ -1000,6 +1000,21 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration) BOOST_CHECK(successParse(text)); } +BOOST_AUTO_TEST_CASE(tuples) +{ + char const* text = R"( + contract C { + function f() { + uint a = (1); + var (b,) = (1,); + var (c,d) = (1, 2 + a); + var (e,) = (1, 2, b); + } + } + )"; + BOOST_CHECK_NO_THROW(parseText(text)); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 039b2a764f3944768bb253102f4c4b788f2dca9c Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 14 Oct 2015 15:19:50 +0200 Subject: Destructuring assignments. --- test/libsolidity/SolidityEndToEndTest.cpp | 47 +++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 1d9a403a..36114689 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5693,14 +5693,14 @@ BOOST_AUTO_TEST_CASE(tuples) data[0] = 3; uint a; uint b; (a, b) = this.h(); - if (a != 1 || b != 2) return 1; + if (a != 5 || b != 6) return 1; uint[] storage c; (a, b, c) = g(); - if (a != 5 || b != 6 || c[0] != 3) return 2; + if (a != 1 || b != 2 || c[0] != 3) return 2; (a, b) = (b, a); - if (a != 6 || b != 5) return 3; - (a, , b, ) = (8, 9, 10, 11, 12); - if (a != 8 || b != 10) return 3; + if (a != 2 || b != 1) return 3; +// (a, , b, ) = (8, 9, 10, 11, 12); +// if (a != 8 || b != 10) return 3; } } )"; @@ -5708,6 +5708,42 @@ BOOST_AUTO_TEST_CASE(tuples) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); } +BOOST_AUTO_TEST_CASE(destructuring_assignment) +{ + char const* sourceCode = R"( + contract C { + uint x = 7; + bytes data; + uint[] y; + uint[] arrayData; + function returnsArray() returns (uint[]) { + arrayData.length = 9; + arrayData[2] = 5; + arrayData[7] = 4; + return arrayData; + } + function f(bytes s) returns (uint) { + uint loc; + uint[] memArray; + (loc, x, y, data, arrayData[3]) = (8, 4, returnsArray(), s, 2); + if (loc != 8) return 1; + if (x != 4) return 2; + if (y.length != 9) return 3; + if (y[2] != 5) return 4; + if (y[7] != 4) return 5; + if (data.length != s.length) return 6; + if (data[3] != s[3]) return 7; + if (arrayData[3] != 2) return 8; + (memArray, loc) = (arrayData, 3); + if (loc != 3) return 9; + if (memArray.length != arrayData.length) return 10; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f(bytes)", u256(0x20), u256(5), string("abcde")) == encodeArgs(u256(0))); +} + BOOST_AUTO_TEST_CASE(destructuring_assignment_wildcard) { char const* sourceCode = R"( @@ -5732,6 +5768,7 @@ BOOST_AUTO_TEST_CASE(destructuring_assignment_wildcard) compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0))); } + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 029b8194892b6b08ce70075bd66f43f66c40e301 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 15 Oct 2015 00:42:36 +0200 Subject: Wildcards. --- test/libsolidity/SolidityEndToEndTest.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 36114689..75e43b73 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5699,8 +5699,8 @@ BOOST_AUTO_TEST_CASE(tuples) if (a != 1 || b != 2 || c[0] != 3) return 2; (a, b) = (b, a); if (a != 2 || b != 1) return 3; -// (a, , b, ) = (8, 9, 10, 11, 12); -// if (a != 8 || b != 10) return 3; + (a, , b, ) = (8, 9, 10, 11, 12); + if (a != 8 || b != 10) return 4; } } )"; @@ -5724,7 +5724,7 @@ BOOST_AUTO_TEST_CASE(destructuring_assignment) } function f(bytes s) returns (uint) { uint loc; - uint[] memArray; + uint[] memory memArray; (loc, x, y, data, arrayData[3]) = (8, 4, returnsArray(), s, 2); if (loc != 8) return 1; if (x != 4) return 2; @@ -5737,6 +5737,9 @@ BOOST_AUTO_TEST_CASE(destructuring_assignment) (memArray, loc) = (arrayData, 3); if (loc != 3) return 9; if (memArray.length != arrayData.length) return 10; + bytes memory memBytes; + (x, memBytes, y[2], ) = (456, s, 789, 101112, 131415); + if (x != 456 || memBytes.length != s.length || y[2] != 789) return 11; } } )"; -- cgit v1.2.3 From 1d4219d43d2839c41e56307f9db04cc23d4741e5 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 15 Oct 2015 18:14:14 +0200 Subject: Some fixes taking other pull requests into account. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 12 ++++-------- test/libsolidity/SolidityParser.cpp | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 0b5a86d7..5002cc4f 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -109,13 +109,9 @@ ASTPointer parseAndAnalyse(string const& _source) return sourceAndError.first; } -bool success(std::string const& _source) +bool success(string const& _source) { - auto sourceAndError = parseAnalyseAndReturnError(_source); - - if (sourceAndError.second && *sourceAndError.second == Error::Type::TypeError) - return false; - return true; + return !parseAnalyseAndReturnError(_source).second; } Error::Type expectError(std::string const& _source, bool _warning = false) @@ -2472,7 +2468,7 @@ BOOST_AUTO_TEST_CASE(tuples) } } )"; - BOOST_CHECK_NO_THROW(parseAndAnalyse(text)); + BOOST_CHECK(success(text)); } BOOST_AUTO_TEST_CASE(tuples_empty_components) @@ -2484,7 +2480,7 @@ BOOST_AUTO_TEST_CASE(tuples_empty_components) } } )"; - SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError); + BOOST_CHECK(expectError(text) == Error::Type::TypeError); } BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_5) diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 9aa4a8ef..c181ae7e 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -1012,7 +1012,7 @@ BOOST_AUTO_TEST_CASE(tuples) } } )"; - BOOST_CHECK_NO_THROW(parseText(text)); + BOOST_CHECK(successParse(text)); } BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3