Skip to content

Commit

Permalink
Add fuzz test for flipTick and update isInitialized function
Browse files Browse the repository at this point in the history
A new fuzz test function `test_fuzz_flipTick` has been added to the TickBitmap library for improved testing accuracy. Additionally, the `isInitialized` function has been re-implemented to enhance its precision and correctness.
  • Loading branch information
shuhuiluo committed May 18, 2024
1 parent f419b51 commit aa029f2
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions test/libraries/TickBitmap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ contract TickBitmapTest is Test, GasSnapshot {
snapEnd();
}

function test_fuzz_flipTick(int24 tick, int24 tickSpacing) public {
tickSpacing = int24(bound(tickSpacing, 1, type(int24).max));

if (tick % tickSpacing != 0) {
vm.expectRevert(abi.encodeWithSelector(TickBitmap.TickMisaligned.selector, tick, tickSpacing));
bitmap.flipTick(tick, tickSpacing);
} else {
bool initialized = isInitialized(tick, tickSpacing);
bitmap.flipTick(tick, tickSpacing);
assertEq(isInitialized(tick, tickSpacing), !initialized);
}
}

function test_nextInitializedTickWithinOneWord_lteFalse_returnsTickToRightIfAtInitializedTick() public view {
(int24 next, bool initialized) = bitmap.nextInitializedTickWithinOneWord(78, 1, false);
assertEq(next, 84);
Expand Down Expand Up @@ -301,9 +314,17 @@ contract TickBitmapTest is Test, GasSnapshot {
}
}

function isInitialized(int24 tick, int24 tickSpacing) internal view returns (bool) {
unchecked {
int24 compressed = tick / tickSpacing;
if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity
(int16 wordPos, uint8 bitPos) = TickBitmap.position(compressed);
return bitmap[wordPos] & (1 << bitPos) != 0;
}
}

function isInitialized(int24 tick) internal view returns (bool) {
(int24 next, bool initialized) = bitmap.nextInitializedTickWithinOneWord(tick, 1, true);
return next == tick ? initialized : false;
return isInitialized(tick, 1);
}

function flipTick(int24 tick) internal {
Expand Down

0 comments on commit aa029f2

Please sign in to comment.