Skip to content

Commit

Permalink
Add fuzz tests for boundary conditions in TickMath
Browse files Browse the repository at this point in the history
This commit adds fuzz tests in the `TickMath` test suite to verify that an exception is thrown when exceeding the allowed tick and square root price ranges. The test cases are designed to provide random values that exceed the range of values that the functions `getSqrtPriceAtTick` and `getTickAtSqrtPrice` can operate on without errors.
  • Loading branch information
shuhuiluo committed May 19, 2024
1 parent 5674d76 commit dc7a0f3
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/libraries/TickMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ contract TickMathTestTest is Test, JavascriptFfi {
tickMath.getSqrtPriceAtTick(MAX_TICK + 1);
}

function test_fuzz_getSqrtPriceAtTick_throwsForTooLarge(int24 tick) public {
if (tick > 0) {
tick = int24(bound(tick, MAX_TICK + 1, type(int24).max));
} else {
tick = int24(bound(tick, type(int24).min, MIN_TICK - 1));
}
vm.expectRevert(TickMath.InvalidTick.selector);
tickMath.getSqrtPriceAtTick(tick);
}

function test_getSqrtPriceAtTick_isValidMinTick() public view {
assertEq(tickMath.getSqrtPriceAtTick(MIN_TICK), tickMath.MIN_SQRT_PRICE());
assertEq(tickMath.getSqrtPriceAtTick(MIN_TICK), 4295128739);
Expand Down Expand Up @@ -96,6 +106,16 @@ contract TickMathTestTest is Test, JavascriptFfi {
tickMath.getTickAtSqrtPrice(MAX_SQRT_PRICE + 1);
}

function test_fuzz_getTickAtSqrtPrice_throwsForInvalid(uint160 sqrtPriceX96, bool gte) public {
if (gte) {
sqrtPriceX96 = uint160(bound(sqrtPriceX96, MAX_SQRT_PRICE, type(uint160).max));
} else {
sqrtPriceX96 = uint160(bound(sqrtPriceX96, 0, MIN_SQRT_PRICE - 1));
}
vm.expectRevert(TickMath.InvalidSqrtPrice.selector);
tickMath.getTickAtSqrtPrice(sqrtPriceX96);
}

function test_getTickAtSqrtPrice_isValidMinSqrtPrice() public view {
assertEq(tickMath.getTickAtSqrtPrice(MIN_SQRT_PRICE), MIN_TICK);
}
Expand Down

0 comments on commit dc7a0f3

Please sign in to comment.