2014-10-31 16:47:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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>
|
2015-01-09 06:39:30 +00:00
|
|
|
* @author Gav Wood <g@ethdev.com>
|
2014-10-31 16:47:43 +00:00
|
|
|
* @date 2014
|
|
|
|
* Unit tests for the solidity expression compiler, testing the behaviour of the code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
2014-11-10 17:07:56 +00:00
|
|
|
#include <tuple>
|
2014-10-31 16:47:43 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-11-26 12:19:17 +00:00
|
|
|
#include <libdevcrypto/SHA3.h>
|
2014-12-11 15:37:17 +00:00
|
|
|
#include <test/solidityExecutionFramework.h>
|
2014-10-31 16:47:43 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2014-12-18 14:21:03 +00:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(SolidityEndToEndTest, ExecutionFramework)
|
2014-10-31 16:47:43 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(smoke_test)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint a) returns(uint d) { return a * 7; }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return a * 7; }, 0, 100);
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(empty_contract)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:27:26 +00:00
|
|
|
BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()).empty());
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(recursive_calls)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint n) returns(uint nfac) {\n"
|
|
|
|
" if (n <= 1) return 1;\n"
|
|
|
|
" else return n * f(n - 1);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-10 14:36:21 +00:00
|
|
|
function<u256(u256)> recursive_calls_cpp = [&recursive_calls_cpp](u256 const& n) -> u256
|
2014-11-09 12:22:47 +00:00
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
if (n <= 1)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return n * recursive_calls_cpp(n - 1);
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", recursive_calls_cpp, 0, 5);
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 12:13:40 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(multiple_functions)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a() returns(uint n) { return 0; }\n"
|
|
|
|
" function b() returns(uint n) { return 1; }\n"
|
|
|
|
" function c() returns(uint n) { return 2; }\n"
|
|
|
|
" function f() returns(uint n) { return 3; }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 16:18:31 +00:00
|
|
|
BOOST_CHECK(callContractFunction("a()", bytes()) == toBigEndian(u256(0)));
|
|
|
|
BOOST_CHECK(callContractFunction("b()", bytes()) == toBigEndian(u256(1)));
|
|
|
|
BOOST_CHECK(callContractFunction("c()", bytes()) == toBigEndian(u256(2)));
|
|
|
|
BOOST_CHECK(callContractFunction("f()", bytes()) == toBigEndian(u256(3)));
|
|
|
|
BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()) == bytes());
|
2014-11-10 12:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 16:47:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(while_loop)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint n) returns(uint nfac) {\n"
|
|
|
|
" nfac = 1;\n"
|
|
|
|
" var i = 2;\n"
|
|
|
|
" while (i <= n) nfac *= i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-07 02:32:37 +00:00
|
|
|
|
2014-11-09 12:22:47 +00:00
|
|
|
auto while_loop_cpp = [](u256 const& n) -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
u256 nfac = 1;
|
|
|
|
u256 i = 2;
|
|
|
|
while (i <= n)
|
|
|
|
nfac *= i++;
|
|
|
|
|
|
|
|
return nfac;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", while_loop_cpp, 0, 5);
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 17:44:05 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(break_outside_loop)
|
|
|
|
{
|
|
|
|
// break and continue outside loops should be simply ignored
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint x) returns(uint y) {\n"
|
|
|
|
" break; continue; return 2;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-10 14:36:21 +00:00
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("f(uint256)", [](u256 const&) -> u256 { return 2; }, u256(0));
|
2014-11-05 17:44:05 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 17:37:27 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(nested_loops)
|
|
|
|
{
|
|
|
|
// tests that break and continue statements in nested loops jump to the correct place
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint x) returns(uint y) {\n"
|
|
|
|
" while (x > 1) {\n"
|
|
|
|
" if (x == 10) break;\n"
|
|
|
|
" while (x > 5) {\n"
|
|
|
|
" if (x == 8) break;\n"
|
|
|
|
" x--;\n"
|
|
|
|
" if (x == 6) continue;\n"
|
|
|
|
" return x;\n"
|
|
|
|
" }\n"
|
|
|
|
" x--;\n"
|
|
|
|
" if (x == 3) continue;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" return x;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-10 14:36:21 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-07 02:32:37 +00:00
|
|
|
|
2014-11-09 12:22:47 +00:00
|
|
|
auto nested_loops_cpp = [](u256 n) -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
while (n > 1)
|
|
|
|
{
|
|
|
|
if (n == 10)
|
|
|
|
break;
|
|
|
|
while (n > 5)
|
|
|
|
{
|
|
|
|
if (n == 8)
|
|
|
|
break;
|
|
|
|
n--;
|
|
|
|
if (n == 6)
|
|
|
|
continue;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
n--;
|
|
|
|
if (n == 3)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", nested_loops_cpp, 0, 12);
|
2014-11-05 17:37:27 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 15:52:47 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(for_loop)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint n) returns(uint nfac) {\n"
|
|
|
|
" nfac = 1;\n"
|
|
|
|
" for (var i = 2; i <= n; i++)\n"
|
2014-12-16 16:35:00 +00:00
|
|
|
" nfac *= i;\n"
|
2014-12-16 15:52:47 +00:00
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
|
|
|
auto for_loop_cpp = [](u256 const& n) -> u256
|
|
|
|
{
|
|
|
|
u256 nfac = 1;
|
|
|
|
for (auto i = 2; i <= n; i++)
|
|
|
|
nfac *= i;
|
|
|
|
return nfac;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", for_loop_cpp, 0, 5);
|
2014-12-16 15:52:47 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 16:35:00 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(for_loop_empty)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f() returns(uint ret) {\n"
|
|
|
|
" ret = 1;\n"
|
|
|
|
" for (;;)\n"
|
|
|
|
" {\n"
|
|
|
|
" ret += 1;\n"
|
|
|
|
" if (ret >= 10) break;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
|
|
|
auto for_loop_empty_cpp = []() -> u256
|
|
|
|
{
|
|
|
|
u256 ret = 1;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ret += 1;
|
|
|
|
if (ret >= 10) break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("f()", for_loop_empty_cpp);
|
2014-12-16 16:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint n) returns(uint nfac) {\n"
|
|
|
|
" nfac = 1;\n"
|
|
|
|
" uint256 i;\n"
|
|
|
|
" for (i = 2; i <= n; i++)\n"
|
|
|
|
" nfac *= i;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
|
|
|
auto for_loop_simple_init_expr_cpp = [](u256 const& n) -> u256
|
|
|
|
{
|
|
|
|
u256 nfac = 1;
|
|
|
|
u256 i;
|
|
|
|
for (i = 2; i <= n; i++)
|
|
|
|
nfac *= i;
|
|
|
|
return nfac;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", for_loop_simple_init_expr_cpp, 0, 5);
|
2014-12-16 16:35:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 16:47:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(calling_other_functions)
|
|
|
|
{
|
|
|
|
// note that the index of a function is its index in the sorted sequence of functions
|
|
|
|
char const* sourceCode = "contract collatz {\n"
|
|
|
|
" function run(uint x) returns(uint y) {\n"
|
|
|
|
" while ((y = x) > 1) {\n"
|
|
|
|
" if (x % 2 == 0) x = evenStep(x);\n"
|
|
|
|
" else x = oddStep(x);\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" function evenStep(uint x) returns(uint y) {\n"
|
|
|
|
" return x / 2;\n"
|
|
|
|
" }\n"
|
|
|
|
" function oddStep(uint x) returns(uint y) {\n"
|
|
|
|
" return 3 * x + 1;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-07 02:32:37 +00:00
|
|
|
|
2014-11-09 12:22:47 +00:00
|
|
|
auto evenStep_cpp = [](u256 const& n) -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
return n / 2;
|
|
|
|
};
|
|
|
|
|
2014-11-09 12:22:47 +00:00
|
|
|
auto oddStep_cpp = [](u256 const& n) -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
return 3 * n + 1;
|
|
|
|
};
|
|
|
|
|
2014-11-10 14:36:21 +00:00
|
|
|
auto collatz_cpp = [&evenStep_cpp, &oddStep_cpp](u256 n) -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
u256 y;
|
|
|
|
while ((y = n) > 1)
|
|
|
|
{
|
|
|
|
if (n % 2 == 0)
|
|
|
|
n = evenStep_cpp(n);
|
|
|
|
else
|
|
|
|
n = oddStep_cpp(n);
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(0));
|
|
|
|
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(1));
|
|
|
|
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(2));
|
|
|
|
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(8));
|
|
|
|
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(127));
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(many_local_variables)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run(uint x1, uint x2, uint x3) returns(uint y) {\n"
|
|
|
|
" var a = 0x1; var b = 0x10; var c = 0x100;\n"
|
|
|
|
" y = a + b + c + x1 + x2 + x3;\n"
|
|
|
|
" y += b + x2;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-10 14:36:21 +00:00
|
|
|
auto f = [](u256 const& x1, u256 const& x2, u256 const& x3) -> u256
|
|
|
|
{
|
|
|
|
u256 a = 0x1;
|
|
|
|
u256 b = 0x10;
|
|
|
|
u256 c = 0x100;
|
|
|
|
u256 y = a + b + c + x1 + x2 + x3;
|
|
|
|
return y + b + x2;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("run(uint256,uint256,uint256)", f, u256(0x1000), u256(0x10000), u256(0x100000));
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 20:29:36 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(packing_unpacking_types)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run(bool a, uint32 b, uint64 c) returns(uint256 y) {\n"
|
|
|
|
" if (a) y = 1;\n"
|
|
|
|
" y = y * 0x100000000 | ~b;\n"
|
|
|
|
" y = y * 0x10000000000000000 | ~c;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", true, fromHex("0f0f0f0f"), fromHex("f0f0f0f0f0f0f0f0"))
|
2014-11-04 20:29:36 +00:00
|
|
|
== fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f"));
|
|
|
|
}
|
|
|
|
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(packing_signed_types)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run() returns(int8 y) {\n"
|
|
|
|
" uint8 x = 0xfa;\n"
|
|
|
|
" return int8(x);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
BOOST_CHECK(callContractFunction("run()")
|
|
|
|
== fromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"));
|
|
|
|
}
|
|
|
|
|
2014-10-31 16:47:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(multiple_return_values)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run(bool x1, uint x2) returns(uint y1, bool y2, uint y3) {\n"
|
|
|
|
" y1 = x2; y2 = x1;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("run(bool,uint256)", true, 0xcd) == encodeArgs(0xcd, true, 0));
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(short_circuiting)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run(uint x) returns(uint y) {\n"
|
|
|
|
" x == 0 || ((x = 8) > 0);\n"
|
|
|
|
" return x;"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-07 02:32:37 +00:00
|
|
|
|
2014-11-09 12:22:47 +00:00
|
|
|
auto short_circuiting_cpp = [](u256 n) -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
n == 0 || (n = 8) > 0;
|
|
|
|
return n;
|
|
|
|
};
|
|
|
|
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("run(uint256)", short_circuiting_cpp, 0, 2);
|
2014-10-31 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 18:13:03 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(high_bits_cleaning)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run() returns(uint256 y) {\n"
|
|
|
|
" uint32 x = uint32(0xffffffff) + 10;\n"
|
|
|
|
" if (x >= 0xffffffff) return 0;\n"
|
|
|
|
" return x;"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-09 12:22:47 +00:00
|
|
|
auto high_bits_cleaning_cpp = []() -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
uint32_t x = uint32_t(0xffffffff) + 10;
|
|
|
|
if (x >= 0xffffffff)
|
|
|
|
return 0;
|
|
|
|
return x;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("run()", high_bits_cleaning_cpp);
|
2014-11-04 18:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(sign_extension)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run() returns(uint256 y) {\n"
|
|
|
|
" int64 x = -int32(0xff);\n"
|
|
|
|
" if (x >= 0xff) return 0;\n"
|
|
|
|
" return -uint256(x);"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-09 12:22:47 +00:00
|
|
|
auto sign_extension_cpp = []() -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
int64_t x = -int32_t(0xff);
|
|
|
|
if (x >= 0xff)
|
|
|
|
return 0;
|
|
|
|
return u256(x) * -1;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("run()", sign_extension_cpp);
|
2014-11-04 20:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(small_unsigned_types)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run() returns(uint256 y) {\n"
|
|
|
|
" uint32 x = uint32(0xffffff) * 0xffffff;\n"
|
|
|
|
" return x / 0x100;"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-09 12:22:47 +00:00
|
|
|
auto small_unsigned_types_cpp = []() -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
uint32_t x = uint32_t(0xffffff) * 0xffffff;
|
|
|
|
return x / 0x100;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("run()", small_unsigned_types_cpp);
|
2014-11-04 20:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(small_signed_types)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run() returns(int256 y) {\n"
|
|
|
|
" return -int32(10) * -int64(20);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2014-11-06 00:39:59 +00:00
|
|
|
compileAndRun(sourceCode);
|
2014-11-09 12:22:47 +00:00
|
|
|
auto small_signed_types_cpp = []() -> u256
|
|
|
|
{
|
2014-11-07 02:32:37 +00:00
|
|
|
return -int32_t(10) * -int64_t(20);
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("run()", small_signed_types_cpp);
|
2014-11-04 18:13:03 +00:00
|
|
|
}
|
2014-10-31 16:47:43 +00:00
|
|
|
|
2014-12-09 17:46:18 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(strings)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function fixed() returns(string32 ret) {\n"
|
|
|
|
" return \"abc\\x00\\xff__\";\n"
|
|
|
|
" }\n"
|
|
|
|
" function pipeThrough(string2 small, bool one) returns(string16 large, bool oneRet) {\n"
|
|
|
|
" oneRet = one;\n"
|
|
|
|
" large = small;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("fixed()") == encodeArgs(string("abc\0\xff__", 7)));
|
|
|
|
BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", string("\0\x02", 2), true) == encodeArgs(string("\0\x2", 2), true));
|
2014-12-09 17:46:18 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 13:19:11 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(empty_string_on_stack)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function run(string0 empty, uint8 inp) returns(uint16 a, string0 b, string4 c) {\n"
|
|
|
|
" var x = \"abc\";\n"
|
|
|
|
" var y = \"\";\n"
|
|
|
|
" var z = inp;\n"
|
|
|
|
" a = z; b = y; c = x;"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("run(string0,uint8)", string(), byte(0x02)) == encodeArgs(0x2, string(""), string("abc\0")));
|
2014-12-11 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(state_smoke_test)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" uint256 value1;\n"
|
|
|
|
" uint256 value2;\n"
|
|
|
|
" function get(uint8 which) returns (uint256 value) {\n"
|
|
|
|
" if (which == 0) return value1;\n"
|
|
|
|
" else return value2;\n"
|
|
|
|
" }\n"
|
|
|
|
" function set(uint8 which, uint256 value) {\n"
|
|
|
|
" if (which == 0) value1 = value;\n"
|
|
|
|
" else value2 = value;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0));
|
|
|
|
BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x1234) == encodeArgs());
|
|
|
|
BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x01), 0x8765) == encodeArgs());
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte( 0x00)) == encodeArgs(0x1234));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(0x8765));
|
|
|
|
BOOST_CHECK(callContractFunction("set(uint8,uint256)", byte(0x00), 0x3) == encodeArgs());
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(0x3));
|
2014-11-07 01:06:37 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 21:15:11 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(compound_assign)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" uint value1;\n"
|
|
|
|
" uint value2;\n"
|
|
|
|
" function f(uint x, uint y) returns (uint w) {\n"
|
|
|
|
" uint value3 = y;"
|
|
|
|
" value1 += x;\n"
|
|
|
|
" value3 *= x;"
|
|
|
|
" value2 *= value3 + value1;\n"
|
|
|
|
" return value2 += 7;"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
|
|
|
u256 value1;
|
|
|
|
u256 value2;
|
|
|
|
auto f = [&](u256 const& _x, u256 const& _y) -> u256
|
|
|
|
{
|
|
|
|
u256 value3 = _y;
|
|
|
|
value1 += _x;
|
|
|
|
value3 *= _x;
|
|
|
|
value2 *= value3 + value1;
|
|
|
|
return value2 += 7;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(0), u256(6));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(1), u256(3));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(2), u256(25));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(3), u256(69));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(4), u256(84));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(5), u256(2));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(6), u256(51));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(7), u256(48));
|
2014-12-18 21:15:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(simple_mapping)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" mapping(uint8 => uint8) table;\n"
|
|
|
|
" function get(uint8 k) returns (uint8 v) {\n"
|
|
|
|
" return table[k];\n"
|
|
|
|
" }\n"
|
|
|
|
" function set(uint8 k, uint8 v) {\n"
|
|
|
|
" table[k] = v;\n"
|
|
|
|
" }\n"
|
|
|
|
"}";
|
|
|
|
compileAndRun(sourceCode);
|
2014-12-08 23:58:02 +00:00
|
|
|
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0)) == encodeArgs(byte(0x00)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x00)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
|
|
|
|
callContractFunction("set(uint8,uint8)", byte(0x01), byte(0xa1));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0x00)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
|
|
|
|
callContractFunction("set(uint8,uint8)", byte(0x00), byte(0xef));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0xa1)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
|
|
|
|
callContractFunction("set(uint8,uint8)", byte(0x01), byte(0x05));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x00)) == encodeArgs(byte(0xef)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0x01)) == encodeArgs(byte(0x05)));
|
|
|
|
BOOST_CHECK(callContractFunction("get(uint8)", byte(0xa7)) == encodeArgs(byte(0x00)));
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(mapping_state)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract Ballot {\n"
|
|
|
|
" mapping(address => bool) canVote;\n"
|
|
|
|
" mapping(address => uint) voteCount;\n"
|
|
|
|
" mapping(address => bool) voted;\n"
|
|
|
|
" function getVoteCount(address addr) returns (uint retVoteCount) {\n"
|
|
|
|
" return voteCount[addr];\n"
|
|
|
|
" }\n"
|
|
|
|
" function grantVoteRight(address addr) {\n"
|
|
|
|
" canVote[addr] = true;\n"
|
|
|
|
" }\n"
|
|
|
|
" function vote(address voter, address vote) returns (bool success) {\n"
|
|
|
|
" if (!canVote[voter] || voted[voter]) return false;\n"
|
|
|
|
" voted[voter] = true;\n"
|
|
|
|
" voteCount[vote] = voteCount[vote] + 1;\n"
|
|
|
|
" return true;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
class Ballot
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
u256 getVoteCount(u160 _address) { return m_voteCount[_address]; }
|
|
|
|
void grantVoteRight(u160 _address) { m_canVote[_address] = true; }
|
|
|
|
bool vote(u160 _voter, u160 _vote)
|
|
|
|
{
|
|
|
|
if (!m_canVote[_voter] || m_voted[_voter]) return false;
|
|
|
|
m_voted[_voter] = true;
|
|
|
|
m_voteCount[_vote]++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
map<u160, bool> m_canVote;
|
|
|
|
map<u160, u256> m_voteCount;
|
|
|
|
map<u160, bool> m_voted;
|
|
|
|
} ballot;
|
|
|
|
|
|
|
|
auto getVoteCount = bind(&Ballot::getVoteCount, &ballot, _1);
|
|
|
|
auto grantVoteRight = bind(&Ballot::grantVoteRight, &ballot, _1);
|
|
|
|
auto vote = bind(&Ballot::vote, &ballot, _1, _2);
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
|
2014-11-10 16:31:09 +00:00
|
|
|
// voting without vote right shourd be rejected
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
|
2014-11-10 16:31:09 +00:00
|
|
|
// grant vote rights
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(0));
|
|
|
|
testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(1));
|
2014-11-10 16:31:09 +00:00
|
|
|
// vote, should increase 2's vote count
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
|
2014-11-10 16:31:09 +00:00
|
|
|
// vote again, should be rejected
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
|
2014-11-10 16:31:09 +00:00
|
|
|
// vote without right to vote
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
|
2014-11-10 16:31:09 +00:00
|
|
|
// grant vote right and now vote again
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(2));
|
|
|
|
testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
|
|
|
|
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(mapping_state_inc_dec)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" uint value;\n"
|
|
|
|
" mapping(uint => uint) table;\n"
|
|
|
|
" function f(uint x) returns (uint y) {\n"
|
|
|
|
" value = x;\n"
|
|
|
|
" if (x > 0) table[++value] = 8;\n"
|
|
|
|
" if (x > 1) value--;\n"
|
|
|
|
" if (x > 2) table[value]++;\n"
|
|
|
|
" return --table[value++];\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
|
|
|
u256 value = 0;
|
|
|
|
map<u256, u256> table;
|
|
|
|
auto f = [&](u256 const& _x) -> u256
|
|
|
|
{
|
|
|
|
value = _x;
|
|
|
|
if (_x > 0)
|
|
|
|
table[++value] = 8;
|
|
|
|
if (_x > 1)
|
|
|
|
value --;
|
|
|
|
if (_x > 2)
|
|
|
|
table[value]++;
|
|
|
|
return --table[value++];
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCppOnRange("f(uint256)", f, 0, 5);
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multi_level_mapping)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" mapping(uint => mapping(uint => uint)) table;\n"
|
|
|
|
" function f(uint x, uint y, uint z) returns (uint w) {\n"
|
|
|
|
" if (z == 0) return table[x][y];\n"
|
|
|
|
" else return table[x][y] = z;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
|
|
|
|
map<u256, map<u256, u256>> table;
|
|
|
|
auto f = [&](u256 const& _x, u256 const& _y, u256 const& _z) -> u256
|
|
|
|
{
|
|
|
|
if (_z == 0) return table[_x][_y];
|
|
|
|
else return table[_x][_y] = _z;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(9));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(7));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
|
|
|
|
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
|
2014-11-10 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 00:12:57 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(structs)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" struct s1 {\n"
|
|
|
|
" uint8 x;\n"
|
|
|
|
" bool y;\n"
|
|
|
|
" }\n"
|
|
|
|
" struct s2 {\n"
|
|
|
|
" uint32 z;\n"
|
|
|
|
" s1 s1data;\n"
|
|
|
|
" mapping(uint8 => s2) recursive;\n"
|
|
|
|
" }\n"
|
|
|
|
" s2 data;\n"
|
|
|
|
" function check() returns (bool ok) {\n"
|
|
|
|
" return data.z == 1 && data.s1data.x == 2 && \n"
|
|
|
|
" data.s1data.y == true && \n"
|
|
|
|
" data.recursive[3].recursive[4].z == 5 && \n"
|
|
|
|
" data.recursive[4].recursive[3].z == 6 && \n"
|
|
|
|
" data.recursive[0].s1data.y == false && \n"
|
|
|
|
" data.recursive[4].z == 9;\n"
|
|
|
|
" }\n"
|
|
|
|
" function set() {\n"
|
|
|
|
" data.z = 1;\n"
|
|
|
|
" data.s1data.x = 2;\n"
|
|
|
|
" data.s1data.y = true;\n"
|
|
|
|
" data.recursive[3].recursive[4].z = 5;\n"
|
|
|
|
" data.recursive[4].recursive[3].z = 6;\n"
|
|
|
|
" data.recursive[0].s1data.y = false;\n"
|
|
|
|
" data.recursive[4].z = 9;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("check()") == encodeArgs(false));
|
2015-01-08 16:18:31 +00:00
|
|
|
BOOST_CHECK(callContractFunction("set()") == bytes());
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("check()") == encodeArgs(true));
|
2014-11-23 20:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(struct_reference)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" struct s2 {\n"
|
|
|
|
" uint32 z;\n"
|
|
|
|
" mapping(uint8 => s2) recursive;\n"
|
|
|
|
" }\n"
|
|
|
|
" s2 data;\n"
|
|
|
|
" function check() returns (bool ok) {\n"
|
|
|
|
" return data.z == 2 && \n"
|
|
|
|
" data.recursive[0].z == 3 && \n"
|
|
|
|
" data.recursive[0].recursive[1].z == 0 && \n"
|
|
|
|
" data.recursive[0].recursive[0].z == 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" function set() {\n"
|
|
|
|
" data.z = 2;\n"
|
|
|
|
" var map = data.recursive;\n"
|
|
|
|
" s2 inner = map[0];\n"
|
|
|
|
" inner.z = 3;\n"
|
|
|
|
" inner.recursive[0].z = inner.recursive[1].z + 1;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("check()") == encodeArgs(false));
|
2015-01-08 16:18:31 +00:00
|
|
|
BOOST_CHECK(callContractFunction("set()") == bytes());
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("check()") == encodeArgs(true));
|
2014-11-13 00:12:57 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 09:24:22 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(constructor)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" mapping(uint => uint) data;\n"
|
|
|
|
" function test() {\n"
|
|
|
|
" data[7] = 8;\n"
|
|
|
|
" }\n"
|
|
|
|
" function get(uint key) returns (uint value) {\n"
|
|
|
|
" return data[key];"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
map<u256, byte> data;
|
|
|
|
data[7] = 8;
|
|
|
|
auto get = [&](u256 const& _x) -> u256
|
|
|
|
{
|
|
|
|
return data[_x];
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("get(uint256)", get, u256(6));
|
|
|
|
testSolidityAgainstCpp("get(uint256)", get, u256(7));
|
2014-11-19 09:24:22 +00:00
|
|
|
}
|
|
|
|
|
2014-11-20 17:33:23 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(balance)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
2014-11-21 18:14:56 +00:00
|
|
|
" function getBalance() returns (uint256 balance) {\n"
|
2014-11-20 17:33:23 +00:00
|
|
|
" return address(this).balance;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode, 23);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("getBalance()") == encodeArgs(23));
|
2014-11-20 17:33:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 12:23:58 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(blockchain)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function someInfo() returns (uint256 value, address coinbase, uint256 blockNumber) {\n"
|
|
|
|
" value = msg.value;\n"
|
|
|
|
" coinbase = block.coinbase;\n"
|
|
|
|
" blockNumber = block.number;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode, 27);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunctionWithValue("someInfo()", 28) == encodeArgs(28, 0, 1));
|
2014-11-24 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 13:43:23 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(function_types)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(bool selector) returns (uint b) {\n"
|
|
|
|
" var f = fun1;\n"
|
|
|
|
" if (selector) f = fun2;\n"
|
|
|
|
" return f(9);\n"
|
|
|
|
" }\n"
|
|
|
|
" function fun1(uint x) returns (uint b) {\n"
|
|
|
|
" return 11;\n"
|
|
|
|
" }\n"
|
|
|
|
" function fun2(uint x) returns (uint b) {\n"
|
|
|
|
" return 12;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("a(bool)", false) == encodeArgs(11));
|
|
|
|
BOOST_CHECK(callContractFunction("a(bool)", true) == encodeArgs(12));
|
2014-11-25 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 22:45:26 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(type_conversions_cleanup)
|
|
|
|
{
|
|
|
|
// 22-byte integer converted to a contract (i.e. address, 20 bytes), converted to a 32 byte
|
|
|
|
// integer should drop the first two bytes
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Test {
|
|
|
|
function test() returns (uint ret) { return uint(address(Test(address(0x11223344556677889900112233445566778899001122)))); }
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-07 21:54:56 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("test()") == bytes({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22,
|
|
|
|
0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22}));
|
2015-01-07 22:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-25 17:23:39 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(send_ether)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(address addr, uint amount) returns (uint ret) {\n"
|
|
|
|
" addr.send(amount);\n"
|
|
|
|
" return address(this).balance;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
u256 amount(130);
|
|
|
|
compileAndRun(sourceCode, amount + 1);
|
|
|
|
u160 address(23);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == encodeArgs(1));
|
2014-11-25 17:23:39 +00:00
|
|
|
BOOST_CHECK_EQUAL(m_state.balance(address), amount);
|
|
|
|
}
|
|
|
|
|
2015-01-09 06:32:28 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(log0)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a() {\n"
|
|
|
|
" log0(1);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2015-01-09 14:00:47 +00:00
|
|
|
compileAndRun(sourceCode);
|
|
|
|
callContractFunction("a()");
|
2015-01-09 06:32:28 +00:00
|
|
|
BOOST_CHECK_EQUAL(m_logs.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
|
|
|
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(log1)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a() {\n"
|
|
|
|
" log1(1, 2);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2015-01-09 14:00:47 +00:00
|
|
|
compileAndRun(sourceCode);
|
|
|
|
callContractFunction("a()");
|
2015-01-09 06:32:28 +00:00
|
|
|
BOOST_CHECK_EQUAL(m_logs.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
|
|
|
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(log2)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a() {\n"
|
|
|
|
" log2(1, 2, 3);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2015-01-09 14:00:47 +00:00
|
|
|
compileAndRun(sourceCode);
|
|
|
|
callContractFunction("a()");
|
2015-01-09 06:32:28 +00:00
|
|
|
BOOST_CHECK_EQUAL(m_logs.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
|
|
|
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 2);
|
|
|
|
for (unsigned i = 0; i < 2; ++i)
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(log3)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a() {\n"
|
|
|
|
" log3(1, 2, 3, 4);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2015-01-09 14:00:47 +00:00
|
|
|
compileAndRun(sourceCode);
|
|
|
|
callContractFunction("a()");
|
2015-01-09 06:32:28 +00:00
|
|
|
BOOST_CHECK_EQUAL(m_logs.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
|
|
|
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 3);
|
|
|
|
for (unsigned i = 0; i < 3; ++i)
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(log4)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a() {\n"
|
|
|
|
" log4(1, 2, 3, 4, 5);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2015-01-09 14:00:47 +00:00
|
|
|
compileAndRun(sourceCode);
|
|
|
|
callContractFunction("a()");
|
2015-01-09 06:32:28 +00:00
|
|
|
BOOST_CHECK_EQUAL(m_logs.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
|
|
|
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 4);
|
|
|
|
for (unsigned i = 0; i < 4; ++i)
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
|
|
|
|
}
|
|
|
|
|
2015-01-09 14:00:47 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(log_in_constructor)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function test() {\n"
|
|
|
|
" log1(1, 2);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
|
|
|
|
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2)));
|
|
|
|
}
|
|
|
|
|
2014-11-26 12:19:17 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(suicide)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(address receiver) returns (uint ret) {\n"
|
|
|
|
" suicide(receiver);\n"
|
|
|
|
" return 10;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
u256 amount(130);
|
|
|
|
compileAndRun(sourceCode, amount);
|
|
|
|
u160 address(23);
|
2015-01-08 16:18:31 +00:00
|
|
|
BOOST_CHECK(callContractFunction("a(address)", address) == bytes());
|
2014-11-26 12:19:17 +00:00
|
|
|
BOOST_CHECK(!m_state.addressHasCode(m_contractAddress));
|
|
|
|
BOOST_CHECK_EQUAL(m_state.balance(address), amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(sha3)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(hash input) returns (hash sha3hash) {\n"
|
|
|
|
" return sha3(input);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
auto f = [&](u256 const& _x) -> u256
|
|
|
|
{
|
|
|
|
return dev::sha3(toBigEndian(_x));
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(4));
|
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(5));
|
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(-1));
|
2014-11-26 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(sha256)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(hash input) returns (hash sha256hash) {\n"
|
|
|
|
" return sha256(input);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
auto f = [&](u256 const& _input) -> u256
|
|
|
|
{
|
|
|
|
h256 ret;
|
|
|
|
dev::sha256(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32));
|
|
|
|
return ret;
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(4));
|
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(5));
|
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(-1));
|
2014-11-26 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(ripemd)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(hash input) returns (hash sha256hash) {\n"
|
|
|
|
" return ripemd160(input);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
auto f = [&](u256 const& _input) -> u256
|
|
|
|
{
|
|
|
|
h256 ret;
|
|
|
|
dev::ripemd160(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32));
|
|
|
|
return u256(ret) >> (256 - 160);
|
|
|
|
};
|
2015-01-08 16:18:31 +00:00
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(4));
|
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(5));
|
|
|
|
testSolidityAgainstCpp("a(hash256)", f, u256(-1));
|
2014-11-26 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(ecrecover)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function a(hash h, uint8 v, hash r, hash s) returns (address addr) {\n"
|
|
|
|
" return ecrecover(h, v, r, s);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
|
|
|
compileAndRun(sourceCode);
|
|
|
|
u256 h("0x18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c");
|
|
|
|
byte v = 28;
|
|
|
|
u256 r("0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f");
|
|
|
|
u256 s("0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549");
|
|
|
|
u160 addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == encodeArgs(addr));
|
2014-11-26 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 18:38:24 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inter_contract_calls)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
function multiply(uint a, uint b) returns (uint c) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function callHelper(uint a, uint b) returns (uint c) {
|
|
|
|
return h.multiply(a, b);
|
|
|
|
}
|
|
|
|
function getHelper() returns (address haddress) {
|
|
|
|
return address(h);
|
|
|
|
}
|
|
|
|
function setHelper(address haddress) {
|
|
|
|
h = Helper(haddress);
|
|
|
|
}
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Helper");
|
2015-01-09 20:36:25 +00:00
|
|
|
u160 const c_helperAddress = m_contractAddress;
|
2014-12-04 18:38:24 +00:00
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-09 20:36:25 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
|
|
|
|
BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
|
2014-12-04 18:38:24 +00:00
|
|
|
u256 a(3456789);
|
|
|
|
u256 b("0x282837623374623234aa74");
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b));
|
2014-12-04 18:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
function sel(uint a, bool select, uint b) returns (uint c) {
|
|
|
|
if (select) return a; else return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function callHelper(uint a, bool select, uint b) returns (uint c) {
|
|
|
|
return h.sel(a, select, b) * 3;
|
|
|
|
}
|
|
|
|
function getHelper() returns (address haddress) {
|
|
|
|
return address(h);
|
|
|
|
}
|
|
|
|
function setHelper(address haddress) {
|
|
|
|
h = Helper(haddress);
|
|
|
|
}
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Helper");
|
2015-01-09 20:36:25 +00:00
|
|
|
u160 const c_helperAddress = m_contractAddress;
|
2014-12-04 18:38:24 +00:00
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-09 20:36:25 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
|
|
|
|
BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
|
2014-12-04 18:38:24 +00:00
|
|
|
u256 a(3456789);
|
|
|
|
u256 b("0x282837623374623234aa74");
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == encodeArgs(a * 3));
|
|
|
|
BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == encodeArgs(b * 3));
|
2014-12-04 18:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
function getAddress() returns (address addr) {
|
|
|
|
return address(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function callHelper() returns (address addr) {
|
|
|
|
return h.getAddress();
|
|
|
|
}
|
|
|
|
function getHelper() returns (address addr) {
|
|
|
|
return address(h);
|
|
|
|
}
|
|
|
|
function setHelper(address addr) {
|
|
|
|
h = Helper(addr);
|
|
|
|
}
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Helper");
|
2015-01-09 20:36:25 +00:00
|
|
|
u160 const c_helperAddress = m_contractAddress;
|
2014-12-04 18:38:24 +00:00
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-09 20:36:25 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
|
|
|
|
BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
|
|
|
|
BOOST_REQUIRE(callContractFunction("callHelper()") == encodeArgs(c_helperAddress));
|
2014-12-04 18:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(calls_to_this)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
function invoke(uint a, uint b) returns (uint c) {
|
|
|
|
return this.multiply(a, b, 10);
|
|
|
|
}
|
|
|
|
function multiply(uint a, uint b, uint8 c) returns (uint ret) {
|
|
|
|
return a * b + c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function callHelper(uint a, uint b) returns (uint ret) {
|
|
|
|
return h.invoke(a, b);
|
|
|
|
}
|
|
|
|
function getHelper() returns (address addr) {
|
|
|
|
return address(h);
|
|
|
|
}
|
|
|
|
function setHelper(address addr) {
|
|
|
|
h = Helper(addr);
|
|
|
|
}
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Helper");
|
2015-01-09 20:36:25 +00:00
|
|
|
u160 const c_helperAddress = m_contractAddress;
|
2014-12-04 18:38:24 +00:00
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-09 20:36:25 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
|
|
|
|
BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
|
2014-12-04 18:38:24 +00:00
|
|
|
u256 a(3456789);
|
|
|
|
u256 b("0x282837623374623234aa74");
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 10));
|
2014-12-04 18:38:24 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 21:18:19 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars)
|
|
|
|
{
|
|
|
|
// note that a reference to another contract's function occupies two stack slots,
|
|
|
|
// so this tests correct stack slot allocation
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
function multiply(uint a, uint b) returns (uint c) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function callHelper(uint a, uint b) returns (uint c) {
|
|
|
|
var fu = h.multiply;
|
|
|
|
var y = 9;
|
|
|
|
var ret = fu(a, b);
|
|
|
|
return ret + y;
|
|
|
|
}
|
|
|
|
function getHelper() returns (address haddress) {
|
|
|
|
return address(h);
|
|
|
|
}
|
|
|
|
function setHelper(address haddress) {
|
|
|
|
h = Helper(haddress);
|
|
|
|
}
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Helper");
|
2015-01-09 20:36:25 +00:00
|
|
|
u160 const c_helperAddress = m_contractAddress;
|
2014-12-08 21:18:19 +00:00
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-09 20:36:25 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
|
|
|
|
BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
|
2014-12-08 21:18:19 +00:00
|
|
|
u256 a(3456789);
|
|
|
|
u256 b("0x282837623374623234aa74");
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == encodeArgs(a * b + 9));
|
2014-12-08 21:18:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:46:18 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(strings_in_calls)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
function invoke(string3 x, bool stop) returns (string4 ret) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function callHelper(string2 x, bool stop) returns (string5 ret) {
|
|
|
|
return h.invoke(x, stop);
|
|
|
|
}
|
|
|
|
function getHelper() returns (address addr) {
|
|
|
|
return address(h);
|
|
|
|
}
|
|
|
|
function setHelper(address addr) {
|
|
|
|
h = Helper(addr);
|
|
|
|
}
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Helper");
|
2015-01-09 20:36:25 +00:00
|
|
|
u160 const c_helperAddress = m_contractAddress;
|
2014-12-09 17:46:18 +00:00
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-09 20:36:25 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("setHelper(address)", c_helperAddress) == bytes());
|
|
|
|
BOOST_REQUIRE(callContractFunction("getHelper()", c_helperAddress) == encodeArgs(c_helperAddress));
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_CHECK(callContractFunction("callHelper(string2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5)));
|
2014-12-09 17:46:18 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 11:59:17 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(constructor_arguments)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Helper {
|
|
|
|
string3 name;
|
|
|
|
bool flag;
|
|
|
|
function Helper(string3 x, bool f) {
|
|
|
|
name = x;
|
|
|
|
flag = f;
|
|
|
|
}
|
|
|
|
function getName() returns (string3 ret) { return name; }
|
|
|
|
function getFlag() returns (bool ret) { return flag; }
|
|
|
|
}
|
|
|
|
contract Main {
|
|
|
|
Helper h;
|
|
|
|
function Main() {
|
|
|
|
h = new Helper("abc", true);
|
|
|
|
}
|
|
|
|
function getFlag() returns (bool ret) { return h.getFlag(); }
|
|
|
|
function getName() returns (string3 ret) { return h.getName(); }
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode, 0, "Main");
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true));
|
|
|
|
BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc"));
|
2014-12-15 11:59:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 21:57:39 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(functions_called_by_constructor)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract Test {
|
|
|
|
string3 name;
|
|
|
|
bool flag;
|
|
|
|
function Test() {
|
|
|
|
setName("abc");
|
|
|
|
}
|
|
|
|
function getName() returns (string3 ret) { return name; }
|
|
|
|
private:
|
|
|
|
function setName(string3 _name) { name = _name; }
|
|
|
|
})";
|
|
|
|
compileAndRun(sourceCode);
|
2015-01-08 23:58:32 +00:00
|
|
|
BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc"));
|
2014-12-15 21:57:39 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 21:54:56 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(contracts_as_addresses)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract helper {
|
|
|
|
}
|
|
|
|
contract test {
|
|
|
|
helper h;
|
|
|
|
function test() { h = new helper(); h.send(5); }
|
|
|
|
function getBalance() returns (uint256 myBalance, uint256 helperBalance) {
|
|
|
|
myBalance = this.balance;
|
|
|
|
helperBalance = h.balance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
compileAndRun(sourceCode, 20);
|
|
|
|
BOOST_REQUIRE(callContractFunction("getBalance()") == toBigEndian(u256(20 - 5)) + toBigEndian(u256(5)));
|
|
|
|
}
|
|
|
|
|
2014-10-31 16:47:43 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end namespaces
|
|
|
|
|