diff options
author | chriseth <c@ethdev.com> | 2015-03-13 17:52:34 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-03-17 01:07:14 +0800 |
commit | 354d84e0a72b66e627be5a50dc1d8608ac32bb7c (patch) | |
tree | 7c996a64ec9185a007c9ceb023e3111d52544154 | |
parent | 10be65f269390257984eef1d04227b88578a85bc (diff) | |
download | dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.tar dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.tar.gz dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.tar.bz2 dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.tar.lz dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.tar.xz dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.tar.zst dexon-solidity-354d84e0a72b66e627be5a50dc1d8608ac32bb7c.zip |
Compute packing offsets.
-rw-r--r-- | SolidityTypes.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/SolidityTypes.cpp b/SolidityTypes.cpp new file mode 100644 index 00000000..8defd1d8 --- /dev/null +++ b/SolidityTypes.cpp @@ -0,0 +1,82 @@ +/* + 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 Christian <c@ethdev.com> + * @date 2015 + * Unit tests for the type system of Solidity. + */ + +#include <libsolidity/Types.h> +#include <boost/test/unit_test.hpp> + +using namespace std; + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +BOOST_AUTO_TEST_SUITE(SolidityTypes) + +BOOST_AUTO_TEST_CASE(storage_layout_simple) +{ + MemberList members(MemberList::MemberMap({ + {string("first"), Type::fromElementaryTypeName("uint128")}, + {string("second"), Type::fromElementaryTypeName("uint120")}, + {string("wraps"), Type::fromElementaryTypeName("uint16")} + })); + BOOST_REQUIRE_EQUAL(u256(2), members.getStorageSize()); + BOOST_REQUIRE(members.getMemberStorageOffset("first") != nullptr); + BOOST_REQUIRE(members.getMemberStorageOffset("second") != nullptr); + BOOST_REQUIRE(members.getMemberStorageOffset("wraps") != nullptr); + BOOST_CHECK(*members.getMemberStorageOffset("first") == make_pair(u256(0), unsigned(0))); + BOOST_CHECK(*members.getMemberStorageOffset("second") == make_pair(u256(0), unsigned(16))); + BOOST_CHECK(*members.getMemberStorageOffset("wraps") == make_pair(u256(1), unsigned(0))); +} + +BOOST_AUTO_TEST_CASE(storage_layout_mapping) +{ + MemberList members(MemberList::MemberMap({ + {string("first"), Type::fromElementaryTypeName("uint128")}, + {string("second"), make_shared<MappingType>( + Type::fromElementaryTypeName("uint8"), + Type::fromElementaryTypeName("uint8") + )}, + {string("third"), Type::fromElementaryTypeName("uint16")}, + {string("final"), make_shared<MappingType>( + Type::fromElementaryTypeName("uint8"), + Type::fromElementaryTypeName("uint8") + )}, + })); + BOOST_REQUIRE_EQUAL(u256(4), members.getStorageSize()); + BOOST_REQUIRE(members.getMemberStorageOffset("first") != nullptr); + BOOST_REQUIRE(members.getMemberStorageOffset("second") != nullptr); + BOOST_REQUIRE(members.getMemberStorageOffset("third") != nullptr); + BOOST_REQUIRE(members.getMemberStorageOffset("final") != nullptr); + BOOST_CHECK(*members.getMemberStorageOffset("first") == make_pair(u256(0), unsigned(0))); + BOOST_CHECK(*members.getMemberStorageOffset("second") == make_pair(u256(1), unsigned(0))); + BOOST_CHECK(*members.getMemberStorageOffset("third") == make_pair(u256(2), unsigned(0))); + BOOST_CHECK(*members.getMemberStorageOffset("final") == make_pair(u256(3), unsigned(0))); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} |