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