test(libraries): add math library unit tests

This commit is contained in:
dualfroz committed 2026-08-27 11:44:00 +02:00
1 parent b2ec603567
commit da99c14cdd
2 files changed
+160

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
import {Test} from "forge-std/Test.sol";
import {Math} from "../src/libraries/Math.sol";
import {FluxLibrary} from "../src/libraries/FluxLibrary.sol";
contract LibHarness {
function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) external pure returns (uint256) {
return FluxLibrary.quote(amountA, reserveA, reserveB);
}
function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut) external pure returns (uint256) {
return FluxLibrary.getAmountOut(amountIn, reserveIn, reserveOut);
}
}
contract MathTest is Test {
LibHarness private harness;
function setUp() public {
harness = new LibHarness();
}
function testSqrtSmallValues() public pure {
assertEq(Math.sqrt(0), 0);
assertEq(Math.sqrt(1), 1);
assertEq(Math.sqrt(2), 1);
assertEq(Math.sqrt(3), 1);
assertEq(Math.sqrt(4), 2);
assertEq(Math.sqrt(15), 3);
assertEq(Math.sqrt(16), 4);
assertEq(Math.sqrt(99), 9);
assertEq(Math.sqrt(100), 10);
}
function testSqrtLargeValue() public pure {
assertEq(Math.sqrt(100e18 * 400e18), 2e20);
assertEq(Math.sqrt(type(uint256).max), 340282366920938463463374607431768211455);
}
function testMin() public pure {
assertEq(Math.min(3, 7), 3);
assertEq(Math.min(7, 3), 3);
assertEq(Math.min(5, 5), 5);
}
function testQuote() public pure {
assertEq(FluxLibrary.quote(10e18, 100e18, 400e18), 40e18);
assertEq(FluxLibrary.quote(50e18, 100e18, 400e18), 200e18);
}
function testGetAmountOut() public pure {
assertEq(FluxLibrary.getAmountOut(10e18, 100e18, 400e18), 36264435755205965263);
}
function testGetAmountIn() public pure {
assertEq(FluxLibrary.getAmountIn(50e18, 100e18, 400e18), 14328700386874910446);
}
function testQuoteRevertsOnZeroLiquidity() public {
vm.expectRevert(bytes("FluxLibrary: INSUFFICIENT_LIQUIDITY"));
harness.quote(10e18, 0, 400e18);
}
function testGetAmountOutRevertsOnZeroInput() public {
vm.expectRevert(bytes("FluxLibrary: INSUFFICIENT_INPUT_AMOUNT"));
harness.getAmountOut(0, 100e18, 400e18);
}
}