Merge pull request #10185 from ethereum/refatorMoveTests

Move some tests to semanticTests
This commit is contained in:
chriseth 2020-11-03 13:50:30 +01:00 committed by GitHub
commit 9305ab987e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
55 changed files with 239 additions and 384 deletions

View File

@ -73,28 +73,6 @@ BOOST_AUTO_TEST_CASE(value_types)
)
}
BOOST_AUTO_TEST_CASE(enums)
{
string sourceCode = R"(
contract C {
enum E { A, B }
function f(E e) public pure returns (uint x) {
assembly { x := e }
}
}
)";
bool newDecoder = solidity::test::CommonOptions::get().useABIEncoderV2;
BOTH_ENCODERS(
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f(uint8)", 0), encodeArgs(u256(0)));
ABI_CHECK(callContractFunction("f(uint8)", 1), encodeArgs(u256(1)));
// The old decoder was not as strict about enums
ABI_CHECK(callContractFunction("f(uint8)", 2), (newDecoder ? encodeArgs() : encodeArgs(2)));
ABI_CHECK(callContractFunction("f(uint8)", u256(-1)), (newDecoder? encodeArgs() : encodeArgs(u256(0xff))));
newDecoder = true;
)
}
BOOST_AUTO_TEST_CASE(cleanup)
{
string sourceCode = R"(
@ -184,113 +162,6 @@ BOOST_AUTO_TEST_CASE(fixed_arrays)
)
}
BOOST_AUTO_TEST_CASE(dynamic_arrays)
{
string sourceCode = R"(
contract C {
function f(uint a, uint16[] memory b, uint c)
public pure returns (uint, uint, uint) {
return (b.length, b[a], c);
}
}
)";
BOTH_ENCODERS(
compileAndRun(sourceCode);
bytes args = encodeArgs(
6, 0x60, 9,
7,
11, 12, 13, 14, 15, 16, 17
);
ABI_CHECK(
callContractFunction("f(uint256,uint16[],uint256)", args),
encodeArgs(u256(7), u256(17), u256(9))
);
)
}
BOOST_AUTO_TEST_CASE(dynamic_nested_arrays)
{
string sourceCode = R"(
contract C {
function f(uint a, uint16[][] memory b, uint[2][][3] memory c, uint d)
public pure returns (uint, uint, uint, uint, uint, uint, uint) {
return (a, b.length, b[1].length, b[1][1], c[1].length, c[1][1][1], d);
}
function test() public view returns (uint, uint, uint, uint, uint, uint, uint) {
uint16[][] memory b = new uint16[][](3);
b[0] = new uint16[](2);
b[0][0] = 0x55;
b[0][1] = 0x56;
b[1] = new uint16[](4);
b[1][0] = 0x65;
b[1][1] = 0x66;
b[1][2] = 0x67;
b[1][3] = 0x68;
uint[2][][3] memory c;
c[0] = new uint[2][](1);
c[0][0][1] = 0x75;
c[1] = new uint[2][](5);
c[1][1][1] = 0x85;
return this.f(0x12, b, c, 0x13);
}
}
)";
NEW_ENCODER(
compileAndRun(sourceCode);
bytes args = encodeArgs(
0x12, 4 * 0x20, 17 * 0x20, 0x13,
// b
3, 3 * 0x20, 6 * 0x20, 11 * 0x20,
2, 85, 86,
4, 101, 102, 103, 104,
0,
// c
3 * 0x20, 6 * 0x20, 17 * 0x20,
1, 0, 117,
5, 0, 0, 0, 133, 0, 0, 0, 0, 0, 0,
0
);
bytes expectation = encodeArgs(0x12, 3, 4, 0x66, 5, 0x85, 0x13);
ABI_CHECK(callContractFunction("test()"), expectation);
ABI_CHECK(callContractFunction("f(uint256,uint16[][],uint256[2][][3],uint256)", args), expectation);
)
}
BOOST_AUTO_TEST_CASE(byte_arrays)
{
string sourceCode = R"(
contract C {
function f(uint a, bytes memory b, uint c)
public pure returns (uint, uint, byte, uint) {
return (a, b.length, b[3], c);
}
function f_external(uint a, bytes calldata b, uint c)
external pure returns (uint, uint, byte, uint) {
return (a, b.length, b[3], c);
}
}
)";
BOTH_ENCODERS(
compileAndRun(sourceCode);
bytes args = encodeArgs(
6, 0x60, 9,
7, "abcdefg"
);
ABI_CHECK(
callContractFunction("f(uint256,bytes,uint256)", args),
encodeArgs(u256(6), u256(7), "d", 9)
);
ABI_CHECK(
callContractFunction("f_external(uint256,bytes,uint256)", args),
encodeArgs(u256(6), u256(7), "d", 9)
);
)
}
BOOST_AUTO_TEST_CASE(calldata_arrays_too_large)
{
string sourceCode = R"(

View File

@ -536,39 +536,6 @@ BOOST_AUTO_TEST_CASE(for_loop)
)
}
BOOST_AUTO_TEST_CASE(for_loop_empty)
{
char const* sourceCode = R"(
contract test {
function f() public returns(uint ret) {
ret = 1;
for (;;) {
ret += 1;
if (ret >= 10) break;
}
}
}
)";
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
auto for_loop_empty_cpp = []() -> u256
{
u256 ret = 1;
for (;;)
{
ret += 1;
if (ret >= 10) break;
}
return ret;
};
testContractAgainstCpp("f()", for_loop_empty_cpp);
)
}
BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr)
{
char const* sourceCode = R"(
@ -648,85 +615,6 @@ BOOST_AUTO_TEST_CASE(for_loop_break_continue)
);
}
BOOST_AUTO_TEST_CASE(calling_other_functions)
{
char const* sourceCode = R"(
contract collatz {
function run(uint x) public returns(uint y) {
while ((y = x) > 1) {
if (x % 2 == 0) x = evenStep(x);
else x = oddStep(x);
}
}
function evenStep(uint x) public returns(uint y) {
return x / 2;
}
function oddStep(uint x) public returns(uint y) {
return 3 * x + 1;
}
}
)";
auto evenStep_cpp = [](u256 const& n) -> u256
{
return n / 2;
};
auto oddStep_cpp = [](u256 const& n) -> u256
{
return 3 * n + 1;
};
auto collatz_cpp = [&evenStep_cpp, &oddStep_cpp](u256 n) -> u256
{
u256 y;
while ((y = n) > 1)
{
if (n % 2 == 0)
n = evenStep_cpp(n);
else
n = oddStep_cpp(n);
}
return y;
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("run(uint256)", collatz_cpp, u256(0));
testContractAgainstCpp("run(uint256)", collatz_cpp, u256(1));
testContractAgainstCpp("run(uint256)", collatz_cpp, u256(2));
testContractAgainstCpp("run(uint256)", collatz_cpp, u256(8));
testContractAgainstCpp("run(uint256)", collatz_cpp, u256(127));
)
}
BOOST_AUTO_TEST_CASE(many_local_variables)
{
char const* sourceCode = R"(
contract test {
function run(uint x1, uint x2, uint x3) public returns(uint y) {
uint8 a = 0x1; uint8 b = 0x10; uint16 c = 0x100;
y = a + b + c + x1 + x2 + x3;
y += b + x2;
}
}
)";
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;
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
testContractAgainstCpp("run(uint256,uint256,uint256)", f, u256(0x1000), u256(0x10000), u256(0x100000));
)
}
BOOST_AUTO_TEST_CASE(short_circuiting)
{
char const* sourceCode = R"(
@ -819,149 +707,6 @@ BOOST_AUTO_TEST_CASE(small_unsigned_types)
testContractAgainstCpp("run()", small_unsigned_types_cpp);
}
BOOST_AUTO_TEST_CASE(small_signed_types)
{
char const* sourceCode = R"(
contract test {
function run() public returns(int256 y) {
return -int32(10) * -int64(20);
}
}
)";
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
auto small_signed_types_cpp = []() -> u256
{
return -int32_t(10) * -int64_t(20);
};
testContractAgainstCpp("run()", small_signed_types_cpp);
);
}
BOOST_AUTO_TEST_CASE(compound_assign)
{
char const* sourceCode = R"(
contract test {
uint value1;
uint value2;
function f(uint x, uint y) public returns (uint w) {
uint value3 = y;
value1 += x;
value3 *= x;
value2 *= value3 + value1;
return value2 += 7;
}
}
)";
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
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;
};
testContractAgainstCpp("f(uint256,uint256)", f, u256(0), u256(6));
testContractAgainstCpp("f(uint256,uint256)", f, u256(1), u256(3));
testContractAgainstCpp("f(uint256,uint256)", f, u256(2), u256(25));
testContractAgainstCpp("f(uint256,uint256)", f, u256(3), u256(69));
testContractAgainstCpp("f(uint256,uint256)", f, u256(4), u256(84));
testContractAgainstCpp("f(uint256,uint256)", f, u256(5), u256(2));
testContractAgainstCpp("f(uint256,uint256)", f, u256(6), u256(51));
testContractAgainstCpp("f(uint256,uint256)", f, u256(7), u256(48));
)
}
BOOST_AUTO_TEST_CASE(mapping_state)
{
char const* sourceCode = R"(
contract Ballot {
mapping(address => bool) canVote;
mapping(address => uint) voteCount;
mapping(address => bool) voted;
function getVoteCount(address addr) public returns (uint retVoteCount) {
return voteCount[addr];
}
function grantVoteRight(address addr) public {
canVote[addr] = true;
}
function vote(address voter, address vote) public returns (bool success) {
if (!canVote[voter] || voted[voter]) return false;
voted[voter] = true;
voteCount[vote] = voteCount[vote] + 1;
return true;
}
}
)";
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;
};
ALSO_VIA_YUL(
DISABLE_EWASM_TESTRUN()
compileAndRun(sourceCode);
Ballot ballot;
auto getVoteCount = bind(&Ballot::getVoteCount, &ballot, _1);
auto grantVoteRight = bind(&Ballot::grantVoteRight, &ballot, _1);
auto vote = bind(&Ballot::vote, &ballot, _1, _2);
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// voting without vote right should be rejected
testContractAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// grant vote rights
testContractAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(0));
testContractAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(1));
// vote, should increase 2's vote count
testContractAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// vote again, should be rejected
testContractAgainstCpp("vote(address,address)", vote, u160(0), u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// vote without right to vote
testContractAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// grant vote right and now vote again
testContractAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(2));
testContractAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testContractAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
)
}
BOOST_AUTO_TEST_CASE(mapping_state_inc_dec)
{
char const* sourceCode = R"(

View File

@ -0,0 +1,16 @@
contract C {
function f(uint a, bytes memory b, uint c)
public pure returns (uint, uint, byte, uint) {
return (a, b.length, b[3], c);
}
function f_external(uint a, bytes calldata b, uint c)
external pure returns (uint, uint, byte, uint) {
return (a, b.length, b[3], c);
}
}
// ====
// compileViaYul: also
// ----
// f(uint256,bytes,uint256): 6, 0x60, 9, 7, "abcdefg" -> 6, 7, "d", 9
// f_external(uint256,bytes,uint256): 6, 0x60, 9, 7, "abcdefg" -> 6, 7, "d", 9

View File

@ -0,0 +1,10 @@
contract C {
function f(uint a, uint16[] memory b, uint c)
public pure returns (uint, uint, uint) {
return (b.length, b[a], c);
}
}
// ====
// compileViaYul: also
// ----
// f(uint256,uint16[],uint256): 6, 0x60, 9, 7, 11, 12, 13, 14, 15, 16, 17 -> 7, 17, 9

View File

@ -0,0 +1,13 @@
contract C {
enum E { A, B }
function f(E e) public pure returns (uint x) {
assembly { x := e }
}
}
// ====
// ABIEncoderV1Only: true
// ----
// f(uint8): 0 -> 0
// f(uint8): 1 -> 1
// f(uint8): 2 -> 2
// f(uint8): 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff -> 0xff

View File

@ -0,0 +1,18 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint a, bytes memory b, uint c)
public pure returns (uint, uint, byte, uint) {
return (a, b.length, b[3], c);
}
function f_external(uint a, bytes calldata b, uint c)
external pure returns (uint, uint, byte, uint) {
return (a, b.length, b[3], c);
}
}
// ====
// compileViaYul: also
// ----
// f(uint256,bytes,uint256): 6, 0x60, 9, 7, "abcdefg" -> 6, 7, "d", 9
// f_external(uint256,bytes,uint256): 6, 0x60, 9, 7, "abcdefg" -> 6, 7, "d", 9

