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

test: try running prb-math solidity tests in the example-project #5808

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions v-next/example-project/contracts/prb-math/test/Base.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19 <0.9.0;

import { StdAssertions } from "forge-std/src/StdAssertions.sol";
import { StdCheats } from "forge-std/src/StdCheats.sol";
import { Vm } from "forge-std/src/Vm.sol";

import { PRBMathAssertions } from "./utils/Assertions.sol";
import { PRBMathUtils } from "./utils/Utils.sol";

/// @notice Base test contract with common logic needed by all tests.
abstract contract Base_Test is StdAssertions, StdCheats, PRBMathAssertions, PRBMathUtils {
/*//////////////////////////////////////////////////////////////////////////
STRUCTS
//////////////////////////////////////////////////////////////////////////*/

struct Users {
address alice;
address bob;
address eve;
}

/*//////////////////////////////////////////////////////////////////////////
CHEATCODES
//////////////////////////////////////////////////////////////////////////*/

/// @dev An instance of the Foundry VM, which contains cheatcodes for testing.
Vm internal constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

/*//////////////////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////////////////*/

int256 internal constant MAX_INT256 = type(int256).max;

uint128 internal constant MAX_UINT128 = type(uint128).max;

uint128 internal constant MAX_UINT40 = type(uint40).max;

int256 internal constant MIN_INT256 = type(int256).min;

/*//////////////////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////////////////*/

Users internal users;

/*//////////////////////////////////////////////////////////////////////////
SET-UP FUNCTION
//////////////////////////////////////////////////////////////////////////*/

function setUp() public virtual {
// Create users for testing.
users = Users({ alice: makeAddr("Alice"), bob: makeAddr("Bob"), eve: makeAddr("Eve") });

// Make Alice the `msg.sender` and `tx.origin` for all subsequent calls.
vm.startPrank({ msgSender: users.alice, txOrigin: users.alice });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19 <0.9.0;

import {
PRBMathCastingUint128 as CastingUint128,
PRBMath_IntoSD1x18_Overflow,
PRBMath_IntoUD2x18_Overflow
} from "@prb/math/src/casting/Uint128.sol";
import { uMAX_SD1x18 } from "@prb/math/src/sd1x18/Constants.sol";
import { SD1x18 } from "@prb/math/src/sd1x18/ValueType.sol";
import { SD59x18 } from "@prb/math/src/sd59x18/ValueType.sol";
import { uMAX_UD2x18 } from "@prb/math/src/ud2x18/Constants.sol";
import { UD2x18 } from "@prb/math/src/ud2x18/ValueType.sol";
import { UD60x18 } from "@prb/math/src/ud60x18/ValueType.sol";

import { Base_Test } from "../../Base.t.sol";

/// @dev Collection of tests for the casting library available for uint128.
contract CastingUint128_Test is Base_Test {
using CastingUint128 for uint128;

function testFuzz_RevertWhen_OverflowSD1x18(uint128 x) external {
x = boundUint128(x, uint128(uint64(uMAX_SD1x18)) + 1, MAX_UINT128);
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoSD1x18_Overflow.selector, x));
x.intoSD1x18();
}

function testFuzz_intoSD1x18(uint128 x) external pure {
x = boundUint128(x, 0, uint128(uint64(uMAX_SD1x18)));
SD1x18 actual = x.intoSD1x18();
SD1x18 expected = SD1x18.wrap(int64(uint64(x)));
assertEq(actual, expected, "uint128 intoSD1x18");
}

function testFuzz_intoSD59x18(uint128 x) external pure {
SD59x18 actual = x.intoSD59x18();
SD59x18 expected = SD59x18.wrap(int256(uint256(x)));
assertEq(actual, expected, "uint128 intoSD59x18");
}

function testFuzz_RevertWhen_OverflowUD2x18(uint128 x) external {
x = boundUint128(x, uint128(uMAX_UD2x18) + 1, MAX_UINT128);
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoUD2x18_Overflow.selector, x));
x.intoUD2x18();
}

function testFuzz_intoUD2x18(uint128 x) external pure {
x = boundUint128(x, 0, uint128(uMAX_UD2x18));
UD2x18 actual = x.intoUD2x18();
UD2x18 expected = UD2x18.wrap(uint64(x));
assertEq(actual, expected, "uint128 intoUD2x18");
}

function testFuzz_intoUD60x18(uint128 x) external pure {
UD60x18 actual = x.intoUD60x18();
UD60x18 expected = UD60x18.wrap(uint256(x));
assertEq(actual, expected, "uint128 intoUD60x18");
}

