Skip to content

Commit edf974e

Browse files
committed
simplify fuzz cases
1 parent 29b1b35 commit edf974e

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

test/utils/Bytes.t.sol

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ 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 (buffer.length == 0) {
21+
// Case 0: buffer is empty
22+
assertEq(result, type(uint256).max);
23+
} else if (result == type(uint256).max) {
24+
// Case 1: search value could not be found
25+
for (uint256 i = pos; i < buffer.length; ++i) assertNotEq(buffer[i], s);
26+
} else {
27+
// Case 2: search value was found
28+
assertEq(buffer[result], s);
29+
// search value is not present anywhere before the found location
30+
for (uint256 i = pos; i < result; ++i) assertNotEq(buffer[i], s);
31+
}
2432
}
2533

2634
function testLastIndexOf(bytes memory buffer, bytes1 s) public pure {
@@ -30,13 +38,18 @@ contract BytesTest is Test {
3038
function testLastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure {
3139
uint256 result = Bytes.lastIndexOf(buffer, s, pos);
3240

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);
41+
if (buffer.length == 0) {
42+
// Case 0: buffer is empty
43+
assertEq(result, type(uint256).max);
44+
} else if (result == type(uint256).max) {
45+
// Case 1: search value could not be found
46+
for (uint256 i = 0; i <= Math.min(pos, buffer.length - 1); ++i) assertNotEq(buffer[i], s);
47+
} else {
48+
// Case 2: search value was found
49+
assertEq(buffer[result], s);
50+
// search value is not present anywhere after the found location
51+
for (uint256 i = result + 1; i <= Math.min(pos, buffer.length - 1); ++i) assertNotEq(buffer[i], s);
3852
}
39-
if (result != type(uint256).max) assertEq(buffer[result], s);
4053
}
4154

4255
// SLICES

0 commit comments

Comments
 (0)