generated from LooksRare/solidity-template
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: UnsafeMathUint256 * chore: Trailing whitespace
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.17; | ||
|
||
library UnsafeMathUint256 { | ||
function unsafeAdd(uint256 a, uint256 b) internal pure returns (uint256) { | ||
unchecked { | ||
return a + b; | ||
} | ||
} | ||
|
||
function unsafeSubtract(uint256 a, uint256 b) internal pure returns (uint256) { | ||
unchecked { | ||
return a - b; | ||
} | ||
} | ||
|
||
function unsafeMultiply(uint256 a, uint256 b) internal pure returns (uint256) { | ||
unchecked { | ||
return a * b; | ||
} | ||
} | ||
|
||
function unsafeDivide(uint256 a, uint256 b) internal pure returns (uint256) { | ||
unchecked { | ||
return a / b; | ||
} | ||
} | ||
} |
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,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.17; | ||
|
||
import {UnsafeMathUint256} from "../../contracts/libraries/UnsafeMathUint256.sol"; | ||
import {TestHelpers} from "./utils/TestHelpers.sol"; | ||
|
||
contract UnsafeMathUint256Test is TestHelpers { | ||
using UnsafeMathUint256 for uint256; | ||
|
||
function testFuzz_unsafeAdd(uint256 number) public { | ||
vm.assume(number < type(uint256).max); | ||
assertEq(number.unsafeAdd(1), number + 1); | ||
} | ||
|
||
function test_unsafeAdd_NumberIsUint256Max() public { | ||
assertEq(type(uint256).max.unsafeAdd(1), 0); | ||
} | ||
|
||
function testFuzz_unsafeSubtract(uint256 number) public { | ||
vm.assume(number > 0); | ||
assertEq(number.unsafeSubtract(1), number - 1); | ||
} | ||
|
||
function test_unsafeSubtract_NumberIs0() public { | ||
assertEq(uint256(0).unsafeSubtract(1), type(uint256).max); | ||
} | ||
|
||
function testFuzz_unsafeMultiply(uint256 a, uint256 b) public { | ||
vm.assume(a <= type(uint128).max && b <= type(uint128).max); | ||
assertEq(a.unsafeMultiply(b), a * b); | ||
} | ||
|
||
function test_unsafeMultiply_NumbersAreGreaterThanUint128() public { | ||
uint256 number = uint256(type(uint128).max) + 1; | ||
assertEq(number.unsafeMultiply(number), 0); | ||
} | ||
|
||
function testFuzz_unsafeDivide(uint256 a, uint256 b) public { | ||
vm.assume(b != 0); | ||
assertEq(a.unsafeDivide(b), a / b); | ||
} | ||
} |