|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity 0.8.27; |
| 3 | + |
| 4 | +import "forge-std/console.sol"; |
| 5 | +import { Test } from "forge-std/Test.sol"; |
| 6 | +import { PPMMath } from "../../contracts/libraries/PPMMath.sol"; |
| 7 | + |
| 8 | +contract PPMMathTest is Test { |
| 9 | + uint32 private constant MAX_PPM = 1000000; |
| 10 | + |
| 11 | + function test_mulPPM(uint256 a, uint256 b) public pure { |
| 12 | + a = bound(a, 0, MAX_PPM); |
| 13 | + b = bound(b, 0, type(uint256).max / MAX_PPM); |
| 14 | + |
| 15 | + uint256 result = PPMMath.mulPPM(a, b); |
| 16 | + assertEq(result, (a * b) / MAX_PPM); |
| 17 | + } |
| 18 | + |
| 19 | + function test_mulPPMRoundUp(uint256 a, uint256 b) public pure { |
| 20 | + a = bound(a, 0, type(uint256).max / MAX_PPM); |
| 21 | + b = bound(b, 0, MAX_PPM); |
| 22 | + |
| 23 | + uint256 result = PPMMath.mulPPMRoundUp(a, b); |
| 24 | + assertEq(result, a - PPMMath.mulPPM(a, MAX_PPM - b)); |
| 25 | + } |
| 26 | + |
| 27 | + function test_isValidPPM(uint256 value) public pure { |
| 28 | + bool result = PPMMath.isValidPPM(value); |
| 29 | + assert(result == (value <= MAX_PPM)); |
| 30 | + } |
| 31 | + |
| 32 | + function test_mullPPM_RevertWhen_InvalidPPM(uint256 a, uint256 b) public { |
| 33 | + a = bound(a, MAX_PPM + 1, type(uint256).max); |
| 34 | + b = bound(b, MAX_PPM + 1, type(uint256).max); |
| 35 | + bytes memory expectedError = abi.encodeWithSelector(PPMMath.PPMMathInvalidMulPPM.selector, a, b); |
| 36 | + vm.expectRevert(expectedError); |
| 37 | + PPMMath.mulPPM(a, b); |
| 38 | + } |
| 39 | + |
| 40 | + function test_mullPPMRoundUp_RevertWhen_InvalidPPM(uint256 a, uint256 b) public { |
| 41 | + b = bound(b, MAX_PPM + 1, type(uint256).max); |
| 42 | + bytes memory expectedError = abi.encodeWithSelector(PPMMath.PPMMathInvalidPPM.selector, b); |
| 43 | + vm.expectRevert(expectedError); |
| 44 | + PPMMath.mulPPMRoundUp(a, b); |
| 45 | + } |
| 46 | +} |
0 commit comments