|
| 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 | + |
| 118 | + function testSpliceWithStartOnly(bytes memory buffer, uint256 start) public pure { |
| 119 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 120 | + bytes memory result = buffer.splice(start); |
| 121 | + |
| 122 | + // Result should be the same object as input (modified in place) |
| 123 | + assertEq(result, buffer); |
| 124 | + |
| 125 | + // Should contain bytes from start to end, moved to beginning |
| 126 | + assertEq(result.length, Math.saturatingSub(originalBuffer.length, start)); |
| 127 | + |
| 128 | + // Verify content matches moved content |
| 129 | + for (uint256 i = 0; i < result.length; ++i) { |
| 130 | + assertEq(result[i], originalBuffer[start + i]); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + function testSplice(bytes memory buffer, uint256 start, uint256 end) public pure { |
| 135 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 136 | + bytes memory result = buffer.splice(start, end); |
| 137 | + |
| 138 | + // Result should be the same object as input (modified in place) |
| 139 | + assertEq(result, buffer); |
| 140 | + |
| 141 | + // Calculate expected bounds after sanitization |
| 142 | + uint256 sanitizedEnd = Math.min(end, originalBuffer.length); |
| 143 | + uint256 sanitizedStart = Math.min(start, sanitizedEnd); |
| 144 | + uint256 expectedLength = sanitizedEnd - sanitizedStart; |
| 145 | + |
| 146 | + assertEq(result.length, expectedLength); |
| 147 | + |
| 148 | + // Verify content matches moved content |
| 149 | + for (uint256 i = 0; i < result.length; ++i) { |
| 150 | + assertEq(result[i], originalBuffer[sanitizedStart + i]); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // REVERSE BITS |
| 155 | + function testSymbolicReverseBytes32(bytes32 value) public pure { |
| 156 | + assertEq(Bytes.reverseBytes32(Bytes.reverseBytes32(value)), value); |
| 157 | + } |
| 158 | + |
| 159 | + function testSymbolicReverseBytes16(bytes16 value) public pure { |
| 160 | + assertEq(Bytes.reverseBytes16(Bytes.reverseBytes16(value)), value); |
| 161 | + } |
| 162 | + |
| 163 | + function testSymbolicReverseBytes16Dirty(bytes16 value) public pure { |
| 164 | + assertEq(Bytes.reverseBytes16(Bytes.reverseBytes16(_dirtyBytes16(value))), value); |
| 165 | + assertEq(Bytes.reverseBytes16(_dirtyBytes16(Bytes.reverseBytes16(value))), value); |
| 166 | + } |
| 167 | + |
| 168 | + function testSymbolicReverseBytes8(bytes8 value) public pure { |
| 169 | + assertEq(Bytes.reverseBytes8(Bytes.reverseBytes8(value)), value); |
| 170 | + } |
| 171 | + |
| 172 | + function testSymbolicReverseBytes8Dirty(bytes8 value) public pure { |
| 173 | + assertEq(Bytes.reverseBytes8(Bytes.reverseBytes8(_dirtyBytes8(value))), value); |
| 174 | + assertEq(Bytes.reverseBytes8(_dirtyBytes8(Bytes.reverseBytes8(value))), value); |
| 175 | + } |
| 176 | + |
| 177 | + function testSymbolicReverseBytes4(bytes4 value) public pure { |
| 178 | + assertEq(Bytes.reverseBytes4(Bytes.reverseBytes4(value)), value); |
| 179 | + } |
| 180 | + |
| 181 | + function testSymbolicReverseBytes4Dirty(bytes4 value) public pure { |
| 182 | + assertEq(Bytes.reverseBytes4(Bytes.reverseBytes4(_dirtyBytes4(value))), value); |
| 183 | + assertEq(Bytes.reverseBytes4(_dirtyBytes4(Bytes.reverseBytes4(value))), value); |
| 184 | + } |
| 185 | + |
| 186 | + function testSymbolicReverseBytes2(bytes2 value) public pure { |
| 187 | + assertEq(Bytes.reverseBytes2(Bytes.reverseBytes2(value)), value); |
| 188 | + } |
| 189 | + |
| 190 | + function testSymbolicReverseBytes2Dirty(bytes2 value) public pure { |
| 191 | + assertEq(Bytes.reverseBytes2(Bytes.reverseBytes2(_dirtyBytes2(value))), value); |
| 192 | + assertEq(Bytes.reverseBytes2(_dirtyBytes2(Bytes.reverseBytes2(value))), value); |
| 193 | + } |
| 194 | + |
| 195 | + // Helpers |
| 196 | + function _dirtyBytes16(bytes16 value) private pure returns (bytes16 dirty) { |
| 197 | + assembly ("memory-safe") { |
| 198 | + dirty := or(value, shr(128, not(0))) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + function _dirtyBytes8(bytes8 value) private pure returns (bytes8 dirty) { |
| 203 | + assembly ("memory-safe") { |
| 204 | + dirty := or(value, shr(192, not(0))) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + function _dirtyBytes4(bytes4 value) private pure returns (bytes4 dirty) { |
| 209 | + assembly ("memory-safe") { |
| 210 | + dirty := or(value, shr(224, not(0))) |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + function _dirtyBytes2(bytes2 value) private pure returns (bytes2 dirty) { |
| 215 | + assembly ("memory-safe") { |
| 216 | + dirty := or(value, shr(240, not(0))) |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments