-
Notifications
You must be signed in to change notification settings - Fork 71
/
test.sol
469 lines (444 loc) · 15.8 KB
/
test.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
pragma solidity >=0.5.0;
contract DSTest {
event log (string);
event logs (bytes);
event log_address (address);
event log_bytes32 (bytes32);
event log_int (int);
event log_uint (uint);
event log_bytes (bytes);
event log_string (string);
event log_named_address (string key, address val);
event log_named_bytes32 (string key, bytes32 val);
event log_named_decimal_int (string key, int val, uint decimals);
event log_named_decimal_uint (string key, uint val, uint decimals);
event log_named_int (string key, int val);
event log_named_uint (string key, uint val);
event log_named_bytes (string key, bytes val);
event log_named_string (string key, string val);
bool public IS_TEST = true;
bool private _failed;
address constant HEVM_ADDRESS =
address(bytes20(uint160(uint256(keccak256('hevm cheat code')))));
modifier mayRevert() { _; }
modifier testopts(string memory) { _; }
function failed() public returns (bool) {
if (_failed) {
return _failed;
} else {
bool globalFailed = false;
if (hasHEVMContext()) {
(, bytes memory retdata) = HEVM_ADDRESS.call(
abi.encodePacked(
bytes4(keccak256("load(address,bytes32)")),
abi.encode(HEVM_ADDRESS, bytes32("failed"))
)
);
globalFailed = abi.decode(retdata, (bool));
}
return globalFailed;
}
}
function fail() internal {
if (hasHEVMContext()) {
(bool status, ) = HEVM_ADDRESS.call(
abi.encodePacked(
bytes4(keccak256("store(address,bytes32,bytes32)")),
abi.encode(HEVM_ADDRESS, bytes32("failed"), bytes32(uint256(0x01)))
)
);
status; // Silence compiler warnings
}
_failed = true;
}
function hasHEVMContext() internal view returns (bool) {
uint256 hevmCodeSize = 0;
assembly {
hevmCodeSize := extcodesize(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D)
}
return hevmCodeSize > 0;
}
modifier logs_gas() {
uint startGas = gasleft();
_;
uint endGas = gasleft();
emit log_named_uint("gas", startGas - endGas);
}
function assertTrue(bool condition) internal {
if (!condition) {
emit log("Error: Assertion Failed");
fail();
}
}
function assertTrue(bool condition, string memory err) internal {
if (!condition) {
emit log_named_string("Error", err);
assertTrue(condition);
}
}
function assertEq(address a, address b) internal {
if (a != b) {
emit log("Error: a == b not satisfied [address]");
emit log_named_address(" Expected", b);
emit log_named_address(" Actual", a);
fail();
}
}
function assertEq(address a, address b, string memory err) internal {
if (a != b) {
emit log_named_string ("Error", err);
assertEq(a, b);
}
}
function assertEq(bytes32 a, bytes32 b) internal {
if (a != b) {
emit log("Error: a == b not satisfied [bytes32]");
emit log_named_bytes32(" Expected", b);
emit log_named_bytes32(" Actual", a);
fail();
}
}
function assertEq(bytes32 a, bytes32 b, string memory err) internal {
if (a != b) {
emit log_named_string ("Error", err);
assertEq(a, b);
}
}
function assertEq32(bytes32 a, bytes32 b) internal {
assertEq(a, b);
}
function assertEq32(bytes32 a, bytes32 b, string memory err) internal {
assertEq(a, b, err);
}
function assertEq(int a, int b) internal {
if (a != b) {
emit log("Error: a == b not satisfied [int]");
emit log_named_int(" Expected", b);
emit log_named_int(" Actual", a);
fail();
}
}
function assertEq(int a, int b, string memory err) internal {
if (a != b) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function assertEq(uint a, uint b) internal {
if (a != b) {
emit log("Error: a == b not satisfied [uint]");
emit log_named_uint(" Expected", b);
emit log_named_uint(" Actual", a);
fail();
}
}
function assertEq(uint a, uint b, string memory err) internal {
if (a != b) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function assertEqDecimal(int a, int b, uint decimals) internal {
if (a != b) {
emit log("Error: a == b not satisfied [decimal int]");
emit log_named_decimal_int(" Expected", b, decimals);
emit log_named_decimal_int(" Actual", a, decimals);
fail();
}
}
function assertEqDecimal(int a, int b, uint decimals, string memory err) internal {
if (a != b) {
emit log_named_string("Error", err);
assertEqDecimal(a, b, decimals);
}
}
function assertEqDecimal(uint a, uint b, uint decimals) internal {
if (a != b) {
emit log("Error: a == b not satisfied [decimal uint]");
emit log_named_decimal_uint(" Expected", b, decimals);
emit log_named_decimal_uint(" Actual", a, decimals);
fail();
}
}
function assertEqDecimal(uint a, uint b, uint decimals, string memory err) internal {
if (a != b) {
emit log_named_string("Error", err);
assertEqDecimal(a, b, decimals);
}
}
function assertGt(uint a, uint b) internal {
if (a <= b) {
emit log("Error: a > b not satisfied [uint]");
emit log_named_uint(" Value a", a);
emit log_named_uint(" Value b", b);
fail();
}
}
function assertGt(uint a, uint b, string memory err) internal {
if (a <= b) {
emit log_named_string("Error", err);
assertGt(a, b);
}
}
function assertGt(int a, int b) internal {
if (a <= b) {
emit log("Error: a > b not satisfied [int]");
emit log_named_int(" Value a", a);
emit log_named_int(" Value b", b);
fail();
}
}
function assertGt(int a, int b, string memory err) internal {
if (a <= b) {
emit log_named_string("Error", err);
assertGt(a, b);
}
}
function assertGtDecimal(int a, int b, uint decimals) internal {
if (a <= b) {
emit log("Error: a > b not satisfied [decimal int]");
emit log_named_decimal_int(" Value a", a, decimals);
emit log_named_decimal_int(" Value b", b, decimals);
fail();
}
}
function assertGtDecimal(int a, int b, uint decimals, string memory err) internal {
if (a <= b) {
emit log_named_string("Error", err);
assertGtDecimal(a, b, decimals);
}
}
function assertGtDecimal(uint a, uint b, uint decimals) internal {
if (a <= b) {
emit log("Error: a > b not satisfied [decimal uint]");
emit log_named_decimal_uint(" Value a", a, decimals);
emit log_named_decimal_uint(" Value b", b, decimals);
fail();
}
}
function assertGtDecimal(uint a, uint b, uint decimals, string memory err) internal {
if (a <= b) {
emit log_named_string("Error", err);
assertGtDecimal(a, b, decimals);
}
}
function assertGe(uint a, uint b) internal {
if (a < b) {
emit log("Error: a >= b not satisfied [uint]");
emit log_named_uint(" Value a", a);
emit log_named_uint(" Value b", b);
fail();
}
}
function assertGe(uint a, uint b, string memory err) internal {
if (a < b) {
emit log_named_string("Error", err);
assertGe(a, b);
}
}
function assertGe(int a, int b) internal {
if (a < b) {
emit log("Error: a >= b not satisfied [int]");
emit log_named_int(" Value a", a);
emit log_named_int(" Value b", b);
fail();
}
}
function assertGe(int a, int b, string memory err) internal {
if (a < b) {
emit log_named_string("Error", err);
assertGe(a, b);
}
}
function assertGeDecimal(int a, int b, uint decimals) internal {
if (a < b) {
emit log("Error: a >= b not satisfied [decimal int]");
emit log_named_decimal_int(" Value a", a, decimals);
emit log_named_decimal_int(" Value b", b, decimals);
fail();
}
}
function assertGeDecimal(int a, int b, uint decimals, string memory err) internal {
if (a < b) {
emit log_named_string("Error", err);
assertGeDecimal(a, b, decimals);
}
}
function assertGeDecimal(uint a, uint b, uint decimals) internal {
if (a < b) {
emit log("Error: a >= b not satisfied [decimal uint]");
emit log_named_decimal_uint(" Value a", a, decimals);
emit log_named_decimal_uint(" Value b", b, decimals);
fail();
}
}
function assertGeDecimal(uint a, uint b, uint decimals, string memory err) internal {
if (a < b) {
emit log_named_string("Error", err);
assertGeDecimal(a, b, decimals);
}
}
function assertLt(uint a, uint b) internal {
if (a >= b) {
emit log("Error: a < b not satisfied [uint]");
emit log_named_uint(" Value a", a);
emit log_named_uint(" Value b", b);
fail();
}
}
function assertLt(uint a, uint b, string memory err) internal {
if (a >= b) {
emit log_named_string("Error", err);
assertLt(a, b);
}
}
function assertLt(int a, int b) internal {
if (a >= b) {
emit log("Error: a < b not satisfied [int]");
emit log_named_int(" Value a", a);
emit log_named_int(" Value b", b);
fail();
}
}
function assertLt(int a, int b, string memory err) internal {
if (a >= b) {
emit log_named_string("Error", err);
assertLt(a, b);
}
}
function assertLtDecimal(int a, int b, uint decimals) internal {
if (a >= b) {
emit log("Error: a < b not satisfied [decimal int]");
emit log_named_decimal_int(" Value a", a, decimals);
emit log_named_decimal_int(" Value b", b, decimals);
fail();
}
}
function assertLtDecimal(int a, int b, uint decimals, string memory err) internal {
if (a >= b) {
emit log_named_string("Error", err);
assertLtDecimal(a, b, decimals);
}
}
function assertLtDecimal(uint a, uint b, uint decimals) internal {
if (a >= b) {
emit log("Error: a < b not satisfied [decimal uint]");
emit log_named_decimal_uint(" Value a", a, decimals);
emit log_named_decimal_uint(" Value b", b, decimals);
fail();
}
}
function assertLtDecimal(uint a, uint b, uint decimals, string memory err) internal {
if (a >= b) {
emit log_named_string("Error", err);
assertLtDecimal(a, b, decimals);
}
}
function assertLe(uint a, uint b) internal {
if (a > b) {
emit log("Error: a <= b not satisfied [uint]");
emit log_named_uint(" Value a", a);
emit log_named_uint(" Value b", b);
fail();
}
}
function assertLe(uint a, uint b, string memory err) internal {
if (a > b) {
emit log_named_string("Error", err);
assertLe(a, b);
}
}
function assertLe(int a, int b) internal {
if (a > b) {
emit log("Error: a <= b not satisfied [int]");
emit log_named_int(" Value a", a);
emit log_named_int(" Value b", b);
fail();
}
}
function assertLe(int a, int b, string memory err) internal {
if (a > b) {
emit log_named_string("Error", err);
assertLe(a, b);
}
}
function assertLeDecimal(int a, int b, uint decimals) internal {
if (a > b) {
emit log("Error: a <= b not satisfied [decimal int]");
emit log_named_decimal_int(" Value a", a, decimals);
emit log_named_decimal_int(" Value b", b, decimals);
fail();
}
}
function assertLeDecimal(int a, int b, uint decimals, string memory err) internal {
if (a > b) {
emit log_named_string("Error", err);
assertLeDecimal(a, b, decimals);
}
}
function assertLeDecimal(uint a, uint b, uint decimals) internal {
if (a > b) {
emit log("Error: a <= b not satisfied [decimal uint]");
emit log_named_decimal_uint(" Value a", a, decimals);
emit log_named_decimal_uint(" Value b", b, decimals);
fail();
}
}
function assertLeDecimal(uint a, uint b, uint decimals, string memory err) internal {
if (a > b) {
emit log_named_string("Error", err);
assertGeDecimal(a, b, decimals);
}
}
function assertEq(string memory a, string memory b) internal {
if (keccak256(abi.encodePacked(a)) != keccak256(abi.encodePacked(b))) {
emit log("Error: a == b not satisfied [string]");
emit log_named_string(" Expected", b);
emit log_named_string(" Actual", a);
fail();
}
}
function assertEq(string memory a, string memory b, string memory err) internal {
if (keccak256(abi.encodePacked(a)) != keccak256(abi.encodePacked(b))) {
emit log_named_string("Error", err);
assertEq(a, b);
}
}
function checkEq0(bytes memory a, bytes memory b) internal pure returns (bool ok) {
ok = true;
if (a.length == b.length) {
for (uint i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
ok = false;
}
}
} else {
ok = false;
}
}
function assertEq0(bytes memory a, bytes memory b) internal {
if (!checkEq0(a, b)) {
emit log("Error: a == b not satisfied [bytes]");
emit log_named_bytes(" Expected", b);
emit log_named_bytes(" Actual", a);
fail();
}
}
function assertEq0(bytes memory a, bytes memory b, string memory err) internal {
if (!checkEq0(a, b)) {
emit log_named_string("Error", err);
assertEq0(a, b);
}
}
}