Skip to content

Commit 97b86e2

Browse files
authored
test: unchecked math test (#147)
1 parent ab86cf9 commit 97b86e2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.20;
3+
4+
import {UncheckedMathTest} from "./_UncheckedMath_Shared.t.sol";
5+
import {UncheckedMath} from "solpp/common/libraries/UncheckedMath.sol";
6+
7+
contract UncheckedAddTest is UncheckedMathTest {
8+
using UncheckedMath for uint256;
9+
10+
function test_Add() public {
11+
uint256 a = 1234;
12+
uint256 b = 4321;
13+
uint256 c = a.uncheckedAdd(b);
14+
assertEq(c, 5555);
15+
}
16+
17+
function test_AddWithOverflow() public {
18+
uint256 a = type(uint256).max;
19+
uint256 b = 1;
20+
21+
// uncheckedAdd does not fail
22+
uint256 c = a.uncheckedAdd(b);
23+
assertEq(c, 0);
24+
25+
// regular addition fails with overflow
26+
vm.expectRevert();
27+
a + b;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.20;
3+
4+
import {UncheckedMathTest} from "./_UncheckedMath_Shared.t.sol";
5+
import {UncheckedMath} from "solpp/common/libraries/UncheckedMath.sol";
6+
7+
contract UncheckedIncTest is UncheckedMathTest {
8+
using UncheckedMath for uint256;
9+
10+
function test_Inc() public {
11+
uint256 a = 1234;
12+
uint256 c = a.uncheckedInc();
13+
assertEq(c, 1235);
14+
}
15+
16+
function test_IncWithOverflow() public {
17+
uint256 a = type(uint256).max;
18+
19+
// uncheckedInc does not fail
20+
uint256 c = a.uncheckedInc();
21+
assertEq(c, 0);
22+
23+
// regular addition fails with overflow
24+
vm.expectRevert();
25+
a + 1;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.20;
3+
4+
import {Test} from "forge-std/Test.sol";
5+
6+
contract UncheckedMathTest is Test {}

0 commit comments

Comments
 (0)