diff options
author | chriseth <chris@ethereum.org> | 2018-11-27 02:47:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-27 02:47:32 +0800 |
commit | 240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e (patch) | |
tree | e11a2e0458eb8bd7ff7e29bb52ae3185b4a4c38b /test/libsolidity/SolidityEndToEndTest.cpp | |
parent | 2f0088f620e88c06387b69d00a2f91f5303cfe1f (diff) | |
parent | 30e6f8d3fb9a16dc1e07367c9010aabe3577e2f1 (diff) | |
download | dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.tar dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.tar.gz dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.tar.bz2 dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.tar.lz dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.tar.xz dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.tar.zst dexon-solidity-240ad0e34ee0e9a28d0ebe8d1c94b9a08b33844e.zip |
Merge pull request #5382 from ethereum/libraryMappingPublic
Allow mapping arguments for public and external library functions.
Diffstat (limited to 'test/libsolidity/SolidityEndToEndTest.cpp')
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index c34a1399..05bb7446 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -8710,6 +8710,90 @@ BOOST_AUTO_TEST_CASE(mapping_returns_in_library_named) ABI_CHECK(callContractFunction("g()"), encodeArgs(u256(0), u256(42), u256(0), u256(0), u256(21), u256(17))); } +BOOST_AUTO_TEST_CASE(using_library_mappings_public) +{ + char const* sourceCode = R"( + library Lib { + function set(mapping(uint => uint) storage m, uint key, uint value) public + { + m[key] = value; + } + } + contract Test { + mapping(uint => uint) m1; + mapping(uint => uint) m2; + function f() public returns (uint, uint, uint, uint, uint, uint) + { + Lib.set(m1, 0, 1); + Lib.set(m1, 2, 42); + Lib.set(m2, 0, 23); + Lib.set(m2, 2, 99); + return (m1[0], m1[1], m1[2], m2[0], m2[1], m2[2]); + } + } + )"; + compileAndRun(sourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(1), u256(0), u256(42), u256(23), u256(0), u256(99))); +} + +BOOST_AUTO_TEST_CASE(using_library_mappings_external) +{ + char const* libSourceCode = R"( + library Lib { + function set(mapping(uint => uint) storage m, uint key, uint value) external + { + m[key] = value * 2; + } + } + )"; + char const* sourceCode = R"( + library Lib { + function set(mapping(uint => uint) storage m, uint key, uint value) external; + } + contract Test { + mapping(uint => uint) m1; + mapping(uint => uint) m2; + function f() public returns (uint, uint, uint, uint, uint, uint) + { + Lib.set(m1, 0, 1); + Lib.set(m1, 2, 42); + Lib.set(m2, 0, 23); + Lib.set(m2, 2, 99); + return (m1[0], m1[1], m1[2], m2[0], m2[1], m2[2]); + } + } + )"; + compileAndRun(libSourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(2), u256(0), u256(84), u256(46), u256(0), u256(198))); +} + +BOOST_AUTO_TEST_CASE(using_library_mappings_return) +{ + char const* sourceCode = R"( + library Lib { + function choose(mapping(uint => mapping(uint => uint)) storage m, uint key) external returns (mapping(uint => uint) storage) { + return m[key]; + } + } + contract Test { + mapping(uint => mapping(uint => uint)) m; + function f() public returns (uint, uint, uint, uint, uint, uint) + { + Lib.choose(m, 0)[0] = 1; + Lib.choose(m, 0)[2] = 42; + Lib.choose(m, 1)[0] = 23; + Lib.choose(m, 1)[2] = 99; + return (m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2]); + } + } + )"; + compileAndRun(sourceCode, 0, "Lib"); + compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}}); + ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(1), u256(0), u256(42), u256(23), u256(0), u256(99))); +} + BOOST_AUTO_TEST_CASE(using_library_structs) { char const* sourceCode = R"( |