diff options
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/Assembly.cpp | 120 | ||||
-rw-r--r-- | libsolidity/SolidityABIJSON.cpp | 3 | ||||
-rw-r--r-- | libsolidity/SolidityCompiler.cpp | 4 | ||||
-rw-r--r-- | libsolidity/SolidityEndToEndTest.cpp | 136 | ||||
-rw-r--r-- | libsolidity/SolidityExpressionCompiler.cpp | 8 | ||||
-rw-r--r-- | libsolidity/SolidityInterface.cpp | 4 | ||||
-rw-r--r-- | libsolidity/SolidityNameAndTypeResolution.cpp | 109 | ||||
-rw-r--r-- | libsolidity/SolidityNatspecJSON.cpp | 4 | ||||
-rw-r--r-- | libsolidity/SolidityOptimizer.cpp | 4 | ||||
-rw-r--r-- | libsolidity/SolidityParser.cpp | 29 | ||||
-rw-r--r-- | libsolidity/SolidityScanner.cpp | 4 | ||||
-rw-r--r-- | libsolidity/SolidityTypes.cpp | 4 |
12 files changed, 358 insertions, 71 deletions
diff --git a/libsolidity/Assembly.cpp b/libsolidity/Assembly.cpp new file mode 100644 index 00000000..8dcee7fb --- /dev/null +++ b/libsolidity/Assembly.cpp @@ -0,0 +1,120 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @author Lefteris Karapetsas <lefteris@ethdev.com> + * @date 2015 + * Unit tests for Assembly Items from evmcore/Assembly.h + */ + +#include <string> +#include <iostream> +#include <boost/test/unit_test.hpp> +#include <libdevcore/Log.h> +#include <libevmcore/SourceLocation.h> +#include <libsolidity/Scanner.h> +#include <libsolidity/Parser.h> +#include <libsolidity/NameAndTypeResolver.h> +#include <libsolidity/Compiler.h> +#include <libsolidity/AST.h> +#include <libevmcore/Assembly.h> + +using namespace std; +using namespace dev::eth; + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +namespace +{ + +eth::AssemblyItems compileContract(const string& _sourceCode) +{ + Parser parser; + ASTPointer<SourceUnit> sourceUnit; + BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode)))); + NameAndTypeResolver resolver({}); + resolver.registerDeclarations(*sourceUnit); + for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) + if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) + { + BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract)); + } + for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) + if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) + { + BOOST_REQUIRE_NO_THROW(resolver.checkTypeRequirements(*contract)); + } + for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) + if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) + { + Compiler compiler; + compiler.compileContract(*contract, map<ContractDefinition const*, bytes const*>{}); + + return compiler.getRuntimeAssemblyItems(); + } + BOOST_FAIL("No contract found in source."); + return AssemblyItems(); +} + +void checkAssemblyLocations(AssemblyItems const& _items, vector<SourceLocation> const& _locations) +{ + BOOST_CHECK_EQUAL(_items.size(), _locations.size()); + for (size_t i = 0; i < min(_items.size(), _locations.size()); ++i) + { + BOOST_CHECK_MESSAGE( + _items[i].getLocation() == _locations[i], + "Location mismatch for assembly item " + to_string(i) + ". Found: " + + to_string(_items[i].getLocation().start) + "-" + + to_string(_items[i].getLocation().end) + ", expected: " + + to_string(_locations[i].start) + "-" + + to_string(_locations[i].end)); + } +} + +} // end anonymous namespace + +BOOST_AUTO_TEST_SUITE(Assembly) + +BOOST_AUTO_TEST_CASE(location_test) +{ + char const* sourceCode = R"( + contract test { + function f() returns (uint256 a) { + return 16; + } + } + )"; + shared_ptr<string const> n = make_shared<string>("source"); + AssemblyItems items = compileContract(sourceCode); + vector<SourceLocation> locations = + vector<SourceLocation>(11, SourceLocation(2, 75, n)) + + vector<SourceLocation>(12, SourceLocation(20, 72, n)) + + vector<SourceLocation>{SourceLocation(42, 51, n), SourceLocation(65, 67, n)} + + vector<SourceLocation>(4, SourceLocation(58, 67, n)) + + vector<SourceLocation>(3, SourceLocation(20, 72, n)); + checkAssemblyLocations(items, locations); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} // end namespaces diff --git a/libsolidity/SolidityABIJSON.cpp b/libsolidity/SolidityABIJSON.cpp index b0633cca..f0e54a94 100644 --- a/libsolidity/SolidityABIJSON.cpp +++ b/libsolidity/SolidityABIJSON.cpp @@ -19,7 +19,6 @@ * @date 2014 * Unit tests for the solidity compiler JSON Interface output. */ -#if ETH_SOLIDITY #include "../TestHelper.h" #include <libsolidity/CompilerStack.h> @@ -501,5 +500,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } - -#endif diff --git a/libsolidity/SolidityCompiler.cpp b/libsolidity/SolidityCompiler.cpp index bb16c88c..7b0ceedb 100644 --- a/libsolidity/SolidityCompiler.cpp +++ b/libsolidity/SolidityCompiler.cpp @@ -20,8 +20,6 @@ * Unit tests for the solidity compiler. */ -#if ETH_SOLIDITY - #include <string> #include <iostream> #include <boost/test/unit_test.hpp> @@ -193,5 +191,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityEndToEndTest.cpp b/libsolidity/SolidityEndToEndTest.cpp index c345f520..8b926d6c 100644 --- a/libsolidity/SolidityEndToEndTest.cpp +++ b/libsolidity/SolidityEndToEndTest.cpp @@ -21,8 +21,6 @@ * Unit tests for the solidity expression compiler, testing the behaviour of the code. */ -#if ETH_SOLIDITY - #include <string> #include <tuple> #include <boost/test/unit_test.hpp> @@ -304,7 +302,6 @@ BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr) BOOST_AUTO_TEST_CASE(calling_other_functions) { - // note that the index of a function is its index in the sorted sequence of functions char const* sourceCode = "contract collatz {\n" " function run(uint x) returns(uint y) {\n" " while ((y = x) > 1) {\n" @@ -1149,26 +1146,6 @@ BOOST_AUTO_TEST_CASE(now) BOOST_CHECK(callContractFunction("someInfo()") == encodeArgs(true)); } -BOOST_AUTO_TEST_CASE(function_types) -{ - char const* sourceCode = "contract test {\n" - " function a(bool selector) returns (uint b) {\n" - " var f = fun1;\n" - " if (selector) f = fun2;\n" - " return f(9);\n" - " }\n" - " function fun1(uint x) returns (uint b) {\n" - " return 11;\n" - " }\n" - " function fun2(uint x) returns (uint b) {\n" - " return 12;\n" - " }\n" - "}\n"; - compileAndRun(sourceCode); - BOOST_CHECK(callContractFunction("a(bool)", false) == encodeArgs(11)); - BOOST_CHECK(callContractFunction("a(bool)", true) == encodeArgs(12)); -} - BOOST_AUTO_TEST_CASE(type_conversions_cleanup) { // 22-byte integer converted to a contract (i.e. address, 20 bytes), converted to a 32 byte @@ -1499,7 +1476,7 @@ BOOST_AUTO_TEST_CASE(ripemd) { h256 ret; dev::ripemd160(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32)); - return u256(ret) >> (256 - 160); + return u256(ret); }; testSolidityAgainstCpp("a(bytes32)", f, u256(4)); testSolidityAgainstCpp("a(bytes32)", f, u256(5)); @@ -1814,7 +1791,7 @@ BOOST_AUTO_TEST_CASE(gas_for_builtin) )"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("test(uint256)", 500) == bytes()); - BOOST_CHECK(callContractFunction("test(uint256)", 800) == encodeArgs(u256("0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), true)); + BOOST_CHECK(callContractFunction("test(uint256)", 800) == encodeArgs(u256("0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc000000000000000000000000"), true)); } BOOST_AUTO_TEST_CASE(value_complex) @@ -3676,6 +3653,94 @@ BOOST_AUTO_TEST_CASE(packed_storage_structs_with_bytes0) BOOST_CHECK(callContractFunction("test()") == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(overloaded_function_call_resolve_to_first) +{ + char const* sourceCode = R"( + contract test { + function f(uint k) returns(uint d) { return k; } + function f(uint a, uint b) returns(uint d) { return a + b; } + function g() returns(uint d) { return f(3); } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(3)); +} + +BOOST_AUTO_TEST_CASE(overloaded_function_call_resolve_to_second) +{ + char const* sourceCode = R"( + contract test { + function f(uint a, uint b) returns(uint d) { return a + b; } + function f(uint k) returns(uint d) { return k; } + function g() returns(uint d) { return f(3, 7); } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(10)); +} + +BOOST_AUTO_TEST_CASE(overloaded_function_call_with_if_else) +{ + char const* sourceCode = R"( + contract test { + function f(uint a, uint b) returns(uint d) { return a + b; } + function f(uint k) returns(uint d) { return k; } + function g(bool flag) returns(uint d) { + if (flag) + return f(3); + else + return f(3, 7); + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("g(bool)", true) == encodeArgs(3)); + BOOST_CHECK(callContractFunction("g(bool)", false) == encodeArgs(10)); +} + +BOOST_AUTO_TEST_CASE(derived_overload_base_function_direct) +{ + char const* sourceCode = R"( + contract B { function f() returns(uint) { return 10; } } + contract C is B { + function f(uint i) returns(uint) { return 2 * i; } + function g() returns(uint) { return f(1); } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(2)); +} + +BOOST_AUTO_TEST_CASE(derived_overload_base_function_indirect) +{ + char const* sourceCode = R"( + contract A { function f(uint a) returns(uint) { return 2 * a; } } + contract B { function f() returns(uint) { return 10; } } + contract C is A, B { + function g() returns(uint) { return f(); } + function h() returns(uint) { return f(1); } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(10)); + BOOST_CHECK(callContractFunction("h()") == encodeArgs(2)); +} + +BOOST_AUTO_TEST_CASE(super_overload) +{ + char const* sourceCode = R"( + contract A { function f(uint a) returns(uint) { return 2 * a; } } + contract B { function f(bool b) returns(uint) { return 10; } } + contract C is A, B { + function g() returns(uint) { return super.f(true); } + function h() returns(uint) { return super.f(1); } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("g()") == encodeArgs(10)); + BOOST_CHECK(callContractFunction("h()") == encodeArgs(2)); +} + BOOST_AUTO_TEST_CASE(packed_storage_signed) { char const* sourceCode = R"( @@ -3699,10 +3764,27 @@ BOOST_AUTO_TEST_CASE(packed_storage_signed) BOOST_CHECK( callContractFunction("test()") == encodeArgs(u256(-2), u256(4), u256(-112), u256(0))); } +BOOST_AUTO_TEST_CASE(external_types_in_calls) +{ + char const* sourceCode = R"( + contract C1 { C1 public bla; function C1(C1 x) { bla = x; } } + contract C { + function test() returns (C1 x, C1 y) { + C1 c = new C1(C1(9)); + x = c.bla(); + y = this.t1(C1(7)); + } + function t1(C1 a) returns (C1) { return a; } + function() returns (C1) { return C1(9); } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(9), u256(7))); + BOOST_CHECK(callContractFunction("nonexisting") == encodeArgs(u256(9))); +} + BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityExpressionCompiler.cpp b/libsolidity/SolidityExpressionCompiler.cpp index 505cac99..b2436cfa 100644 --- a/libsolidity/SolidityExpressionCompiler.cpp +++ b/libsolidity/SolidityExpressionCompiler.cpp @@ -20,8 +20,6 @@ * Unit tests for the solidity expression compiler. */ -#if ETH_SOLIDITY - #include <string> #include <libdevcore/Log.h> @@ -80,7 +78,9 @@ Declaration const& resolveDeclaration( // bracers are required, cause msvc couldnt handle this macro in for statement for (string const& namePart: _namespacedName) { - BOOST_REQUIRE(declaration = _resolver.resolveName(namePart, declaration)); + auto declarations = _resolver.resolveName(namePart, declaration); + BOOST_REQUIRE(!declarations.empty()); + BOOST_REQUIRE(declaration = *declarations.begin()); } BOOST_REQUIRE(declaration); return *declaration; @@ -491,5 +491,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityInterface.cpp b/libsolidity/SolidityInterface.cpp index ab6cb902..c8f74e3a 100644 --- a/libsolidity/SolidityInterface.cpp +++ b/libsolidity/SolidityInterface.cpp @@ -20,8 +20,6 @@ * Unit tests for generating source interfaces for Solidity contracts. */ -#if ETH_SOLIDITY - #include "../TestHelper.h" #include <libsolidity/CompilerStack.h> #include <libsolidity/AST.h> @@ -149,5 +147,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } - -#endif diff --git a/libsolidity/SolidityNameAndTypeResolution.cpp b/libsolidity/SolidityNameAndTypeResolution.cpp index 917ea000..c317dad9 100644 --- a/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/libsolidity/SolidityNameAndTypeResolution.cpp @@ -20,8 +20,6 @@ * Unit tests for the name and type resolution of the solidity parser. */ -#if ETH_SOLIDITY - #include <string> #include <libdevcore/Log.h> @@ -625,23 +623,23 @@ BOOST_AUTO_TEST_CASE(cyclic_inheritance) BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); } -BOOST_AUTO_TEST_CASE(illegal_override_direct) +BOOST_AUTO_TEST_CASE(legal_override_direct) { char const* text = R"( contract B { function f() {} } contract C is B { function f(uint i) {} } )"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); + BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); } -BOOST_AUTO_TEST_CASE(illegal_override_indirect) +BOOST_AUTO_TEST_CASE(legal_override_indirect) { char const* text = R"( contract A { function f(uint a) {} } contract B { function f() {} } contract C is A, B { } )"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); + BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); } BOOST_AUTO_TEST_CASE(illegal_override_visibility) @@ -1656,6 +1654,103 @@ BOOST_AUTO_TEST_CASE(bytes0_array) BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); } +BOOST_AUTO_TEST_CASE(overloaded_function_cannot_resolve) +{ + char const* sourceCode = R"( + contract test { + function f() returns(uint) { return 1; } + function f(uint a) returns(uint) { return a; } + function g() returns(uint) { return f(3, 5); } + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); +} + +BOOST_AUTO_TEST_CASE(ambiguous_overloaded_function) +{ + // literal 1 can be both converted to uint and uint8, so the call is ambiguous. + char const* sourceCode = R"( + contract test { + function f(uint8 a) returns(uint) { return a; } + function f(uint a) returns(uint) { return 2*a; } + function g() returns(uint) { return f(1); } + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); +} + +BOOST_AUTO_TEST_CASE(assignment_of_nonoverloaded_function) +{ + char const* sourceCode = R"( + contract test { + function f(uint a) returns(uint) { return 2 * a; } + function g() returns(uint) { var x = f; return x(7); } + } + )"; + ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(sourceCode), "Type resolving failed"); +} + +BOOST_AUTO_TEST_CASE(assignment_of_overloaded_function) +{ + char const* sourceCode = R"( + contract test { + function f() returns(uint) { return 1; } + function f(uint a) returns(uint) { return 2 * a; } + function g() returns(uint) { var x = f; return x(7); } + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); +} + +BOOST_AUTO_TEST_CASE(external_types_clash) +{ + char const* sourceCode = R"( + contract base { + enum a { X } + function f(a) { } + } + contract test is base { + function f(uint8 a) { } + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); +} + +BOOST_AUTO_TEST_CASE(override_changes_return_types) +{ + char const* sourceCode = R"( + contract base { + function f(uint a) returns (uint) { } + } + contract test is base { + function f(uint a) returns (uint8) { } + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); +} + +BOOST_AUTO_TEST_CASE(multiple_constructors) +{ + char const* sourceCode = R"( + contract test { + function test(uint a) { } + function test() {} + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), DeclarationError); +} + +BOOST_AUTO_TEST_CASE(equal_overload) +{ + char const* sourceCode = R"( + contract test { + function test(uint a) returns (uint b) { } + function test(uint a) external {} + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), DeclarationError); +} + BOOST_AUTO_TEST_CASE(uninitialized_var) { char const* sourceCode = R"( @@ -1671,5 +1766,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityNatspecJSON.cpp b/libsolidity/SolidityNatspecJSON.cpp index 99adcf19..d2c1ec18 100644 --- a/libsolidity/SolidityNatspecJSON.cpp +++ b/libsolidity/SolidityNatspecJSON.cpp @@ -20,8 +20,6 @@ * Unit tests for the solidity compiler JSON Interface output. */ -#if ETH_SOLIDITY - #include "../TestHelper.h" #include <json/json.h> #include <libsolidity/CompilerStack.h> @@ -539,5 +537,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } - -#endif diff --git a/libsolidity/SolidityOptimizer.cpp b/libsolidity/SolidityOptimizer.cpp index 8ab1de8f..af9b5146 100644 --- a/libsolidity/SolidityOptimizer.cpp +++ b/libsolidity/SolidityOptimizer.cpp @@ -20,8 +20,6 @@ * Tests for the Solidity optimizer. */ -#if ETH_SOLIDITY - #include <string> #include <tuple> #include <boost/test/unit_test.hpp> @@ -816,5 +814,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityParser.cpp b/libsolidity/SolidityParser.cpp index 7baa1292..cad0e1f2 100644 --- a/libsolidity/SolidityParser.cpp +++ b/libsolidity/SolidityParser.cpp @@ -20,8 +20,6 @@ * Unit tests for the solidity parser. */ -#if ETH_SOLIDITY - #include <string> #include <memory> #include <libdevcore/Log.h> @@ -136,6 +134,31 @@ BOOST_AUTO_TEST_CASE(missing_argument_in_named_args) BOOST_CHECK_THROW(parseText(text), ParserError); } +BOOST_AUTO_TEST_CASE(two_exact_functions) +{ + char const* text = R"( + contract test { + function fun(uint a) returns(uint r) { return a; } + function fun(uint a) returns(uint r) { return a; } + } + )"; + // with support of overloaded functions, during parsing, + // we can't determine whether they match exactly, however + // it will throw DeclarationError in following stage. + BOOST_CHECK_NO_THROW(parseText(text)); +} + +BOOST_AUTO_TEST_CASE(overloaded_functions) +{ + char const* text = R"( + contract test { + function fun(uint a) returns(uint r) { return a; } + function fun(uint a, uint b) returns(uint r) { return a + b; } + } + )"; + BOOST_CHECK_NO_THROW(parseText(text)); +} + BOOST_AUTO_TEST_CASE(function_natspec_documentation) { ASTPointer<ContractDefinition> contract; @@ -855,5 +878,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityScanner.cpp b/libsolidity/SolidityScanner.cpp index 20b946ee..8d3e5392 100644 --- a/libsolidity/SolidityScanner.cpp +++ b/libsolidity/SolidityScanner.cpp @@ -20,8 +20,6 @@ * Unit tests for the solidity scanner. */ -#if ETH_SOLIDITY - #include <libsolidity/Scanner.h> #include <boost/test/unit_test.hpp> @@ -288,5 +286,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } // end namespaces - -#endif diff --git a/libsolidity/SolidityTypes.cpp b/libsolidity/SolidityTypes.cpp index da8b4830..6b630647 100644 --- a/libsolidity/SolidityTypes.cpp +++ b/libsolidity/SolidityTypes.cpp @@ -20,8 +20,6 @@ * Unit tests for the type system of Solidity. */ -#if ETH_SOLIDITY - #include <libsolidity/Types.h> #include <boost/test/unit_test.hpp> @@ -93,5 +91,3 @@ BOOST_AUTO_TEST_SUITE_END() } } } - -#endif |