Sort tests.

This commit is contained in:
chriseth committed 2020-03-19 14:42:25 +01:00
1 parent 8834b1acaf
commit f25157a5f8
295 files changed

No files matched your search

@@ -0,0 +1,10 @@
contract C {
uint256 constant x = 0x123 + 0x456;
function f() public returns (uint256) {
return x + 1;
}
}
// ----
// f() -> 0x57a
@@ -0,0 +1,13 @@
contract test {
constructor() public payable {}
function getBalance() public returns (uint256 balance) {
return address(this).balance;
}
}
// ====
// compileViaYul: also
// ----
// constructor(), 23 wei ->
// getBalance() -> 23
@@ -0,0 +1,19 @@
contract C {
function f(uint256 x) public returns (uint256 a) {
assembly {
a := byte(x, 31)
}
}
function g(uint256 x) public returns (uint256 a) {
assembly {
a := byte(31, x)
}
}
}
// ====
// compileViaYul: also
// ----
// f(uint256): 2 -> 0
// g(uint256): 2 -> 2
@@ -0,0 +1,42 @@
contract D {
bytes32 public x;
constructor() public {
bytes32 codeHash;
assembly {
let size := codesize()
codecopy(mload(0x40), 0, size)
codeHash := keccak256(mload(0x40), size)
}
x = codeHash;
}
}
contract C {
function testRuntime() public returns (bool) {
D d = new D();
bytes32 runtimeHash = keccak256(type(D).runtimeCode);
bytes32 otherHash;
uint256 size;
assembly {
size := extcodesize(d)
extcodecopy(d, mload(0x40), 0, size)
otherHash := keccak256(mload(0x40), size)
}
require(size == type(D).runtimeCode.length);
require(runtimeHash == otherHash);
return true;
}
function testCreation() public returns (bool) {
D d = new D();
bytes32 creationHash = keccak256(type(D).creationCode);
require(creationHash == d.x());
return true;
}
}
// ----
// testRuntime() -> true
// testCreation() -> true
@@ -0,0 +1,26 @@
contract D {
uint256 x;
constructor() public {
x = 7;
}
function f() public view returns (uint256) {
return x;
}
}
contract C {
function test() public returns (uint256) {
bytes memory c = type(D).creationCode;
D d;
assembly {
d := create(0, add(c, 0x20), mload(c))
}
return d.f();
}
}
// ----
// test() -> 7
@@ -0,0 +1,19 @@
contract D {
function f() public pure returns (uint256) {
return 7;
}
}
contract C {
function diff() public pure returns (uint256 remainder) {
bytes memory a = type(D).creationCode;
bytes memory b = type(D).runtimeCode;
assembly {
remainder := mod(sub(b, a), 0x20)
}
}
}
// ----
// diff() -> 0 # This checks that the allocation function pads to multiples of 32 bytes #
@@ -0,0 +1,20 @@
contract A {
function f() public {
new B();
}
}
contract B {
function f() public {}
}
contract C {
function f() public {
new B();
}
}
// ----
// constructor() ->
@@ -0,0 +1,15 @@
contract C {
function f() public returns (uint256 r) {
uint256;
uint256;
uint256;
uint256;
int256 x = -7;
return uint256(x);
}
}
// ====
// compileViaYul: also
// ----
// f() -> -7
@@ -0,0 +1,17 @@
contract Lib {
struct S {
uint256 a;
uint256 b;
}
}
contract Test {
function f() public returns (uint256 r) {
Lib.S memory x = Lib.S({a: 2, b: 3});
r = x.b;
}
}
// ----
// f() -> 3
@@ -0,0 +1,10 @@
contract C {
function f() public returns (uint256) {
uint256 x = 1;
(x) = 2;
return x;
}
}
// ----
// f() -> 2
@@ -0,0 +1,36 @@
contract C {
uint256 x = 7;
bytes data;
uint256[] y;
uint256[] arrayData;
function returnsArray() public returns (uint256[] memory) {
arrayData = new uint256[](9);
arrayData[2] = 5;
arrayData[7] = 4;
return arrayData;
}
function f(bytes memory s) public returns (uint256) {
uint256 loc;
uint256[] memory memArray;
(loc, x, y, data, arrayData[3]) = (8, 4, returnsArray(), s, 2);
if (loc != 8) return 1;
if (x != 4) return 2;
if (y.length != 9) return 3;
if (y[2] != 5) return 4;
if (y[7] != 4) return 5;
if (data.length != s.length) return 6;
if (data[3] != s[3]) return 7;
if (arrayData[3] != 2) return 8;
(memArray, loc) = (arrayData, 3);
if (loc != 3) return 9;
if (memArray.length != arrayData.length) return 10;
bytes memory memBytes;
(x, memBytes, y[2], , ) = (456, s, 789, 101112, 131415);
if (x != 456 || memBytes.length != s.length || y[2] != 789) return 11;
}
}
// ----
// f(bytes): 0x20, 0x5, "abcde" -> 0
@@ -0,0 +1,10 @@
contract test {
function f(uint256 k) public returns (uint256) {
return k;
}
}
// ====
// compileViaYul: also
// ----
// f(uint256): 9 -> 9
@@ -0,0 +1,28 @@
contract C1 {
C1 public bla;
constructor(C1 x) public {
bla = x;
}
}
contract C {
function test() public returns (C1 x, C1 y) {
C1 c = new C1(C1(9));
x = c.bla();
y = this.t1(C1(7));
}
function t1(C1 a) public returns (C1) {
return a;
}
function t2() public returns (C1) {
return C1(9);
}
}
// ----
// test() -> 9, 7
// t2() -> 9
@@ -0,0 +1,10 @@
contract test {
function f() public returns (bool) {
int256 x = -2**255;
assert(-x == x);
return true;
}
}
// ----
// f() -> true
@@ -0,0 +1,20 @@
contract C {
uint256 v;
function f() public returns (bool) {
uint256 startGas = gasleft();
v++;
assert(startGas > gasleft());
return true;
}
function g() public returns (bool) {
uint256 startGas = gasleft();
assert(startGas > gasleft());
return true;
}
}
// ----
// f() -> true
// g() -> true
@@ -0,0 +1,14 @@
contract C {
function gasleft() public returns (uint256) {
return 0;
}
function f() public returns (uint256) {
return gasleft();
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0
@@ -0,0 +1,19 @@
contract test {
constructor() public {
m_b = 6;
m_c = 8;
}
uint256 m_a = 5;
uint256 m_b;
uint256 m_c = 7;
function get() public returns (uint256 a, uint256 b, uint256 c) {
a = m_a;
b = m_b;
c = m_c;
}
}
// ----
// get() -> 5, 6, 8
@@ -0,0 +1,24 @@
contract Base {
constructor() public {}
uint256 m_base = 5;
function getBMember() public returns (uint256 i) {
return m_base;
}
}
contract Derived is Base {
constructor() public {}
uint256 m_derived = 6;
function getDMember() public returns (uint256 i) {
return m_derived;
}
}
// ----
// getBMember() -> 5
// getDMember() -> 6
@@ -0,0 +1,9 @@
contract c {
function f() public returns (int8) {
int8[5] memory foo3 = [int8(1), -1, 0, 0, 0];
return foo3[0];
}
}
// ----
// f() -> 1
@@ -0,0 +1,19 @@
// A long time ago, some opcodes were renamed, which involved the opcodes
// "iszero" and "not".
contract C {
function f() public returns (bool) {
bytes32 x = bytes32(uint256(1));
assembly {
x := not(x)
}
if (x != ~bytes32(uint256(1))) return false;
assembly {
x := iszero(x)
}
if (x != bytes32(0)) return false;
return true;
}
}
// ----
// f() -> true
@@ -0,0 +1,20 @@
contract C {
bytes32 public x;
uint256 public a;
function f(bytes32 _x, uint256 _a) public {
x = _x;
a = _a;
}
function g() public {
this.f("", 2);
}
}
// ----
// x() -> 0
// a() -> 0
// g() ->
// x() -> 0
// a() -> 2
@@ -0,0 +1,10 @@
contract C {
function f() public returns (bytes memory x) {
x = "12345";
x[3] = 0x61;
x[0] = 0x62;
}
}
// ----
// f() -> 0x20, 5, "b23a5"
@@ -0,0 +1,47 @@
contract C {
function g() public returns (uint256 a, uint256 b, uint256 c) {
a = 1;
b = 2;
c = 3;
}
function h() public returns (uint256 a, uint256 b, uint256 c, uint256 d) {
a = 1;
b = 2;
c = 3;
d = 4;
}
function f1() public returns (bool) {
(uint256 x, uint256 y, uint256 z) = g();
if (x != 1 || y != 2 || z != 3) return false;
(, uint256 a, ) = g();
if (a != 2) return false;
(uint256 b, , ) = g();
if (b != 1) return false;
(, , uint256 c) = g();
if (c != 3) return false;
return true;
}
function f2() public returns (bool) {
(uint256 a1, , uint256 a3, ) = h();
if (a1 != 1 || a3 != 3) return false;
(uint256 b1, uint256 b2, , ) = h();
if (b1 != 1 || b2 != 2) return false;
(, uint256 c2, uint256 c3, ) = h();
if (c2 != 2 || c3 != 3) return false;
(, , uint256 d3, uint256 d4) = h();
if (d3 != 3 || d4 != 4) return false;
(uint256 e1, , uint256 e3, uint256 e4) = h();
if (e1 != 1 || e3 != 3 || e4 != 4) return false;
return true;
}
function f() public returns (bool) {
return f1() && f2();
}
}
// ----
// f() -> true
@@ -0,0 +1,63 @@
contract C {
mapping(uint256 => Invoice) public invoices;
struct Invoice {
uint256 AID;
bool Aboola;
bool Aboolc;
bool exists;
}
function nredit(uint256 startindex)
public
pure
returns (
uint256[500] memory CIDs,
uint256[500] memory dates,
uint256[500] memory RIDs,
bool[500] memory Cboolas,
uint256[500] memory amounts
)
{}
function return500InvoicesByDates(
uint256 begindate,
uint256 enddate,
uint256 startindex
)
public
view
returns (
uint256[500] memory AIDs,
bool[500] memory Aboolas,
uint256[500] memory dates,
bytes32[3][500] memory Abytesas,
bytes32[3][500] memory bytesbs,
bytes32[2][500] memory bytescs,
uint256[500] memory amounts,
bool[500] memory Aboolbs,
bool[500] memory Aboolcs
)
{}
function return500PaymentsByDates(
uint256 begindate,
uint256 enddate,
uint256 startindex
)
public
view
returns (
uint256[500] memory BIDs,
uint256[500] memory dates,
uint256[500] memory RIDs,
bool[500] memory Bboolas,
bytes32[3][500] memory bytesbs,
bytes32[2][500] memory bytescs,
uint256[500] memory amounts,
bool[500] memory Bboolbs
)
{}
}
// ----
// constructor() ->
@@ -0,0 +1,26 @@
pragma experimental ABIEncoderV2;
contract C {
struct S1 {
uint256 a;
uint256 b;
}
struct S2 {
uint256 a;
uint256 b;
S1 s;
uint256 c;
}
function f(S2 calldata s)
external
pure
returns (uint256 a, uint256 b, uint256 sa, uint256 sb, uint256 c)
{
return (s.a, s.b, s.s.a, s.s.b, s.c);
}
}
// ----
// f((uint256,uint256,(uint256,uint256),uint256)): 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5
@@ -0,0 +1,27 @@
pragma experimental ABIEncoderV2;
contract C {
struct S1 {
uint256 a;
uint256 b;
}
struct S2 {
uint256 a;
uint256 b;
S1 s;
uint256 c;
}
function f(S2 calldata s)
external
pure
returns (uint256 a, uint256 b, uint256 sa, uint256 sb, uint256 c)
{
S2 memory m = s;
return (m.a, m.b, m.s.a, m.s.b, m.c);
}
}
// ----
// f((uint256,uint256,(uint256,uint256),uint256)): 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5
@@ -0,0 +1,10 @@
contract test {
int8 public x = 2;
int8 public y = 127;
int16 public q = 250;
}
// ----
// x() -> 2
// y() -> 127
// q() -> 250
@@ -0,0 +1,20 @@
contract C {
function f() public view returns (uint256) {
return msg.sender.balance;
}
}
contract D {
C c = new C();
constructor() public payable {}
function f() public view returns (uint256) {
return c.f();
}
}
// ----
// constructor(), 27 wei ->
// f() -> 27
@@ -0,0 +1,35 @@
contract Base {
uint256 data;
function setData(uint256 i) public {
data = i;
}
function getViaBase() public returns (uint256 i) {
return data;
}
}
contract A is Base {
function setViaA(uint256 i) public {
setData(i);
}
}
contract B is Base {
function getViaB() public returns (uint256 i) {
return getViaBase();
}
}
contract Derived is Base, B, A {}
// ====
// compileViaYul: also
// ----
// getViaB() -> 0
// setViaA(uint256): 23 ->
// getViaB() -> 23
@@ -0,0 +1,15 @@
// The EVM cannot provide access to dynamically-sized return values, so we have to skip them.
contract C {
function f() public returns (uint256, uint256[] memory, uint256) {
return (7, new uint256[](2), 8);
}
function g() public returns (uint256, uint256) {
// Previous implementation "moved" b to the second place and did not skip.
(uint256 a, , uint256 b) = this.f();
return (a, b);
}
}
// ----
// g() -> 7, 8
@@ -0,0 +1,22 @@
// For accessors, the dynamic types are already removed in the external signature itself.
contract C {
struct S {
uint256 x;
string a; // this is present in the accessor
uint256[] b; // this is not present
uint256 y;
}
S public s;
function g() public returns (uint256, uint256) {
s.x = 2;
s.a = "abc";
s.b = [7, 8, 9];
s.y = 6;
(uint256 x, , uint256 y) = this.s();
return (x, y);
}
}
// ----
// g() -> 2, 6
@@ -0,0 +1,11 @@
contract A {
uint256 x = 1;
uint256 y = 2;
function a() public returns (uint256 x) {
x = A.y;
}
}
// ----
// a() -> 2
@@ -0,0 +1,10 @@
contract Scope {
uint256 stateVar = 42;
function getStateVar() public view returns (uint256 stateVar) {
stateVar = Scope.stateVar;
}
}
// ----
// getStateVar() -> 42
@@ -0,0 +1,11 @@
contract Test {
mapping(string => uint256) data;
function f() public returns (uint256) {
data["abc"] = 2;
return data["abc"];
}
}
// ----
// f() -> 2
@@ -0,0 +1,14 @@
// this test just checks that the copy loop does not mess up the stack
contract C {
function save() public returns (uint256 r) {
r = 23;
savedData = msg.data;
r = 24;
}
bytes savedData;
}
// ----
// save() -> 24 # empty copy loop #
// save(): "abcdefg" -> 24
@@ -0,0 +1,17 @@
contract C {
function f() public returns (string memory, uint256) {
return ("abc", 8);
}
function g() public returns (string memory, string memory) {
return (h(), "def");
}
function h() public returns (string memory) {
return ("abc");
}
}
// ----
// f() -> 0x40, 0x8, 0x3, "abc"
// g() -> 0x40, 0x80, 0x3, "abc", 0x3, "def"
@@ -0,0 +1,29 @@
contract A {
function f() public virtual returns (uint256 r) {
return 1;
}
}
contract B is A {
function f() public virtual override returns (uint256 r) {
return super.f() | 2;
}
}
contract C is A {
function f() public virtual override returns (uint256 r) {
return super.f() | 4;
}
}
contract D is B, C {
function f() public override(B, C) returns (uint256 r) {
return super.f() | 8;
}
}
// ----
// f() -> 15
@@ -0,0 +1,10 @@
contract A {
function f() public {
super;
}
}
// ====
// compileViaYul: also
// ----
// f() ->
@@ -0,0 +1,34 @@
// This tests a swap in storage which does not work as one
// might expect because we do not have temporary storage.
// (x, y) = (y, x) is the same as
// y = x;
// x = y;
contract c {
struct S {
uint256 a;
uint256 b;
}
S public x;
S public y;
function set() public {
x.a = 1;
x.b = 2;
y.a = 3;
y.b = 4;
}
function swap() public {
(x, y) = (y, x);
}
}
// ----
// x() -> 0, 0
// y() -> 0, 0
// set() ->
// x() -> 1, 2
// y() -> 3, 4
// swap() ->
// x() -> 1, 2
// y() -> 1, 2
@@ -0,0 +1,11 @@
contract test {
function f(bool cond) public pure returns (uint256) {
uint32 x = 0x1234_ab;
uint256 y = 0x1234_abcd_1234;
return cond ? x : y;
}
}
// ----
// f(bool): true -> 0x1234ab
// f(bool): false -> 0x1234abcd1234
@@ -0,0 +1,30 @@
contract C {
uint256[] data;
uint256[] m_c;
function g() internal returns (uint256 a, uint256 b, uint256[] storage c) {
return (1, 2, data);
}
function h() external returns (uint256 a, uint256 b) {
return (5, 6);
}
function f() public returns (uint256) {
data.push(3);
uint256 a;
uint256 b;
(a, b) = this.h();
if (a != 5 || b != 6) return 1;
uint256[] storage c = m_c;
(a, b, c) = g();
if (a != 1 || b != 2 || c[0] != 3) return 2;
(a, b) = (b, a);
if (a != 2 || b != 1) return 3;
(a, , b, , ) = (8, 9, 10, 11, 12);
if (a != 8 || b != 10) return 4;
}
}
// ----
// f() -> 0
@@ -0,0 +1,26 @@
contract C {
struct S {
uint256 x;
}
S s;
function g() internal returns (uint256, S storage, uint256) {
s.x = 7;
return (1, s, 2);
}
function f() public returns (bool) {
(uint256 x1, S storage y1, uint256 z1) = g();
if (x1 != 1 || y1.x != 7 || z1 != 2) return false;
(, S storage y2, ) = g();
if (y2.x != 7) return false;
(uint256 x2, , ) = g();
if (x2 != 1) return false;
(, , uint256 z2) = g();
if (z2 != 2) return false;
return true;
}
}
// ----
// f() -> true
@@ -0,0 +1,23 @@
contract helper {
function getBalance() public payable returns (uint256 myBalance) {
return address(this).balance;
}
}
contract test {
helper h;
constructor() public payable {
h = new helper();
}
function sendAmount(uint256 amount) public payable returns (uint256 bal) {
uint256 someStackElement = 20;
return h.getBalance.value(amount).gas(1000).value(amount + 3)();
}
}
// ----
// constructor(), 20 wei ->
// sendAmount(uint256): 5 -> 8
@@ -0,0 +1,22 @@
contract helper {
function getBalance() public payable returns (uint256 myBalance) {
return address(this).balance;
}
}
contract test {
helper h;
constructor() public payable {
h = new helper();
}
function sendAmount(uint256 amount) public returns (uint256 bal) {
return h.getBalance.value(amount).gas(1000).value(amount + 3)(); // overwrite value
}
}
// ----
// constructor(), 20 wei ->
// sendAmount(uint256): 5 -> 8
@@ -0,0 +1,40 @@
contract C {
uint256 public x;
function f(uint256 y) public payable {
x = y;
}
function g(uint256 y) external {
x = y;
}
function h() public {
this.g(12);
}
}
contract D {
C c = new C();
function f() public payable returns (uint256) {
c.g(3);
return c.x();
}
function g() public returns (uint256) {
c.g(8);
return c.x();
}
function h() public returns (uint256) {
c.h();
return c.x();
}
}
// ----
// f() -> 3
// g() -> 8
// h() -> 12