Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Extract 45 more tests from SolidityEndToEndTest.cpp #8518

Merged
merged 2 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,296 changes: 14 additions & 1,282 deletions test/libsolidity/SolidityEndToEndTest.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract C {
function f(bytes calldata data)
external
pure
returns (uint256, bytes memory r)
{
return abi.decode(data, (uint256, bytes));
}
}
// ----
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract C {
function f(bytes memory data) public pure returns (uint256, bytes memory) {
return abi.decode(data, (uint256, bytes));
}
}
// ----
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
contract C {
bytes data;

function f(bytes memory _data) public returns (uint256, bytes memory) {
data = _data;
return abi.decode(data, (uint256, bytes));
}
}
// ----
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Tests that this will not end up using a "bytes0" type
// (which would assert)
contract C {
function f() public pure returns (bytes memory, bytes memory) {
return (abi.encode(""), abi.encodePacked(""));
}
}
// ====
// ABIEncoderV1Only: true
// ----
// f() -> 0x40, 0xc0, 0x60, 0x20, 0x0, 0x0, 0x0
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
contract Test {
uint24[3][] public data;

function set(uint24[3][] memory _data) public returns (uint256) {
data = _data;
return data.length;
}

function get() public returns (uint24[3][] memory) {
return data;
}
}
// ----
// set(uint24[3][]): 0x20, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12 -> 0x06
// data(uint256,uint256): 0x02, 0x02 -> 0x09
// data(uint256,uint256): 0x05, 0x01 -> 0x11
// data(uint256,uint256): 0x06, 0x00 -> FAILURE
// get() -> 0x20, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
contract Test {
function set(uint24[3][] memory _data, uint256 a, uint256 b)
public
returns (uint256 l, uint256 e)
{
l = _data.length;
e = _data[a][b];
}
}
// ====
// compileViaYul: also
// ----
// set(uint24[3][],uint256,uint256): 0x60, 0x03, 0x02, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12 -> 0x06, 0x0c
13 changes: 13 additions & 0 deletions test/libsolidity/semanticTests/array/bytes_memory_index_access.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
contract Test {
function set(bytes memory _data, uint256 i)
public
returns (uint256 l, bytes1 c)
{
l = _data.length;
c = _data[i];
}
}
// ====
// compileViaYul: also
// ----
// set(bytes,uint256): 0x40, 0x03, 0x08, "abcdefgh" -> 0x08, "d"
45 changes: 45 additions & 0 deletions test/libsolidity/semanticTests/array/fixed_arrays_in_storage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
contract c {
struct Data {
uint256 x;
uint256 y;
}
Data[2**10] data;
uint256[2**10 + 3] ids;

function setIDStatic(uint256 id) public {
ids[2] = id;
}

function setID(uint256 index, uint256 id) public {
ids[index] = id;
}

function setData(uint256 index, uint256 x, uint256 y) public {
data[index].x = x;
data[index].y = y;
}

function getID(uint256 index) public returns (uint256) {
return ids[index];
}

function getData(uint256 index) public returns (uint256 x, uint256 y) {
x = data[index].x;
y = data[index].y;
}

function getLengths() public returns (uint256 l1, uint256 l2) {
l1 = data.length;
l2 = ids.length;
}
}
// ----
// setIDStatic(uint256): 0xb ->
// getID(uint256): 0x2 -> 0xb
// setID(uint256,uint256): 0x7, 0x8 ->
// getID(uint256): 0x7 -> 0x8
// setData(uint256,uint256,uint256): 0x7, 0x8, 0x9 ->
// setData(uint256,uint256,uint256): 0x8, 0xa, 0xb ->
// getData(uint256): 0x7 -> 0x8, 0x9
// getData(uint256): 0x8 -> 0xa, 0xb
// getLengths() -> 0x400, 0x403
17 changes: 17 additions & 0 deletions test/libsolidity/semanticTests/array/fixed_bytes_index_access.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contract C {
bytes16[] public data;

function f(bytes32 x) public returns (bytes1) {
return x[2];
}

function g(bytes32 x) public returns (uint256) {
data = [x[0], x[1], x[2]];
data[0] = "12345";
return uint256(uint8(data[0][4]));
}
}
// ----
// f(bytes32): "789" -> "9"
// g(bytes32): "789" -> 0x35
// data(uint256): 0x01 -> "8"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contract Test {
uint24[3][][4] data;

function set(uint24[3][][4] memory x)
internal
returns (uint24[3][][4] memory)
{
x[1][2][2] = 1;
x[1][3][2] = 7;
return x;
}

function f() public returns (uint24[3][] memory) {
while (data[1].length < 4) data[1].push();
return set(data)[1];
}
}
// ----
// f() -> 0x20, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x07
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
contract Test {
function set(uint24[3][4] memory x) public {
x[2][2] = 1;
x[3][2] = 7;
}

function f() public returns (uint24[3][4] memory) {
uint24[3][4] memory data;
set(data);
return data;
}
}
// ----
// f() -> 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x07
26 changes: 26 additions & 0 deletions test/libsolidity/semanticTests/array/reusing_memory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Invoke some features that use memory and test that they do not interfere with each other.
contract Helper {
uint256 public flag;

constructor(uint256 x) public {
flag = x;
}
}


