DoubleEndedQueue.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expectRevertCustomError } = require('../../helpers/customError');
  3. const DoubleEndedQueue = artifacts.require('$DoubleEndedQueue');
  4. /** Rebuild the content of the deque as a JS array. */
  5. const getContent = deque =>
  6. deque.$length(0).then(bn =>
  7. Promise.all(
  8. Array(bn.toNumber())
  9. .fill()
  10. .map((_, i) => deque.$at(0, i)),
  11. ),
  12. );
  13. contract('DoubleEndedQueue', function () {
  14. const bytesA = '0xdeadbeef'.padEnd(66, '0');
  15. const bytesB = '0x0123456789'.padEnd(66, '0');
  16. const bytesC = '0x42424242'.padEnd(66, '0');
  17. const bytesD = '0x171717'.padEnd(66, '0');
  18. beforeEach(async function () {
  19. this.deque = await DoubleEndedQueue.new();
  20. });
  21. describe('when empty', function () {
  22. it('getters', async function () {
  23. expect(await this.deque.$empty(0)).to.be.equal(true);
  24. expect(await getContent(this.deque)).to.have.ordered.members([]);
  25. });
  26. it('reverts on accesses', async function () {
  27. await expectRevertCustomError(this.deque.$popBack(0), 'QueueEmpty', []);
  28. await expectRevertCustomError(this.deque.$popFront(0), 'QueueEmpty', []);
  29. await expectRevertCustomError(this.deque.$back(0), 'QueueEmpty', []);
  30. await expectRevertCustomError(this.deque.$front(0), 'QueueEmpty', []);
  31. });
  32. });
  33. describe('when not empty', function () {
  34. beforeEach(async function () {
  35. await this.deque.$pushBack(0, bytesB);
  36. await this.deque.$pushFront(0, bytesA);
  37. await this.deque.$pushBack(0, bytesC);
  38. this.content = [bytesA, bytesB, bytesC];
  39. });
  40. it('getters', async function () {
  41. expect(await this.deque.$empty(0)).to.be.equal(false);
  42. expect(await this.deque.$length(0)).to.be.bignumber.equal(this.content.length.toString());
  43. expect(await this.deque.$front(0)).to.be.equal(this.content[0]);
  44. expect(await this.deque.$back(0)).to.be.equal(this.content[this.content.length - 1]);
  45. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  46. });
  47. it('out of bounds access', async function () {
  48. await expectRevertCustomError(this.deque.$at(0, this.content.length), 'QueueOutOfBounds', []);
  49. });
  50. describe('push', function () {
  51. it('front', async function () {
  52. await this.deque.$pushFront(0, bytesD);
  53. this.content.unshift(bytesD); // add element at the beginning
  54. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  55. });
  56. it('back', async function () {
  57. await this.deque.$pushBack(0, bytesD);
  58. this.content.push(bytesD); // add element at the end
  59. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  60. });
  61. });
  62. describe('pop', function () {
  63. it('front', async function () {
  64. const value = this.content.shift(); // remove first element
  65. expectEvent(await this.deque.$popFront(0), 'return$popFront', { value });
  66. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  67. });
  68. it('back', async function () {
  69. const value = this.content.pop(); // remove last element
  70. expectEvent(await this.deque.$popBack(0), 'return$popBack', { value });
  71. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  72. });
  73. });
  74. it('clear', async function () {
  75. await this.deque.$clear(0);
  76. expect(await this.deque.$empty(0)).to.be.equal(true);
  77. expect(await getContent(this.deque)).to.have.ordered.members([]);
  78. });
  79. });
  80. });