Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BalanceDelta tests #537

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 80 additions & 9 deletions test/types/BalanceDelta.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {BalanceDelta, toBalanceDelta} from "../../src/types/BalanceDelta.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);
}
}
Loading