Skip to content

Commit

Permalink
test: refactor bound tests
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Nov 16, 2023
1 parent d68db06 commit 10ec24d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions test/UtilsLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import "../lib/forge-std/src/Test.sol";
contract UtilsTest is Test {
using UtilsLib for int256;

function testBound(int256 x, int256 low, int256 high) public {
if (x <= high) {
if (x >= low) assertEq(x.bound(low, high), x);
else assertEq(x.bound(low, high), low);
} else {
if (low <= high) assertEq(x.bound(low, high), high);
else assertEq(x.bound(low, high), low);
}
function testBoundExpected(int256 x, int256 low, int256 high) public {
int256 expected = x <= high ? (x >= low ? x : low) : (low <= high ? high : low);
assertEq(x.bound(low, high), expected);
}

function testBoundMaxMin(int256 x, int256 low, int256 high) public {
int256 maxMin = _max(_min(x, high), low);
assertEq(x.bound(low, high), maxMin);
}

function _min(int256 a, int256 b) private pure returns (int256) {
return a > b ? b : a;
}

function _max(int256 a, int256 b) private pure returns (int256) {
return a > b ? a : b;
}
}

0 comments on commit 10ec24d

Please sign in to comment.