diff --git a/l1-contracts/test/foundry/unit/concrete/UncheckedMath/UncheckedAdd.t.sol b/l1-contracts/test/foundry/unit/concrete/UncheckedMath/UncheckedAdd.t.sol new file mode 100644 index 000000000..847818a65 --- /dev/null +++ b/l1-contracts/test/foundry/unit/concrete/UncheckedMath/UncheckedAdd.t.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.20; + +import {UncheckedMathTest} from "./_UncheckedMath_Shared.t.sol"; +import {UncheckedMath} from "solpp/common/libraries/UncheckedMath.sol"; + +contract UncheckedAddTest is UncheckedMathTest { + using UncheckedMath for uint256; + + function test_Add() public { + uint256 a = 1234; + uint256 b = 4321; + uint256 c = a.uncheckedAdd(b); + assertEq(c, 5555); + } + + function test_AddWithOverflow() public { + uint256 a = type(uint256).max; + uint256 b = 1; + + // uncheckedAdd does not fail + uint256 c = a.uncheckedAdd(b); + assertEq(c, 0); + + // regular addition fails with overflow + vm.expectRevert(); + a + b; + } +} diff --git a/l1-contracts/test/foundry/unit/concrete/UncheckedMath/UncheckedInc.t.sol b/l1-contracts/test/foundry/unit/concrete/UncheckedMath/UncheckedInc.t.sol new file mode 100644 index 000000000..aa1a669f7 --- /dev/null +++ b/l1-contracts/test/foundry/unit/concrete/UncheckedMath/UncheckedInc.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.20; + +import {UncheckedMathTest} from "./_UncheckedMath_Shared.t.sol"; +import {UncheckedMath} from "solpp/common/libraries/UncheckedMath.sol"; + +contract UncheckedIncTest is UncheckedMathTest { + using UncheckedMath for uint256; + + function test_Inc() public { + uint256 a = 1234; + uint256 c = a.uncheckedInc(); + assertEq(c, 1235); + } + + function test_IncWithOverflow() public { + uint256 a = type(uint256).max; + + // uncheckedInc does not fail + uint256 c = a.uncheckedInc(); + assertEq(c, 0); + + // regular addition fails with overflow + vm.expectRevert(); + a + 1; + } +} diff --git a/l1-contracts/test/foundry/unit/concrete/UncheckedMath/_UncheckedMath_Shared.t.sol b/l1-contracts/test/foundry/unit/concrete/UncheckedMath/_UncheckedMath_Shared.t.sol new file mode 100644 index 000000000..badc0233e --- /dev/null +++ b/l1-contracts/test/foundry/unit/concrete/UncheckedMath/_UncheckedMath_Shared.t.sol @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.20; + +import {Test} from "forge-std/Test.sol"; + +contract UncheckedMathTest is Test {}