DoubleEndedQueue.test.js 3.4 KB

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