Bytes.t.sol 4.5 KB

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