Extracting some Solidity end-to-end tests.

This commit is contained in:
Christian Parpart 2019-07-01 15:24:05 +02:00 committed by chriseth
parent b8dbf7d2a8
commit 62f6e4bad3
16 changed files with 202 additions and 277 deletions

View File

@ -75,32 +75,6 @@ BOOST_AUTO_TEST_CASE(exp_operator)
testContractAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return u256(1 << a.convert_to<int>()); }, 0, 16);
}
BOOST_AUTO_TEST_CASE(exp_operator_const)
{
char const* sourceCode = R"(
contract test {
function f() public returns(uint d) { return 2 ** 3; }
}
)";
ALSO_VIA_YUL(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(8)));
)
}
BOOST_AUTO_TEST_CASE(exp_operator_const_signed)
{
char const* sourceCode = R"(
contract test {
function f() public returns(int d) { return (-2) ** 3; }
}
)";
ALSO_VIA_YUL(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(-8)));
)
}
BOOST_AUTO_TEST_CASE(exp_zero)
{
char const* sourceCode = R"(
@ -112,162 +86,6 @@ BOOST_AUTO_TEST_CASE(exp_zero)
testContractAgainstCppOnRange("f(uint256)", [](u256 const&) -> u256 { return u256(1); }, 0, 16);
}
BOOST_AUTO_TEST_CASE(exp_zero_literal)
{
char const* sourceCode = R"(
contract test {
function f() public returns(uint d) { return 0 ** 0; }
}
)";
ALSO_VIA_YUL(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(1)));
)
}
BOOST_AUTO_TEST_CASE(conditional_expression_true_literal)
{
char const* sourceCode = R"(
contract test {
function f() public returns(uint d) {
return true ? 5 : 10;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(5)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_false_literal)
{
char const* sourceCode = R"(
contract test {
function f() public returns(uint d) {
return false ? 5 : 10;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()", bytes()), toBigEndian(u256(10)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_multiple)
{
char const* sourceCode = R"(
contract test {
function f(uint x) public returns(uint d) {
return x > 100 ?
x > 1000 ? 1000 : 100
:
x > 50 ? 50 : 10;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(uint256)", u256(1001)), toBigEndian(u256(1000)));
ABI_CHECK(callContractFunction("f(uint256)", u256(500)), toBigEndian(u256(100)));
ABI_CHECK(callContractFunction("f(uint256)", u256(80)), toBigEndian(u256(50)));
ABI_CHECK(callContractFunction("f(uint256)", u256(40)), toBigEndian(u256(10)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_with_return_values)
{
char const* sourceCode = R"(
contract test {
function f(bool cond, uint v) public returns (uint a, uint b) {
cond ? a = v : b = v;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool,uint256)", true, u256(20)), encodeArgs(u256(20), u256(0)));
ABI_CHECK(callContractFunction("f(bool,uint256)", false, u256(20)), encodeArgs(u256(0), u256(20)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_storage_memory_1)
{
char const* sourceCode = R"(
contract test {
bytes2[2] data1;
function f(bool cond) public returns (uint) {
bytes2[2] memory x;
x[0] = "aa";
bytes2[2] memory y;
y[0] = "bb";
data1 = cond ? x : y;
uint ret = 0;
if (data1[0] == "aa")
{
ret = 1;
}
if (data1[0] == "bb")
{
ret = 2;
}
return ret;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1)));
ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(2)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_storage_memory_2)
{
char const* sourceCode = R"(
contract test {
bytes2[2] data1;
function f(bool cond) public returns (uint) {
data1[0] = "cc";
bytes2[2] memory x;
bytes2[2] memory y;
y[0] = "bb";
x = cond ? y : data1;
uint ret = 0;
if (x[0] == "bb")
{
ret = 1;
}
if (x[0] == "cc")
{
ret = 2;
}
return ret;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1)));
ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(2)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_different_types)
{
char const* sourceCode = R"(
contract test {
function f(bool cond) public returns (uint) {
uint8 x = 0xcd;
uint16 y = 0xabab;
return cond ? x : y;
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(0xcd)));
ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(0xabab)));
}
/* let's add this back when I figure out the correct type conversion.
BOOST_AUTO_TEST_CASE(conditional_expression_string_literal)
{
@ -284,87 +102,6 @@ BOOST_AUTO_TEST_CASE(conditional_expression_string_literal)
}
*/
BOOST_AUTO_TEST_CASE(conditional_expression_tuples)
{
char const* sourceCode = R"(
contract test {
function f(bool cond) public returns (uint, uint) {
return cond ? (1, 2) : (3, 4);
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1), u256(2)));
ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(3), u256(4)));
}
BOOST_AUTO_TEST_CASE(conditional_expression_functions)
{
char const* sourceCode = R"(
contract test {
function x() public returns (uint) { return 1; }
function y() public returns (uint) { return 2; }
function f(bool cond) public returns (uint) {
function () returns (uint) z = cond ? x : y;
return z();
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(bool)", true), encodeArgs(u256(1)));
ABI_CHECK(callContractFunction("f(bool)", false), encodeArgs(u256(2)));
}
BOOST_AUTO_TEST_CASE(c99_scoping_activation)
{
char const* sourceCode = R"(
contract test {
function f() pure public returns (uint) {
uint x = 7;
{
x = 3; // This should still assign to the outer variable
uint x;
x = 4; // This should assign to the new one
}
return x;
}
function g() pure public returns (uint x) {
x = 7;
{
x = 3;
uint x;
return x; // This returns the new variable, i.e. 0
}
}
function h() pure public returns (uint x, uint a, uint b) {
x = 7;
{
x = 3;
a = x; // This should read from the outer
uint x = 4;
b = x;
}
}
function i() pure public returns (uint x, uint a) {
x = 7;
{
x = 3;
uint x = x; // This should read from the outer and assign to the inner
a = x;
}
}
}
)";
ALSO_VIA_YUL(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()"), encodeArgs(3));
ABI_CHECK(callContractFunction("g()"), encodeArgs(0));
ABI_CHECK(callContractFunction("h()"), encodeArgs(3, 3, 4));
ABI_CHECK(callContractFunction("i()"), encodeArgs(3, 3));
)
}
BOOST_AUTO_TEST_CASE(recursive_calls)
{
char const* sourceCode = R"(
@ -409,20 +146,6 @@ BOOST_AUTO_TEST_CASE(multiple_functions)
)
}
BOOST_AUTO_TEST_CASE(named_args)
{
char const* sourceCode = R"(
contract test {
function a(uint a, uint b, uint c) public returns (uint r) { r = a * 100 + b * 10 + c * 1; }
function b() public returns (uint r) { r = a({a: 1, b: 2, c: 3}); }
}
)";
ALSO_VIA_YUL(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("b()", bytes()), toBigEndian(u256(123)));
)
}
BOOST_AUTO_TEST_CASE(disorder_named_args)
{
char const* sourceCode = R"(

View File

@ -0,0 +1,43 @@
contract test {
function f() pure public returns (uint) {
uint x = 7;
{
x = 3; // This should still assign to the outer variable
uint x;
x = 4; // This should assign to the new one
}
return x;
}
function g() pure public returns (uint x) {
x = 7;
{
x = 3;
uint x;
return x; // This returns the new variable, i.e. 0
}
}
function h() pure public returns (uint x, uint a, uint b) {
x = 7;
{
x = 3;
a = x; // This should read from the outer
uint x = 4;
b = x;
}
}
function i() pure public returns (uint x, uint a) {
x = 7;
{
x = 3;
uint x = x; // This should read from the outer and assign to the inner
a = x;
}
}
}
// ====
// compileViaYul: also
// ----
// f() -> 3
// g() -> 0
// h() -> 3, 3, 4
// i() -> 3, 3

View File

@ -0,0 +1,10 @@
contract test {
function f(bool cond) public returns (uint) {
uint8 x = 0xcd;
uint16 y = 0xabab;
return cond ? x : y;
}
}
// ----
// f(bool): true -> 0xcd
// f(bool): false -> 0xabab

View File

@ -0,0 +1,7 @@
contract test {
function f() public returns(uint d) {
return false ? 5 : 10;
}
}
// ----
// f() -> 10

View File

@ -0,0 +1,12 @@
contract test {
function x() public returns (uint) { return 1; }
function y() public returns (uint) { return 2; }
function f(bool cond) public returns (uint) {
function () returns (uint) z = cond ? x : y;
return z();
}
}
// ----
// f(bool): true -> 1
// f(bool): false -> 2

View File

@ -0,0 +1,13 @@
contract test {
function f(uint x) public returns(uint d) {
return x > 100 ?
x > 1000 ? 1000 : 100
:
x > 50 ? 50 : 10;
}
}
// ----
// f(uint256): 1001 -> 1000
// f(uint256): 500 -> 100
// f(uint256): 80 -> 50
// f(uint256): 40 -> 10

View File

@ -0,0 +1,27 @@
contract test {
bytes2[2] data1;
function f(bool cond) public returns (uint) {
bytes2[2] memory x;
x[0] = "aa";
bytes2[2] memory y;
y[0] = "bb";
data1 = cond ? x : y;
uint ret = 0;
if (data1[0] == "aa")
{
ret = 1;
}
if (data1[0] == "bb")
{
ret = 2;
}
return ret;
}
}
// ----
// f(bool): true -> 1
// f(bool): false -> 2

View File

@ -0,0 +1,28 @@
contract test {
bytes2[2] data1;
function f(bool cond) public returns (uint) {
data1[0] = "cc";
bytes2[2] memory x;
bytes2[2] memory y;
y[0] = "bb";
x = cond ? y : data1;
uint ret = 0;
if (x[0] == "bb")
{
ret = 1;
}
if (x[0] == "cc")
{
ret = 2;
}
return ret;
}
}
// ----
// f(bool): true -> 1
// f(bool): false -> 2

View File

@ -0,0 +1,7 @@
contract test {
function f() public returns(uint d) {
return true ? 5 : 10;
}
}
// ----
// f() -> 5

View File

@ -0,0 +1,8 @@
contract test {
function f(bool cond) public returns (uint, uint) {
return cond ? (1, 2) : (3, 4);
}
}
// ----
// f(bool): true -> 1, 2
// f(bool): false -> 3, 4

View File

@ -0,0 +1,8 @@
contract test {
function f(bool cond, uint v) public returns (uint a, uint b) {
cond ? a = v : b = v;
}
}
// ----
// f(bool,uint256): true, 20 -> 20, 0
// f(bool,uint256): false, 20 -> 0, 20

View File

@ -0,0 +1,7 @@
contract test {
function f() public returns(uint d) { return 2 ** 3; }
}
// ====
// compileViaYul: also
// ----
// f() -> 8

View File

@ -0,0 +1,7 @@
contract test {
function f() public returns(int d) { return (-2) ** 3; }
}
// ====
// compileViaYul: also
// ----
// f() -> -8

View File

@ -0,0 +1,7 @@
contract test {
function f() public returns(uint d) { return 0 ** 0; }
}
// ====
// compileViaYul: also
// ----
// f() -> 1

View File

@ -0,0 +1,12 @@
contract test {
function a() public returns(uint n) { return 0; }
function b() public returns(uint n) { return 1; }
function c() public returns(uint n) { return 2; }
function f() public returns(uint n) { return 3; }
}
// ----
// a() -> 0
// b() -> 1
// c() -> 2
// f() -> 3
// i_am_not_there() -> FAILURE

View File

@ -0,0 +1,6 @@
contract test {
function a(uint a, uint b, uint c) public returns (uint r) { r = a * 100 + b * 10 + c * 1; }
function b() public returns (uint r) { r = a({a: 1, b: 2, c: 3}); }
}
// ----
// b() -> 123