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: unchecked math test #147

Merged
merged 1 commit into from
Jan 8, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import {Test} from "forge-std/Test.sol";

contract UncheckedMathTest is Test {}
Loading