From 0b7c358772ab611bc356654ffa057fe5beffe4af Mon Sep 17 00:00:00 2001 From: Hayden Shively <17186559+haydenshively@users.noreply.github.com> Date: Sun, 15 Jan 2023 15:32:50 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Every=20possible=20safeCast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 157 ++++++++++---- src/test/SafeCastLib.t.sol | 416 +++++++++++++++++++++++++++++++++++++ src/utils/SafeCastLib.sol | 120 +++++++++++ 3 files changed, 654 insertions(+), 39 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index c4c5c840..a3cefb63 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -347,48 +347,127 @@ SSTORE2Test:testWriteReadCustomStartBound(bytes,uint256,bytes) (runs: 256, μ: 4 SSTORE2Test:testWriteReadEmptyBound() (gas: 34677) SSTORE2Test:testWriteReadFullBoundedRead() (gas: 53672) SSTORE2Test:testWriteReadFullStartBound() (gas: 34764) -SafeCastLibTest:testFailSafeCastTo128() (gas: 321) -SafeCastLibTest:testFailSafeCastTo128(uint256) (runs: 256, μ: 443, ~: 443) -SafeCastLibTest:testFailSafeCastTo16() (gas: 343) -SafeCastLibTest:testFailSafeCastTo16(uint256) (runs: 256, μ: 401, ~: 401) -SafeCastLibTest:testFailSafeCastTo160() (gas: 342) -SafeCastLibTest:testFailSafeCastTo160(uint256) (runs: 256, μ: 422, ~: 422) -SafeCastLibTest:testFailSafeCastTo192() (gas: 322) -SafeCastLibTest:testFailSafeCastTo192(uint256) (runs: 256, μ: 401, ~: 401) -SafeCastLibTest:testFailSafeCastTo224() (gas: 365) -SafeCastLibTest:testFailSafeCastTo224(uint256) (runs: 256, μ: 445, ~: 445) -SafeCastLibTest:testFailSafeCastTo24(uint256) (runs: 256, μ: 402, ~: 402) -SafeCastLibTest:testFailSafeCastTo248() (gas: 321) -SafeCastLibTest:testFailSafeCastTo248(uint256) (runs: 256, μ: 444, ~: 444) +SafeCastLibTest:testFailSafeCastTo104() (gas: 387) +SafeCastLibTest:testFailSafeCastTo104(uint256) (runs: 256, μ: 468, ~: 468) +SafeCastLibTest:testFailSafeCastTo112() (gas: 388) +SafeCastLibTest:testFailSafeCastTo112(uint256) (runs: 256, μ: 445, ~: 445) +SafeCastLibTest:testFailSafeCastTo120() (gas: 409) +SafeCastLibTest:testFailSafeCastTo120(uint256) (runs: 256, μ: 490, ~: 490) +SafeCastLibTest:testFailSafeCastTo128() (gas: 365) +SafeCastLibTest:testFailSafeCastTo128(uint256) (runs: 256, μ: 487, ~: 487) +SafeCastLibTest:testFailSafeCastTo136() (gas: 409) +SafeCastLibTest:testFailSafeCastTo136(uint256) (runs: 256, μ: 489, ~: 489) +SafeCastLibTest:testFailSafeCastTo144() (gas: 365) +SafeCastLibTest:testFailSafeCastTo144(uint256) (runs: 256, μ: 423, ~: 423) +SafeCastLibTest:testFailSafeCastTo152() (gas: 368) +SafeCastLibTest:testFailSafeCastTo152(uint256) (runs: 256, μ: 468, ~: 468) +SafeCastLibTest:testFailSafeCastTo16() (gas: 388) +SafeCastLibTest:testFailSafeCastTo16(uint256) (runs: 256, μ: 468, ~: 468) +SafeCastLibTest:testFailSafeCastTo160() (gas: 409) +SafeCastLibTest:testFailSafeCastTo160(uint256) (runs: 256, μ: 444, ~: 444) +SafeCastLibTest:testFailSafeCastTo168() (gas: 341) +SafeCastLibTest:testFailSafeCastTo168(uint256) (runs: 256, μ: 488, ~: 488) +SafeCastLibTest:testFailSafeCastTo176() (gas: 363) +SafeCastLibTest:testFailSafeCastTo176(uint256) (runs: 256, μ: 489, ~: 489) +SafeCastLibTest:testFailSafeCastTo184() (gas: 343) +SafeCastLibTest:testFailSafeCastTo184(uint256) (runs: 256, μ: 490, ~: 490) +SafeCastLibTest:testFailSafeCastTo192() (gas: 367) +SafeCastLibTest:testFailSafeCastTo192(uint256) (runs: 256, μ: 446, ~: 446) +SafeCastLibTest:testFailSafeCastTo200() (gas: 343) +SafeCastLibTest:testFailSafeCastTo200(uint256) (runs: 256, μ: 490, ~: 490) +SafeCastLibTest:testFailSafeCastTo208() (gas: 386) +SafeCastLibTest:testFailSafeCastTo208(uint256) (runs: 256, μ: 446, ~: 446) +SafeCastLibTest:testFailSafeCastTo216() (gas: 365) +SafeCastLibTest:testFailSafeCastTo216(uint256) (runs: 256, μ: 424, ~: 424) +SafeCastLibTest:testFailSafeCastTo224() (gas: 409) +SafeCastLibTest:testFailSafeCastTo224(uint256) (runs: 256, μ: 423, ~: 423) +SafeCastLibTest:testFailSafeCastTo232() (gas: 410) +SafeCastLibTest:testFailSafeCastTo232(uint256) (runs: 256, μ: 467, ~: 467) +SafeCastLibTest:testFailSafeCastTo24() (gas: 387) +SafeCastLibTest:testFailSafeCastTo24(uint256) (runs: 256, μ: 424, ~: 424) +SafeCastLibTest:testFailSafeCastTo240() (gas: 364) +SafeCastLibTest:testFailSafeCastTo240(uint256) (runs: 256, μ: 467, ~: 467) +SafeCastLibTest:testFailSafeCastTo248() (gas: 365) +SafeCastLibTest:testFailSafeCastTo248(uint256) (runs: 256, μ: 466, ~: 466) SafeCastLibTest:testFailSafeCastTo32() (gas: 364) -SafeCastLibTest:testFailSafeCastTo32(uint256) (runs: 256, μ: 446, ~: 446) -SafeCastLibTest:testFailSafeCastTo64() (gas: 343) -SafeCastLibTest:testFailSafeCastTo64(uint256) (runs: 256, μ: 423, ~: 423) +SafeCastLibTest:testFailSafeCastTo32(uint256) (runs: 256, μ: 468, ~: 468) +SafeCastLibTest:testFailSafeCastTo40() (gas: 366) +SafeCastLibTest:testFailSafeCastTo40(uint256) (runs: 256, μ: 422, ~: 422) +SafeCastLibTest:testFailSafeCastTo48() (gas: 366) +SafeCastLibTest:testFailSafeCastTo48(uint256) (runs: 256, μ: 488, ~: 488) +SafeCastLibTest:testFailSafeCastTo56() (gas: 388) +SafeCastLibTest:testFailSafeCastTo56(uint256) (runs: 256, μ: 445, ~: 445) +SafeCastLibTest:testFailSafeCastTo64() (gas: 410) +SafeCastLibTest:testFailSafeCastTo64(uint256) (runs: 256, μ: 446, ~: 446) +SafeCastLibTest:testFailSafeCastTo72() (gas: 410) +SafeCastLibTest:testFailSafeCastTo72(uint256) (runs: 256, μ: 467, ~: 467) SafeCastLibTest:testFailSafeCastTo8() (gas: 341) SafeCastLibTest:testFailSafeCastTo8(uint256) (runs: 256, μ: 421, ~: 421) -SafeCastLibTest:testFailSafeCastTo96() (gas: 343) -SafeCastLibTest:testFailSafeCastTo96(uint256) (runs: 256, μ: 424, ~: 424) -SafeCastLibTest:testSafeCastTo128() (gas: 472) -SafeCastLibTest:testSafeCastTo128(uint256) (runs: 256, μ: 2756, ~: 2756) -SafeCastLibTest:testSafeCastTo16() (gas: 447) -SafeCastLibTest:testSafeCastTo16(uint256) (runs: 256, μ: 2734, ~: 2734) -SafeCastLibTest:testSafeCastTo160() (gas: 470) -SafeCastLibTest:testSafeCastTo160(uint256) (runs: 256, μ: 2731, ~: 2731) -SafeCastLibTest:testSafeCastTo192() (gas: 449) -SafeCastLibTest:testSafeCastTo192(uint256) (runs: 256, μ: 2711, ~: 2711) -SafeCastLibTest:testSafeCastTo224() (gas: 491) -SafeCastLibTest:testSafeCastTo224(uint256) (runs: 256, μ: 2710, ~: 2710) -SafeCastLibTest:testSafeCastTo24() (gas: 492) -SafeCastLibTest:testSafeCastTo248() (gas: 450) -SafeCastLibTest:testSafeCastTo248(uint256) (runs: 256, μ: 2755, ~: 2755) -SafeCastLibTest:testSafeCastTo32() (gas: 449) -SafeCastLibTest:testSafeCastTo32(uint256) (runs: 256, μ: 2733, ~: 2733) -SafeCastLibTest:testSafeCastTo64() (gas: 492) -SafeCastLibTest:testSafeCastTo64(uint256) (runs: 256, μ: 2732, ~: 2732) -SafeCastLibTest:testSafeCastTo8() (gas: 491) -SafeCastLibTest:testSafeCastTo8(uint256) (runs: 256, μ: 2710, ~: 2710) -SafeCastLibTest:testSafeCastTo96() (gas: 469) -SafeCastLibTest:testSafeCastTo96(uint256) (runs: 256, μ: 2711, ~: 2711) +SafeCastLibTest:testFailSafeCastTo80() (gas: 343) +SafeCastLibTest:testFailSafeCastTo80(uint256) (runs: 256, μ: 424, ~: 424) +SafeCastLibTest:testFailSafeCastTo88() (gas: 344) +SafeCastLibTest:testFailSafeCastTo88(uint256) (runs: 256, μ: 489, ~: 489) +SafeCastLibTest:testFailSafeCastTo96() (gas: 366) +SafeCastLibTest:testFailSafeCastTo96(uint256) (runs: 256, μ: 469, ~: 469) +SafeCastLibTest:testSafeCastTo104() (gas: 515) +SafeCastLibTest:testSafeCastTo104(uint256) (runs: 256, μ: 2779, ~: 2779) +SafeCastLibTest:testSafeCastTo112() (gas: 469) +SafeCastLibTest:testSafeCastTo112(uint256) (runs: 256, μ: 2755, ~: 2755) +SafeCastLibTest:testSafeCastTo120() (gas: 491) +SafeCastLibTest:testSafeCastTo120(uint256) (runs: 256, μ: 2735, ~: 2735) +SafeCastLibTest:testSafeCastTo128() (gas: 516) +SafeCastLibTest:testSafeCastTo128(uint256) (runs: 256, μ: 2735, ~: 2735) +SafeCastLibTest:testSafeCastTo136() (gas: 470) +SafeCastLibTest:testSafeCastTo136(uint256) (runs: 256, μ: 2757, ~: 2757) +SafeCastLibTest:testSafeCastTo144() (gas: 514) +SafeCastLibTest:testSafeCastTo144(uint256) (runs: 256, μ: 2798, ~: 2798) +SafeCastLibTest:testSafeCastTo152() (gas: 494) +SafeCastLibTest:testSafeCastTo152(uint256) (runs: 256, μ: 2734, ~: 2734) +SafeCastLibTest:testSafeCastTo16() (gas: 469) +SafeCastLibTest:testSafeCastTo16(uint256) (runs: 256, μ: 2779, ~: 2779) +SafeCastLibTest:testSafeCastTo160() (gas: 491) +SafeCastLibTest:testSafeCastTo160(uint256) (runs: 256, μ: 2775, ~: 2775) +SafeCastLibTest:testSafeCastTo168() (gas: 494) +SafeCastLibTest:testSafeCastTo168(uint256) (runs: 256, μ: 2799, ~: 2799) +SafeCastLibTest:testSafeCastTo176() (gas: 493) +SafeCastLibTest:testSafeCastTo176(uint256) (runs: 256, μ: 2734, ~: 2734) +SafeCastLibTest:testSafeCastTo184() (gas: 513) +SafeCastLibTest:testSafeCastTo184(uint256) (runs: 256, μ: 2801, ~: 2801) +SafeCastLibTest:testSafeCastTo192() (gas: 494) +SafeCastLibTest:testSafeCastTo192(uint256) (runs: 256, μ: 2734, ~: 2734) +SafeCastLibTest:testSafeCastTo200() (gas: 470) +SafeCastLibTest:testSafeCastTo200(uint256) (runs: 256, μ: 2734, ~: 2734) +SafeCastLibTest:testSafeCastTo208() (gas: 472) +SafeCastLibTest:testSafeCastTo208(uint256) (runs: 256, μ: 2756, ~: 2756) +SafeCastLibTest:testSafeCastTo216() (gas: 493) +SafeCastLibTest:testSafeCastTo216(uint256) (runs: 256, μ: 2777, ~: 2777) +SafeCastLibTest:testSafeCastTo224() (gas: 469) +SafeCastLibTest:testSafeCastTo224(uint256) (runs: 256, μ: 2733, ~: 2733) +SafeCastLibTest:testSafeCastTo232() (gas: 492) +SafeCastLibTest:testSafeCastTo232(uint256) (runs: 256, μ: 2735, ~: 2735) +SafeCastLibTest:testSafeCastTo24() (gas: 515) +SafeCastLibTest:testSafeCastTo24(uint256) (runs: 256, μ: 2733, ~: 2733) +SafeCastLibTest:testSafeCastTo240() (gas: 513) +SafeCastLibTest:testSafeCastTo240(uint256) (runs: 256, μ: 2800, ~: 2800) +SafeCastLibTest:testSafeCastTo248() (gas: 472) +SafeCastLibTest:testSafeCastTo248(uint256) (runs: 256, μ: 2777, ~: 2777) +SafeCastLibTest:testSafeCastTo32() (gas: 516) +SafeCastLibTest:testSafeCastTo32(uint256) (runs: 256, μ: 2777, ~: 2777) +SafeCastLibTest:testSafeCastTo40() (gas: 517) +SafeCastLibTest:testSafeCastTo40(uint256) (runs: 256, μ: 2756, ~: 2756) +SafeCastLibTest:testSafeCastTo48() (gas: 469) +SafeCastLibTest:testSafeCastTo48(uint256) (runs: 256, μ: 2778, ~: 2778) +SafeCastLibTest:testSafeCastTo56() (gas: 470) +SafeCastLibTest:testSafeCastTo56(uint256) (runs: 256, μ: 2801, ~: 2801) +SafeCastLibTest:testSafeCastTo64() (gas: 537) +SafeCastLibTest:testSafeCastTo64(uint256) (runs: 256, μ: 2799, ~: 2799) +SafeCastLibTest:testSafeCastTo72(uint256) (runs: 256, μ: 2798, ~: 2798) +SafeCastLibTest:testSafeCastTo8() (gas: 513) +SafeCastLibTest:testSafeCastTo8(uint256) (runs: 256, μ: 2755, ~: 2755) +SafeCastLibTest:testSafeCastTo80(uint256) (runs: 256, μ: 2736, ~: 2736) +SafeCastLibTest:testSafeCastTo88(uint256) (runs: 256, μ: 2755, ~: 2755) +SafeCastLibTest:testSafeCastTo96() (gas: 536) +SafeCastLibTest:testSafeCastTo96(uint256) (runs: 256, μ: 2800, ~: 2800) SafeTransferLibTest:testApproveWithGarbage(address,uint256,bytes,bytes) (runs: 256, μ: 2664, ~: 2231) SafeTransferLibTest:testApproveWithMissingReturn() (gas: 30751) SafeTransferLibTest:testApproveWithMissingReturn(address,uint256,bytes) (runs: 256, μ: 30328, ~: 31566) diff --git a/src/test/SafeCastLib.t.sol b/src/test/SafeCastLib.t.sol index 0a564538..cf627c23 100644 --- a/src/test/SafeCastLib.t.sol +++ b/src/test/SafeCastLib.t.sol @@ -11,26 +11,96 @@ contract SafeCastLibTest is DSTestPlus { assertEq(SafeCastLib.safeCastTo248(2.5e27), 2.5e27); } + function testSafeCastTo240() public { + assertEq(SafeCastLib.safeCastTo240(2.5e45), 2.5e45); + assertEq(SafeCastLib.safeCastTo240(2.5e27), 2.5e27); + } + + function testSafeCastTo232() public { + assertEq(SafeCastLib.safeCastTo232(2.5e45), 2.5e45); + assertEq(SafeCastLib.safeCastTo232(2.5e27), 2.5e27); + } + function testSafeCastTo224() public { assertEq(SafeCastLib.safeCastTo224(2.5e36), 2.5e36); assertEq(SafeCastLib.safeCastTo224(2.5e27), 2.5e27); } + + function testSafeCastTo216() public { + assertEq(SafeCastLib.safeCastTo216(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo216(2.5e27), 2.5e27); + } + + function testSafeCastTo208() public { + assertEq(SafeCastLib.safeCastTo208(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo208(2.5e27), 2.5e27); + } + + function testSafeCastTo200() public { + assertEq(SafeCastLib.safeCastTo200(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo200(2.5e27), 2.5e27); + } function testSafeCastTo192() public { assertEq(SafeCastLib.safeCastTo192(2.5e36), 2.5e36); assertEq(SafeCastLib.safeCastTo192(2.5e27), 2.5e27); } + function testSafeCastTo184() public { + assertEq(SafeCastLib.safeCastTo184(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo184(2.5e27), 2.5e27); + } + + function testSafeCastTo176() public { + assertEq(SafeCastLib.safeCastTo176(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo176(2.5e27), 2.5e27); + } + + function testSafeCastTo168() public { + assertEq(SafeCastLib.safeCastTo168(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo168(2.5e27), 2.5e27); + } + function testSafeCastTo160() public { assertEq(SafeCastLib.safeCastTo160(2.5e36), 2.5e36); assertEq(SafeCastLib.safeCastTo160(2.5e27), 2.5e27); } + function testSafeCastTo152() public { + assertEq(SafeCastLib.safeCastTo152(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo152(2.5e27), 2.5e27); + } + + function testSafeCastTo144() public { + assertEq(SafeCastLib.safeCastTo144(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo144(2.5e27), 2.5e27); + } + + function testSafeCastTo136() public { + assertEq(SafeCastLib.safeCastTo136(2.5e36), 2.5e36); + assertEq(SafeCastLib.safeCastTo136(2.5e27), 2.5e27); + } + function testSafeCastTo128() public { assertEq(SafeCastLib.safeCastTo128(2.5e27), 2.5e27); assertEq(SafeCastLib.safeCastTo128(2.5e18), 2.5e18); } + function testSafeCastTo120() public { + assertEq(SafeCastLib.safeCastTo120(2.5e27), 2.5e27); + assertEq(SafeCastLib.safeCastTo120(2.5e18), 2.5e18); + } + + function testSafeCastTo112() public { + assertEq(SafeCastLib.safeCastTo112(2.5e27), 2.5e27); + assertEq(SafeCastLib.safeCastTo112(2.5e18), 2.5e18); + } + + function testSafeCastTo104() public { + assertEq(SafeCastLib.safeCastTo104(2.5e27), 2.5e27); + assertEq(SafeCastLib.safeCastTo104(2.5e18), 2.5e18); + } + function testSafeCastTo96() public { assertEq(SafeCastLib.safeCastTo96(2.5e18), 2.5e18); assertEq(SafeCastLib.safeCastTo96(2.5e17), 2.5e17); @@ -41,6 +111,21 @@ contract SafeCastLibTest is DSTestPlus { assertEq(SafeCastLib.safeCastTo64(2.5e17), 2.5e17); } + function testSafeCastTo56() public { + assertEq(SafeCastLib.safeCastTo56(2.5e16), 2.5e16); + assertEq(SafeCastLib.safeCastTo56(2.5e15), 2.5e15); + } + + function testSafeCastTo48() public { + assertEq(SafeCastLib.safeCastTo48(2.5e12), 2.5e12); + assertEq(SafeCastLib.safeCastTo48(2.5e11), 2.5e11); + } + + function testSafeCastTo40() public { + assertEq(SafeCastLib.safeCastTo40(2.5e10), 2.5e10); + assertEq(SafeCastLib.safeCastTo40(2.5e9), 2.5e9); + } + function testSafeCastTo32() public { assertEq(SafeCastLib.safeCastTo32(2.5e8), 2.5e8); assertEq(SafeCastLib.safeCastTo32(2.5e7), 2.5e7); @@ -65,34 +150,118 @@ contract SafeCastLibTest is DSTestPlus { SafeCastLib.safeCastTo248(type(uint248).max + 1); } + function testFailSafeCastTo240() public pure { + SafeCastLib.safeCastTo240(type(uint240).max + 1); + } + + function testFailSafeCastTo232() public pure { + SafeCastLib.safeCastTo232(type(uint232).max + 1); + } + function testFailSafeCastTo224() public pure { SafeCastLib.safeCastTo224(type(uint224).max + 1); } + function testFailSafeCastTo216() public pure { + SafeCastLib.safeCastTo216(type(uint216).max + 1); + } + + function testFailSafeCastTo208() public pure { + SafeCastLib.safeCastTo208(type(uint208).max + 1); + } + + function testFailSafeCastTo200() public pure { + SafeCastLib.safeCastTo200(type(uint200).max + 1); + } + function testFailSafeCastTo192() public pure { SafeCastLib.safeCastTo192(type(uint192).max + 1); } + function testFailSafeCastTo184() public pure { + SafeCastLib.safeCastTo184(type(uint184).max + 1); + } + + function testFailSafeCastTo176() public pure { + SafeCastLib.safeCastTo176(type(uint176).max + 1); + } + + function testFailSafeCastTo168() public pure { + SafeCastLib.safeCastTo168(type(uint168).max + 1); + } + function testFailSafeCastTo160() public pure { SafeCastLib.safeCastTo160(type(uint160).max + 1); } + function testFailSafeCastTo152() public pure { + SafeCastLib.safeCastTo152(type(uint152).max + 1); + } + + function testFailSafeCastTo144() public pure { + SafeCastLib.safeCastTo144(type(uint144).max + 1); + } + + function testFailSafeCastTo136() public pure { + SafeCastLib.safeCastTo136(type(uint136).max + 1); + } + function testFailSafeCastTo128() public pure { SafeCastLib.safeCastTo128(type(uint128).max + 1); } + function testFailSafeCastTo120() public pure { + SafeCastLib.safeCastTo120(type(uint120).max + 1); + } + + function testFailSafeCastTo112() public pure { + SafeCastLib.safeCastTo112(type(uint112).max + 1); + } + + function testFailSafeCastTo104() public pure { + SafeCastLib.safeCastTo104(type(uint104).max + 1); + } + function testFailSafeCastTo96() public pure { SafeCastLib.safeCastTo96(type(uint96).max + 1); } + function testFailSafeCastTo88() public pure { + SafeCastLib.safeCastTo88(type(uint88).max + 1); + } + + function testFailSafeCastTo80() public pure { + SafeCastLib.safeCastTo80(type(uint80).max + 1); + } + + function testFailSafeCastTo72() public pure { + SafeCastLib.safeCastTo72(type(uint72).max + 1); + } + function testFailSafeCastTo64() public pure { SafeCastLib.safeCastTo64(type(uint64).max + 1); } + function testFailSafeCastTo56() public pure { + SafeCastLib.safeCastTo56(type(uint56).max + 1); + } + + function testFailSafeCastTo48() public pure { + SafeCastLib.safeCastTo48(type(uint48).max + 1); + } + + function testFailSafeCastTo40() public pure { + SafeCastLib.safeCastTo40(type(uint40).max + 1); + } + function testFailSafeCastTo32() public pure { SafeCastLib.safeCastTo32(type(uint32).max + 1); } + function testFailSafeCastTo24() public pure { + SafeCastLib.safeCastTo24(type(uint24).max + 1); + } + function testFailSafeCastTo16() public pure { SafeCastLib.safeCastTo16(type(uint16).max + 1); } @@ -107,48 +276,175 @@ contract SafeCastLibTest is DSTestPlus { assertEq(SafeCastLib.safeCastTo248(x), x); } + function testSafeCastTo240(uint256 x) public { + x = bound(x, 0, type(uint240).max); + + assertEq(SafeCastLib.safeCastTo240(x), x); + } + + function testSafeCastTo232(uint256 x) public { + x = bound(x, 0, type(uint232).max); + + assertEq(SafeCastLib.safeCastTo232(x), x); + } + function testSafeCastTo224(uint256 x) public { x = bound(x, 0, type(uint224).max); assertEq(SafeCastLib.safeCastTo224(x), x); } + function testSafeCastTo216(uint256 x) public { + x = bound(x, 0, type(uint216).max); + + assertEq(SafeCastLib.safeCastTo216(x), x); + } + + function testSafeCastTo208(uint256 x) public { + x = bound(x, 0, type(uint208).max); + + assertEq(SafeCastLib.safeCastTo208(x), x); + } + + function testSafeCastTo200(uint256 x) public { + x = bound(x, 0, type(uint200).max); + + assertEq(SafeCastLib.safeCastTo200(x), x); + } + function testSafeCastTo192(uint256 x) public { x = bound(x, 0, type(uint192).max); assertEq(SafeCastLib.safeCastTo192(x), x); } + function testSafeCastTo184(uint256 x) public { + x = bound(x, 0, type(uint184).max); + + assertEq(SafeCastLib.safeCastTo184(x), x); + } + + function testSafeCastTo176(uint256 x) public { + x = bound(x, 0, type(uint176).max); + + assertEq(SafeCastLib.safeCastTo176(x), x); + } + + function testSafeCastTo168(uint256 x) public { + x = bound(x, 0, type(uint168).max); + + assertEq(SafeCastLib.safeCastTo168(x), x); + } + function testSafeCastTo160(uint256 x) public { x = bound(x, 0, type(uint160).max); assertEq(SafeCastLib.safeCastTo160(x), x); } + function testSafeCastTo152(uint256 x) public { + x = bound(x, 0, type(uint152).max); + + assertEq(SafeCastLib.safeCastTo152(x), x); + } + + function testSafeCastTo144(uint256 x) public { + x = bound(x, 0, type(uint144).max); + + assertEq(SafeCastLib.safeCastTo144(x), x); + } + + function testSafeCastTo136(uint256 x) public { + x = bound(x, 0, type(uint136).max); + + assertEq(SafeCastLib.safeCastTo136(x), x); + } + function testSafeCastTo128(uint256 x) public { x = bound(x, 0, type(uint128).max); assertEq(SafeCastLib.safeCastTo128(x), x); } + function testSafeCastTo120(uint256 x) public { + x = bound(x, 0, type(uint120).max); + + assertEq(SafeCastLib.safeCastTo120(x), x); + } + + function testSafeCastTo112(uint256 x) public { + x = bound(x, 0, type(uint112).max); + + assertEq(SafeCastLib.safeCastTo112(x), x); + } + + function testSafeCastTo104(uint256 x) public { + x = bound(x, 0, type(uint104).max); + + assertEq(SafeCastLib.safeCastTo104(x), x); + } + function testSafeCastTo96(uint256 x) public { x = bound(x, 0, type(uint96).max); assertEq(SafeCastLib.safeCastTo96(x), x); } + function testSafeCastTo88(uint256 x) public { + x = bound(x, 0, type(uint88).max); + + assertEq(SafeCastLib.safeCastTo88(x), x); + } + + + function testSafeCastTo80(uint256 x) public { + x = bound(x, 0, type(uint80).max); + + assertEq(SafeCastLib.safeCastTo80(x), x); + } + + function testSafeCastTo72(uint256 x) public { + x = bound(x, 0, type(uint72).max); + + assertEq(SafeCastLib.safeCastTo72(x), x); + } + function testSafeCastTo64(uint256 x) public { x = bound(x, 0, type(uint64).max); assertEq(SafeCastLib.safeCastTo64(x), x); } + function testSafeCastTo56(uint256 x) public { + x = bound(x, 0, type(uint56).max); + + assertEq(SafeCastLib.safeCastTo56(x), x); + } + + function testSafeCastTo48(uint256 x) public { + x = bound(x, 0, type(uint48).max); + + assertEq(SafeCastLib.safeCastTo48(x), x); + } + + function testSafeCastTo40(uint256 x) public { + x = bound(x, 0, type(uint40).max); + + assertEq(SafeCastLib.safeCastTo40(x), x); + } + function testSafeCastTo32(uint256 x) public { x = bound(x, 0, type(uint32).max); assertEq(SafeCastLib.safeCastTo32(x), x); } + function testSafeCastTo24(uint256 x) public { + x = bound(x, 0, type(uint24).max); + + assertEq(SafeCastLib.safeCastTo24(x), x); + } + function testSafeCastTo16(uint256 x) public { x = bound(x, 0, type(uint16).max); @@ -167,42 +463,162 @@ contract SafeCastLibTest is DSTestPlus { SafeCastLib.safeCastTo248(x); } + function testFailSafeCastTo240(uint256 x) public { + x = bound(x, type(uint240).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo240(x); + } + + function testFailSafeCastTo232(uint256 x) public { + x = bound(x, type(uint232).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo232(x); + } + function testFailSafeCastTo224(uint256 x) public { x = bound(x, type(uint224).max + 1, type(uint256).max); SafeCastLib.safeCastTo224(x); } + function testFailSafeCastTo216(uint256 x) public { + x = bound(x, type(uint216).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo216(x); + } + + function testFailSafeCastTo208(uint256 x) public { + x = bound(x, type(uint208).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo208(x); + } + + function testFailSafeCastTo200(uint256 x) public { + x = bound(x, type(uint200).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo200(x); + } + function testFailSafeCastTo192(uint256 x) public { x = bound(x, type(uint192).max + 1, type(uint256).max); SafeCastLib.safeCastTo192(x); } + function testFailSafeCastTo184(uint256 x) public { + x = bound(x, type(uint184).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo184(x); + } + + function testFailSafeCastTo176(uint256 x) public { + x = bound(x, type(uint176).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo176(x); + } + + function testFailSafeCastTo168(uint256 x) public { + x = bound(x, type(uint168).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo168(x); + } + function testFailSafeCastTo160(uint256 x) public { x = bound(x, type(uint160).max + 1, type(uint256).max); SafeCastLib.safeCastTo160(x); } + function testFailSafeCastTo152(uint256 x) public { + x = bound(x, type(uint152).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo152(x); + } + + function testFailSafeCastTo144(uint256 x) public { + x = bound(x, type(uint144).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo144(x); + } + + function testFailSafeCastTo136(uint256 x) public { + x = bound(x, type(uint136).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo136(x); + } + function testFailSafeCastTo128(uint256 x) public { x = bound(x, type(uint128).max + 1, type(uint256).max); SafeCastLib.safeCastTo128(x); } + function testFailSafeCastTo120(uint256 x) public { + x = bound(x, type(uint120).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo120(x); + } + + function testFailSafeCastTo112(uint256 x) public { + x = bound(x, type(uint112).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo112(x); + } + + function testFailSafeCastTo104(uint256 x) public { + x = bound(x, type(uint104).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo104(x); + } + function testFailSafeCastTo96(uint256 x) public { x = bound(x, type(uint96).max + 1, type(uint256).max); SafeCastLib.safeCastTo96(x); } + function testFailSafeCastTo88(uint256 x) public { + x = bound(x, type(uint88).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo88(x); + } + + function testFailSafeCastTo80(uint256 x) public { + x = bound(x, type(uint80).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo80(x); + } + + function testFailSafeCastTo72(uint256 x) public { + x = bound(x, type(uint72).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo72(x); + } + function testFailSafeCastTo64(uint256 x) public { x = bound(x, type(uint64).max + 1, type(uint256).max); SafeCastLib.safeCastTo64(x); } + function testFailSafeCastTo56(uint256 x) public { + x = bound(x, type(uint56).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo56(x); + } + + function testFailSafeCastTo48(uint256 x) public { + x = bound(x, type(uint48).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo48(x); + } + + function testFailSafeCastTo40(uint256 x) public { + x = bound(x, type(uint40).max + 1, type(uint256).max); + + SafeCastLib.safeCastTo40(x); + } + function testFailSafeCastTo32(uint256 x) public { x = bound(x, type(uint32).max + 1, type(uint256).max); diff --git a/src/utils/SafeCastLib.sol b/src/utils/SafeCastLib.sol index ebbed225..5a7cc965 100644 --- a/src/utils/SafeCastLib.sol +++ b/src/utils/SafeCastLib.sol @@ -11,17 +11,65 @@ library SafeCastLib { y = uint248(x); } + function safeCastTo240(uint256 x) internal pure returns (uint240 y) { + require(x < 1 << 240); + + y = uint240(x); + } + + function safeCastTo232(uint256 x) internal pure returns (uint232 y) { + require(x < 1 << 232); + + y = uint232(x); + } + function safeCastTo224(uint256 x) internal pure returns (uint224 y) { require(x < 1 << 224); y = uint224(x); } + function safeCastTo216(uint256 x) internal pure returns (uint216 y) { + require(x < 1 << 216); + + y = uint216(x); + } + + function safeCastTo208(uint256 x) internal pure returns (uint208 y) { + require(x < 1 << 208); + + y = uint208(x); + } + + function safeCastTo200(uint256 x) internal pure returns (uint200 y) { + require(x < 1 << 200); + + y = uint200(x); + } + function safeCastTo192(uint256 x) internal pure returns (uint192 y) { require(x < 1 << 192); y = uint192(x); } + + function safeCastTo184(uint256 x) internal pure returns (uint184 y) { + require(x < 1 << 184); + + y = uint184(x); + } + + function safeCastTo176(uint256 x) internal pure returns (uint176 y) { + require(x < 1 << 176); + + y = uint176(x); + } + + function safeCastTo168(uint256 x) internal pure returns (uint168 y) { + require(x < 1 << 168); + + y = uint168(x); + } function safeCastTo160(uint256 x) internal pure returns (uint160 y) { require(x < 1 << 160); @@ -29,24 +77,96 @@ library SafeCastLib { y = uint160(x); } + function safeCastTo152(uint256 x) internal pure returns (uint152 y) { + require(x < 1 << 152); + + y = uint152(x); + } + + function safeCastTo144(uint256 x) internal pure returns (uint144 y) { + require(x < 1 << 144); + + y = uint144(x); + } + + function safeCastTo136(uint256 x) internal pure returns (uint136 y) { + require(x < 1 << 136); + + y = uint136(x); + } + function safeCastTo128(uint256 x) internal pure returns (uint128 y) { require(x < 1 << 128); y = uint128(x); } + function safeCastTo120(uint256 x) internal pure returns (uint120 y) { + require(x < 1 << 120); + + y = uint120(x); + } + + function safeCastTo112(uint256 x) internal pure returns (uint112 y) { + require(x < 1 << 112); + + y = uint112(x); + } + + function safeCastTo104(uint256 x) internal pure returns (uint104 y) { + require(x < 1 << 104); + + y = uint104(x); + } + function safeCastTo96(uint256 x) internal pure returns (uint96 y) { require(x < 1 << 96); y = uint96(x); } + function safeCastTo88(uint256 x) internal pure returns (uint88 y) { + require(x < 1 << 88); + + y = uint88(x); + } + + function safeCastTo80(uint256 x) internal pure returns (uint80 y) { + require(x < 1 << 80); + + y = uint80(x); + } + + function safeCastTo72(uint256 x) internal pure returns (uint72 y) { + require(x < 1 << 72); + + y = uint72(x); + } + function safeCastTo64(uint256 x) internal pure returns (uint64 y) { require(x < 1 << 64); y = uint64(x); } + function safeCastTo56(uint256 x) internal pure returns (uint56 y) { + require(x < 1 << 56); + + y = uint56(x); + } + + function safeCastTo48(uint256 x) internal pure returns (uint48 y) { + require(x < 1 << 48); + + y = uint48(x); + } + + function safeCastTo40(uint256 x) internal pure returns (uint40 y) { + require(x < 1 << 40); + + y = uint40(x); + } + function safeCastTo32(uint256 x) internal pure returns (uint32 y) { require(x < 1 << 32);