Bytes.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const mock = await ethers.deployContract('$Bytes');
  6. return { mock };
  7. }
  8. const lorem = ethers.toUtf8Bytes(
  9. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
  10. );
  11. const present = lorem.at(1);
  12. const absent = 255;
  13. describe('Bytes', function () {
  14. before(async function () {
  15. Object.assign(this, await loadFixture(fixture));
  16. });
  17. describe('indexOf', function () {
  18. it('first', async function () {
  19. await expect(this.mock.$indexOf(lorem, ethers.toBeHex(present))).to.eventually.equal(lorem.indexOf(present));
  20. });
  21. it('from index', async function () {
  22. for (const start in Array(lorem.length + 10).fill()) {
  23. const index = lorem.indexOf(present, start);
  24. const result = index === -1 ? ethers.MaxUint256 : index;
  25. await expect(
  26. this.mock.$indexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start)),
  27. ).to.eventually.equal(result);
  28. }
  29. });
  30. it('absent', async function () {
  31. await expect(this.mock.$indexOf(lorem, ethers.toBeHex(absent))).to.eventually.equal(ethers.MaxUint256);
  32. });
  33. it('empty buffer', async function () {
  34. await expect(this.mock.$indexOf('0x', '0x00')).to.eventually.equal(ethers.MaxUint256);
  35. await expect(this.mock.$indexOf('0x', '0x00', ethers.Typed.uint256(17))).to.eventually.equal(ethers.MaxUint256);
  36. });
  37. });
  38. describe('lastIndexOf', function () {
  39. it('first', async function () {
  40. await expect(this.mock.$lastIndexOf(lorem, ethers.toBeHex(present))).to.eventually.equal(
  41. lorem.lastIndexOf(present),
  42. );
  43. });
  44. it('from index', async function () {
  45. for (const start in Array(lorem.length + 10).fill()) {
  46. const index = lorem.lastIndexOf(present, start);
  47. const result = index === -1 ? ethers.MaxUint256 : index;
  48. await expect(
  49. this.mock.$lastIndexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start)),
  50. ).to.eventually.equal(result);
  51. }
  52. });
  53. it('absent', async function () {
  54. await expect(this.mock.$lastIndexOf(lorem, ethers.toBeHex(absent))).to.eventually.equal(ethers.MaxUint256);
  55. });
  56. it('empty buffer', async function () {
  57. await expect(this.mock.$lastIndexOf('0x', '0x00')).to.eventually.equal(ethers.MaxUint256);
  58. await expect(this.mock.$lastIndexOf('0x', '0x00', ethers.Typed.uint256(17))).to.eventually.equal(
  59. ethers.MaxUint256,
  60. );
  61. });
  62. });
  63. describe('slice', function () {
  64. describe('slice(bytes, uint256)', function () {
  65. for (const [descr, start] of Object.entries({
  66. 'start = 0': 0,
  67. 'start within bound': 10,
  68. 'start out of bound': 1000,
  69. })) {
  70. it(descr, async function () {
  71. const result = ethers.hexlify(lorem.slice(start));
  72. await expect(this.mock.$slice(lorem, start)).to.eventually.equal(result);
  73. });
  74. }
  75. });
  76. describe('slice(bytes, uint256, uint256)', function () {
  77. for (const [descr, [start, end]] of Object.entries({
  78. 'start = 0': [0, 42],
  79. 'start and end within bound': [17, 42],
  80. 'end out of bound': [42, 1000],
  81. 'start = end': [17, 17],
  82. 'start > end': [42, 17],
  83. })) {
  84. it(descr, async function () {
  85. const result = ethers.hexlify(lorem.slice(start, end));
  86. await expect(this.mock.$slice(lorem, start, ethers.Typed.uint256(end))).to.eventually.equal(result);
  87. });
  88. }
  89. });
  90. });
  91. });