contract Main {
mapping(uint256 => uint256) map;

function f(uint256 x) public returns (uint256) {
map[x] = x;
return
(new Helper(uint256(keccak256(abi.encodePacked(this.g(map[x]))))))
.flag();
}

function g(uint256 a) public returns (uint256) {
return map[a];
}
}
// ----
// f(uint256): 0x34 -> 0x46bddb1178e94d7f2892ff5f366840eb658911794f2c3a44c450aa2c505186c1
20 changes: 20 additions & 0 deletions test/libsolidity/semanticTests/array/string_allocation_bug.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
contract Sample {
struct s {
uint16 x;
uint16 y;
string a;
string b;
}
s[2] public p;

constructor() public {
s memory m;
m.x = 0xbbbb;
m.y = 0xcccc;
m.a = "hello";
m.b = "world";
p[0] = m;
}
}
// ----
// p(uint256): 0x0 -> 0xbbbb, 0xcccc, 0x80, 0xc0, 0x05, "hello", 0x05, "world"
17 changes: 17 additions & 0 deletions test/libsolidity/semanticTests/array/string_bytes_conversion.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contract Test {
string s;
bytes b;

function f(string memory _s, uint256 n) public returns (bytes1) {
b = bytes(_s);
s = string(b);
return bytes(s)[n];
}

function l() public returns (uint256) {
return bytes(s).length;
}
}
// ----
// f(string,uint256): 0x40, 0x02, 0x06, "abcdef" -> "c"
// l() -> 0x06
35 changes: 35 additions & 0 deletions test/libsolidity/semanticTests/array/strings_in_struct.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
contract buggystruct {
Buggy public bug;

struct Buggy {
uint256 first;
uint256 second;
uint256 third;
string last;
}

constructor() public {
bug = Buggy(10, 20, 30, "asdfghjkl");
}

function getFirst() public returns (uint256) {
return bug.first;
}

function getSecond() public returns (uint256) {
return bug.second;
}

function getThird() public returns (uint256) {
return bug.third;
}

function getLast() public returns (string memory) {
return bug.last;
}
}
// ----
// getFirst() -> 0x0a
// getSecond() -> 0x14
// getThird() -> 0x1e
// getLast() -> 0x20, 0x09, "asdfghjkl"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract C {
bytes32 constant x = keccak256("abc");

function f() public returns (bytes32) {
return x;
}
}
// ----
// f() -> 0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
contract C {
uint256 public x;

function f() public pure returns (bytes4) {
return this.f.selector;
}

function g() public returns (bytes4) {
function () pure external returns (bytes4) fun = this.f;
return fun.selector;
}

function h() public returns (bytes4) {
function () pure external returns (bytes4) fun = this.f;
return fun.selector;
}

function i() public pure returns (bytes4) {
return this.x.selector;
}
}
// ----
// f() -> 0x26121ff000000000000000000000000000000000000000000000000000000000
// g() -> 0x26121ff000000000000000000000000000000000000000000000000000000000
// h() -> 0x26121ff000000000000000000000000000000000000000000000000000000000
// i() -> 0x0c55699c00000000000000000000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
contract c {
bytes data;

function foo() public returns (bytes32) {
data.push("x");
data.push("y");
data.push("z");
return keccak256(abi.encodePacked("b", keccak256(data), "a"));
}
}
// ----
// foo() -> 0xb338eefce206f9f57b83aa738deecd5326dc4b72dd81ee6a7c621a6facb7acdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract c {
function foo(uint256 a, uint256 b, uint256 c) public returns (bytes32 d) {
d = keccak256(abi.encodePacked(a, b, c));
}
}
// ----
// foo(uint256,uint256,uint256): 0xa, 0xc, 0xd -> 0xbc740a98aae5923e8f04c9aa798c9ee82f69e319997699f2782c40828db9fd81
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract c {
function foo(uint256 a, uint16 b) public returns (bytes32 d) {
d = keccak256(abi.encodePacked(a, b, uint8(145)));
}
}
// ----
// foo(uint256,uint16): 0xa, 0xc -> 0x88acd45f75907e7c560318bc1a5249850a0999c4896717b1167d05d116e6dbad
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
contract c {
function foo() public returns (bytes32 d) {
d = keccak256("foo");
}

function bar(uint256 a, uint16 b) public returns (bytes32 d) {
d = keccak256(abi.encodePacked(a, b, uint8(145), "foo"));
}
}
// ----
// foo() -> 0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d
// bar(uint256,uint16): 0xa, 0xc -> 0x6990f36476dc412b1c4baa48e2d9f4aa4bb313f61fda367c8fdbbb2232dc6146
9 changes: 9 additions & 0 deletions test/libsolidity/semanticTests/builtinFunctions/msg_sig.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract test {
function foo(uint256 a) public returns (bytes4 value) {
return msg.sig;
}
}
// ====
// compileViaYul: also
// ----
// foo(uint256): 0x0 -> 0x2fbebd3800000000000000000000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
contract test {
function boo() public returns (bytes4 value) {
return msg.sig;
}

function foo(uint256 a) public returns (bytes4 value) {
return boo();
}
}
// ====
// compileViaYul: also
// ----
// foo(uint256): 0x0 -> 0x2fbebd3800000000000000000000000000000000000000000000000000000000
Loading