-
Notifications
You must be signed in to change notification settings - Fork 6
/
TransientBytes.t.sol
84 lines (70 loc) · 2.6 KB
/
TransientBytes.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {Test} from "forge-std/Test.sol";
import {TransientBytes} from "../src/TransientBytesLib.sol";
/// @author philogy <https://github.com/philogy>
contract TransientBytesTest is Test {
TransientBytes tbytes;
uint256 internal constant MAX_LENGTH = type(uint32).max;
function test_defaultEmpty() public {
assertEq(tbytes.get(), "");
assertEq(tbytes.length(), 0);
}
function test_setMem(bytes memory inner) public {
vm.assume(inner.length <= MAX_LENGTH);
tbytes.set(inner);
assertEq(tbytes.get(), inner);
assertEq(tbytes.length(), inner.length);
tbytes.agus();
assertEq(tbytes.get(), "");
assertEq(tbytes.length(), 0);
}
function test_setCd(bytes calldata inner) public {
vm.assume(inner.length <= MAX_LENGTH);
tbytes.setCd(inner);
assertEq(tbytes.get(), inner);
assertEq(tbytes.length(), inner.length);
tbytes.agus();
assertEq(tbytes.get(), "");
assertEq(tbytes.length(), 0);
}
function test_multipleSetMemNoAgus(bytes memory inner1, bytes memory inner2) public {
vm.assume(inner1.length <= MAX_LENGTH);
vm.assume(inner2.length <= MAX_LENGTH);
tbytes.set(inner1);
assertEq(tbytes.get(), inner1);
tbytes.set(inner2);
assertEq(tbytes.get(), inner2);
}
function test_multipleSetCdNoAgus(bytes calldata inner1, bytes calldata inner2) public {
vm.assume(inner1.length <= MAX_LENGTH);
vm.assume(inner2.length <= MAX_LENGTH);
tbytes.setCd(inner1);
assertEq(tbytes.get(), inner1);
tbytes.setCd(inner2);
assertEq(tbytes.get(), inner2);
}
function test_gasUsed_setTillBoundary() public {
bytes32 a = keccak256("a");
bytes32 b = keccak256("b");
bytes memory data = abi.encodePacked(bytes28(a), b);
assertEq(data.length, 0x20 * 2 - 4);
uint256 g0 = gasleft();
tbytes.set(data);
uint256 g1 = gasleft();
emit log_named_uint("used", g0 - g1);
}
function test_gasUsed_setTillBoundary_five() public {
bytes32 w1 = keccak256("w1");
bytes32 w2 = keccak256("w2");
bytes32 w3 = keccak256("w3");
bytes32 w4 = keccak256("w4");
bytes32 w5 = keccak256("w5");
bytes memory data = abi.encodePacked(bytes28(w1), w2, w3, w4, w5);
assertEq(data.length, 0x20 * 5 - 4);
uint256 g0 = gasleft();
tbytes.set(data);
uint256 g1 = gasleft();
emit log_named_uint("used", g0 - g1);
}
}