Skip to content

Commit 6d20d77

Browse files
committed
simplify fuzz cases
1 parent 29b1b35 commit 6d20d77

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

test/utils/Bytes.t.sol

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ contract BytesTest is Test {
1717
function testIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure {
1818
uint256 result = Bytes.indexOf(buffer, s, pos);
1919

20-
// The search value should not me present between `pos` (included) and `result` excluded.
21-
// Do not search after the end of the buffer
22-
for (uint256 i = pos; i < Math.min(result, buffer.length); ++i) assertNotEq(buffer[i], s);
23-
if (result != type(uint256).max) assertEq(buffer[result], s);
20+
if (result == type(uint256).max) {
21+
// Case 1: search value could not be found
22+
for (uint256 i = pos; i < buffer.length; ++i) assertNotEq(buffer[i], s);
23+
} else {
24+
// Case 2: search value was found
25+
assertEq(buffer[result], s);
26+
// search value is not present anywhere before the found location
27+
for (uint256 i = pos; i < result; ++i) assertNotEq(buffer[i], s);
28+
}
2429
}
2530

2631
function testLastIndexOf(bytes memory buffer, bytes1 s) public pure {
@@ -30,13 +35,16 @@ contract BytesTest is Test {
3035
function testLastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure {
3136
uint256 result = Bytes.lastIndexOf(buffer, s, pos);
3237

33-
// Case found: the search value should not be present between `result` (excluded) and `pos` (included)
34-
// Case not found: the search value should not be present anywhere before `pos` (included)
35-
unchecked {
36-
// using unchecked gives us `result + 1 == 0` in the "not found" case
37-
for (uint256 i = result + 1; i < Math.min(pos + 1, buffer.length); ++i) assertNotEq(buffer[i], s);
38+
uint256 end = pos == type(uint256).max ? buffer.length : Math.min(pos + 1, buffer.length);
39+
if (result == type(uint256).max) {
40+
// Case 1: search value could not be found
41+
for (uint256 i = 0; i < end; ++i) assertNotEq(buffer[i], s);
42+
} else {
43+
// Case 2: search value was found
44+
assertEq(buffer[result], s);
45+
// search value is not present anywhere after the found location
46+
for (uint256 i = result + 1; i < end; ++i) assertNotEq(buffer[i], s);
3847
}
39-
if (result != type(uint256).max) assertEq(buffer[result], s);
4048
}
4149

4250
// SLICES

0 commit comments

Comments
 (0)