CircularBuffer.test.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. const { generators } = require('../../helpers/random');
  6. const LENGTH = 4;
  7. async function fixture() {
  8. const mock = await ethers.deployContract('$CircularBuffer');
  9. await mock.$setup(0, LENGTH);
  10. return { mock };
  11. }
  12. describe('CircularBuffer', function () {
  13. beforeEach(async function () {
  14. Object.assign(this, await loadFixture(fixture));
  15. });
  16. it('starts empty', async function () {
  17. expect(await this.mock.$count(0)).to.equal(0n);
  18. expect(await this.mock.$length(0)).to.equal(LENGTH);
  19. expect(await this.mock.$includes(0, ethers.ZeroHash)).to.be.false;
  20. await expect(this.mock.$last(0, 0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  21. });
  22. it('push', async function () {
  23. const values = Array.from({ length: LENGTH + 3 }, generators.bytes32);
  24. for (const [i, value] of values.map((v, i) => [i, v])) {
  25. // push value
  26. await this.mock.$push(0, value);
  27. // view of the values
  28. const pushed = values.slice(0, i + 1);
  29. const stored = pushed.slice(-LENGTH);
  30. const dropped = pushed.slice(0, -LENGTH);
  31. // check count
  32. expect(await this.mock.$length(0)).to.equal(LENGTH);
  33. expect(await this.mock.$count(0)).to.equal(stored.length);
  34. // check last
  35. for (const j in stored) {
  36. expect(await this.mock.$last(0, j)).to.equal(stored.at(-j - 1));
  37. }
  38. await expect(this.mock.$last(0, stored.length + 1)).to.be.revertedWithPanic(
  39. PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS,
  40. );
  41. // check included and non-included values
  42. for (const v of stored) {
  43. expect(await this.mock.$includes(0, v)).to.be.true;
  44. }
  45. for (const v of dropped) {
  46. expect(await this.mock.$includes(0, v)).to.be.false;
  47. }
  48. expect(await this.mock.$includes(0, ethers.ZeroHash)).to.be.false;
  49. }
  50. });
  51. it('clear', async function () {
  52. const value = generators.bytes32();
  53. await this.mock.$push(0, value);
  54. expect(await this.mock.$count(0)).to.equal(1n);
  55. expect(await this.mock.$length(0)).to.equal(LENGTH);
  56. expect(await this.mock.$includes(0, value)).to.be.true;
  57. await this.mock.$last(0, 0); // not revert
  58. await this.mock.$clear(0);
  59. expect(await this.mock.$count(0)).to.equal(0n);
  60. expect(await this.mock.$length(0)).to.equal(LENGTH);
  61. expect(await this.mock.$includes(0, value)).to.be.false;
  62. await expect(this.mock.$last(0, 0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  63. });
  64. });