aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/GasCosts.cpp
blob: 15658a916b1087c083821d7314e9d25b999ad9fd (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
/*
    This file is part of solidity.

    solidity 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.

    solidity 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 solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Tests that check that the cost of certain operations stay within range.
 */

#include <test/libsolidity/SolidityExecutionFramework.h>

#include <cmath>

using namespace std;
using namespace langutil;
using namespace dev::eth;
using namespace dev::solidity;
using namespace dev::test;

namespace dev
{
namespace solidity
{
namespace test
{

#define CHECK_GAS(_gasNoOpt, _gasOpt, _tolerance) \
    do \
    { \
        u256 gasOpt{_gasOpt}; \
        u256 gasNoOpt{_gasNoOpt}; \
        u256 tolerance{_tolerance}; \
        u256 gas = m_optimize ? gasOpt : gasNoOpt; \
        u256 diff = gas < m_gasUsed ? m_gasUsed - gas : gas - m_gasUsed; \
        BOOST_CHECK_MESSAGE( \
            diff <= tolerance, \
            "Gas used: " + \
            m_gasUsed.str() + \
            " - expected: " + \
            gas.str() + \
            " (tolerance: " + \
            tolerance.str() + \
            ")" \
        ); \
    } while(0)

BOOST_FIXTURE_TEST_SUITE(GasCostTests, SolidityExecutionFramework)

BOOST_AUTO_TEST_CASE(string_storage)
{
    char const* sourceCode = R"(
        contract C {
            function f() pure public {
                require(false, "Not Authorized. This function can only be called by the custodian or owner of this contract");
            }
        }
    )";
    compileAndRun(sourceCode);

    if (Options::get().evmVersion() <= EVMVersion::byzantium())
        CHECK_GAS(134435, 130591, 100);
    else
        CHECK_GAS(127225, 124873, 100);
    if (Options::get().evmVersion() >= EVMVersion::byzantium())
    {
        callContractFunction("f()");
        if (Options::get().evmVersion() == EVMVersion::byzantium())
            CHECK_GAS(21551, 21526, 20);
        else
            CHECK_GAS(21546, 21526, 20);
    }
}

BOOST_AUTO_TEST_SUITE_END()

}
}
}