Bytes.t.sol 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. function testSymbolicEqual(bytes memory a) public pure {
  9. assertTrue(Bytes.equal(a, a));
  10. }
  11. // INDEX OF
  12. function testIndexOf(bytes memory buffer, bytes1 s) public pure {
  13. uint256 result = Bytes.indexOf(buffer, s);
  14. if (buffer.length == 0) {
  15. // Case 0: buffer is empty
  16. assertEq(result, type(uint256).max);
  17. } else if (result == type(uint256).max) {
  18. // Case 1: search value could not be found
  19. for (uint256 i = 0; i < buffer.length; ++i) assertNotEq(buffer[i], s);
  20. } else {
  21. // Case 2: search value was found
  22. assertEq(buffer[result], s);
  23. // search value is not present anywhere before the found location
  24. for (uint256 i = 0; i < result; ++i) assertNotEq(buffer[i], s);
  25. }
  26. }
  27. function testIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure {
  28. uint256 result = Bytes.indexOf(buffer, s, pos);
  29. if (buffer.length == 0) {
  30. // Case 0: buffer is empty
  31. assertEq(result, type(uint256).max);
  32. } else if (result == type(uint256).max) {
  33. // Case 1: search value could not be found
  34. for (uint256 i = pos; i < buffer.length; ++i) assertNotEq(buffer[i], s);
  35. } else {
  36. // Case 2: search value was found
  37. assertEq(buffer[result], s);
  38. // search value is not present anywhere before the found location
  39. for (uint256 i = pos; i < result; ++i) assertNotEq(buffer[i], s);
  40. }
  41. }
  42. function testLastIndexOf(bytes memory buffer, bytes1 s) public pure {
  43. uint256 result = Bytes.lastIndexOf(buffer, s);
  44. if (buffer.length == 0) {
  45. // Case 0: buffer is empty
  46. assertEq(result, type(uint256).max);
  47. } else if (result == type(uint256).max) {
  48. // Case 1: search value could not be found
  49. for (uint256 i = 0; i < buffer.length; ++i) assertNotEq(buffer[i], s);
  50. } else {
  51. // Case 2: search value was found
  52. assertEq(buffer[result], s);
  53. // search value is not present anywhere after the found location
  54. for (uint256 i = result + 1; i < buffer.length; ++i) assertNotEq(buffer[i], s);
  55. }
  56. }
  57. function testLastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) public pure {
  58. uint256 result = Bytes.lastIndexOf(buffer, s, pos);
  59. if (buffer.length == 0) {
  60. // Case 0: buffer is empty
  61. assertEq(result, type(uint256).max);
  62. } else if (result == type(uint256).max) {
  63. // Case 1: search value could not be found
  64. for (uint256 i = 0; i <= Math.min(pos, buffer.length - 1); ++i) assertNotEq(buffer[i], s);
  65. } else {
  66. // Case 2: search value was found
  67. assertEq(buffer[result], s);
  68. // search value is not present anywhere after the found location
  69. for (uint256 i = result + 1; i <= Math.min(pos, buffer.length - 1); ++i) assertNotEq(buffer[i], s);
  70. }
  71. }
  72. // SLICES
  73. function testSliceWithStartOnly(bytes memory buffer, uint256 start) public pure {
  74. bytes memory originalBuffer = bytes.concat(buffer);
  75. bytes memory result = buffer.slice(start);
  76. // Original buffer was not modified
  77. assertEq(buffer, originalBuffer);
  78. // Should return bytes from start to end
  79. assertEq(result.length, Math.saturatingSub(buffer.length, start));
  80. // Verify content matches
  81. for (uint256 i = 0; i < result.length; ++i) {
  82. assertEq(result[i], buffer[start + i]);
  83. }
  84. }
  85. function testSlice(bytes memory buffer, uint256 start, uint256 end) public pure {
  86. bytes memory originalBuffer = bytes.concat(buffer);
  87. bytes memory result = buffer.slice(start, end);
  88. // Original buffer was not modified
  89. assertEq(buffer, originalBuffer);
  90. // Calculate expected bounds after sanitization
  91. uint256 sanitizedEnd = Math.min(end, buffer.length);
  92. uint256 sanitizedStart = Math.min(start, sanitizedEnd);
  93. uint256 expectedLength = sanitizedEnd - sanitizedStart;
  94. assertEq(result.length, expectedLength);
  95. // Verify content matches when there's content to verify
  96. for (uint256 i = 0; i < result.length; ++i) {
  97. assertEq(result[i], buffer[sanitizedStart + i]);
  98. }
  99. }
  100. function testSpliceWithStartOnly(bytes memory buffer, uint256 start) public pure {
  101. bytes memory originalBuffer = bytes.concat(buffer);
  102. bytes memory result = buffer.splice(start);
  103. // Result should be the same object as input (modified in place)
  104. assertEq(result, buffer);
  105. // Should contain bytes from start to end, moved to beginning
  106. assertEq(result.length, Math.saturatingSub(originalBuffer.length, start));
  107. // Verify content matches moved content
  108. for (uint256 i = 0; i < result.length; ++i) {
  109. assertEq(result[i], originalBuffer[start + i]);
  110. }
  111. }
  112. function testSplice(bytes memory buffer, uint256 start, uint256 end) public pure {
  113. bytes memory originalBuffer = bytes.concat(buffer);
  114. bytes memory result = buffer.splice(start, end);
  115. // Result should be the same object as input (modified in place)
  116. assertEq(result, buffer);
  117. // Calculate expected bounds after sanitization
  118. uint256 sanitizedEnd = Math.min(end, originalBuffer.length);
  119. uint256 sanitizedStart = Math.min(start, sanitizedEnd);
  120. uint256 expectedLength = sanitizedEnd - sanitizedStart;
  121. assertEq(result.length, expectedLength);
  122. // Verify content matches moved content
  123. for (uint256 i = 0; i < result.length; ++i) {
  124. assertEq(result[i], originalBuffer[sanitizedStart + i]);
  125. }
  126. }
  127. // REVERSE BITS
  128. function testSymbolicReverseBytes32(bytes32 value) public pure {
  129. assertEq(Bytes.reverseBytes32(Bytes.reverseBytes32(value)), value);
  130. }
  131. function testSymbolicReverseBytes16(bytes16 value) public pure {
  132. assertEq(Bytes.reverseBytes16(Bytes.reverseBytes16(value)), value);
  133. }
  134. function testSymbolicReverseBytes16Dirty(bytes16 value) public pure {
  135. assertEq(Bytes.reverseBytes16(Bytes.reverseBytes16(_dirtyBytes16(value))), value);
  136. assertEq(Bytes.reverseBytes16(_dirtyBytes16(Bytes.reverseBytes16(value))), value);
  137. }
  138. function testSymbolicReverseBytes8(bytes8 value) public pure {
  139. assertEq(Bytes.reverseBytes8(Bytes.reverseBytes8(value)), value);
  140. }
  141. function testSymbolicReverseBytes8Dirty(bytes8 value) public pure {
  142. assertEq(Bytes.reverseBytes8(Bytes.reverseBytes8(_dirtyBytes8(value))), value);
  143. assertEq(Bytes.reverseBytes8(_dirtyBytes8(Bytes.reverseBytes8(value))), value);
  144. }
  145. function testSymbolicReverseBytes4(bytes4 value) public pure {
  146. assertEq(Bytes.reverseBytes4(Bytes.reverseBytes4(value)), value);
  147. }
  148. function testSymbolicReverseBytes4Dirty(bytes4 value) public pure {
  149. assertEq(Bytes.reverseBytes4(Bytes.reverseBytes4(_dirtyBytes4(value))), value);
  150. assertEq(Bytes.reverseBytes4(_dirtyBytes4(Bytes.reverseBytes4(value))), value);
  151. }
  152. function testSymbolicReverseBytes2(bytes2 value) public pure {
  153. assertEq(Bytes.reverseBytes2(Bytes.reverseBytes2(value)), value);
  154. }
  155. function testSymbolicReverseBytes2Dirty(bytes2 value) public pure {
  156. assertEq(Bytes.reverseBytes2(Bytes.reverseBytes2(_dirtyBytes2(value))), value);
  157. assertEq(Bytes.reverseBytes2(_dirtyBytes2(Bytes.reverseBytes2(value))), value);
  158. }
  159. // CLZ (Count Leading Zeros)
  160. function testClz(bytes memory buffer) public pure {
  161. uint256 result = Bytes.clz(buffer);
  162. // index and offset of the first non zero bit
  163. uint256 index = result / 8;
  164. uint256 offset = result % 8;
  165. // Result should never exceed buffer length
  166. assertLe(index, buffer.length);
  167. // All bytes before index position must be zero
  168. for (uint256 i = 0; i < index; ++i) {
  169. assertEq(buffer[i], 0);
  170. }
  171. // If index < buffer.length, byte at index position must be non-zero
  172. if (index < buffer.length) {
  173. // bit at position offset must be non zero
  174. bytes1 singleBitMask = bytes1(0x80) >> offset;
  175. assertEq(buffer[index] & singleBitMask, singleBitMask);
  176. // all bits before offset must be zero
  177. bytes1 multiBitsMask = bytes1(0xff) << (8 - offset);
  178. assertEq(buffer[index] & multiBitsMask, 0);
  179. }
  180. }
  181. // Helpers
  182. function _dirtyBytes16(bytes16 value) private pure returns (bytes16 dirty) {
  183. assembly ("memory-safe") {
  184. dirty := or(value, shr(128, not(0)))
  185. }
  186. }
  187. function _dirtyBytes8(bytes8 value) private pure returns (bytes8 dirty) {
  188. assembly ("memory-safe") {
  189. dirty := or(value, shr(192, not(0)))
  190. }
  191. }
  192. function _dirtyBytes4(bytes4 value) private pure returns (bytes4 dirty) {
  193. assembly ("memory-safe") {
  194. dirty := or(value, shr(224, not(0)))
  195. }
  196. }
  197. function _dirtyBytes2(bytes2 value) private pure returns (bytes2 dirty) {
  198. assembly ("memory-safe") {
  199. dirty := or(value, shr(240, not(0)))
  200. }
  201. }
  202. }