From b0cd33ffe539f7a79495e8b4cde917e223dbd518 Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Mon, 29 Apr 2024 14:38:34 +0800 Subject: [PATCH] test: update test --- test/libraries/BalanceDelta.t.sol | 87 ++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/test/libraries/BalanceDelta.t.sol b/test/libraries/BalanceDelta.t.sol index dc1dcf5b..f611d6a2 100644 --- a/test/libraries/BalanceDelta.t.sol +++ b/test/libraries/BalanceDelta.t.sol @@ -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); @@ -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(); } @@ -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); + } }