mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Tests for gas costs.
This commit is contained in:
parent
ead13b1d21
commit
d80d324de7
@ -183,6 +183,7 @@ test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] )
|
|||||||
"LLLEndToEndTest",
|
"LLLEndToEndTest",
|
||||||
#endif
|
#endif
|
||||||
"GasMeterTests",
|
"GasMeterTests",
|
||||||
|
"GasCostTests",
|
||||||
"SolidityEndToEndTest",
|
"SolidityEndToEndTest",
|
||||||
"SolidityOptimizer"
|
"SolidityOptimizer"
|
||||||
})
|
})
|
||||||
|
15
test/cmdlineTests/data_storage.sol
Normal file
15
test/cmdlineTests/data_storage.sol
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
pragma solidity >=0.0;
|
||||||
|
|
||||||
|
contract C {
|
||||||
|
function f() pure public {
|
||||||
|
require(false, "1234567890123456789012345678901");
|
||||||
|
require(false, "12345678901234567890123456789012");
|
||||||
|
require(false, "123456789012345678901234567890123");
|
||||||
|
require(false, "1234567890123456789012345678901234");
|
||||||
|
require(false, "12345678901234567890123456789012345");
|
||||||
|
require(false, "123456789012345678901234567890123456");
|
||||||
|
require(false, "123456789012345678901234567890121234567890123456789012345678901");
|
||||||
|
require(false, "1234567890123456789012345678901212345678901234567890123456789012");
|
||||||
|
require(false, "12345678901234567890123456789012123456789012345678901234567890123");
|
||||||
|
}
|
||||||
|
}
|
1
test/cmdlineTests/data_storage.sol.args
Normal file
1
test/cmdlineTests/data_storage.sol.args
Normal file
@ -0,0 +1 @@
|
|||||||
|
--gas
|
7
test/cmdlineTests/data_storage.sol.stdout
Normal file
7
test/cmdlineTests/data_storage.sol.stdout
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
======= data_storage.sol:C =======
|
||||||
|
Gas estimation:
|
||||||
|
construction:
|
||||||
|
306 + 264400 = 264706
|
||||||
|
external:
|
||||||
|
f(): 263
|
89
test/libsolidity/GasCosts.cpp
Normal file
89
test/libsolidity/GasCosts.cpp
Normal file
@ -0,0 +1,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()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12245,7 +12245,7 @@ BOOST_AUTO_TEST_CASE(include_creation_bytecode_only_once)
|
|||||||
compileAndRun(sourceCode);
|
compileAndRun(sourceCode);
|
||||||
BOOST_CHECK_LE(
|
BOOST_CHECK_LE(
|
||||||
double(m_compiler.object("Double").bytecode.size()),
|
double(m_compiler.object("Double").bytecode.size()),
|
||||||
1.1 * double(m_compiler.object("Single").bytecode.size())
|
1.2 * double(m_compiler.object("Single").bytecode.size())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user