arnaucube
arnaucube/poseidon-solidity-examplePublic Self-hosted repository
poseidon-solidity-example/lib/forge-std/test/StdAssertions.t.sol
lib/forge-std/test/StdAssertions.t.solRaw
  1// SPDX-License-Identifier: MIT
  2pragma solidity >=0.7.0 <0.9.0;
  3
  4import "../src/Test.sol";
  5
  6contract StdAssertionsTest is Test {
  7    string constant CUSTOM_ERROR = "guh!";
  8
  9    bool constant EXPECT_PASS = false;
 10    bool constant EXPECT_FAIL = true;
 11
 12    TestTest t = new TestTest();
 13
 14    /*//////////////////////////////////////////////////////////////////////////
 15                                    FAIL(STRING)
 16    //////////////////////////////////////////////////////////////////////////*/
 17
 18    function testShouldFail() external {
 19        vm.expectEmit(false, false, false, true);
 20        emit log_named_string("Error", CUSTOM_ERROR);
 21        t._fail(CUSTOM_ERROR);
 22    }
 23
 24    /*//////////////////////////////////////////////////////////////////////////
 25                                    ASSERT_FALSE
 26    //////////////////////////////////////////////////////////////////////////*/
 27
 28    function testAssertFalse_Pass() external {
 29        t._assertFalse(false, EXPECT_PASS);
 30    }
 31
 32    function testAssertFalse_Fail() external {
 33        vm.expectEmit(false, false, false, true);
 34        emit log("Error: Assertion Failed");
 35        t._assertFalse(true, EXPECT_FAIL);
 36    }
 37
 38    function testAssertFalse_Err_Pass() external {
 39        t._assertFalse(false, CUSTOM_ERROR, EXPECT_PASS);
 40    }
 41
 42    function testAssertFalse_Err_Fail() external {
 43        vm.expectEmit(false, false, false, true);
 44        emit log_named_string("Error", CUSTOM_ERROR);
 45        t._assertFalse(true, CUSTOM_ERROR, EXPECT_FAIL);
 46    }
 47
 48    /*//////////////////////////////////////////////////////////////////////////
 49                                    ASSERT_EQ(BOOL)
 50    //////////////////////////////////////////////////////////////////////////*/
 51
 52    function testAssertEq_Bool_Pass(bool a) external {
 53        t._assertEq(a, a, EXPECT_PASS);
 54    }
 55
 56    function testAssertEq_Bool_Fail(bool a, bool b) external {
 57        vm.assume(a != b);
 58
 59        vm.expectEmit(false, false, false, true);
 60        emit log("Error: a == b not satisfied [bool]");
 61        t._assertEq(a, b, EXPECT_FAIL);
 62    }
 63
 64    function testAssertEq_BoolErr_Pass(bool a) external {
 65        t._assertEq(a, a, CUSTOM_ERROR, EXPECT_PASS);
 66    }
 67
 68    function testAssertEq_BoolErr_Fail(bool a, bool b) external {
 69        vm.assume(a != b);
 70
 71        vm.expectEmit(false, false, false, true);
 72        emit log_named_string("Error", CUSTOM_ERROR);
 73        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
 74    }
 75
 76    /*//////////////////////////////////////////////////////////////////////////
 77                                    ASSERT_EQ(BYTES)
 78    //////////////////////////////////////////////////////////////////////////*/
 79
 80    function testAssertEq_Bytes_Pass(bytes calldata a) external {
 81        t._assertEq(a, a, EXPECT_PASS);
 82    }
 83
 84    function testAssertEq_Bytes_Fail(bytes calldata a, bytes calldata b) external {
 85        vm.assume(keccak256(a) != keccak256(b));
 86
 87        vm.expectEmit(false, false, false, true);
 88        emit log("Error: a == b not satisfied [bytes]");
 89        t._assertEq(a, b, EXPECT_FAIL);
 90    }
 91
 92    function testAssertEq_BytesErr_Pass(bytes calldata a) external {
 93        t._assertEq(a, a, CUSTOM_ERROR, EXPECT_PASS);
 94    }
 95
 96    function testAssertEq_BytesErr_Fail(bytes calldata a, bytes calldata b) external {
 97        vm.assume(keccak256(a) != keccak256(b));
 98
 99        vm.expectEmit(false, false, false, true);
100        emit log_named_string("Error", CUSTOM_ERROR);
101        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
102    }
103
104    /*//////////////////////////////////////////////////////////////////////////
105                                    ASSERT_EQ(ARRAY)
106    //////////////////////////////////////////////////////////////////////////*/
107
108    function testAssertEq_UintArr_Pass(uint256 e0, uint256 e1, uint256 e2) public {
109        uint256[] memory a = new uint256[](3);
110        a[0] = e0;
111        a[1] = e1;
112        a[2] = e2;
113        uint256[] memory b = new uint256[](3);
114        b[0] = e0;
115        b[1] = e1;
116        b[2] = e2;
117
118        t._assertEq(a, b, EXPECT_PASS);
119    }
120
121    function testAssertEq_IntArr_Pass(int256 e0, int256 e1, int256 e2) public {
122        int256[] memory a = new int256[](3);
123        a[0] = e0;
124        a[1] = e1;
125        a[2] = e2;
126        int256[] memory b = new int256[](3);
127        b[0] = e0;
128        b[1] = e1;
129        b[2] = e2;
130
131        t._assertEq(a, b, EXPECT_PASS);
132    }
133
134    function testAssertEq_AddressArr_Pass(address e0, address e1, address e2) public {
135        address[] memory a = new address[](3);
136        a[0] = e0;
137        a[1] = e1;
138        a[2] = e2;
139        address[] memory b = new address[](3);
140        b[0] = e0;
141        b[1] = e1;
142        b[2] = e2;
143
144        t._assertEq(a, b, EXPECT_PASS);
145    }
146
147    function testAssertEq_UintArr_FailEl(uint256 e1) public {
148        vm.assume(e1 != 0);
149        uint256[] memory a = new uint256[](3);
150        uint256[] memory b = new uint256[](3);
151        b[1] = e1;
152
153        vm.expectEmit(false, false, false, true);
154        emit log("Error: a == b not satisfied [uint[]]");
155        t._assertEq(a, b, EXPECT_FAIL);
156    }
157
158    function testAssertEq_IntArr_FailEl(int256 e1) public {
159        vm.assume(e1 != 0);
160        int256[] memory a = new int256[](3);
161        int256[] memory b = new int256[](3);
162        b[1] = e1;
163
164        vm.expectEmit(false, false, false, true);
165        emit log("Error: a == b not satisfied [int[]]");
166        t._assertEq(a, b, EXPECT_FAIL);
167    }
168
169    function testAssertEq_AddressArr_FailEl(address e1) public {
170        vm.assume(e1 != address(0));
171        address[] memory a = new address[](3);
172        address[] memory b = new address[](3);
173        b[1] = e1;
174
175        vm.expectEmit(false, false, false, true);
176        emit log("Error: a == b not satisfied [address[]]");
177        t._assertEq(a, b, EXPECT_FAIL);
178    }
179
180    function testAssertEq_UintArrErr_FailEl(uint256 e1) public {
181        vm.assume(e1 != 0);
182        uint256[] memory a = new uint256[](3);
183        uint256[] memory b = new uint256[](3);
184        b[1] = e1;
185
186        vm.expectEmit(false, false, false, true);
187        emit log_named_string("Error", CUSTOM_ERROR);
188        vm.expectEmit(false, false, false, true);
189        emit log("Error: a == b not satisfied [uint[]]");
190        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
191    }
192
193    function testAssertEq_IntArrErr_FailEl(int256 e1) public {
194        vm.assume(e1 != 0);
195        int256[] memory a = new int256[](3);
196        int256[] memory b = new int256[](3);
197        b[1] = e1;
198
199        vm.expectEmit(false, false, false, true);
200        emit log_named_string("Error", CUSTOM_ERROR);
201        vm.expectEmit(false, false, false, true);
202        emit log("Error: a == b not satisfied [int[]]");
203        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
204    }
205
206    function testAssertEq_AddressArrErr_FailEl(address e1) public {
207        vm.assume(e1 != address(0));
208        address[] memory a = new address[](3);
209        address[] memory b = new address[](3);
210        b[1] = e1;
211
212        vm.expectEmit(false, false, false, true);
213        emit log_named_string("Error", CUSTOM_ERROR);
214        vm.expectEmit(false, false, false, true);
215        emit log("Error: a == b not satisfied [address[]]");
216        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
217    }
218
219    function testAssertEq_UintArr_FailLen(uint256 lenA, uint256 lenB) public {
220        vm.assume(lenA != lenB);
221        vm.assume(lenA <= 10000);
222        vm.assume(lenB <= 10000);
223        uint256[] memory a = new uint256[](lenA);
224        uint256[] memory b = new uint256[](lenB);
225
226        vm.expectEmit(false, false, false, true);
227        emit log("Error: a == b not satisfied [uint[]]");
228        t._assertEq(a, b, EXPECT_FAIL);
229    }
230
231    function testAssertEq_IntArr_FailLen(uint256 lenA, uint256 lenB) public {
232        vm.assume(lenA != lenB);
233        vm.assume(lenA <= 10000);
234        vm.assume(lenB <= 10000);
235        int256[] memory a = new int256[](lenA);
236        int256[] memory b = new int256[](lenB);
237
238        vm.expectEmit(false, false, false, true);
239        emit log("Error: a == b not satisfied [int[]]");
240        t._assertEq(a, b, EXPECT_FAIL);
241    }
242
243    function testAssertEq_AddressArr_FailLen(uint256 lenA, uint256 lenB) public {
244        vm.assume(lenA != lenB);
245        vm.assume(lenA <= 10000);
246        vm.assume(lenB <= 10000);
247        address[] memory a = new address[](lenA);
248        address[] memory b = new address[](lenB);
249
250        vm.expectEmit(false, false, false, true);
251        emit log("Error: a == b not satisfied [address[]]");
252        t._assertEq(a, b, EXPECT_FAIL);
253    }
254
255    function testAssertEq_UintArrErr_FailLen(uint256 lenA, uint256 lenB) public {
256        vm.assume(lenA != lenB);
257        vm.assume(lenA <= 10000);
258        vm.assume(lenB <= 10000);
259        uint256[] memory a = new uint256[](lenA);
260        uint256[] memory b = new uint256[](lenB);
261
262        vm.expectEmit(false, false, false, true);
263        emit log_named_string("Error", CUSTOM_ERROR);
264        vm.expectEmit(false, false, false, true);
265        emit log("Error: a == b not satisfied [uint[]]");
266        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
267    }
268
269    function testAssertEq_IntArrErr_FailLen(uint256 lenA, uint256 lenB) public {
270        vm.assume(lenA != lenB);
271        vm.assume(lenA <= 10000);
272        vm.assume(lenB <= 10000);
273        int256[] memory a = new int256[](lenA);
274        int256[] memory b = new int256[](lenB);
275
276        vm.expectEmit(false, false, false, true);
277        emit log_named_string("Error", CUSTOM_ERROR);
278        vm.expectEmit(false, false, false, true);
279        emit log("Error: a == b not satisfied [int[]]");
280        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
281    }
282
283    function testAssertEq_AddressArrErr_FailLen(uint256 lenA, uint256 lenB) public {
284        vm.assume(lenA != lenB);
285        vm.assume(lenA <= 10000);
286        vm.assume(lenB <= 10000);
287        address[] memory a = new address[](lenA);
288        address[] memory b = new address[](lenB);
289
290        vm.expectEmit(false, false, false, true);
291        emit log_named_string("Error", CUSTOM_ERROR);
292        vm.expectEmit(false, false, false, true);
293        emit log("Error: a == b not satisfied [address[]]");
294        t._assertEq(a, b, CUSTOM_ERROR, EXPECT_FAIL);
295    }
296
297    /*//////////////////////////////////////////////////////////////////////////
298                                    ASSERT_EQ(UINT)
299    //////////////////////////////////////////////////////////////////////////*/
300
301    function testAssertEqUint() public {
302        assertEqUint(uint8(1), uint128(1));
303        assertEqUint(uint64(2), uint64(2));
304    }
305
306    function testFailAssertEqUint() public {
307        assertEqUint(uint64(1), uint96(2));
308        assertEqUint(uint160(3), uint160(4));
309    }
310
311    /*//////////////////////////////////////////////////////////////////////////
312                                    APPROX_EQ_ABS(UINT)
313    //////////////////////////////////////////////////////////////////////////*/
314
315    function testAssertApproxEqAbs_Uint_Pass(uint256 a, uint256 b, uint256 maxDelta) external {
316        vm.assume(stdMath.delta(a, b) <= maxDelta);
317
318        t._assertApproxEqAbs(a, b, maxDelta, EXPECT_PASS);
319    }
320
321    function testAssertApproxEqAbs_Uint_Fail(uint256 a, uint256 b, uint256 maxDelta) external {
322        vm.assume(stdMath.delta(a, b) > maxDelta);
323
324        vm.expectEmit(false, false, false, true);
325        emit log("Error: a ~= b not satisfied [uint]");
326        t._assertApproxEqAbs(a, b, maxDelta, EXPECT_FAIL);
327    }
328
329    function testAssertApproxEqAbs_UintErr_Pass(uint256 a, uint256 b, uint256 maxDelta) external {
330        vm.assume(stdMath.delta(a, b) <= maxDelta);
331
332        t._assertApproxEqAbs(a, b, maxDelta, CUSTOM_ERROR, EXPECT_PASS);
333    }
334
335    function testAssertApproxEqAbs_UintErr_Fail(uint256 a, uint256 b, uint256 maxDelta) external {
336        vm.assume(stdMath.delta(a, b) > maxDelta);
337
338        vm.expectEmit(false, false, false, true);
339        emit log_named_string("Error", CUSTOM_ERROR);
340        t._assertApproxEqAbs(a, b, maxDelta, CUSTOM_ERROR, EXPECT_FAIL);
341    }
342
343    /*//////////////////////////////////////////////////////////////////////////
344                                    APPROX_EQ_ABS_DECIMAL(UINT)
345    //////////////////////////////////////////////////////////////////////////*/
346
347    function testAssertApproxEqAbsDecimal_Uint_Pass(uint256 a, uint256 b, uint256 maxDelta, uint256 decimals)
348        external
349    {
350        vm.assume(stdMath.delta(a, b) <= maxDelta);
351
352        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, EXPECT_PASS);
353    }
354
355    function testAssertApproxEqAbsDecimal_Uint_Fail(uint256 a, uint256 b, uint256 maxDelta, uint256 decimals)
356        external
357    {
358        vm.assume(stdMath.delta(a, b) > maxDelta);
359
360        vm.expectEmit(false, false, false, true);
361        emit log("Error: a ~= b not satisfied [uint]");
362        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, EXPECT_FAIL);
363    }
364
365    function testAssertApproxEqAbsDecimal_UintErr_Pass(uint256 a, uint256 b, uint256 maxDelta, uint256 decimals)
366        external
367    {
368        vm.assume(stdMath.delta(a, b) <= maxDelta);
369
370        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, CUSTOM_ERROR, EXPECT_PASS);
371    }
372
373    function testAssertApproxEqAbsDecimal_UintErr_Fail(uint256 a, uint256 b, uint256 maxDelta, uint256 decimals)
374        external
375    {
376        vm.assume(stdMath.delta(a, b) > maxDelta);
377
378        vm.expectEmit(false, false, false, true);
379        emit log_named_string("Error", CUSTOM_ERROR);
380        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, CUSTOM_ERROR, EXPECT_FAIL);
381    }
382
383    /*//////////////////////////////////////////////////////////////////////////
384                                    APPROX_EQ_ABS(INT)
385    //////////////////////////////////////////////////////////////////////////*/
386
387    function testAssertApproxEqAbs_Int_Pass(int256 a, int256 b, uint256 maxDelta) external {
388        vm.assume(stdMath.delta(a, b) <= maxDelta);
389
390        t._assertApproxEqAbs(a, b, maxDelta, EXPECT_PASS);
391    }
392
393    function testAssertApproxEqAbs_Int_Fail(int256 a, int256 b, uint256 maxDelta) external {
394        vm.assume(stdMath.delta(a, b) > maxDelta);
395
396        vm.expectEmit(false, false, false, true);
397        emit log("Error: a ~= b not satisfied [int]");
398        t._assertApproxEqAbs(a, b, maxDelta, EXPECT_FAIL);
399    }
400
401    function testAssertApproxEqAbs_IntErr_Pass(int256 a, int256 b, uint256 maxDelta) external {
402        vm.assume(stdMath.delta(a, b) <= maxDelta);
403
404        t._assertApproxEqAbs(a, b, maxDelta, CUSTOM_ERROR, EXPECT_PASS);
405    }
406
407    function testAssertApproxEqAbs_IntErr_Fail(int256 a, int256 b, uint256 maxDelta) external {
408        vm.assume(stdMath.delta(a, b) > maxDelta);
409
410        vm.expectEmit(false, false, false, true);
411        emit log_named_string("Error", CUSTOM_ERROR);
412        t._assertApproxEqAbs(a, b, maxDelta, CUSTOM_ERROR, EXPECT_FAIL);
413    }
414
415    /*//////////////////////////////////////////////////////////////////////////
416                                    APPROX_EQ_ABS_DECIMAL(INT)
417    //////////////////////////////////////////////////////////////////////////*/
418
419    function testAssertApproxEqAbsDecimal_Int_Pass(int256 a, int256 b, uint256 maxDelta, uint256 decimals) external {
420        vm.assume(stdMath.delta(a, b) <= maxDelta);
421
422        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, EXPECT_PASS);
423    }
424
425    function testAssertApproxEqAbsDecimal_Int_Fail(int256 a, int256 b, uint256 maxDelta, uint256 decimals) external {
426        vm.assume(stdMath.delta(a, b) > maxDelta);
427
428        vm.expectEmit(false, false, false, true);
429        emit log("Error: a ~= b not satisfied [int]");
430        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, EXPECT_FAIL);
431    }
432
433    function testAssertApproxEqAbsDecimal_IntErr_Pass(int256 a, int256 b, uint256 maxDelta, uint256 decimals)
434        external
435    {
436        vm.assume(stdMath.delta(a, b) <= maxDelta);
437
438        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, CUSTOM_ERROR, EXPECT_PASS);
439    }
440
441    function testAssertApproxEqAbsDecimal_IntErr_Fail(int256 a, int256 b, uint256 maxDelta, uint256 decimals)
442        external
443    {
444        vm.assume(stdMath.delta(a, b) > maxDelta);
445
446        vm.expectEmit(false, false, false, true);
447        emit log_named_string("Error", CUSTOM_ERROR);
448        t._assertApproxEqAbsDecimal(a, b, maxDelta, decimals, CUSTOM_ERROR, EXPECT_FAIL);
449    }
450
451    /*//////////////////////////////////////////////////////////////////////////
452                                    APPROX_EQ_REL(UINT)
453    //////////////////////////////////////////////////////////////////////////*/
454
455    function testAssertApproxEqRel_Uint_Pass(uint256 a, uint256 b, uint256 maxPercentDelta) external {
456        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
457        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
458
459        t._assertApproxEqRel(a, b, maxPercentDelta, EXPECT_PASS);
460    }
461
462    function testAssertApproxEqRel_Uint_Fail(uint256 a, uint256 b, uint256 maxPercentDelta) external {
463        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
464        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
465
466        vm.expectEmit(false, false, false, true);
467        emit log("Error: a ~= b not satisfied [uint]");
468        t._assertApproxEqRel(a, b, maxPercentDelta, EXPECT_FAIL);
469    }
470
471    function testAssertApproxEqRel_UintErr_Pass(uint256 a, uint256 b, uint256 maxPercentDelta) external {
472        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
473        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
474
475        t._assertApproxEqRel(a, b, maxPercentDelta, CUSTOM_ERROR, EXPECT_PASS);
476    }
477
478    function testAssertApproxEqRel_UintErr_Fail(uint256 a, uint256 b, uint256 maxPercentDelta) external {
479        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
480        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
481
482        vm.expectEmit(false, false, false, true);
483        emit log_named_string("Error", CUSTOM_ERROR);
484        t._assertApproxEqRel(a, b, maxPercentDelta, CUSTOM_ERROR, EXPECT_FAIL);
485    }
486
487    /*//////////////////////////////////////////////////////////////////////////
488                                    APPROX_EQ_REL_DECIMAL(UINT)
489    //////////////////////////////////////////////////////////////////////////*/
490
491    function testAssertApproxEqRelDecimal_Uint_Pass(uint256 a, uint256 b, uint256 maxPercentDelta, uint256 decimals)
492        external
493    {
494        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
495        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
496
497        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, EXPECT_PASS);
498    }
499
500    function testAssertApproxEqRelDecimal_Uint_Fail(uint256 a, uint256 b, uint256 maxPercentDelta, uint256 decimals)
501        external
502    {
503        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
504        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
505
506        vm.expectEmit(false, false, false, true);
507        emit log("Error: a ~= b not satisfied [uint]");
508        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, EXPECT_FAIL);
509    }
510
511    function testAssertApproxEqRelDecimal_UintErr_Pass(uint256 a, uint256 b, uint256 maxPercentDelta, uint256 decimals)
512        external
513    {
514        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
515        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
516
517        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, CUSTOM_ERROR, EXPECT_PASS);
518    }
519
520    function testAssertApproxEqRelDecimal_UintErr_Fail(uint256 a, uint256 b, uint256 maxPercentDelta, uint256 decimals)
521        external
522    {
523        vm.assume(a < type(uint128).max && b < type(uint128).max && b != 0);
524        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
525
526        vm.expectEmit(false, false, false, true);
527        emit log_named_string("Error", CUSTOM_ERROR);
528        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, CUSTOM_ERROR, EXPECT_FAIL);
529    }
530
531    /*//////////////////////////////////////////////////////////////////////////
532                                    APPROX_EQ_REL(INT)
533    //////////////////////////////////////////////////////////////////////////*/
534
535    function testAssertApproxEqRel_Int_Pass(int128 a, int128 b, uint128 maxPercentDelta) external {
536        vm.assume(b != 0);
537        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
538
539        t._assertApproxEqRel(a, b, maxPercentDelta, EXPECT_PASS);
540    }
541
542    function testAssertApproxEqRel_Int_Fail(int128 a, int128 b, uint128 maxPercentDelta) external {
543        vm.assume(b != 0);
544        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
545
546        vm.expectEmit(false, false, false, true);
547        emit log("Error: a ~= b not satisfied [int]");
548        t._assertApproxEqRel(a, b, maxPercentDelta, EXPECT_FAIL);
549    }
550
551    function testAssertApproxEqRel_IntErr_Pass(int128 a, int128 b, uint128 maxPercentDelta) external {
552        vm.assume(b != 0);
553        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
554
555        t._assertApproxEqRel(a, b, maxPercentDelta, CUSTOM_ERROR, EXPECT_PASS);
556    }
557
558    function testAssertApproxEqRel_IntErr_Fail(int128 a, int128 b, uint128 maxPercentDelta) external {
559        vm.assume(b != 0);
560        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
561
562        vm.expectEmit(false, false, false, true);
563        emit log_named_string("Error", CUSTOM_ERROR);
564        t._assertApproxEqRel(a, b, maxPercentDelta, CUSTOM_ERROR, EXPECT_FAIL);
565    }
566
567    /*//////////////////////////////////////////////////////////////////////////
568                                    APPROX_EQ_REL_DECIMAL(INT)
569    //////////////////////////////////////////////////////////////////////////*/
570
571    function testAssertApproxEqRelDecimal_Int_Pass(int128 a, int128 b, uint128 maxPercentDelta, uint128 decimals)
572        external
573    {
574        vm.assume(b != 0);
575        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
576
577        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, EXPECT_PASS);
578    }
579
580    function testAssertApproxEqRelDecimal_Int_Fail(int128 a, int128 b, uint128 maxPercentDelta, uint128 decimals)
581        external
582    {
583        vm.assume(b != 0);
584        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
585
586        vm.expectEmit(false, false, false, true);
587        emit log("Error: a ~= b not satisfied [int]");
588        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, EXPECT_FAIL);
589    }
590
591    function testAssertApproxEqRelDecimal_IntErr_Pass(int128 a, int128 b, uint128 maxPercentDelta, uint128 decimals)
592        external
593    {
594        vm.assume(b != 0);
595        vm.assume(stdMath.percentDelta(a, b) <= maxPercentDelta);
596
597        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, CUSTOM_ERROR, EXPECT_PASS);
598    }
599
600    function testAssertApproxEqRelDecimal_IntErr_Fail(int128 a, int128 b, uint128 maxPercentDelta, uint128 decimals)
601        external
602    {
603        vm.assume(b != 0);
604        vm.assume(stdMath.percentDelta(a, b) > maxPercentDelta);
605
606        vm.expectEmit(false, false, false, true);
607        emit log_named_string("Error", CUSTOM_ERROR);
608        t._assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, CUSTOM_ERROR, EXPECT_FAIL);
609    }
610}
611
612contract TestTest is Test {
613    modifier expectFailure(bool expectFail) {
614        bool preState = vm.load(HEVM_ADDRESS, bytes32("failed")) != bytes32(0x00);
615        _;
616        bool postState = vm.load(HEVM_ADDRESS, bytes32("failed")) != bytes32(0x00);
617
618        if (preState == true) {
619            return;
620        }
621
622        if (expectFail) {
623            require(postState == true, "expected failure not triggered");
624
625            // unwind the expected failure
626            vm.store(HEVM_ADDRESS, bytes32("failed"), bytes32(uint256(0x00)));
627        } else {
628            require(postState == false, "unexpected failure was triggered");
629        }
630    }
631
632    function _fail(string memory err) external expectFailure(true) {
633        fail(err);
634    }
635
636    function _assertFalse(bool data, bool expectFail) external expectFailure(expectFail) {
637        assertFalse(data);
638    }
639
640    function _assertFalse(bool data, string memory err, bool expectFail) external expectFailure(expectFail) {
641        assertFalse(data, err);
642    }
643
644    function _assertEq(bool a, bool b, bool expectFail) external expectFailure(expectFail) {
645        assertEq(a, b);
646    }
647
648    function _assertEq(bool a, bool b, string memory err, bool expectFail) external expectFailure(expectFail) {
649        assertEq(a, b, err);
650    }
651
652    function _assertEq(bytes memory a, bytes memory b, bool expectFail) external expectFailure(expectFail) {
653        assertEq(a, b);
654    }
655
656    function _assertEq(bytes memory a, bytes memory b, string memory err, bool expectFail)
657        external
658        expectFailure(expectFail)
659    {
660        assertEq(a, b, err);
661    }
662
663    function _assertEq(uint256[] memory a, uint256[] memory b, bool expectFail) external expectFailure(expectFail) {
664        assertEq(a, b);
665    }
666
667    function _assertEq(int256[] memory a, int256[] memory b, bool expectFail) external expectFailure(expectFail) {
668        assertEq(a, b);
669    }
670
671    function _assertEq(address[] memory a, address[] memory b, bool expectFail) external expectFailure(expectFail) {
672        assertEq(a, b);
673    }
674
675    function _assertEq(uint256[] memory a, uint256[] memory b, string memory err, bool expectFail)
676        external
677        expectFailure(expectFail)
678    {
679        assertEq(a, b, err);
680    }
681
682    function _assertEq(int256[] memory a, int256[] memory b, string memory err, bool expectFail)
683        external
684        expectFailure(expectFail)
685    {
686        assertEq(a, b, err);
687    }
688
689    function _assertEq(address[] memory a, address[] memory b, string memory err, bool expectFail)
690        external
691        expectFailure(expectFail)
692    {
693        assertEq(a, b, err);
694    }
695
696    function _assertApproxEqAbs(uint256 a, uint256 b, uint256 maxDelta, bool expectFail)
697        external
698        expectFailure(expectFail)
699    {
700        assertApproxEqAbs(a, b, maxDelta);
701    }
702
703    function _assertApproxEqAbs(uint256 a, uint256 b, uint256 maxDelta, string memory err, bool expectFail)
704        external
705        expectFailure(expectFail)
706    {
707        assertApproxEqAbs(a, b, maxDelta, err);
708    }
709
710    function _assertApproxEqAbsDecimal(uint256 a, uint256 b, uint256 maxDelta, uint256 decimals, bool expectFail)
711        external
712        expectFailure(expectFail)
713    {
714        assertApproxEqAbsDecimal(a, b, maxDelta, decimals);
715    }
716
717    function _assertApproxEqAbsDecimal(
718        uint256 a,
719        uint256 b,
720        uint256 maxDelta,
721        uint256 decimals,
722        string memory err,
723        bool expectFail
724    ) external expectFailure(expectFail) {
725        assertApproxEqAbsDecimal(a, b, maxDelta, decimals, err);
726    }
727
728    function _assertApproxEqAbs(int256 a, int256 b, uint256 maxDelta, bool expectFail)
729        external
730        expectFailure(expectFail)
731    {
732        assertApproxEqAbs(a, b, maxDelta);
733    }
734
735    function _assertApproxEqAbs(int256 a, int256 b, uint256 maxDelta, string memory err, bool expectFail)
736        external
737        expectFailure(expectFail)
738    {
739        assertApproxEqAbs(a, b, maxDelta, err);
740    }
741
742    function _assertApproxEqAbsDecimal(int256 a, int256 b, uint256 maxDelta, uint256 decimals, bool expectFail)
743        external
744        expectFailure(expectFail)
745    {
746        assertApproxEqAbsDecimal(a, b, maxDelta, decimals);
747    }
748
749    function _assertApproxEqAbsDecimal(
750        int256 a,
751        int256 b,
752        uint256 maxDelta,
753        uint256 decimals,
754        string memory err,
755        bool expectFail
756    ) external expectFailure(expectFail) {
757        assertApproxEqAbsDecimal(a, b, maxDelta, decimals, err);
758    }
759
760    function _assertApproxEqRel(uint256 a, uint256 b, uint256 maxPercentDelta, bool expectFail)
761        external
762        expectFailure(expectFail)
763    {
764        assertApproxEqRel(a, b, maxPercentDelta);
765    }
766
767    function _assertApproxEqRel(uint256 a, uint256 b, uint256 maxPercentDelta, string memory err, bool expectFail)
768        external
769        expectFailure(expectFail)
770    {
771        assertApproxEqRel(a, b, maxPercentDelta, err);
772    }
773
774    function _assertApproxEqRelDecimal(uint256 a, uint256 b, uint256 maxPercentDelta, uint256 decimals, bool expectFail)
775        external
776        expectFailure(expectFail)
777    {
778        assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals);
779    }
780
781    function _assertApproxEqRelDecimal(
782        uint256 a,
783        uint256 b,
784        uint256 maxPercentDelta,
785        uint256 decimals,
786        string memory err,
787        bool expectFail
788    ) external expectFailure(expectFail) {
789        assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, err);
790    }
791
792    function _assertApproxEqRel(int256 a, int256 b, uint256 maxPercentDelta, bool expectFail)
793        external
794        expectFailure(expectFail)
795    {
796        assertApproxEqRel(a, b, maxPercentDelta);
797    }
798
799    function _assertApproxEqRel(int256 a, int256 b, uint256 maxPercentDelta, string memory err, bool expectFail)
800        external
801        expectFailure(expectFail)
802    {
803        assertApproxEqRel(a, b, maxPercentDelta, err);
804    }
805
806    function _assertApproxEqRelDecimal(int256 a, int256 b, uint256 maxPercentDelta, uint256 decimals, bool expectFail)
807        external
808        expectFailure(expectFail)
809    {
810        assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals);
811    }
812
813    function _assertApproxEqRelDecimal(
814        int256 a,
815        int256 b,
816        uint256 maxPercentDelta,
817        uint256 decimals,
818        string memory err,
819        bool expectFail
820    ) external expectFailure(expectFail) {
821        assertApproxEqRelDecimal(a, b, maxPercentDelta, decimals, err);
822    }
823}