View File

@ -0,0 +1,12 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint a, uint16[] memory b, uint c)
public pure returns (uint, uint, uint) {
return (b.length, b[a], c);
}
}
// ====
// compileViaYul: also
// ----
// f(uint256,uint16[],uint256): 6, 0x60, 9, 7, 11, 12, 13, 14, 15, 16, 17 -> 7, 17, 9

View File

@ -0,0 +1,32 @@
pragma experimental ABIEncoderV2;
contract C {
function f(uint a, uint16[][] memory b, uint[2][][3] memory c, uint d)
public pure returns (uint, uint, uint, uint, uint, uint, uint) {
return (a, b.length, b[1].length, b[1][1], c[1].length, c[1][1][1], d);
}
function test() public view returns (uint, uint, uint, uint, uint, uint, uint) {
uint16[][] memory b = new uint16[][](3);
b[0] = new uint16[](2);
b[0][0] = 0x55;
b[0][1] = 0x56;
b[1] = new uint16[](4);
b[1][0] = 0x65;
b[1][1] = 0x66;
b[1][2] = 0x67;
b[1][3] = 0x68;
uint[2][][3] memory c;
c[0] = new uint[2][](1);
c[0][0][1] = 0x75;
c[1] = new uint[2][](5);
c[1][1][1] = 0x85;
return this.f(12, b, c, 13);
}
}
// ====
// compileViaYul: also
// ----
// test() -> 12, 3, 4, 0x66, 5, 0x85, 13
// f(uint256,uint16[][],uint256[2][][3],uint256): 12, 0x80, 0x220, 13, 3, 0x60, 0xC0, 0x160, 2, 85, 86, 4, 101, 102, 103, 104, 0, 0x60, 0xC0, 0x220, 1, 0, 117, 5, 0, 0, 0, 133, 0, 0, 0, 0, 0, 0, 0 -> 12, 3, 4, 0x66, 5, 0x85, 13

