From ca0122cfd13fc3556794d6b4b5abadb6893ac412 Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Wed, 24 Apr 2024 14:40:39 -0400 Subject: [PATCH 1/4] add UnsafeMath test file --- test/libraries/UnsafeMath.t.sol | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 test/libraries/UnsafeMath.t.sol diff --git a/test/libraries/UnsafeMath.t.sol b/test/libraries/UnsafeMath.t.sol new file mode 100644 index 000000000..5f3904561 --- /dev/null +++ b/test/libraries/UnsafeMath.t.sol @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import {Test} from "forge-std/Test.sol"; From c24575be9fbefb9f37d28b12ba5159aa3e92d75d Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Wed, 24 Apr 2024 17:26:52 -0400 Subject: [PATCH 2/4] unsafe math test --- test/libraries/UnsafeMath.t.sol | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/libraries/UnsafeMath.t.sol b/test/libraries/UnsafeMath.t.sol index 5f3904561..ced2d4333 100644 --- a/test/libraries/UnsafeMath.t.sol +++ b/test/libraries/UnsafeMath.t.sol @@ -2,3 +2,35 @@ pragma solidity ^0.8.20; import {Test} from "forge-std/Test.sol"; +import {UnsafeMath} from "../../src/libraries/UnsafeMath.sol"; + +contract UnsafeMathTest is Test { + using UnsafeMath for uint256; + + uint256 constant Q128 = 2 ** 128; + uint256 constant MAX_UINT256 = type(uint256).max; + + function test_divRoundingUp_zeroDoesNotRevert(uint256 x) public { + x.divRoundingUp(0); + } + + function test_divRoundingUp_maxInput() public { + assertEq(MAX_UINT256.divRoundingUp(MAX_UINT256), 1); + } + + function test_divRoundingUp_RoundsUp() public { + uint256 result = Q128 / 3 + 1; + assertEq(Q128.divRoundingUp(3), result); + } + + function test_fuzz_divRoundingUp(uint256 x, uint256 y) public { + vm.assume(y != 0); + uint256 z = x.divRoundingUp(y); + uint256 diff = z - (x / y); + if (x % y == 0) { + assertEq(diff, 0); + } else { + assertEq(diff, 1); + } + } +} \ No newline at end of file From 4c7dafc7ad1f63d4b9463e3a581f2130b2e4634e Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Wed, 24 Apr 2024 17:31:21 -0400 Subject: [PATCH 3/4] fix format, remove echidna test --- src/test/UnsafeMathEchidnaTest.sol | 17 ----------------- test/libraries/UnsafeMath.t.sol | 4 ++-- 2 files changed, 2 insertions(+), 19 deletions(-) delete mode 100644 src/test/UnsafeMathEchidnaTest.sol diff --git a/src/test/UnsafeMathEchidnaTest.sol b/src/test/UnsafeMathEchidnaTest.sol deleted file mode 100644 index 7d65ed0d9..000000000 --- a/src/test/UnsafeMathEchidnaTest.sol +++ /dev/null @@ -1,17 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.20; - -import {UnsafeMath} from "../libraries/UnsafeMath.sol"; - -contract UnsafeMathEchidnaTest { - function checkDivRoundingUp(uint256 x, uint256 d) external pure { - require(d > 0); - uint256 z = UnsafeMath.divRoundingUp(x, d); - uint256 diff = z - (x / d); - if (x % d == 0) { - assert(diff == 0); - } else { - assert(diff == 1); - } - } -} diff --git a/test/libraries/UnsafeMath.t.sol b/test/libraries/UnsafeMath.t.sol index ced2d4333..963216615 100644 --- a/test/libraries/UnsafeMath.t.sol +++ b/test/libraries/UnsafeMath.t.sol @@ -10,7 +10,7 @@ contract UnsafeMathTest is Test { uint256 constant Q128 = 2 ** 128; uint256 constant MAX_UINT256 = type(uint256).max; - function test_divRoundingUp_zeroDoesNotRevert(uint256 x) public { + function test_divRoundingUp_zeroDoesNotRevert(uint256 x) public pure { x.divRoundingUp(0); } @@ -33,4 +33,4 @@ contract UnsafeMathTest is Test { assertEq(diff, 1); } } -} \ No newline at end of file +} From bc5f8c0a95d512198091c83cff66b550d6e88cdf Mon Sep 17 00:00:00 2001 From: Diana Kocsis Date: Thu, 25 Apr 2024 10:36:22 -0400 Subject: [PATCH 4/4] add invariant test --- test/libraries/UnsafeMath.t.sol | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/libraries/UnsafeMath.t.sol b/test/libraries/UnsafeMath.t.sol index 963216615..501d0bbf1 100644 --- a/test/libraries/UnsafeMath.t.sol +++ b/test/libraries/UnsafeMath.t.sol @@ -24,6 +24,12 @@ contract UnsafeMathTest is Test { } function test_fuzz_divRoundingUp(uint256 x, uint256 y) public { + vm.assume(y != 0); + uint256 result = x.divRoundingUp(y); + assertTrue(result == x / y || result == x / y + 1); + } + + function test_invariant_divRoundingUp(uint256 x, uint256 y) public { vm.assume(y != 0); uint256 z = x.divRoundingUp(y); uint256 diff = z - (x / y);