|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +import {Test} from "forge-std/Test.sol"; |
| 6 | +import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; |
| 7 | +import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol"; |
| 8 | + |
| 9 | +contract BytesTest is Test { |
| 10 | + using Bytes for bytes; |
| 11 | + |
| 12 | + // INDEX OF |
| 13 | + function testIndexOf(bytes memory buffer, bytes1 s) public pure { |
| 14 | + uint256 result = Bytes.indexOf(buffer, s); |
| 15 | + |
| 16 | + if (buffer.length == 0) { |
| 17 | + // Case 0: buffer is empty |
| 18 | + assertEq(result, type(uint256).max); |
| 19 | + } else if (result == type(uint256).max) { |
| 20 | + // Case 1: search value could not be found |
| 21 | + for (uint256 i = 0; i < buffer.length; ++i) assertNotEq(buffer[i], s); |
| 22 | + } else { |
| 23 | + // Case 2: search value was found |
| 24 | + assertEq(buffer[result], s); |
| 25 | + // search value is not present anywhere before the found location |
| 26 | + for (uint256 i = 0; i < result; ++i) assertNotEq(buffer[i], s); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + function testIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure { |
| 31 | + uint256 result = Bytes.indexOf(buffer, s, pos); |
| 32 | + |
| 33 | + if (buffer.length == 0) { |
| 34 | + // Case 0: buffer is empty |
| 35 | + assertEq(result, type(uint256).max); |
| 36 | + } else if (result == type(uint256).max) { |
| 37 | + // Case 1: search value could not be found |
| 38 | + for (uint256 i = pos; i < buffer.length; ++i) assertNotEq(buffer[i], s); |
| 39 | + } else { |
| 40 | + // Case 2: search value was found |
| 41 | + assertEq(buffer[result], s); |
| 42 | + // search value is not present anywhere before the found location |
| 43 | + for (uint256 i = pos; i < result; ++i) assertNotEq(buffer[i], s); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + function testLastIndexOf(bytes memory buffer, bytes1 s) public pure { |
| 48 | + uint256 result = Bytes.lastIndexOf(buffer, s); |
| 49 | + |
| 50 | + if (buffer.length == 0) { |
| 51 | + // Case 0: buffer is empty |
| 52 | + assertEq(result, type(uint256).max); |
| 53 | + } else if (result == type(uint256).max) { |
| 54 | + // Case 1: search value could not be found |
| 55 | + for (uint256 i = 0; i < buffer.length; ++i) assertNotEq(buffer[i], s); |
| 56 | + } else { |
| 57 | + // Case 2: search value was found |
| 58 | + assertEq(buffer[result], s); |
| 59 | + // search value is not present anywhere after the found location |
| 60 | + for (uint256 i = result + 1; i < buffer.length; ++i) assertNotEq(buffer[i], s); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function testLastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure { |
| 65 | + uint256 result = Bytes.lastIndexOf(buffer, s, pos); |
| 66 | + |
| 67 | + if (buffer.length == 0) { |
| 68 | + // Case 0: buffer is empty |
| 69 | + assertEq(result, type(uint256).max); |
| 70 | + } else if (result == type(uint256).max) { |
| 71 | + // Case 1: search value could not be found |
| 72 | + for (uint256 i = 0; i <= Math.min(pos, buffer.length - 1); ++i) assertNotEq(buffer[i], s); |
| 73 | + } else { |
| 74 | + // Case 2: search value was found |
| 75 | + assertEq(buffer[result], s); |
| 76 | + // search value is not present anywhere after the found location |
| 77 | + for (uint256 i = result + 1; i <= Math.min(pos, buffer.length - 1); ++i) assertNotEq(buffer[i], s); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // SLICES |
| 82 | + function testSliceWithStartOnly(bytes memory buffer, uint256 start) public pure { |
| 83 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 84 | + bytes memory result = buffer.slice(start); |
| 85 | + |
| 86 | + // Original buffer was not modified |
| 87 | + assertEq(buffer, originalBuffer); |
| 88 | + |
| 89 | + // Should return bytes from start to end |
| 90 | + assertEq(result.length, Math.saturatingSub(buffer.length, start)); |
| 91 | + |
| 92 | + // Verify content matches |
| 93 | + for (uint256 i = 0; i < result.length; ++i) { |
| 94 | + assertEq(result[i], buffer[start + i]); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function testSlice(bytes memory buffer, uint256 start, uint256 end) public pure { |
| 99 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 100 | + bytes memory result = buffer.slice(start, end); |
| 101 | + |
| 102 | + // Original buffer was not modified |
| 103 | + assertEq(buffer, originalBuffer); |
| 104 | + |
| 105 | + // Calculate expected bounds after sanitization |
| 106 | + uint256 sanitizedEnd = Math.min(end, buffer.length); |
| 107 | + uint256 sanitizedStart = Math.min(start, sanitizedEnd); |
| 108 | + uint256 expectedLength = sanitizedEnd - sanitizedStart; |
| 109 | + |
| 110 | + assertEq(result.length, expectedLength); |
| 111 | + |
| 112 | + // Verify content matches when there's content to verify |
| 113 | + for (uint256 i = 0; i < result.length; ++i) { |
| 114 | + assertEq(result[i], buffer[sanitizedStart + i]); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments