Skip to content

Commit d2bf537

Browse files
committed
test: add fuzz tests for msb and sqrt
1 parent dae52a4 commit d2bf537

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

test/fuzz/common/msb.t.sol

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.19 <0.9.0;
3+
4+
import { msb } from "src/Common.sol";
5+
6+
import { Base_Test } from "../../Base.t.sol";
7+
8+
/// @dev Collection of tests for the most significant bit function available in `Common.sol`.
9+
contract Common_Sqrt_Test is Base_Test {
10+
function testFuzz_Msb_FitsUint8(uint256 x) external pure {
11+
assertLe(msb(x), type(uint8).max, "Common msb");
12+
}
13+
14+
modifier whenNotZero(uint256 x) {
15+
vm.assume(x != 0);
16+
_;
17+
}
18+
19+
function testFuzz_Msb_ShiftsXToOneBit(uint256 x) external pure whenNotZero(x) {
20+
assertEq(x >> msb(x), 1, "Common msb");
21+
}
22+
23+
function testFuzz_Msb_Shifts1ToLessThanOrEqualToX(uint256 x) external pure whenNotZero(x) {
24+
assertLe(1 << msb(x), x, "Common msb");
25+
}
26+
27+
modifier whenShiftLeftDoesNotOverflow(uint256 x) {
28+
vm.assume(x <= type(uint256).max >> 1);
29+
_;
30+
}
31+
32+
function testFuzz_Msb_Shifts2ToMoreThanX(uint256 x) external pure whenShiftLeftDoesNotOverflow(x) {
33+
assertGt(2 << msb(x), x, "Common msb");
34+
}
35+
}

test/fuzz/common/sqrt.t.sol

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.19 <0.9.0;
3+
4+
import { sqrt } from "src/Common.sol";
5+
6+
import { Base_Test } from "../../Base.t.sol";
7+
8+
/// @dev Collection of tests for the square root function available in `Common.sol`.
9+
contract Common_Sqrt_Test is Base_Test {
10+
uint256 internal constant MAX_SQRT = type(uint128).max;
11+
12+
function testFuzz_Sqrt_Of256BitNumber(uint256 x) external pure {
13+
vm.assertLe(sqrt(x) ** 2, x, "Common sqrt");
14+
if (x < MAX_SQRT ** 2) {
15+
vm.assertGt((sqrt(x) + 1) ** 2, x, "Common sqrt");
16+
} else {
17+
// This case cannot be tested nicely using 'vm.assume()',
18+
// because the fuzzer fails to find enough valid input.
19+
// That's why we have to embed it here with an 'if'.
20+
vm.assertEq(sqrt(x), MAX_SQRT, "Common sqrt");
21+
}
22+
}
23+
24+
modifier whenSquareDoesNotOverflow(uint256 x) {
25+
vm.assume(x <= MAX_SQRT);
26+
_;
27+
}
28+
29+
function testFuzz_Sqrt_OfPerfectSquare(uint256 x) external pure whenSquareDoesNotOverflow(x) {
30+
vm.assertEq(sqrt(x ** 2), x, "Common sqrt");
31+
}
32+
33+
modifier whenNotZero(uint256 x) {
34+
vm.assume(x != 0);
35+
_;
36+
}
37+
38+
function testFuzz_Sqrt_OfAlmostPerfectSquare(uint256 x) external pure whenNotZero(x) whenSquareDoesNotOverflow(x) {
39+
vm.assertEq(sqrt(x ** 2 - 1), x - 1, "Common sqrt");
40+
}
41+
42+
modifier whenEven(uint256 x) {
43+
vm.assume(x % 2 == 0);
44+
_;
45+
}
46+
47+
function testFuzz_Sqrt_OfPowerOfTwo(uint8 x) external pure whenEven(x) {
48+
vm.assertEq(sqrt(2 ** x), 2 ** (x / 2), "Common sqrt");
49+
}
50+
}

0 commit comments

Comments
 (0)