Browse Source

Merge pull request #501 from chriseth/sol_testFramework

Tests on ranges of input data.
cl-refactor
Gav Wood 10 years ago
parent
commit
bbb927a8c8
  1. 151
      test/solidityEndToEndTest.cpp

151
test/solidityEndToEndTest.cpp

@ -22,6 +22,7 @@
*/ */
#include <string> #include <string>
#include <tuple>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <libethereum/State.h> #include <libethereum/State.h>
#include <libethereum/Executive.h> #include <libethereum/Executive.h>
@ -41,7 +42,7 @@ class ExecutionFramework
public: public:
ExecutionFramework() { g_logVerbosity = 0; } ExecutionFramework() { g_logVerbosity = 0; }
bytes const& compileAndRun(std::string const& _sourceCode) bytes const& compileAndRun(string const& _sourceCode)
{ {
bytes code = dev::solidity::CompilerStack::compile(_sourceCode); bytes code = dev::solidity::CompilerStack::compile(_sourceCode);
sendMessage(code, true); sendMessage(code, true);
@ -49,28 +50,64 @@ public:
return m_output; return m_output;
} }
bytes const& callFunction(byte _index, bytes const& _data) bytes const& callContractFunction(byte _index, bytes const& _data = bytes())
{ {
sendMessage(bytes(1, _index) + _data, false); sendMessage(bytes(1, _index) + _data, false);
return m_output; return m_output;
} }
bytes const& callFunction(byte _index, u256 const& _argument1) template <class... Args>
bytes const& callContractFunction(byte _index, Args const&... _arguments)
{ {
return callFunction(_index, toBigEndian(_argument1)); return callContractFunction(_index, argsToBigEndian(_arguments...));
} }
bool testSolidityAgainstCpp(byte _index, std::function<u256(u256)> const& _cppfun, u256 const& _argument1) template <class CppFunction, class... Args>
void testSolidityAgainstCpp(byte _index, CppFunction const& _cppFunction, Args const&... _arguments)
{ {
return toBigEndian(_cppfun(_argument1)) == callFunction(_index, toBigEndian(_argument1)); bytes solidityResult = callContractFunction(_index, _arguments...);
bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...);
BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match."
"\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult));
} }
bool testSolidityAgainstCpp(byte _index, std::function<u256()> const& _cppfun) template <class CppFunction, class... Args>
void testSolidityAgainstCppOnRange(byte _index, CppFunction const& _cppFunction,
u256 const& _rangeStart, u256 const& _rangeEnd)
{ {
return toBigEndian(_cppfun()) == callFunction(_index, bytes()); for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument)
{
bytes solidityResult = callContractFunction(_index, argument);
bytes cppResult = callCppAndEncodeResult(_cppFunction, argument);
BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match."
"\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult) +
"\nArgument: " + toHex(toBigEndian(argument)));
}
} }
private: private:
template <class FirstArg, class... Args>
bytes argsToBigEndian(FirstArg const& _firstArg, Args const&... _followingArgs) const
{
return toBigEndian(_firstArg) + argsToBigEndian(_followingArgs...);
}
bytes argsToBigEndian() const { return bytes(); }
template <class CppFunction, class... Args>
auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments)
-> typename enable_if<is_void<decltype(_cppFunction(_arguments...))>::value, bytes>::type
{
_cppFunction(_arguments...);
return bytes();
}
template <class CppFunction, class... Args>
auto callCppAndEncodeResult(CppFunction const& _cppFunction, Args const&... _arguments)
-> typename enable_if<!is_void<decltype(_cppFunction(_arguments...))>::value, bytes>::type
{
return toBigEndian(_cppFunction(_arguments...));
}
void sendMessage(bytes const& _data, bool _isCreation) void sendMessage(bytes const& _data, bool _isCreation)
{ {
eth::Executive executive(m_state); eth::Executive executive(m_state);
@ -91,7 +128,10 @@ private:
BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
} }
else else
{
BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
BOOST_REQUIRE(!executive.call(m_contractAddress, Address(), 0, m_gasPrice, &_data, m_gas, Address())); BOOST_REQUIRE(!executive.call(m_contractAddress, Address(), 0, m_gasPrice, &_data, m_gas, Address()));
}
BOOST_REQUIRE(executive.go()); BOOST_REQUIRE(executive.go());
executive.finalize(); executive.finalize();
m_output = executive.out().toVector(); m_output = executive.out().toVector();
@ -112,8 +152,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
" function f(uint a) returns(uint d) { return a * 7; }\n" " function f(uint a) returns(uint d) { return a * 7; }\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
u256 a = 0x200030004; testSolidityAgainstCppOnRange(0, [](u256 const& a) -> u256 { return a * 7; }, 0, 100);
BOOST_CHECK(callFunction(0, a) == toBigEndian(a * 7));
} }
BOOST_AUTO_TEST_CASE(empty_contract) BOOST_AUTO_TEST_CASE(empty_contract)
@ -121,7 +160,7 @@ BOOST_AUTO_TEST_CASE(empty_contract)
char const* sourceCode = "contract test {\n" char const* sourceCode = "contract test {\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
BOOST_CHECK(callFunction(0, bytes()).empty()); BOOST_CHECK(callContractFunction(0, bytes()).empty());
} }
BOOST_AUTO_TEST_CASE(recursive_calls) BOOST_AUTO_TEST_CASE(recursive_calls)
@ -133,7 +172,7 @@ BOOST_AUTO_TEST_CASE(recursive_calls)
" }\n" " }\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
std::function<u256(u256)> recursive_calls_cpp = [&recursive_calls_cpp](u256 const& n) -> u256 function<u256(u256)> recursive_calls_cpp = [&recursive_calls_cpp](u256 const& n) -> u256
{ {
if (n <= 1) if (n <= 1)
return 1; return 1;
@ -141,11 +180,7 @@ BOOST_AUTO_TEST_CASE(recursive_calls)
return n * recursive_calls_cpp(n - 1); return n * recursive_calls_cpp(n - 1);
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, recursive_calls_cpp, u256(0))); testSolidityAgainstCppOnRange(0, recursive_calls_cpp, 0, 5);
BOOST_CHECK(testSolidityAgainstCpp(0, recursive_calls_cpp, u256(1)));
BOOST_CHECK(testSolidityAgainstCpp(0, recursive_calls_cpp, u256(2)));
BOOST_CHECK(testSolidityAgainstCpp(0, recursive_calls_cpp, u256(3)));
BOOST_CHECK(testSolidityAgainstCpp(0, recursive_calls_cpp, u256(4)));
} }
BOOST_AUTO_TEST_CASE(multiple_functions) BOOST_AUTO_TEST_CASE(multiple_functions)
@ -185,11 +220,7 @@ BOOST_AUTO_TEST_CASE(while_loop)
return nfac; return nfac;
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, while_loop_cpp, u256(0))); testSolidityAgainstCppOnRange(0, while_loop_cpp, 0, 5);
BOOST_CHECK(testSolidityAgainstCpp(0, while_loop_cpp, u256(1)));
BOOST_CHECK(testSolidityAgainstCpp(0, while_loop_cpp, u256(2)));
BOOST_CHECK(testSolidityAgainstCpp(0, while_loop_cpp, u256(3)));
BOOST_CHECK(testSolidityAgainstCpp(0, while_loop_cpp, u256(4)));
} }
BOOST_AUTO_TEST_CASE(break_outside_loop) BOOST_AUTO_TEST_CASE(break_outside_loop)
@ -200,9 +231,8 @@ BOOST_AUTO_TEST_CASE(break_outside_loop)
" break; continue; return 2;\n" " break; continue; return 2;\n"
" }\n" " }\n"
"}\n"; "}\n";
ExecutionFramework framework; compileAndRun(sourceCode);
framework.compileAndRun(sourceCode); testSolidityAgainstCpp(0, [](u256 const& _a) -> u256 { return 2; }, u256(0));
BOOST_CHECK(framework.callFunction(0, u256(0)) == toBigEndian(u256(2)));
} }
BOOST_AUTO_TEST_CASE(nested_loops) BOOST_AUTO_TEST_CASE(nested_loops)
@ -225,8 +255,7 @@ BOOST_AUTO_TEST_CASE(nested_loops)
" return x;\n" " return x;\n"
" }\n" " }\n"
"}\n"; "}\n";
ExecutionFramework framework; compileAndRun(sourceCode);
framework.compileAndRun(sourceCode);
auto nested_loops_cpp = [](u256 n) -> u256 auto nested_loops_cpp = [](u256 n) -> u256
{ {
@ -252,18 +281,7 @@ BOOST_AUTO_TEST_CASE(nested_loops)
return n; return n;
}; };
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(0))); testSolidityAgainstCppOnRange(0, nested_loops_cpp, 0, 12);
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(1)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(2)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(3)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(4)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(5)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(6)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(7)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(8)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(9)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(10)));
BOOST_CHECK(framework.testSolidityAgainstCpp(0, nested_loops_cpp, u256(11)));
} }
BOOST_AUTO_TEST_CASE(calling_other_functions) BOOST_AUTO_TEST_CASE(calling_other_functions)
@ -295,7 +313,8 @@ BOOST_AUTO_TEST_CASE(calling_other_functions)
return 3 * n + 1; return 3 * n + 1;
}; };
auto collatz_cpp = [&evenStep_cpp, &oddStep_cpp] (u256 n) -> u256 { auto collatz_cpp = [&evenStep_cpp, &oddStep_cpp](u256 n) -> u256
{
u256 y; u256 y;
while ((y = n) > 1) while ((y = n) > 1)
{ {
@ -307,11 +326,11 @@ BOOST_AUTO_TEST_CASE(calling_other_functions)
return y; return y;
}; };
BOOST_CHECK(testSolidityAgainstCpp(2, collatz_cpp, u256(0))); testSolidityAgainstCpp(2, collatz_cpp, u256(0));
BOOST_CHECK(testSolidityAgainstCpp(2, collatz_cpp, u256(1))); testSolidityAgainstCpp(2, collatz_cpp, u256(1));
BOOST_CHECK(testSolidityAgainstCpp(2, collatz_cpp, u256(2))); testSolidityAgainstCpp(2, collatz_cpp, u256(2));
BOOST_CHECK(testSolidityAgainstCpp(2, collatz_cpp, u256(8))); testSolidityAgainstCpp(2, collatz_cpp, u256(8));
BOOST_CHECK(testSolidityAgainstCpp(2, collatz_cpp, u256(127))); testSolidityAgainstCpp(2, collatz_cpp, u256(127));
} }
BOOST_AUTO_TEST_CASE(many_local_variables) BOOST_AUTO_TEST_CASE(many_local_variables)
@ -324,8 +343,15 @@ BOOST_AUTO_TEST_CASE(many_local_variables)
" }\n" " }\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
BOOST_CHECK(callFunction(0, toBigEndian(u256(0x1000)) + toBigEndian(u256(0x10000)) + toBigEndian(u256(0x100000))) auto f = [](u256 const& x1, u256 const& x2, u256 const& x3) -> u256
== toBigEndian(u256(0x121121))); {
u256 a = 0x1;
u256 b = 0x10;
u256 c = 0x100;
u256 y = a + b + c + x1 + x2 + x3;
return y + b + x2;
};
testSolidityAgainstCpp(0, f, u256(0x1000), u256(0x10000), u256(0x100000));
} }
BOOST_AUTO_TEST_CASE(packing_unpacking_types) BOOST_AUTO_TEST_CASE(packing_unpacking_types)
@ -338,7 +364,7 @@ BOOST_AUTO_TEST_CASE(packing_unpacking_types)
" }\n" " }\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
BOOST_CHECK(callFunction(0, fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0")) BOOST_CHECK(callContractFunction(0, fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0"))
== fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f")); == fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f"));
} }
@ -350,7 +376,7 @@ BOOST_AUTO_TEST_CASE(multiple_return_values)
" }\n" " }\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
BOOST_CHECK(callFunction(0, bytes(1, 1) + toBigEndian(u256(0xcd))) BOOST_CHECK(callContractFunction(0, bytes(1, 1) + toBigEndian(u256(0xcd)))
== toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0))); == toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0)));
} }
@ -370,8 +396,7 @@ BOOST_AUTO_TEST_CASE(short_circuiting)
return n; return n;
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, short_circuiting_cpp, u256(0))); testSolidityAgainstCppOnRange(0, short_circuiting_cpp, 0, 2);
BOOST_CHECK(testSolidityAgainstCpp(0, short_circuiting_cpp, u256(1)));
} }
BOOST_AUTO_TEST_CASE(high_bits_cleaning) BOOST_AUTO_TEST_CASE(high_bits_cleaning)
@ -391,7 +416,7 @@ BOOST_AUTO_TEST_CASE(high_bits_cleaning)
return 0; return 0;
return x; return x;
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, high_bits_cleaning_cpp)); testSolidityAgainstCpp(0, high_bits_cleaning_cpp);
} }
BOOST_AUTO_TEST_CASE(sign_extension) BOOST_AUTO_TEST_CASE(sign_extension)
@ -411,7 +436,7 @@ BOOST_AUTO_TEST_CASE(sign_extension)
return 0; return 0;
return u256(x) * -1; return u256(x) * -1;
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, sign_extension_cpp)); testSolidityAgainstCpp(0, sign_extension_cpp);
} }
BOOST_AUTO_TEST_CASE(small_unsigned_types) BOOST_AUTO_TEST_CASE(small_unsigned_types)
@ -428,7 +453,7 @@ BOOST_AUTO_TEST_CASE(small_unsigned_types)
uint32_t x = uint32_t(0xffffff) * 0xffffff; uint32_t x = uint32_t(0xffffff) * 0xffffff;
return x / 0x100; return x / 0x100;
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, small_unsigned_types_cpp)); testSolidityAgainstCpp(0, small_unsigned_types_cpp);
} }
BOOST_AUTO_TEST_CASE(small_signed_types) BOOST_AUTO_TEST_CASE(small_signed_types)
@ -443,7 +468,7 @@ BOOST_AUTO_TEST_CASE(small_signed_types)
{ {
return -int32_t(10) * -int64_t(20); return -int32_t(10) * -int64_t(20);
}; };
BOOST_CHECK(testSolidityAgainstCpp(0, small_signed_types_cpp)); testSolidityAgainstCpp(0, small_signed_types_cpp);
} }
BOOST_AUTO_TEST_CASE(state_smoke_test) BOOST_AUTO_TEST_CASE(state_smoke_test)
@ -461,14 +486,14 @@ BOOST_AUTO_TEST_CASE(state_smoke_test)
" }\n" " }\n"
"}\n"; "}\n";
compileAndRun(sourceCode); compileAndRun(sourceCode);
BOOST_CHECK(callFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0))); BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0)));
BOOST_CHECK(callFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0))); BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0)));
BOOST_CHECK(callFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes()); BOOST_CHECK(callContractFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes());
BOOST_CHECK(callFunction(1, bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes()); BOOST_CHECK(callContractFunction(1, bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes());
BOOST_CHECK(callFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x1234))); BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x1234)));
BOOST_CHECK(callFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0x8765))); BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0x8765)));
BOOST_CHECK(callFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes()); BOOST_CHECK(callContractFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes());
BOOST_CHECK(callFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x3))); BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x3)));
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save