DoubleEndedQueue.test.js 3.7 KB

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