DoubleEndedQueue.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. async function fixture() {
  6. const mock = await ethers.deployContract('$DoubleEndedQueue');
  7. /** Rebuild the content of the deque as a JS array. */
  8. const getContent = () =>
  9. mock.$length(0).then(length => Promise.all(Array.from({ length: Number(length) }, (_, i) => mock.$at(0, i))));
  10. return { mock, getContent };
  11. }
  12. describe('DoubleEndedQueue', function () {
  13. const coder = ethers.AbiCoder.defaultAbiCoder();
  14. const bytesA = coder.encode(['uint256'], [0xdeadbeef]);
  15. const bytesB = coder.encode(['uint256'], [0x0123456789]);
  16. const bytesC = coder.encode(['uint256'], [0x42424242]);
  17. const bytesD = coder.encode(['uint256'], [0x171717]);
  18. beforeEach(async function () {
  19. Object.assign(this, await loadFixture(fixture));
  20. });
  21. describe('when empty', function () {
  22. it('getters', async function () {
  23. expect(await this.mock.$empty(0)).to.be.true;
  24. expect(await this.getContent()).to.have.ordered.members([]);
  25. });
  26. it('reverts on accesses', async function () {
  27. await expect(this.mock.$popBack(0)).to.be.revertedWithPanic(PANIC_CODES.POP_ON_EMPTY_ARRAY);
  28. await expect(this.mock.$popFront(0)).to.be.revertedWithPanic(PANIC_CODES.POP_ON_EMPTY_ARRAY);
  29. await expect(this.mock.$back(0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  30. await expect(this.mock.$front(0)).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  31. });
  32. });
  33. describe('when not empty', function () {
  34. beforeEach(async function () {
  35. await this.mock.$pushBack(0, bytesB);
  36. await this.mock.$pushFront(0, bytesA);
  37. await this.mock.$pushBack(0, bytesC);
  38. this.content = [bytesA, bytesB, bytesC];
  39. });
  40. it('getters', async function () {
  41. expect(await this.mock.$empty(0)).to.be.false;
  42. expect(await this.mock.$length(0)).to.equal(this.content.length);
  43. expect(await this.mock.$front(0)).to.equal(this.content[0]);
  44. expect(await this.mock.$back(0)).to.equal(this.content[this.content.length - 1]);
  45. expect(await this.getContent()).to.have.ordered.members(this.content);
  46. });
  47. it('out of bounds access', async function () {
  48. await expect(this.mock.$at(0, this.content.length)).to.be.revertedWithPanic(
  49. PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS,
  50. );
  51. });
  52. describe('push', function () {
  53. it('front', async function () {
  54. await this.mock.$pushFront(0, bytesD);
  55. this.content.unshift(bytesD); // add element at the beginning
  56. expect(await this.getContent()).to.have.ordered.members(this.content);
  57. });
  58. it('back', async function () {
  59. await this.mock.$pushBack(0, bytesD);
  60. this.content.push(bytesD); // add element at the end
  61. expect(await this.getContent()).to.have.ordered.members(this.content);
  62. });
  63. });
  64. describe('pop', function () {
  65. it('front', async function () {
  66. const value = this.content.shift(); // remove first element
  67. await expect(this.mock.$popFront(0)).to.emit(this.mock, 'return$popFront').withArgs(value);
  68. expect(await this.getContent()).to.have.ordered.members(this.content);
  69. });
  70. it('back', async function () {
  71. const value = this.content.pop(); // remove last element
  72. await expect(this.mock.$popBack(0)).to.emit(this.mock, 'return$popBack').withArgs(value);
  73. expect(await this.getContent()).to.have.ordered.members(this.content);
  74. });
  75. });
  76. it('clear', async function () {
  77. await this.mock.$clear(0);
  78. expect(await this.mock.$empty(0)).to.be.true;
  79. expect(await this.getContent()).to.have.ordered.members([]);
  80. });
  81. });
  82. });