mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Sort tests.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
function f(bytes calldata data) external pure returns (uint256[] memory) {
|
||||
return abi.decode(data, (uint256[]));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f(bytes): 0x20, 0xc0, 0x20, 0x4, 0x3, 0x4, 0x5, 0x6 -> 0x20, 0x4, 0x3, 0x4, 0x5, 0x6
|
||||
@@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
function f(bytes calldata data)
|
||||
external
|
||||
pure
|
||||
returns (uint256[2][3] memory)
|
||||
{
|
||||
return abi.decode(data, (uint256[2][3]));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f(bytes): 0x20, 0xc0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 -> 1, 2, 3, 4, 5, 6
|
||||
@@ -0,0 +1,15 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
function f(bytes calldata data)
|
||||
external
|
||||
pure
|
||||
returns (uint256[2][3] memory)
|
||||
{
|
||||
return abi.decode(data, (uint256[2][3]));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f(bytes): 0x20, 0xc0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 -> 1, 2, 3, 4, 5, 6
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
function f(bytes memory data) public pure returns (uint256) {
|
||||
return abi.decode(data, (uint256));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f(bytes): 0x20, 0x20, 0x21 -> 33
|
||||
@@ -0,0 +1,22 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256[] b;
|
||||
}
|
||||
|
||||
function f() public pure returns (S memory) {
|
||||
S memory s;
|
||||
s.a = 8;
|
||||
s.b = new uint256[](3);
|
||||
s.b[0] = 9;
|
||||
s.b[1] = 10;
|
||||
s.b[2] = 11;
|
||||
return abi.decode(abi.encode(s), (S));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> 0x20, 0x8, 0x40, 0x3, 0x9, 0xa, 0xb
|
||||
@@ -0,0 +1,16 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256[] b;
|
||||
}
|
||||
|
||||
function f(bytes calldata data) external pure returns (S memory) {
|
||||
return abi.decode(data, (S));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f(bytes): 0x20, 0xe0, 0x20, 0x21, 0x40, 0x3, 0xa, 0xb, 0xc -> 0x20, 0x21, 0x40, 0x3, 0xa, 0xb, 0xc
|
||||
@@ -0,0 +1,24 @@
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
|
||||
contract C {
|
||||
bytes data;
|
||||
struct S {
|
||||
uint256 a;
|
||||
uint256[] b;
|
||||
}
|
||||
|
||||
function f() public returns (S memory) {
|
||||
S memory s;
|
||||
s.a = 8;
|
||||
s.b = new uint256[](3);
|
||||
s.b[0] = 9;
|
||||
s.b[1] = 10;
|
||||
s.b[2] = 11;
|
||||
data = abi.encode(s);
|
||||
return abi.decode(data, (S));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> 0x20, 0x8, 0x40, 0x3, 0x9, 0xa, 0xb
|
||||
@@ -0,0 +1,36 @@
|
||||
contract C {
|
||||
function f0() public returns (bytes memory) {
|
||||
return abi.encode();
|
||||
}
|
||||
|
||||
function f1() public returns (bytes memory) {
|
||||
return abi.encode(1, 2);
|
||||
}
|
||||
|
||||
function f2() public returns (bytes memory) {
|
||||
string memory x = "abc";
|
||||
return abi.encode(1, x, 2);
|
||||
}
|
||||
|
||||
function f3() public returns (bytes memory r) {
|
||||
// test that memory is properly allocated
|
||||
string memory x = "abc";
|
||||
r = abi.encode(1, x, 2);
|
||||
bytes memory y = "def";
|
||||
require(y[0] == "d");
|
||||
y[0] = "e";
|
||||
require(y[0] == "e");
|
||||
}
|
||||
|
||||
function f4() public returns (bytes memory) {
|
||||
bytes4 x = "abcd";
|
||||
return abi.encode(bytes2(x));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f0() -> 0x20, 0x0
|
||||
// f1() -> 0x20, 0x40, 0x1, 0x2
|
||||
// f2() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
|
||||
// f3() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
|
||||
// f4() -> 0x20, 0x20, "ab"
|
||||
@@ -0,0 +1,26 @@
|
||||
contract C {
|
||||
bool x;
|
||||
|
||||
function c(uint256 a, uint256[] memory b) public {
|
||||
require(a == 5);
|
||||
require(b.length == 2);
|
||||
require(b[0] == 6);
|
||||
require(b[1] == 7);
|
||||
x = true;
|
||||
}
|
||||
|
||||
function f() public returns (bool) {
|
||||
uint256 a = 5;
|
||||
uint256[] memory b = new uint256[](2);
|
||||
b[0] = 6;
|
||||
b[1] = 7;
|
||||
(bool success, ) = address(this).call(
|
||||
abi.encodeWithSignature("c(uint256,uint256[])", a, b)
|
||||
);
|
||||
require(success);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> true
|
||||
@@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
function f() public pure returns (uint256, bytes memory) {
|
||||
bytes memory arg = "abcdefg";
|
||||
return abi.decode(abi.encode(uint256(33), arg), (uint256, bytes));
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> 0x21, 0x40, 0x7, "abcdefg"
|
||||
@@ -0,0 +1,9 @@
|
||||
// Tests that rational numbers (even negative ones) are encoded properly.
|
||||
contract C {
|
||||
function f() public pure returns (bytes memory) {
|
||||
return abi.encode(1, -2);
|
||||
}
|
||||
}
|
||||
|
||||
// ----
|
||||
// f() -> 0x20, 0x40, 0x1, -2
|
||||
@@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
function f(uint256 a, uint256 b) external returns (uint256 c, uint256 d, uint256 e, uint256 f) {
|
||||
(c, d) = abi.decode(msg.data[4:], (uint256, uint256));
|
||||
e = abi.decode(msg.data[4 : 4 + 32], (uint256));
|
||||
f = abi.decode(msg.data[4 + 32 : 4 + 32 + 32], (uint256));
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// f(uint256,uint256): 42, 23 -> 42, 23, 42, 23
|
||||
@@ -0,0 +1,26 @@
|
||||
contract C {
|
||||
function test(bytes memory buf) public view returns (bool same, bool inplaceDecoded) {
|
||||
(uint256[] memory arr1, uint256[] memory arr2) = abi.decode(buf, (uint256[],uint256[]));
|
||||
assembly {
|
||||
// Check whether arr1 and arr2 end up at the same memory location.
|
||||
// This used to be the case, if both tail pointers in buf pointed to the
|
||||
// same memory region, i.e. this used to be false in the first two, but true
|
||||
// in the last three calls below. The desired behaviour is to always get distinct
|
||||
// memory regions, i.e. this should be false.
|
||||
same := eq(arr1, arr2)
|
||||
// Check whether (given the particular tail pointer of 0x40 for arr1 in the calls below)
|
||||
// arr1 points to the part of buf containing the encoding of arr1.
|
||||
// The position of the encoding of arr1 in buf is at offset 0x20 (length) + 0x40 (tail pointer)
|
||||
// of buf.
|
||||
// This used to be the case for all the test calls below, whereas now arr1 is always copied
|
||||
// from buf to a new memory area. Should always be false.
|
||||
inplaceDecoded := eq(arr1, add(buf, 0x60))
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// test(bytes): 0x20, 0x80, 0x40, 0x60, 0, 0 -> false, false
|
||||
// test(bytes): 0x20, 0xC0, 0x40, 0x80, 1, 0x42, 1, 0x42 -> false, false
|
||||
// test(bytes): 0x20, 0x80, 0x40, 0x40, 1, 0x42 -> false, false
|
||||
// test(bytes): 0x20, 0x60, 0x40, 0x40, 0 -> false, false
|
||||
// test(bytes): 0x20, 0x80, 0x40, 0x40, 1, 0x42 -> false, false
|
||||
Reference in New Issue
Block a user