-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: test casting utilities for basic types
docs: add basic NatSpec for casting libraries refactor: reorder custom errors alphabetically refactor: simplify casting from "uint40" to "uint64" test: add "boundUint128" helpers
- Loading branch information
Showing
7 changed files
with
186 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.13 <0.9.0; | ||
|
||
import { | ||
PRBMathCastingUint128 as CastingUint128, | ||
PRBMath_IntoSD1x18_Overflow, | ||
PRBMath_IntoUD2x18_Overflow | ||
} from "src/casting/Uint128.sol"; | ||
import { uMAX_SD1x18 } from "src/sd1x18/Constants.sol"; | ||
import { SD1x18 } from "src/sd1x18/ValueType.sol"; | ||
import { SD59x18 } from "src/sd59x18/ValueType.sol"; | ||
import { uMAX_UD2x18 } from "src/ud2x18/Constants.sol"; | ||
import { UD2x18 } from "src/ud2x18/ValueType.sol"; | ||
import { UD60x18 } from "src/ud60x18/ValueType.sol"; | ||
|
||
import { BaseTest } from "../BaseTest.t.sol"; | ||
|
||
/// @dev Collection of tests for the casting library available for uint128. | ||
contract CastingUint128_Test is BaseTest { | ||
using CastingUint128 for uint128; | ||
|
||
function test_RevertWhen_OverflowSD1x18(uint128 x) external { | ||
x = boundUint128(x, uint128(uint64(uMAX_SD1x18)) + 1, type(uint128).max); | ||
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoSD1x18_Overflow.selector, x)); | ||
x.intoSD1x18(); | ||
} | ||
|
||
function testFuzz_intoSD1x18(uint128 x) external { | ||
x = boundUint128(x, 0, uint128(uint64(uMAX_SD1x18))); | ||
SD1x18 actual = x.intoSD1x18(); | ||
SD1x18 expected = SD1x18.wrap(int64(uint64(x))); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function testFuzz_intoSD59x18(uint128 x) external { | ||
SD59x18 actual = x.intoSD59x18(); | ||
SD59x18 expected = SD59x18.wrap(int256(uint256(x))); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function test_RevertWhen_OverflowUD2x18(uint128 x) external { | ||
x = boundUint128(x, uint128(uMAX_UD2x18) + 1, type(uint128).max); | ||
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoUD2x18_Overflow.selector, x)); | ||
x.intoUD2x18(); | ||
} | ||
|
||
function testFuzz_intoUD2x18(uint128 x) external { | ||
x = boundUint128(x, 0, uint128(uMAX_UD2x18)); | ||
UD2x18 actual = x.intoUD2x18(); | ||
UD2x18 expected = UD2x18.wrap(uint64(x)); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function testFuzz_intoUD60x18(uint128 x) external { | ||
UD60x18 actual = x.intoUD60x18(); | ||
UD60x18 expected = UD60x18.wrap(uint256(x)); | ||
assertEq(actual, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.13 <0.9.0; | ||
|
||
import { | ||
PRBMathCastingUint256 as CastingUint256, | ||
PRBMath_IntoSD1x18_Overflow, | ||
PRBMath_IntoSD59x18_Overflow, | ||
PRBMath_IntoUD2x18_Overflow | ||
} from "src/casting/Uint256.sol"; | ||
import { uMAX_SD1x18 } from "src/sd1x18/Constants.sol"; | ||
import { SD1x18 } from "src/sd1x18/ValueType.sol"; | ||
import { uMAX_SD59x18 } from "src/sd59x18/Constants.sol"; | ||
import { SD59x18 } from "src/sd59x18/ValueType.sol"; | ||
import { uMAX_UD2x18 } from "src/ud2x18/Constants.sol"; | ||
import { UD2x18 } from "src/ud2x18/ValueType.sol"; | ||
import { UD60x18 } from "src/ud60x18/ValueType.sol"; | ||
|
||
import { BaseTest } from "../BaseTest.t.sol"; | ||
|
||
/// @dev Collection of tests for the casting library available for uint256. | ||
contract CastingUint256_Test is BaseTest { | ||
using CastingUint256 for uint256; | ||
|
||
function test_RevertWhen_OverflowSD1x18(uint256 x) external { | ||
x = bound(x, uint256(uint64(uMAX_SD1x18)) + 1, type(uint256).max); | ||
vm.expectRevert(abi.encodeWithSelector(PRBMath_IntoSD1x18_Overflow.selector, x)); | ||
x.intoSD1x18(); | ||
} | ||
|
||
function testFuzz_intoSD1x18(uint256 x) external { | ||
x = bound(x, 0, uint256(uint64(uMAX_SD1x18))); | ||
SD1x18 actual = x.intoSD1x18(); | ||
SD1x18 expected = SD1x18.wrap(int64(uint64(x))); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function test_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 { | ||
x = bound(x, 0, uint256(uMAX_SD59x18)); | ||
SD59x18 actual = x.intoSD59x18(); | ||
SD59x18 expected = SD59x18.wrap(int256(uint256(x))); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function test_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 { | ||
x = bound(x, 0, uint256(uMAX_UD2x18)); | ||
UD2x18 actual = x.intoUD2x18(); | ||
UD2x18 expected = UD2x18.wrap(uint64(x)); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function testFuzz_intoUD60x18(uint256 x) external { | ||
UD60x18 actual = x.intoUD60x18(); | ||
UD60x18 expected = UD60x18.wrap(x); | ||
assertEq(actual, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.13 <0.9.0; | ||
|
||
import { PRBMathCastingUint40 as CastingUint40 } from "src/casting/Uint40.sol"; | ||
import { SD1x18 } from "src/sd1x18/ValueType.sol"; | ||
import { SD59x18 } from "src/sd59x18/ValueType.sol"; | ||
import { UD2x18 } from "src/ud2x18/ValueType.sol"; | ||
import { UD60x18 } from "src/ud60x18/ValueType.sol"; | ||
|
||
import { BaseTest } from "../BaseTest.t.sol"; | ||
|
||
/// @dev Collection of tests for the casting library available for uint40. | ||
contract CastingUint40_Test is BaseTest { | ||
using CastingUint40 for uint40; | ||
|
||
function testFuzz_intoSD1x18(uint40 x) external { | ||
SD1x18 actual = x.intoSD1x18(); | ||
SD1x18 expected = SD1x18.wrap(int64(uint64(x))); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function testFuzz_intoSD59x18(uint40 x) external { | ||
SD59x18 actual = x.intoSD59x18(); | ||
SD59x18 expected = SD59x18.wrap(int256(uint256(x))); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function testFuzz_intoUD2x18(uint40 x) external { | ||
UD2x18 actual = x.intoUD2x18(); | ||
UD2x18 expected = UD2x18.wrap(uint64(x)); | ||
assertEq(actual, expected); | ||
} | ||
|
||
function testFuzz_intoUD60x18(uint40 x) external { | ||
UD60x18 actual = x.intoUD60x18(); | ||
UD60x18 expected = UD60x18.wrap(uint256(x)); | ||
assertEq(actual, expected); | ||
} | ||
} |