From d0a854b31202a2b5de80c3a4a87679d116865e37 Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Mon, 22 Feb 2021 17:45:11 +0100 Subject: [PATCH] Adding semantic tests for bytes.concat. --- .../array/concat/bytes_concat_2_args.sol | 30 +++++++++++++++ .../array/concat/bytes_concat_3_args.sol | 10 +++++ .../array/concat/bytes_concat_as_argument.sol | 18 +++++++++ .../concat/bytes_concat_different_types.sol | 37 +++++++++++++++++++ .../array/concat/bytes_concat_empty.sol | 9 +++++ .../array/concat/bytes_concat_nested.sol | 9 +++++ 6 files changed, 113 insertions(+) create mode 100644 test/libsolidity/semanticTests/array/concat/bytes_concat_2_args.sol create mode 100644 test/libsolidity/semanticTests/array/concat/bytes_concat_3_args.sol create mode 100644 test/libsolidity/semanticTests/array/concat/bytes_concat_as_argument.sol create mode 100644 test/libsolidity/semanticTests/array/concat/bytes_concat_different_types.sol create mode 100644 test/libsolidity/semanticTests/array/concat/bytes_concat_empty.sol create mode 100644 test/libsolidity/semanticTests/array/concat/bytes_concat_nested.sol diff --git a/test/libsolidity/semanticTests/array/concat/bytes_concat_2_args.sol b/test/libsolidity/semanticTests/array/concat/bytes_concat_2_args.sol new file mode 100644 index 000000000..059db7001 --- /dev/null +++ b/test/libsolidity/semanticTests/array/concat/bytes_concat_2_args.sol @@ -0,0 +1,30 @@ +contract C { + function f(bytes memory a, bytes memory b) public returns (bytes memory) { + return bytes.concat(a, b); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes, bytes): 0x40, 0x80, 32, "abcdabcdabcdabcdabcdabcdabcdabcd", 5, "bcdef" -> 0x20, 37, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdef" +// +// f(bytes, bytes): +// 0x40, 0xa0, 64, "abcdabcdabcdabcdabcdabcdabcdabcd", "abcdabcdabcdabcdabcdabcdabcdabcd", 5, "bcdef" +// -> +// 0x20, 69, "abcdabcdabcdabcdabcdabcdabcdabcd", "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdef" +// f(bytes, bytes): 0x40, 0x80, 3, "abc", 3, "def" -> 0x20, 6, "abcdef" +// +// f(bytes, bytes): +// 0x40, 0xa0, 34, "abcdabcdabcdabcdabcdabcdabcdabcd", "ab", 30, "cdabcdabcdabcdabcdabcdabcdabcd" +// -> +// 0x20, 64, "abcdabcdabcdabcdabcdabcdabcdabcd", "abcdabcdabcdabcdabcdabcdabcdabcd" +// +// f(bytes, bytes): +// 0x40, 0xa0, 34, "abcdabcdabcdabcdabcdabcdabcdabcd", "ab", 34, "cdabcdabcdabcdabcdabcdabcdabcdab", "cd" +// -> +// 0x20, 68, "abcdabcdabcdabcdabcdabcdabcdabcd", "abcdabcdabcdabcdabcdabcdabcdabcd", "abcd" +// +// f(bytes, bytes): +// 0x40, 0x80, 3, "abc", 30, "dabcdabcdabcdabcdabcdabcdabcda" +// -> +// 0x20, 33, "abcdabcdabcdabcdabcdabcdabcdabcd", "a" \ No newline at end of file diff --git a/test/libsolidity/semanticTests/array/concat/bytes_concat_3_args.sol b/test/libsolidity/semanticTests/array/concat/bytes_concat_3_args.sol new file mode 100644 index 000000000..8736e80e0 --- /dev/null +++ b/test/libsolidity/semanticTests/array/concat/bytes_concat_3_args.sol @@ -0,0 +1,10 @@ +contract C { + function f(bytes memory a, bytes memory b, bytes memory c) public returns (bytes memory) { + return bytes.concat(a, b, c); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes, bytes, bytes): 0x60, 0xa0, 0xe0, 32, "abcdabcdabcdabcdabcdabcdabcdabcd", 5, "bcdef", 3, "abc" -> 0x20, 40, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdefabc" +// f(bytes, bytes, bytes): 0x60, 0xa0, 0xe0, 3, "abc", 2, "de", 3, "fgh" -> 0x20, 8, "abcdefgh" diff --git a/test/libsolidity/semanticTests/array/concat/bytes_concat_as_argument.sol b/test/libsolidity/semanticTests/array/concat/bytes_concat_as_argument.sol new file mode 100644 index 000000000..950ff3320 --- /dev/null +++ b/test/libsolidity/semanticTests/array/concat/bytes_concat_as_argument.sol @@ -0,0 +1,18 @@ +contract C { + function f(bytes memory a, bytes memory b) public returns (bytes32) { + return keccak256(bytes.concat(a, b)); + } + + function h(bytes memory a) internal returns (uint256) { + return a.length; + } + + function g(bytes memory a, bytes memory b) public returns (uint256) { + return h(bytes.concat(a, b)); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes,bytes): 0x40, 0x80, 32, "abcdabcdabcdabcdabcdabcdabcdabcd", 5, "bcdef" -> 0x1106e19b6f06d1cce71c2d816754f83dfa5b95df958c5cbf12b7c472320c427c +// g(bytes,bytes): 0x40, 0x80, 32, "abcdabcdabcdabcdabcdabcdabcdabcd", 5, "bcdef" -> 37 diff --git a/test/libsolidity/semanticTests/array/concat/bytes_concat_different_types.sol b/test/libsolidity/semanticTests/array/concat/bytes_concat_different_types.sol new file mode 100644 index 000000000..060cf555c --- /dev/null +++ b/test/libsolidity/semanticTests/array/concat/bytes_concat_different_types.sol @@ -0,0 +1,37 @@ +contract C { + bytes s = "bcdef"; + + function f(bytes memory a) public returns (bytes memory) { + return bytes.concat(a, "bcdef"); + } + function g(bytes calldata a) public returns (bytes memory) { + return bytes.concat(a, "abcdefghabcdefghabcdefghabcdefghab"); + } + function h(bytes calldata a) public returns (bytes memory) { + return bytes.concat(a, s); + } + function j(bytes calldata a) public returns (bytes memory) { + bytes storage ref = s; + return bytes.concat(a, ref, s); + } + function k(bytes calldata a, string memory b) public returns (bytes memory) { + return bytes.concat(a, bytes(b)); + } + function slice(bytes calldata a) public returns (bytes memory) { + require(a.length > 2, ""); + return bytes.concat(a[:2], a[2:]); + } + function strParam(string calldata a) public returns (bytes memory) { + return bytes.concat(bytes(a), "bcdef"); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes): 0x20, 32, "abcdabcdabcdabcdabcdabcdabcdabcd" -> 0x20, 37, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdef" +// g(bytes): 0x20, 32, "abcdabcdabcdabcdabcdabcdabcdabcd" -> 0x20, 66, "abcdabcdabcdabcdabcdabcdabcdabcd", "abcdefghabcdefghabcdefghabcdefgh", "ab" +// h(bytes): 0x20, 32, "abcdabcdabcdabcdabcdabcdabcdabcd" -> 0x20, 37, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdef" +// j(bytes): 0x20, 32, "abcdabcdabcdabcdabcdabcdabcdabcd" -> 0x20, 42, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdefbcdef" +// k(bytes, string): 0x40, 0x80, 32, "abcdabcdabcdabcdabcdabcdabcdabcd", 5, "bcdef" -> 0x20, 37, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdef" +// slice(bytes): 0x20, 4, "abcd" -> 0x20, 4, "abcd" +// strParam(string): 0x20, 32, "abcdabcdabcdabcdabcdabcdabcdabcd" -> 0x20, 37, "abcdabcdabcdabcdabcdabcdabcdabcd", "bcdef" diff --git a/test/libsolidity/semanticTests/array/concat/bytes_concat_empty.sol b/test/libsolidity/semanticTests/array/concat/bytes_concat_empty.sol new file mode 100644 index 000000000..6f7fbc367 --- /dev/null +++ b/test/libsolidity/semanticTests/array/concat/bytes_concat_empty.sol @@ -0,0 +1,9 @@ +contract C { + function f() public returns (bytes memory) { + return bytes.concat(); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 0x20, 0 diff --git a/test/libsolidity/semanticTests/array/concat/bytes_concat_nested.sol b/test/libsolidity/semanticTests/array/concat/bytes_concat_nested.sol new file mode 100644 index 000000000..0f6fae7bb --- /dev/null +++ b/test/libsolidity/semanticTests/array/concat/bytes_concat_nested.sol @@ -0,0 +1,9 @@ +contract C { + function f(bytes memory a, bytes memory b, bytes memory c) public returns (bytes memory) { + return bytes.concat(bytes.concat(a, b), c); + } +} +// ==== +// compileViaYul: also +// ---- +// f(bytes, bytes, bytes): 0x60, 0x60, 0x60, 2, "ab" -> 0x20, 6, "ababab" \ No newline at end of file