diff options
author | chriseth <c@ethdev.com> | 2015-09-12 01:35:31 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-09-12 01:35:31 +0800 |
commit | 4360e0459622e7843ca9f8d7fb5113da0b8a044e (patch) | |
tree | e8f5fe5f139517c000c57fa271617621420ccb5d /test | |
parent | 3c25420b8476f1516ac8cb10f5c1552609a08675 (diff) | |
parent | d89832fa898d18f1962eff14acf3f2f7b1c4e3bc (diff) | |
download | dexon-solidity-4360e0459622e7843ca9f8d7fb5113da0b8a044e.tar.gz dexon-solidity-4360e0459622e7843ca9f8d7fb5113da0b8a044e.tar.zst dexon-solidity-4360e0459622e7843ca9f8d7fb5113da0b8a044e.zip |
Merge pull request #66 from chriseth/sol_libraries
Calling libraries.
Diffstat (limited to 'test')
-rw-r--r-- | test/contracts/AuctionRegistrar.cpp | 2 | ||||
-rw-r--r-- | test/contracts/FixedFeeRegistrar.cpp | 2 | ||||
-rw-r--r-- | test/contracts/Wallet.cpp | 2 | ||||
-rw-r--r-- | test/libsolidity/Assembly.cpp | 2 | ||||
-rw-r--r-- | test/libsolidity/GasMeter.cpp | 2 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 32 | ||||
-rw-r--r-- | test/libsolidity/SolidityExpressionCompiler.cpp | 2 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 49 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 10 | ||||
-rw-r--r-- | test/libsolidity/solidityExecutionFramework.h | 14 |
10 files changed, 106 insertions, 11 deletions
diff --git a/test/contracts/AuctionRegistrar.cpp b/test/contracts/AuctionRegistrar.cpp index 0935c874..a8b57f2f 100644 --- a/test/contracts/AuctionRegistrar.cpp +++ b/test/contracts/AuctionRegistrar.cpp @@ -233,7 +233,7 @@ protected: m_compiler.reset(false, m_addStandardSources); m_compiler.addSource("", registrarCode); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); - s_compiledRegistrar.reset(new bytes(m_compiler.bytecode("GlobalRegistrar"))); + s_compiledRegistrar.reset(new bytes(m_compiler.object("GlobalRegistrar").bytecode)); } sendMessage(*s_compiledRegistrar, true); BOOST_REQUIRE(!m_output.empty()); diff --git a/test/contracts/FixedFeeRegistrar.cpp b/test/contracts/FixedFeeRegistrar.cpp index 266eb923..f42f4492 100644 --- a/test/contracts/FixedFeeRegistrar.cpp +++ b/test/contracts/FixedFeeRegistrar.cpp @@ -125,7 +125,7 @@ protected: m_compiler.reset(false, m_addStandardSources); m_compiler.addSource("", registrarCode); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); - s_compiledRegistrar.reset(new bytes(m_compiler.bytecode("FixedFeeRegistrar"))); + s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode)); } sendMessage(*s_compiledRegistrar, true); BOOST_REQUIRE(!m_output.empty()); diff --git a/test/contracts/Wallet.cpp b/test/contracts/Wallet.cpp index 32a53199..4a4848f1 100644 --- a/test/contracts/Wallet.cpp +++ b/test/contracts/Wallet.cpp @@ -440,7 +440,7 @@ protected: m_compiler.reset(false, m_addStandardSources); m_compiler.addSource("", walletCode); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); - s_compiledWallet.reset(new bytes(m_compiler.bytecode("Wallet"))); + s_compiledWallet.reset(new bytes(m_compiler.object("Wallet").bytecode)); } bytes args = encodeArgs(u256(0x60), _required, _dailyLimit, u256(_owners.size()), _owners); sendMessage(*s_compiledWallet + args, true, _value); diff --git a/test/libsolidity/Assembly.cpp b/test/libsolidity/Assembly.cpp index eb4999e7..5bd6fed3 100644 --- a/test/libsolidity/Assembly.cpp +++ b/test/libsolidity/Assembly.cpp @@ -66,7 +66,7 @@ eth::AssemblyItems compileContract(const string& _sourceCode) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) { Compiler compiler; - compiler.compileContract(*contract, map<ContractDefinition const*, bytes const*>{}); + compiler.compileContract(*contract, map<ContractDefinition const*, Assembly const*>{}); return compiler.runtimeAssemblyItems(); } diff --git a/test/libsolidity/GasMeter.cpp b/test/libsolidity/GasMeter.cpp index 897d12fd..6d7c06a3 100644 --- a/test/libsolidity/GasMeter.cpp +++ b/test/libsolidity/GasMeter.cpp @@ -63,7 +63,7 @@ public: auto state = make_shared<KnownState>(); PathGasMeter meter(*m_compiler.assemblyItems()); GasMeter::GasConsumption gas = meter.estimateMax(0, state); - u256 bytecodeSize(m_compiler.runtimeBytecode().size()); + u256 bytecodeSize(m_compiler.runtimeObject().bytecode.size()); gas += bytecodeSize * c_createDataGas; BOOST_REQUIRE(!gas.isInfinite); BOOST_CHECK(gas.value == m_gasUsed); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index aa423330..c56845aa 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5230,6 +5230,38 @@ BOOST_AUTO_TEST_CASE(storage_string_as_mapping_key_without_variable) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); } +BOOST_AUTO_TEST_CASE(library_call) +{ + char const* sourceCode = R"( + library Lib { function m(uint x, uint y) returns (uint) { return x * y; } } + contract Test { + function f(uint x) returns (uint) { + return Lib.m(x, 9); + } + } + )"; + compileAndRun(sourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(33)) == encodeArgs(u256(33) * 9)); +} + +BOOST_AUTO_TEST_CASE(library_stray_values) +{ + char const* sourceCode = R"( + library Lib { function m(uint x, uint y) returns (uint) { return x * y; } } + contract Test { + function f(uint x) returns (uint) { + Lib; + Lib.m; + return x + 9; + } + } + )"; + compileAndRun(sourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f(uint256)", u256(33)) == encodeArgs(u256(42))); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index 9b1c026f..747668b0 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -138,7 +138,7 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _ for (vector<string> const& function: _functions) context << context.functionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver))); - bytes instructions = context.assembledBytecode(); + bytes instructions = context.assembledObject().bytecode; // debug // cout << eth::disassemble(instructions) << endl; return instructions; diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index ff91d8e5..5d174367 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2193,6 +2193,55 @@ BOOST_AUTO_TEST_CASE(string_bytes_conversion) BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); } +BOOST_AUTO_TEST_CASE(inheriting_from_library) +{ + char const* text = R"( + library Lib {} + contract Test is Lib {} + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(inheriting_library) +{ + char const* text = R"( + contract Test {} + library Lib is Test {} + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(library_having_variables) +{ + char const* text = R"( + library Lib { uint x; } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(valid_library) +{ + char const* text = R"( + library Lib { uint constant x = 9; } + )"; + BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); +} + +BOOST_AUTO_TEST_CASE(call_to_library_function) +{ + char const* text = R"( + library Lib { + uint constant public pimil = 3141592; + function min(uint x, uint y) returns (uint); + } + contract Test { + function f() { + uint t = Lib.min(Lib.pimil(), 7); + } + } + )"; + BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); +} BOOST_AUTO_TEST_CASE(creating_contract_within_the_contract) { diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 14b9e9e4..1e034863 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -924,6 +924,16 @@ BOOST_AUTO_TEST_CASE(empty_comment) BOOST_CHECK_NO_THROW(parseText(text)); } +BOOST_AUTO_TEST_CASE(library_simple) +{ + char const* text = R"( + library Lib { + function f() { } + } + )"; + BOOST_CHECK_NO_THROW(parseText(text)); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index 86e7d351..3370044c 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -53,14 +53,17 @@ public: std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "", - bytes const& _arguments = bytes() + bytes const& _arguments = bytes(), + std::map<std::string, Address> const& _libraryAddresses = std::map<std::string, Address>() ) { m_compiler.reset(false, m_addStandardSources); m_compiler.addSource("", _sourceCode); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); - bytes code = m_compiler.bytecode(_contractName); - sendMessage(code + _arguments, true, _value); + eth::LinkerObject obj = m_compiler.object(_contractName); + obj.link(_libraryAddresses); + BOOST_REQUIRE(obj.linkReferences.empty()); + sendMessage(obj.bytecode + _arguments, true, _value); return m_output; } @@ -76,10 +79,11 @@ public: std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "", - bytes const& _arguments = bytes() + bytes const& _arguments = bytes(), + std::map<std::string, Address> const& _libraryAddresses = std::map<std::string, Address>() ) { - compileAndRunWithoutCheck(_sourceCode, _value, _contractName, _arguments); + compileAndRunWithoutCheck(_sourceCode, _value, _contractName, _arguments, _libraryAddresses); BOOST_REQUIRE(!m_output.empty()); return m_output; } |