DoubleEndedQueue.test.js 3.5 KB

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