View File

@ -0,0 +1,15 @@
pragma experimental ABIEncoderV2;
contract C {
enum E { A, B }
function f(E e) public pure returns (uint x) {
assembly { x := e }
}
}
// ====
// compileViaYul: also
// ----
// f(uint8): 0 -> 0
// f(uint8): 1 -> 1
// f(uint8): 2 -> FAILURE
// f(uint8): 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff -> FAILURE

View File

@ -0,0 +1,13 @@
contract test {
function f() public returns(uint ret) {
ret = 1;
for (;;) {
ret += 1;
if (ret >= 10) break;
}
}
}
// ====
// compileViaYul: also
// ----
// f() -> 10

View File

@ -0,0 +1,22 @@
contract collatz {
function run(uint x) public returns(uint y) {
while ((y = x) > 1) {
if (x % 2 == 0) x = evenStep(x);
else x = oddStep(x);
}
}
function evenStep(uint x) public returns(uint y) {
return x / 2;
}
function oddStep(uint x) public returns(uint y) {
return 3 * x + 1;
}
}
// ====
// compileViaYul: also
// ----
// run(uint256): 0 -> 0
// run(uint256): 1 -> 1
// run(uint256): 2 -> 1
// run(uint256): 8 -> 1
// run(uint256): 127 -> 1

