Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMist committed Apr 29, 2024
1 parent 33565dc commit b0cd33f
Showing 1 changed file with 79 additions and 8 deletions.
87 changes: 79 additions & 8 deletions test/libraries/BalanceDelta.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Test} from "forge-std/Test.sol";
import {BalanceDelta, toBalanceDelta} from "../../src/types/BalanceDelta.sol";

contract TestBalanceDelta is Test {
function testToBalanceDelta() public {
function test_toBalanceDelta() public {
BalanceDelta balanceDelta = toBalanceDelta(0, 0);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);
Expand All @@ -27,17 +27,52 @@ contract TestBalanceDelta is Test {
assertEq(balanceDelta.amount1(), type(int128).min);
}

function testToBalanceDelta(int128 x, int128 y) public {
function test_fuzz_toBalanceDelta(int128 x, int128 y) public {
BalanceDelta balanceDelta = toBalanceDelta(x, y);
int256 expectedBD = int256(uint256(bytes32(abi.encodePacked(x, y))));
assertEq(BalanceDelta.unwrap(balanceDelta), expectedBD);
}

function test_fuzz_amount0_amount1(int128 x, int128 y) public {
BalanceDelta balanceDelta = toBalanceDelta(x, y);
assertEq(balanceDelta.amount0(), x);
assertEq(balanceDelta.amount1(), y);
}

function testAdd(int128 a, int128 b, int128 c, int128 d) public {
function test_add() public {
BalanceDelta balanceDelta = toBalanceDelta(0, 0) + toBalanceDelta(0, 0);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);

balanceDelta = toBalanceDelta(-1000, 1000) + toBalanceDelta(1000, -1000);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);

balanceDelta =
toBalanceDelta(type(int128).min, type(int128).max) + toBalanceDelta(type(int128).max, type(int128).min);
assertEq(balanceDelta.amount0(), -1);
assertEq(balanceDelta.amount1(), -1);

balanceDelta = toBalanceDelta(type(int128).max / 2 + 1, type(int128).max / 2 + 1)
+ toBalanceDelta(type(int128).max / 2, type(int128).max / 2);
assertEq(balanceDelta.amount0(), type(int128).max);
assertEq(balanceDelta.amount1(), type(int128).max);
}

function test_add_revertsOnOverflow() public {
// should revert because type(int128).max + 1 is not possible
vm.expectRevert();
toBalanceDelta(type(int128).max, 0) + toBalanceDelta(1, 0);

vm.expectRevert();
toBalanceDelta(0, type(int128).max) + toBalanceDelta(0, 1);
}

function test_fuzz_add(int128 a, int128 b, int128 c, int128 d) public {
int256 ac = int256(a) + c;
int256 bd = int256(b) + d;

// make sure the addition doesn't overflow
// if the addition overflows it should revert
if (ac != int128(ac) || bd != int128(bd)) {
vm.expectRevert();
}
Expand All @@ -47,16 +82,52 @@ contract TestBalanceDelta is Test {
assertEq(balanceDelta.amount1(), bd);
}

function testSub(int128 a, int128 b, int128 c, int128 d) public {
function test_sub() public {
BalanceDelta balanceDelta = toBalanceDelta(0, 0) - toBalanceDelta(0, 0);
assertEq(balanceDelta.amount0(), 0);
assertEq(balanceDelta.amount1(), 0);

balanceDelta = toBalanceDelta(-1000, 1000) - toBalanceDelta(1000, -1000);
assertEq(balanceDelta.amount0(), -2000);
assertEq(balanceDelta.amount1(), 2000);

balanceDelta =
toBalanceDelta(-1000, -1000) - toBalanceDelta(-(type(int128).min + 1000), -(type(int128).min + 1000));
assertEq(balanceDelta.amount0(), type(int128).min);
assertEq(balanceDelta.amount1(), type(int128).min);

balanceDelta = toBalanceDelta(type(int128).min / 2, type(int128).min / 2)
- toBalanceDelta(-(type(int128).min / 2), -(type(int128).min / 2));
assertEq(balanceDelta.amount0(), type(int128).min);
assertEq(balanceDelta.amount1(), type(int128).min);
}

function test_sub_revertsOnUnderflow() public {
// should revert because type(int128).min - 1 is not possible
vm.expectRevert();
toBalanceDelta(type(int128).min, 0) - toBalanceDelta(1, 0);

vm.expectRevert();
toBalanceDelta(0, type(int128).min) - toBalanceDelta(0, 1);
}

function test_fuzz_sub(int128 a, int128 b, int128 c, int128 d) public {
int256 ac = int256(a) - c;
int256 bd = int256(b) - d;

// make sure the subtraction doesn't underflow
vm.assume(ac == int128(ac));
vm.assume(bd == int128(bd));
// if the subtraction underflows it should revert
if (ac != int128(ac) || bd != int128(bd)) {
vm.expectRevert();
}

BalanceDelta balanceDelta = toBalanceDelta(a, b) - toBalanceDelta(c, d);
assertEq(balanceDelta.amount0(), ac);
assertEq(balanceDelta.amount1(), bd);
}

function test_fuzz_eq(int128 a, int128 b, int128 c, int128 d) public {
bool isEqual = (toBalanceDelta(a, b) == toBalanceDelta(c, d));
if (a == c && b == d) assertTrue(isEqual);
else assertFalse(isEqual);
}
}

0 comments on commit b0cd33f

Please sign in to comment.