Bytes.t.sol 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. function testSpliceWithStartOnly(bytes memory buffer, uint256 start) public pure {
  98. bytes memory originalBuffer = bytes.concat(buffer);
  99. bytes memory result = buffer.splice(start);
  100. // Result should be the same object as input (modified in place)
  101. assertEq(result, buffer);
  102. // Should contain bytes from start to end, moved to beginning
  103. assertEq(result.length, Math.saturatingSub(originalBuffer.length, start));
  104. // Verify content matches moved content
  105. for (uint256 i = 0; i < result.length; ++i) {
  106. assertEq(result[i], originalBuffer[start + i]);
  107. }
  108. }
  109. function testSplice(bytes memory buffer, uint256 start, uint256 end) public pure {
  110. bytes memory originalBuffer = bytes.concat(buffer);
  111. bytes memory result = buffer.splice(start, end);
  112. // Result should be the same object as input (modified in place)
  113. assertEq(result, buffer);
  114. // Calculate expected bounds after sanitization
  115. uint256 sanitizedEnd = Math.min(end, originalBuffer.length);
  116. uint256 sanitizedStart = Math.min(start, sanitizedEnd);
  117. uint256 expectedLength = sanitizedEnd - sanitizedStart;
  118. assertEq(result.length, expectedLength);
  119. // Verify content matches moved content
  120. for (uint256 i = 0; i < result.length; ++i) {
  121. assertEq(result[i], originalBuffer[sanitizedStart + i]);
  122. }
  123. }
  124. // REVERSE BITS
  125. function testSymbolicReverseBytes32(bytes32 value) public pure {
  126. assertEq(Bytes.reverseBytes32(Bytes.reverseBytes32(value)), value);
  127. }
  128. function testSymbolicReverseBytes16(bytes16 value) public pure {
  129. assertEq(Bytes.reverseBytes16(Bytes.reverseBytes16(value)), value);
  130. }
  131. function testSymbolicReverseBytes16Dirty(bytes16 value) public pure {
  132. assertEq(Bytes.reverseBytes16(Bytes.reverseBytes16(_dirtyBytes16(value))), value);
  133. assertEq(Bytes.reverseBytes16(_dirtyBytes16(Bytes.reverseBytes16(value))), value);
  134. }
  135. function testSymbolicReverseBytes8(bytes8 value) public pure {
  136. assertEq(Bytes.reverseBytes8(Bytes.reverseBytes8(value)), value);
  137. }
  138. function testSymbolicReverseBytes8Dirty(bytes8 value) public pure {
  139. assertEq(Bytes.reverseBytes8(Bytes.reverseBytes8(_dirtyBytes8(value))), value);
  140. assertEq(Bytes.reverseBytes8(_dirtyBytes8(Bytes.reverseBytes8(value))), value);
  141. }
  142. function testSymbolicReverseBytes4(bytes4 value) public pure {
  143. assertEq(Bytes.reverseBytes4(Bytes.reverseBytes4(value)), value);
  144. }
  145. function testSymbolicReverseBytes4Dirty(bytes4 value) public pure {
  146. assertEq(Bytes.reverseBytes4(Bytes.reverseBytes4(_dirtyBytes4(value))), value);
  147. assertEq(Bytes.reverseBytes4(_dirtyBytes4(Bytes.reverseBytes4(value))), value);
  148. }
  149. function testSymbolicReverseBytes2(bytes2 value) public pure {
  150. assertEq(Bytes.reverseBytes2(Bytes.reverseBytes2(value)), value);
  151. }
  152. function testSymbolicReverseBytes2Dirty(bytes2 value) public pure {
  153. assertEq(Bytes.reverseBytes2(Bytes.reverseBytes2(_dirtyBytes2(value))), value);
  154. assertEq(Bytes.reverseBytes2(_dirtyBytes2(Bytes.reverseBytes2(value))), value);
  155. }
  156. // Helpers
  157. function _dirtyBytes16(bytes16 value) private pure returns (bytes16 dirty) {
  158. assembly ("memory-safe") {
  159. dirty := or(value, shr(128, not(0)))
  160. }
  161. }
  162. function _dirtyBytes8(bytes8 value) private pure returns (bytes8 dirty) {
  163. assembly ("memory-safe") {
  164. dirty := or(value, shr(192, not(0)))
  165. }
  166. }
  167. function _dirtyBytes4(bytes4 value) private pure returns (bytes4 dirty) {
  168. assembly ("memory-safe") {
  169. dirty := or(value, shr(224, not(0)))
  170. }
  171. }
  172. function _dirtyBytes2(bytes2 value) private pure returns (bytes2 dirty) {
  173. assembly ("memory-safe") {
  174. dirty := or(value, shr(240, not(0)))
  175. }
  176. }
  177. }