View File

@ -0,0 +1,11 @@
contract test {
function run(uint x1, uint x2, uint x3) public returns(uint y) {
uint8 a = 0x1; uint8 b = 0x10; uint16 c = 0x100;
y = a + b + c + x1 + x2 + x3;
y += b + x2;
}
}
// ====
// compileViaYul: also
// ----
// run(uint256,uint256,uint256): 0x1000, 0x10000, 0x100000 -> 0x121121

View File

@ -0,0 +1,9 @@
contract test {
function run() public returns(int256 y) {
return -int32(10) * -int64(20);
}
}
// ====
// compileViaYul: also
// ----
// run() -> 200

View File

@ -0,0 +1,22 @@
contract test {
uint value1;
uint value2;
function f(uint x, uint y) public returns (uint w) {
uint value3 = y;
value1 += x;
value3 *= x;
value2 *= value3 + value1;
return value2 += 7;
}
}
// ====
// compileViaYul: also
// ----
// f(uint256,uint256): 0, 6 -> 7
// f(uint256,uint256): 1, 3 -> 0x23
// f(uint256,uint256): 2, 25 -> 0x0746
// f(uint256,uint256): 3, 69 -> 396613
// f(uint256,uint256): 4, 84 -> 137228105
// f(uint256,uint256): 5, 2 -> 0xcc7c5e28
// f(uint256,uint256): 6, 51 -> 1121839760671
// f(uint256,uint256): 7, 48 -> 408349672884251

View File

@ -0,0 +1,46 @@
contract Ballot {
mapping(address => bool) canVote;
mapping(address => uint) voteCount;
mapping(address => bool) voted;
function getVoteCount(address addr) public returns (uint retVoteCount) {
return voteCount[addr];
}
function grantVoteRight(address addr) public {
canVote[addr] = true;
}
function vote(address voter, address vote) public returns (bool success) {
if (!canVote[voter] || voted[voter]) return false;
voted[voter] = true;
voteCount[vote] = voteCount[vote] + 1;
return true;
}
}
// ====
// compileViaYul: also
// ----
// getVoteCount(address): 0 -> 0
// getVoteCount(address): 1 -> 0
// getVoteCount(address): 2 -> 0
// vote(address,address): 0, 2 -> false
// getVoteCount(address): 0 -> 0
// getVoteCount(address): 1 -> 0
// getVoteCount(address): 2 -> 0
// grantVoteRight(address): 0 ->
// grantVoteRight(address): 1 ->
// vote(address,address): 0, 2 -> true
// getVoteCount(address): 0 -> 0
// getVoteCount(address): 1 -> 0
// getVoteCount(address): 2 -> 1
// vote(address,address): 0, 1 -> false
// getVoteCount(address): 0 -> 0
// getVoteCount(address): 1 -> 0
// getVoteCount(address): 2 -> 1
// vote(address,address): 2, 1 -> false
// getVoteCount(address): 0 -> 0
// getVoteCount(address): 1 -> 0
// getVoteCount(address): 2 -> 1
// grantVoteRight(address): 2 ->
// vote(address,address): 2, 1 -> true
// getVoteCount(address): 0 -> 0
// getVoteCount(address): 1 -> 1
// getVoteCount(address): 2 -> 1