diff options
Diffstat (limited to 'test/libsolidity')
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 155 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 160 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 15 |
3 files changed, 329 insertions, 1 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 0b356145..f625f533 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -1483,6 +1483,22 @@ BOOST_AUTO_TEST_CASE(suicide) BOOST_CHECK_EQUAL(m_state.balance(address), amount); } +BOOST_AUTO_TEST_CASE(selfdestruct) +{ + char const* sourceCode = "contract test {\n" + " function a(address receiver) returns (uint ret) {\n" + " selfdestruct(receiver);\n" + " return 10;\n" + " }\n" + "}\n"; + u256 amount(130); + compileAndRun(sourceCode, amount); + u160 address(23); + BOOST_CHECK(callContractFunction("a(address)", address) == bytes()); + BOOST_CHECK(!m_state.addressHasCode(m_contractAddress)); + BOOST_CHECK_EQUAL(m_state.balance(address), amount); +} + BOOST_AUTO_TEST_CASE(sha3) { char const* sourceCode = "contract test {\n" @@ -4671,6 +4687,23 @@ BOOST_AUTO_TEST_CASE(arrays_in_constructors) ); } +BOOST_AUTO_TEST_CASE(fixed_arrays_in_constructors) +{ + char const* sourceCode = R"( + contract Creator { + uint public r; + address public ch; + function Creator(address[3] s, uint x) { + r = x; + ch = s[2]; + } + } + )"; + compileAndRun(sourceCode, 0, "Creator", encodeArgs(u256(1), u256(2), u256(3), u256(4))); + BOOST_REQUIRE(callContractFunction("r()") == encodeArgs(u256(4))); + BOOST_REQUIRE(callContractFunction("ch()") == encodeArgs(u256(3))); +} + BOOST_AUTO_TEST_CASE(arrays_from_and_to_storage) { char const* sourceCode = R"( @@ -5625,7 +5658,7 @@ BOOST_AUTO_TEST_CASE(version_stamp_for_libraries) bytes runtimeCode = compileAndRun(sourceCode, 0, "lib"); BOOST_CHECK(runtimeCode.size() >= 8); BOOST_CHECK_EQUAL(runtimeCode[0], int(eth::Instruction::PUSH6)); // might change once we switch to 1.x.x - BOOST_CHECK_EQUAL(runtimeCode[1], 1); // might change once we switch away from x.1.x + BOOST_CHECK_EQUAL(runtimeCode[1], 2); // might change once we switch away from x.2.x BOOST_CHECK_EQUAL(runtimeCode[7], int(eth::Instruction::POP)); } @@ -5924,6 +5957,126 @@ BOOST_AUTO_TEST_CASE(string_allocation_bug) )); } +BOOST_AUTO_TEST_CASE(using_for_function_on_int) +{ + char const* sourceCode = R"( + library D { function double(uint self) returns (uint) { return 2*self; } } + contract C { + using D for uint; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + compileAndRun(sourceCode, 0, "D"); + compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(9)) == encodeArgs(u256(2 * 9))); +} + +BOOST_AUTO_TEST_CASE(using_for_function_on_struct) +{ + char const* sourceCode = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s public x; + function f(uint a) returns (uint) { + x.a = 3; + return x.mul(a); + } + } + )"; + compileAndRun(sourceCode, 0, "D"); + compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(3 * 7))); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(3 * 7))); +} + +BOOST_AUTO_TEST_CASE(using_for_overload) +{ + char const* sourceCode = R"( + library D { + struct s { uint a; } + function mul(s storage self, uint x) returns (uint) { return self.a *= x; } + function mul(s storage self, bytes32 x) returns (bytes32) { } + } + contract C { + using D for D.s; + D.s public x; + function f(uint a) returns (uint) { + x.a = 6; + return x.mul(a); + } + } + )"; + compileAndRun(sourceCode, 0, "D"); + compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(6 * 7))); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(6 * 7))); +} + +BOOST_AUTO_TEST_CASE(using_for_by_name) +{ + char const* sourceCode = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s public x; + function f(uint a) returns (uint) { + x.a = 6; + return x.mul({x: a}); + } + } + )"; + compileAndRun(sourceCode, 0, "D"); + compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(6 * 7))); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(6 * 7))); +} + +BOOST_AUTO_TEST_CASE(bound_function_in_var) +{ + char const* sourceCode = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s public x; + function f(uint a) returns (uint) { + x.a = 6; + var g = x.mul; + return g({x: a}); + } + } + )"; + compileAndRun(sourceCode, 0, "D"); + compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(7)) == encodeArgs(u256(6 * 7))); + BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(6 * 7))); +} + +BOOST_AUTO_TEST_CASE(bound_function_to_string) +{ + char const* sourceCode = R"( + library D { function length(string memory self) returns (uint) { return bytes(self).length; } } + contract C { + using D for string; + string x; + function f() returns (uint) { + x = "abc"; + return x.length(); + } + function g() returns (uint) { + string memory s = "abc"; + return s.length(); + } + } + )"; + compileAndRun(sourceCode, 0, "D"); + compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"D", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(3))); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(u256(3))); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 4f26fa4d..73a9b660 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2529,6 +2529,166 @@ BOOST_AUTO_TEST_CASE(member_access_parser_ambiguity) BOOST_CHECK(success(text)); } +BOOST_AUTO_TEST_CASE(using_for_library) +{ + char const* text = R"( + library D { } + contract C { + using D for uint; + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_not_library) +{ + char const* text = R"( + contract D { } + contract C { + using D for uint; + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(using_for_function_exists) +{ + char const* text = R"( + library D { function double(uint self) returns (uint) { return 2*self; } } + contract C { + using D for uint; + function f(uint a) { + a.double; + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_function_on_int) +{ + char const* text = R"( + library D { function double(uint self) returns (uint) { return 2*self; } } + contract C { + using D for uint; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_function_on_struct) +{ + char const* text = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + return x.mul(a); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_overload) +{ + char const* text = R"( + library D { + struct s { uint a; } + function mul(s storage self, uint x) returns (uint) { return self.a *= x; } + function mul(s storage self, bytes32 x) returns (bytes32) { } + } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + return x.mul(a); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_by_name) +{ + char const* text = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + return x.mul({x: a}); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_mismatch) +{ + char const* text = R"( + library D { function double(bytes32 self) returns (uint) { return 2; } } + contract C { + using D for uint; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(using_for_not_used) +{ + // This is an error because the function is only bound to uint. + // Had it been bound to *, it would have worked. + char const* text = R"( + library D { function double(uint self) returns (uint) { return 2; } } + contract C { + using D for uint; + function f(uint16 a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(using_for_arbitrary_mismatch) +{ + // Bound to a, but self type does not match. + char const* text = R"( + library D { function double(bytes32 self) returns (uint) { return 2; } } + contract C { + using D for *; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(bound_function_in_var) +{ + char const* text = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + var g = x.mul; + return g({x: a}); + } + } + )"; + BOOST_CHECK(success(text)); +} + BOOST_AUTO_TEST_CASE(create_memory_arrays) { char const* text = R"( diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 7451397e..fd9076c3 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -1032,6 +1032,21 @@ BOOST_AUTO_TEST_CASE(member_access_parser_ambiguity) BOOST_CHECK(successParse(text)); } +BOOST_AUTO_TEST_CASE(using_for) +{ + char const* text = R"( + contract C { + struct s { uint a; } + using LibraryName for uint; + using Library2 for *; + using Lib for s; + function f() { + } + } + )"; + BOOST_CHECK(successParse(text)); +} + BOOST_AUTO_TEST_SUITE_END() } |