DoubleEndedQueue.test.js 3.8 KB

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