forked from Vectorized/dn404
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayOps.t.sol
179 lines (166 loc) · 5.83 KB
/
ArrayOps.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "./utils/SoladyTest.sol";
import {LibPRNG} from "solady/utils/LibPRNG.sol";
contract ArrayOpsTest is SoladyTest {
using LibPRNG for *;
/// @dev Returns an array of zero addresses.
function _zeroAddresses(uint256 n) private pure returns (address[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(0x40)
mstore(0x40, add(add(result, 0x20), shl(5, n)))
mstore(result, n)
codecopy(add(result, 0x20), codesize(), shl(5, n))
}
}
/// @dev Returns an array each set to `value`.
function _filled(uint256 n, uint256 value) private pure returns (uint256[] memory result) {
/// @solidity memory-safe-assembly
assembly {
result := mload(0x40)
let o := add(result, 0x20)
let end := add(o, shl(5, n))
mstore(0x40, end)
mstore(result, n)
for {} iszero(eq(o, end)) { o := add(o, 0x20) } { mstore(o, value) }
}
}
/// @dev Returns an array each set to `value`.
function _filled(uint256 n, address value) private pure returns (address[] memory result) {
result = _toAddresses(_filled(n, uint160(value)));
}
/// @dev Concatenates the arrays.
function _concat(uint256[] memory a, uint256[] memory b)
private
view
returns (uint256[] memory result)
{
uint256 aN = a.length;
uint256 bN = b.length;
if (aN == uint256(0)) return b;
if (bN == uint256(0)) return a;
/// @solidity memory-safe-assembly
assembly {
let n := add(aN, bN)
if n {
result := mload(0x40)
mstore(result, n)
let o := add(result, 0x20)
mstore(0x40, add(o, shl(5, n)))
let aL := shl(5, aN)
pop(staticcall(gas(), 4, add(a, 0x20), aL, o, aL))
pop(staticcall(gas(), 4, add(b, 0x20), shl(5, bN), add(o, aL), shl(5, bN)))
}
}
}
/// @dev Concatenates the arrays.
function _concat(address[] memory a, address[] memory b)
private
view
returns (address[] memory result)
{
result = _toAddresses(_concat(_toUints(a), _toUints(b)));
}
/// @dev Reinterpret cast to an uint array.
function _toUints(address[] memory a) private pure returns (uint256[] memory casted) {
/// @solidity memory-safe-assembly
assembly {
casted := a
}
}
/// @dev Reinterpret cast to an address array.
function _toAddresses(uint256[] memory a) private pure returns (address[] memory casted) {
/// @solidity memory-safe-assembly
assembly {
casted := a
}
}
function testZeroAddresses(uint256) public {
uint256 n = _bound(_random(), 0, 32);
unchecked {
if (_random() % 16 == 0) _brutalizeMemory();
address[] memory a = _zeroAddresses(n);
if (_random() % 16 == 0) _brutalizeMemory();
_checkMemory();
for (uint256 i; i != n; ++i) {
assertEq(a[i], address(0));
}
assertEq(a.length, n);
}
}
function testFilled(uint256) public {
uint256 n = _bound(_random(), 0, 32);
unchecked {
if (_random() % 16 == 0) _brutalizeMemory();
uint256 value = _random();
uint256[] memory a = _filled(n, value);
if (_random() % 16 == 0) _brutalizeMemory();
_checkMemory();
for (uint256 i; i != n; ++i) {
assertEq(a[i], value);
}
assertEq(a.length, n);
}
unchecked {
if (_random() % 16 == 0) _brutalizeMemory();
uint256 value = _random();
address addressValue;
/// @solidity memory-safe-assembly
assembly {
addressValue := value
}
address[] memory a = _filled(n, addressValue);
if (_random() % 16 == 0) _brutalizeMemory();
_checkMemory();
for (uint256 i; i != n; ++i) {
assertEq(a[i], addressValue);
}
assertEq(a.length, n);
}
}
struct _TestConcatTemps {
uint256[] a;
uint256[] b;
uint256[] c;
uint256[] combined;
uint256 n;
}
function testConcat(uint256 seed) public {
_TestConcatTemps memory t;
t.a = new uint256[](_bound(_random(), 0, 32));
if (_random() % 16 == 0) _brutalizeMemory();
t.b = new uint256[](_bound(_random(), 0, 32));
if (_random() % 16 == 0) _brutalizeMemory();
t.c = new uint256[](_bound(_random(), 0, 32));
t.n = t.a.length + t.b.length + t.c.length;
t.combined = new uint256[](t.n);
if (_random() % 16 == 0) _brutalizeMemory();
unchecked {
LibPRNG.PRNG memory prng;
prng.state = seed;
for (uint256 i; i != t.n; ++i) {
t.combined[i] = prng.next();
}
}
if (_random() % 16 == 0) _brutalizeMemory();
unchecked {
LibPRNG.PRNG memory prng;
prng.state = seed;
for (uint256 i; i != t.a.length; ++i) {
t.a[i] = prng.next();
}
for (uint256 i; i != t.b.length; ++i) {
t.b[i] = prng.next();
}
for (uint256 i; i != t.c.length; ++i) {
t.c[i] = prng.next();
}
if (_random() % 16 == 0) _brutalizeMemory();
uint256[] memory concatenated = _concat(t.a, _concat(t.b, t.c));
if (_random() % 16 == 0) _brutalizeMemory();
_checkMemory();
assertEq(concatenated, t.combined);
}
}
}