Bytes.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { MAX_UINT128, MAX_UINT64, MAX_UINT32, MAX_UINT16 } = require('../helpers/constants');
  5. // Helper functions for fixed bytes types
  6. const bytes32 = value => ethers.toBeHex(value, 32);
  7. const bytes16 = value => ethers.toBeHex(value, 16);
  8. const bytes8 = value => ethers.toBeHex(value, 8);
  9. const bytes4 = value => ethers.toBeHex(value, 4);
  10. const bytes2 = value => ethers.toBeHex(value, 2);
  11. async function fixture() {
  12. const mock = await ethers.deployContract('$Bytes');
  13. return { mock };
  14. }
  15. const lorem = ethers.toUtf8Bytes(
  16. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
  17. );
  18. const present = lorem.at(1);
  19. const absent = 255;
  20. describe('Bytes', function () {
  21. before(async function () {
  22. Object.assign(this, await loadFixture(fixture));
  23. });
  24. describe('indexOf', function () {
  25. it('first', async function () {
  26. await expect(this.mock.$indexOf(lorem, ethers.toBeHex(present))).to.eventually.equal(lorem.indexOf(present));
  27. });
  28. it('from index', async function () {
  29. for (const start in Array(lorem.length + 10).fill()) {
  30. const index = lorem.indexOf(present, start);
  31. const result = index === -1 ? ethers.MaxUint256 : index;
  32. await expect(
  33. this.mock.$indexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start)),
  34. ).to.eventually.equal(result);
  35. }
  36. });
  37. it('absent', async function () {
  38. await expect(this.mock.$indexOf(lorem, ethers.toBeHex(absent))).to.eventually.equal(ethers.MaxUint256);
  39. });
  40. it('empty buffer', async function () {
  41. await expect(this.mock.$indexOf('0x', '0x00')).to.eventually.equal(ethers.MaxUint256);
  42. await expect(this.mock.$indexOf('0x', '0x00', ethers.Typed.uint256(17))).to.eventually.equal(ethers.MaxUint256);
  43. });
  44. });
  45. describe('lastIndexOf', function () {
  46. it('first', async function () {
  47. await expect(this.mock.$lastIndexOf(lorem, ethers.toBeHex(present))).to.eventually.equal(
  48. lorem.lastIndexOf(present),
  49. );
  50. });
  51. it('from index', async function () {
  52. for (const start in Array(lorem.length + 10).fill()) {
  53. const index = lorem.lastIndexOf(present, start);
  54. const result = index === -1 ? ethers.MaxUint256 : index;
  55. await expect(
  56. this.mock.$lastIndexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start)),
  57. ).to.eventually.equal(result);
  58. }
  59. });
  60. it('absent', async function () {
  61. await expect(this.mock.$lastIndexOf(lorem, ethers.toBeHex(absent))).to.eventually.equal(ethers.MaxUint256);
  62. });
  63. it('empty buffer', async function () {
  64. await expect(this.mock.$lastIndexOf('0x', '0x00')).to.eventually.equal(ethers.MaxUint256);
  65. await expect(this.mock.$lastIndexOf('0x', '0x00', ethers.Typed.uint256(17))).to.eventually.equal(
  66. ethers.MaxUint256,
  67. );
  68. });
  69. });
  70. describe('slice & splice', function () {
  71. describe('slice(bytes, uint256) & splice(bytes, uint256)', function () {
  72. for (const [descr, start] of Object.entries({
  73. 'start = 0': 0,
  74. 'start within bound': 10,
  75. 'start out of bound': 1000,
  76. })) {
  77. it(descr, async function () {
  78. const result = ethers.hexlify(lorem.slice(start));
  79. await expect(this.mock.$slice(lorem, start)).to.eventually.equal(result);
  80. await expect(this.mock.$splice(lorem, start)).to.eventually.equal(result);
  81. });
  82. }
  83. });
  84. describe('slice(bytes, uint256, uint256) & splice(bytes, uint256, uint256)', function () {
  85. for (const [descr, [start, end]] of Object.entries({
  86. 'start = 0': [0, 42],
  87. 'start and end within bound': [17, 42],
  88. 'end out of bound': [42, 1000],
  89. 'start = end': [17, 17],
  90. 'start > end': [42, 17],
  91. })) {
  92. it(descr, async function () {
  93. const result = ethers.hexlify(lorem.slice(start, end));
  94. await expect(this.mock.$slice(lorem, start, ethers.Typed.uint256(end))).to.eventually.equal(result);
  95. await expect(this.mock.$splice(lorem, start, ethers.Typed.uint256(end))).to.eventually.equal(result);
  96. });
  97. }
  98. });
  99. });
  100. describe('clz bytes', function () {
  101. it('empty buffer', async function () {
  102. await expect(this.mock.$clz('0x')).to.eventually.equal(0);
  103. });
  104. it('single zero byte', async function () {
  105. await expect(this.mock.$clz('0x00')).to.eventually.equal(8);
  106. });
  107. it('single non-zero byte', async function () {
  108. await expect(this.mock.$clz('0x01')).to.eventually.equal(7);
  109. await expect(this.mock.$clz('0xff')).to.eventually.equal(0);
  110. });
  111. it('multiple leading zeros', async function () {
  112. await expect(this.mock.$clz('0x0000000001')).to.eventually.equal(39);
  113. await expect(
  114. this.mock.$clz('0x0000000000000000000000000000000000000000000000000000000000000001'),
  115. ).to.eventually.equal(255);
  116. });
  117. it('all zeros of various lengths', async function () {
  118. await expect(this.mock.$clz('0x00000000')).to.eventually.equal(32);
  119. await expect(
  120. this.mock.$clz('0x0000000000000000000000000000000000000000000000000000000000000000'),
  121. ).to.eventually.equal(256);
  122. // Complete chunks
  123. await expect(this.mock.$clz('0x' + '00'.repeat(32) + '01')).to.eventually.equal(263); // 32*8+7
  124. await expect(this.mock.$clz('0x' + '00'.repeat(64) + '01')).to.eventually.equal(519); // 64*8+7
  125. // Partial last chunk
  126. await expect(this.mock.$clz('0x' + '00'.repeat(33) + '01')).to.eventually.equal(271); // 33*8+7
  127. await expect(this.mock.$clz('0x' + '00'.repeat(34) + '01')).to.eventually.equal(279); // 34*8+7
  128. await expect(this.mock.$clz('0x' + '00'.repeat(40) + '01' + '00'.repeat(9))).to.eventually.equal(327); // 40*8+7
  129. await expect(this.mock.$clz('0x' + '00'.repeat(50))).to.eventually.equal(400); // 50*8
  130. // First byte of each chunk non-zero
  131. await expect(this.mock.$clz('0x80' + '00'.repeat(31))).to.eventually.equal(0);
  132. await expect(this.mock.$clz('0x01' + '00'.repeat(31))).to.eventually.equal(7);
  133. await expect(this.mock.$clz('0x' + '00'.repeat(32) + '80' + '00'.repeat(31))).to.eventually.equal(256); // 32*8
  134. await expect(this.mock.$clz('0x' + '00'.repeat(32) + '01' + '00'.repeat(31))).to.eventually.equal(263); // 32*8+7
  135. // Last byte of each chunk non-zero
  136. await expect(this.mock.$clz('0x' + '00'.repeat(31) + '01')).to.eventually.equal(255); // 31*8+7
  137. await expect(this.mock.$clz('0x' + '00'.repeat(63) + '01')).to.eventually.equal(511); // 63*8+7
  138. // Middle byte of each chunk non-zero
  139. await expect(this.mock.$clz('0x' + '00'.repeat(16) + '01' + '00'.repeat(15))).to.eventually.equal(135); // 16*8+7
  140. await expect(this.mock.$clz('0x' + '00'.repeat(32) + '01' + '00'.repeat(31))).to.eventually.equal(263); // 32*8+7
  141. await expect(this.mock.$clz('0x' + '00'.repeat(48) + '01' + '00'.repeat(47))).to.eventually.equal(391); // 48*8+7
  142. await expect(this.mock.$clz('0x' + '00'.repeat(64) + '01' + '00'.repeat(63))).to.eventually.equal(519); // 64*8+7
  143. });
  144. });
  145. describe('equal', function () {
  146. it('identical buffers', async function () {
  147. await expect(this.mock.$equal(lorem, lorem)).to.eventually.be.true;
  148. });
  149. it('same content', async function () {
  150. const copy = new Uint8Array(lorem);
  151. await expect(this.mock.$equal(lorem, copy)).to.eventually.be.true;
  152. });
  153. it('different content', async function () {
  154. const different = ethers.toUtf8Bytes('Different content');
  155. await expect(this.mock.$equal(lorem, different)).to.eventually.be.false;
  156. });
  157. it('different lengths', async function () {
  158. const shorter = lorem.slice(0, 10);
  159. await expect(this.mock.$equal(lorem, shorter)).to.eventually.be.false;
  160. });
  161. it('empty buffers', async function () {
  162. const empty1 = new Uint8Array(0);
  163. const empty2 = new Uint8Array(0);
  164. await expect(this.mock.$equal(empty1, empty2)).to.eventually.be.true;
  165. });
  166. it('one empty one not', async function () {
  167. const empty = new Uint8Array(0);
  168. await expect(this.mock.$equal(lorem, empty)).to.eventually.be.false;
  169. });
  170. });
  171. describe('reverseBits', function () {
  172. describe('reverseBytes32', function () {
  173. it('reverses bytes correctly', async function () {
  174. await expect(this.mock.$reverseBytes32(bytes32(0))).to.eventually.equal(bytes32(0));
  175. await expect(this.mock.$reverseBytes32(bytes32(ethers.MaxUint256))).to.eventually.equal(
  176. bytes32(ethers.MaxUint256),
  177. );
  178. // Test complex pattern that clearly shows byte reversal
  179. await expect(
  180. this.mock.$reverseBytes32('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'),
  181. ).to.eventually.equal('0xefcdab8967452301efcdab8967452301efcdab8967452301efcdab8967452301');
  182. });
  183. it('double reverse returns original', async function () {
  184. const values = [0n, 1n, 0x12345678n, ethers.MaxUint256];
  185. for (const value of values) {
  186. const reversed = await this.mock.$reverseBytes32(bytes32(value));
  187. await expect(this.mock.$reverseBytes32(reversed)).to.eventually.equal(bytes32(value));
  188. }
  189. });
  190. });
  191. describe('reverseBytes16', function () {
  192. it('reverses bytes correctly', async function () {
  193. await expect(this.mock.$reverseBytes16(bytes16(0))).to.eventually.equal(bytes16(0));
  194. await expect(this.mock.$reverseBytes16(bytes16(MAX_UINT128))).to.eventually.equal(bytes16(MAX_UINT128));
  195. // Test complex pattern that clearly shows byte reversal
  196. await expect(this.mock.$reverseBytes16('0x0123456789abcdef0123456789abcdef')).to.eventually.equal(
  197. '0xefcdab8967452301efcdab8967452301',
  198. );
  199. });
  200. it('double reverse returns original', async function () {
  201. const values = [0n, 1n, 0x12345678n, MAX_UINT128];
  202. for (const value of values) {
  203. const reversed = await this.mock.$reverseBytes16(bytes16(value));
  204. // Cast back to uint128 for comparison since function returns uint256
  205. await expect(this.mock.$reverseBytes16(reversed)).to.eventually.equal(bytes16(value & MAX_UINT128));
  206. }
  207. });
  208. });
  209. describe('reverseBytes8', function () {
  210. it('reverses bytes correctly', async function () {
  211. await expect(this.mock.$reverseBytes8(bytes8(0))).to.eventually.equal(bytes8(0));
  212. await expect(this.mock.$reverseBytes8(bytes8(MAX_UINT64))).to.eventually.equal(bytes8(MAX_UINT64));
  213. // Test known pattern: 0x123456789ABCDEF0 -> 0xF0DEBC9A78563412
  214. await expect(this.mock.$reverseBytes8('0x123456789abcdef0')).to.eventually.equal('0xf0debc9a78563412');
  215. });
  216. it('double reverse returns original', async function () {
  217. const values = [0n, 1n, 0x12345678n, MAX_UINT64];
  218. for (const value of values) {
  219. const reversed = await this.mock.$reverseBytes8(bytes8(value));
  220. // Cast back to uint64 for comparison since function returns uint256
  221. await expect(this.mock.$reverseBytes8(reversed)).to.eventually.equal(bytes8(value & MAX_UINT64));
  222. }
  223. });
  224. });
  225. describe('reverseBytes4', function () {
  226. it('reverses bytes correctly', async function () {
  227. await expect(this.mock.$reverseBytes4(bytes4(0))).to.eventually.equal(bytes4(0));
  228. await expect(this.mock.$reverseBytes4(bytes4(MAX_UINT32))).to.eventually.equal(bytes4(MAX_UINT32));
  229. // Test known pattern: 0x12345678 -> 0x78563412
  230. await expect(this.mock.$reverseBytes4(bytes4(0x12345678))).to.eventually.equal(bytes4(0x78563412));
  231. });
  232. it('double reverse returns original', async function () {
  233. const values = [0n, 1n, 0x12345678n, MAX_UINT32];
  234. for (const value of values) {
  235. const reversed = await this.mock.$reverseBytes4(bytes4(value));
  236. // Cast back to uint32 for comparison since function returns uint256
  237. await expect(this.mock.$reverseBytes4(reversed)).to.eventually.equal(bytes4(value & MAX_UINT32));
  238. }
  239. });
  240. });
  241. describe('reverseBytes2', function () {
  242. it('reverses bytes correctly', async function () {
  243. await expect(this.mock.$reverseBytes2(bytes2(0))).to.eventually.equal(bytes2(0));
  244. await expect(this.mock.$reverseBytes2(bytes2(MAX_UINT16))).to.eventually.equal(bytes2(MAX_UINT16));
  245. // Test known pattern: 0x1234 -> 0x3412
  246. await expect(this.mock.$reverseBytes2(bytes2(0x1234))).to.eventually.equal(bytes2(0x3412));
  247. });
  248. it('double reverse returns original', async function () {
  249. const values = [0n, 1n, 0x1234n, MAX_UINT16];
  250. for (const value of values) {
  251. const reversed = await this.mock.$reverseBytes2(bytes2(value));
  252. // Cast back to uint16 for comparison since function returns uint256
  253. await expect(this.mock.$reverseBytes2(reversed)).to.eventually.equal(bytes2(value & MAX_UINT16));
  254. }
  255. });
  256. });
  257. describe('edge cases', function () {
  258. it('handles single byte values', async function () {
  259. await expect(this.mock.$reverseBytes2(bytes2(0x00ff))).to.eventually.equal(bytes2(0xff00));
  260. await expect(this.mock.$reverseBytes4(bytes4(0x000000ff))).to.eventually.equal(bytes4(0xff000000));
  261. });
  262. it('handles alternating patterns', async function () {
  263. await expect(this.mock.$reverseBytes2(bytes2(0xaaaa))).to.eventually.equal(bytes2(0xaaaa));
  264. await expect(this.mock.$reverseBytes2(bytes2(0x5555))).to.eventually.equal(bytes2(0x5555));
  265. await expect(this.mock.$reverseBytes4(bytes4(0xaaaaaaaa))).to.eventually.equal(bytes4(0xaaaaaaaa));
  266. await expect(this.mock.$reverseBytes4(bytes4(0x55555555))).to.eventually.equal(bytes4(0x55555555));
  267. });
  268. });
  269. });
  270. });