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 new file mode 100644 index 000000000..501d0bbf1 --- /dev/null +++ b/test/libraries/UnsafeMath.t.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: UNLICENSED +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 pure { + 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 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); + if (x % y == 0) { + assertEq(diff, 0); + } else { + assertEq(diff, 1); + } + } +}