diff options
author | chriseth <c@ethdev.com> | 2015-10-07 00:29:05 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-10-07 00:29:05 +0800 |
commit | d35a4b849da427629334665cc1bb931a259adac9 (patch) | |
tree | 79b38a402947d3fe093e0573dee2c44d969a6c22 /test | |
parent | c6e637677b724558f192eea19d10583da225b083 (diff) | |
parent | 55af63e4638d6552ff5fdb4f78fc582536daa43a (diff) | |
download | dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.tar dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.tar.gz dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.tar.bz2 dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.tar.lz dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.tar.xz dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.tar.zst dexon-solidity-d35a4b849da427629334665cc1bb931a259adac9.zip |
Merge pull request #117 from chriseth/internalTypesForLibrary
Internal types for library
Diffstat (limited to 'test')
-rw-r--r-- | test/libsolidity/SolidityABIJSON.cpp | 30 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 67 | ||||
-rw-r--r-- | test/libsolidity/SolidityInterface.cpp | 16 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 10 |
4 files changed, 118 insertions, 5 deletions
diff --git a/test/libsolidity/SolidityABIJSON.cpp b/test/libsolidity/SolidityABIJSON.cpp index 69504e3d..f3004b5f 100644 --- a/test/libsolidity/SolidityABIJSON.cpp +++ b/test/libsolidity/SolidityABIJSON.cpp @@ -595,6 +595,36 @@ BOOST_AUTO_TEST_CASE(strings_and_arrays) checkInterface(sourceCode, interface); } +BOOST_AUTO_TEST_CASE(library_function) +{ + char const* sourceCode = R"( + library test { + struct StructType { uint a; } + function f(StructType storage b, uint[] storage c, test d) returns (uint[] e, StructType storage f){} + } + )"; + + char const* interface = R"( + [ + { + "constant" : false, + "name": "f", + "inputs": [ + { "name": "b", "type": "test.StructType storage" }, + { "name": "c", "type": "uint256[] storage" }, + { "name": "d", "type": "test" } + ], + "outputs": [ + { "name": "e", "type": "uint256[]" }, + { "name": "f", "type": "test.StructType storage" } + ], + "type" : "function" + } + ] + )"; + checkInterface(sourceCode, interface); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index c24701d5..a17a3be5 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5382,6 +5382,62 @@ BOOST_AUTO_TEST_CASE(fixed_arrays_as_return_type) ); } +BOOST_AUTO_TEST_CASE(internal_types_in_library) +{ + char const* sourceCode = R"( + library Lib { + function find(uint16[] storage _haystack, uint16 _needle) constant returns (uint) + { + for (uint i = 0; i < _haystack.length; ++i) + if (_haystack[i] == _needle) + return i; + return uint(-1); + } + } + contract Test { + mapping(string => uint16[]) data; + function f() returns (uint a, uint b) + { + data["abc"].length = 20; + data["abc"][4] = 9; + data["abc"][17] = 3; + a = Lib.find(data["abc"], 9); + b = Lib.find(data["abc"], 3); + } + } + )"; + compileAndRun(sourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(4), u256(17))); +} + +BOOST_AUTO_TEST_CASE(using_library_structs) +{ + char const* sourceCode = R"( + library Lib { + struct Data { uint a; uint[] b; } + function set(Data storage _s) + { + _s.a = 7; + _s.b.length = 20; + _s.b[19] = 8; + } + } + contract Test { + mapping(string => Lib.Data) data; + function f() returns (uint a, uint b) + { + Lib.set(data["abc"]); + a = data["abc"].a; + b = data["abc"].b[19]; + } + } + )"; + compileAndRun(sourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(7), u256(8))); +} + BOOST_AUTO_TEST_CASE(short_strings) { // This test verifies that the byte array encoding that combines length and data works @@ -5511,6 +5567,17 @@ BOOST_AUTO_TEST_CASE(calldata_offset) BOOST_CHECK(callContractFunction("last()", encodeArgs()) == encodeDyn(string("nd"))); } +BOOST_AUTO_TEST_CASE(version_stamp_for_libraries) +{ + char const* sourceCode = "library lib {}"; + m_optimize = true; + 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[7], int(eth::Instruction::POP)); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityInterface.cpp b/test/libsolidity/SolidityInterface.cpp index d77bccbd..f0d2be20 100644 --- a/test/libsolidity/SolidityInterface.cpp +++ b/test/libsolidity/SolidityInterface.cpp @@ -142,6 +142,22 @@ BOOST_AUTO_TEST_CASE(inheritance) sourcePart(*contract.definedFunctions().at(1))})); } +BOOST_AUTO_TEST_CASE(libraries) +{ + char const* sourceCode = R"( + library Lib { + struct Str { uint a; } + enum E { E1, E2 } + function f(uint[] x,Str storage y,E z) external; + } + )"; + ContractDefinition const& contract = checkInterface(sourceCode); + BOOST_CHECK(contract.isLibrary()); + set<string> expectedFunctions({"function f(uint256[] x,Lib.Str storage y,Lib.E z);"}); + BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size()); + BOOST_CHECK(expectedFunctions == set<string>({sourcePart(*contract.definedFunctions().at(0))})); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index c386e2b4..b4f16913 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -933,24 +933,24 @@ BOOST_AUTO_TEST_CASE(state_variable_accessors) BOOST_REQUIRE((contract = retrieveContract(source, 0)) != nullptr); FunctionTypePointer function = retrieveFunctionBySignature(contract, "foo()"); BOOST_REQUIRE(function && function->hasDeclaration()); - auto returnParams = function->returnParameterTypeNames(); + auto returnParams = function->returnParameterTypeNames(false); BOOST_CHECK_EQUAL(returnParams.at(0), "uint256"); BOOST_CHECK(function->isConstant()); function = retrieveFunctionBySignature(contract, "map(uint256)"); BOOST_REQUIRE(function && function->hasDeclaration()); - auto params = function->parameterTypeNames(); + auto params = function->parameterTypeNames(false); BOOST_CHECK_EQUAL(params.at(0), "uint256"); - returnParams = function->returnParameterTypeNames(); + returnParams = function->returnParameterTypeNames(false); BOOST_CHECK_EQUAL(returnParams.at(0), "bytes4"); BOOST_CHECK(function->isConstant()); function = retrieveFunctionBySignature(contract, "multiple_map(uint256,uint256)"); BOOST_REQUIRE(function && function->hasDeclaration()); - params = function->parameterTypeNames(); + params = function->parameterTypeNames(false); BOOST_CHECK_EQUAL(params.at(0), "uint256"); BOOST_CHECK_EQUAL(params.at(1), "uint256"); - returnParams = function->returnParameterTypeNames(); + returnParams = function->returnParameterTypeNames(false); BOOST_CHECK_EQUAL(returnParams.at(0), "bytes4"); BOOST_CHECK(function->isConstant()); } |