function boundUint128(uint128 x, uint128 min, uint128 max) internal pure returns (uint128 result) {
result = uint128(_bound(uint256(x), uint256(min), uint256(max)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19 <0.9.0;

import {
PRBMathCastingUint256 as CastingUint256,
PRBMath_IntoSD1x18_Overflow,
PRBMath_IntoSD59x18_Overflow,
PRBMath_IntoUD2x18_Overflow
} from "@prb/math/src/casting/Uint256.sol";
import { uMAX_SD1x18 } from "@prb/math/src/sd1x18/Constants.sol";
import { SD1x18 } from "@prb/math/src/sd1x18/ValueType.sol";
import { uMAX_SD59x18 } from "@prb/math/src/sd59x18/Constants.sol";
import { SD59x18 } from "@prb/math/src/sd59x18/ValueType.sol";
import { uMAX_UD2x18 } from "@prb/math/src/ud2x18/Constants.sol";
import { UD2x18 } from "@prb/math/src/ud2x18/ValueType.sol";
import { UD60x18 } from "@prb/math/src/ud60x18/ValueType.sol";

import { Base_Test } from "../../Base.t.sol";

/// @dev Collection of tests for the casting library available for uint256.
contract CastingUint256_Test is Base_Test {
using CastingUint256 for uint256;

function testFuzz_RevertWhen_OverflowSD1x18(uint256 x) external {
x = _bound(x, uint64(uMAX_SD1x18) + 1, type(uint256).max);
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoSD1x18_Overflow.selector, x));
x.intoSD1x18();
}

function testFuzz_intoSD1x18(uint256 x) external pure {
x = _bound(x, 0, uint64(uMAX_SD1x18));
SD1x18 actual = x.intoSD1x18();
SD1x18 expected = SD1x18.wrap(int64(uint64(x)));
assertEq(actual, expected, "uint256 intoSD1x18");
}

function testFuzz_RevertWhen_OverflowSD59x18(uint256 x) external {
x = _bound(x, uint256(uMAX_SD59x18) + 1, type(uint256).max);
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoSD59x18_Overflow.selector, x));
x.intoSD59x18();
}

function testFuzz_intoSD59x18(uint256 x) external pure {
x = _bound(x, 0, uint256(uMAX_SD59x18));
SD59x18 actual = x.intoSD59x18();
SD59x18 expected = SD59x18.wrap(int256(uint256(x)));
assertEq(actual, expected, "uint256 intoSD59x18");
}

function testFuzz_RevertWhen_OverflowUD2x18(uint256 x) external {
x = _bound(x, uint256(uMAX_UD2x18) + 1, type(uint256).max);
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoUD2x18_Overflow.selector, x));
x.intoUD2x18();
}

function testFuzz_intoUD2x18(uint256 x) external pure {
x = _bound(x, 0, uint256(uMAX_UD2x18));
UD2x18 actual = x.intoUD2x18();
UD2x18 expected = UD2x18.wrap(uint64(x));
assertEq(actual, expected, "uint256 intoUD2x18");
}

function testFuzz_intoUD60x18(uint256 x) external pure {
UD60x18 actual = x.intoUD60x18();
UD60x18 expected = UD60x18.wrap(x);
assertEq(actual, expected, "uint256 intoUD60x18");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19 <0.9.0;

import { PRBMathCastingUint40 as CastingUint40 } from "@prb/math/src/casting/Uint40.sol";
import { SD1x18 } from "@prb/math/src/sd1x18/ValueType.sol";
import { SD59x18 } from "@prb/math/src/sd59x18/ValueType.sol";
import { UD2x18 } from "@prb/math/src/ud2x18/ValueType.sol";
import { UD60x18 } from "@prb/math/src/ud60x18/ValueType.sol";

import { Base_Test } from "../../Base.t.sol";

/// @dev Collection of tests for the casting library available for uint40.
contract CastingUint40_Test is Base_Test {
using CastingUint40 for uint40;

function testFuzz_intoSD1x18(uint40 x) external pure {
SD1x18 actual = x.intoSD1x18();
SD1x18 expected = SD1x18.wrap(int64(uint64(x)));
assertEq(actual, expected, "uint40 intoSD1x18");
}

function testFuzz_intoSD59x18(uint40 x) external pure {
SD59x18 actual = x.intoSD59x18();
SD59x18 expected = SD59x18.wrap(int256(uint256(x)));
assertEq(actual, expected, "uint40 intoSD59x18");
}

function testFuzz_intoUD2x18(uint40 x) external pure {
UD2x18 actual = x.intoUD2x18();
UD2x18 expected = UD2x18.wrap(uint64(x));
assertEq(actual, expected, "uint40 intoUD2x18");
}

function testFuzz_intoUD60x18(uint40 x) external pure {
UD60x18 actual = x.intoUD60x18();
UD60x18 expected = UD60x18.wrap(uint256(x));
assertEq(actual, expected, "uint40 intoUD60x18");
}
}
Loading