|
3 | 3 | pragma solidity ^0.8.20;
|
4 | 4 |
|
5 | 5 | import {Test} from "forge-std/Test.sol";
|
| 6 | +import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; |
6 | 7 | import {Bytes} from "@openzeppelin/contracts/utils/Bytes.sol";
|
7 | 8 |
|
8 | 9 | contract BytesTest is Test {
|
| 10 | + using Bytes for bytes; |
| 11 | + |
| 12 | + function testSliceWithStartOnly(bytes memory buffer, uint256 start) public pure { |
| 13 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 14 | + bytes memory result = buffer.slice(start); |
| 15 | + |
| 16 | + // Original buffer was not modified |
| 17 | + assertEq(buffer, originalBuffer); |
| 18 | + |
| 19 | + // Should return bytes from start to end |
| 20 | + assertEq(result.length, Math.saturatingSub(buffer.length, start)); |
| 21 | + |
| 22 | + // Verify content matches |
| 23 | + for (uint256 i = 0; i < result.length; ++i) { |
| 24 | + assertEq(result[i], buffer[start + i]); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + function testSlice(bytes memory buffer, uint256 start, uint256 end) public pure { |
| 29 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 30 | + bytes memory result = buffer.slice(start, end); |
| 31 | + |
| 32 | + // Original buffer was not modified |
| 33 | + assertEq(buffer, originalBuffer); |
| 34 | + |
| 35 | + // Calculate expected bounds after sanitization |
| 36 | + uint256 sanitizedEnd = Math.min(end, buffer.length); |
| 37 | + uint256 sanitizedStart = Math.min(start, sanitizedEnd); |
| 38 | + uint256 expectedLength = sanitizedEnd - sanitizedStart; |
| 39 | + |
| 40 | + assertEq(result.length, expectedLength); |
| 41 | + |
| 42 | + // Verify content matches when there's content to verify |
| 43 | + for (uint256 i = 0; i < result.length; ++i) { |
| 44 | + assertEq(result[i], buffer[sanitizedStart + i]); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + function testSpliceWithStartOnly(bytes memory buffer, uint256 start) public pure { |
| 49 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 50 | + bytes memory result = buffer.splice(start); |
| 51 | + |
| 52 | + // Result should be the same object as input (modified in place) |
| 53 | + assertEq(result, buffer); |
| 54 | + |
| 55 | + // Should contain bytes from start to end, moved to beginning |
| 56 | + assertEq(result.length, Math.saturatingSub(originalBuffer.length, start)); |
| 57 | + |
| 58 | + // Verify content matches moved content |
| 59 | + for (uint256 i = 0; i < result.length; ++i) { |
| 60 | + assertEq(result[i], originalBuffer[start + i]); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + function testSplice(bytes memory buffer, uint256 start, uint256 end) public pure { |
| 65 | + bytes memory originalBuffer = bytes.concat(buffer); |
| 66 | + bytes memory result = buffer.splice(start, end); |
| 67 | + |
| 68 | + // Result should be the same object as input (modified in place) |
| 69 | + assertEq(result, buffer); |
| 70 | + |
| 71 | + // Calculate expected bounds after sanitization |
| 72 | + uint256 sanitizedEnd = Math.min(end, originalBuffer.length); |
| 73 | + uint256 sanitizedStart = Math.min(start, sanitizedEnd); |
| 74 | + uint256 expectedLength = sanitizedEnd - sanitizedStart; |
| 75 | + |
| 76 | + assertEq(result.length, expectedLength); |
| 77 | + |
| 78 | + // Verify content matches moved content |
| 79 | + for (uint256 i = 0; i < result.length; ++i) { |
| 80 | + assertEq(result[i], originalBuffer[sanitizedStart + i]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
9 | 84 | // REVERSE BITS
|
10 | 85 | function testSymbolicReverseBytes32(bytes32 value) public pure {
|
11 | 86 | assertEq(Bytes.reverseBytes32(Bytes.reverseBytes32(value)), value);
|
|
0 commit comments