aboutsummaryrefslogtreecommitdiffstats
path: root/SolidityTypes.cpp
blob: 6b6306479830e90cb6df3f9c37f774fc9e030456 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
    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_CASE(storage_layout_arrays)
{
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(1), 32).getStorageSize() == 1);
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(1), 33).getStorageSize() == 2);
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(2), 31).getStorageSize() == 2);
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(7), 8).getStorageSize() == 2);
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(7), 9).getStorageSize() == 3);
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(31), 9).getStorageSize() == 9);
    BOOST_CHECK(ArrayType(ArrayType::Location::Storage, make_shared<FixedBytesType>(32), 9).getStorageSize() == 9);
}

BOOST_AUTO_TEST_SUITE_END()

}
}
}