DoubleEndedQueue.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  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. /** Revert handler that supports custom errors. */
  11. async function expectRevert (promise, reason) {
  12. try {
  13. await promise;
  14. expect.fail('Expected promise to throw but it didn\'t');
  15. } catch (error) {
  16. if (reason) {
  17. expect(error.message).to.include(reason);
  18. }
  19. }
  20. }
  21. contract('DoubleEndedQueue', function (accounts) {
  22. const bytesA = '0xdeadbeef'.padEnd(66, '0');
  23. const bytesB = '0x0123456789'.padEnd(66, '0');
  24. const bytesC = '0x42424242'.padEnd(66, '0');
  25. const bytesD = '0x171717'.padEnd(66, '0');
  26. beforeEach(async function () {
  27. this.deque = await Bytes32DequeMock.new();
  28. });
  29. describe('when empty', function () {
  30. it('getters', async function () {
  31. expect(await this.deque.empty()).to.be.equal(true);
  32. expect(await getContent(this.deque)).to.have.ordered.members([]);
  33. });
  34. it('reverts on accesses', async function () {
  35. await expectRevert(this.deque.popBack(), 'Empty()');
  36. await expectRevert(this.deque.popFront(), 'Empty()');
  37. await expectRevert(this.deque.back(), 'Empty()');
  38. await expectRevert(this.deque.front(), 'Empty()');
  39. });
  40. });
  41. describe('when not empty', function () {
  42. beforeEach(async function () {
  43. await this.deque.pushBack(bytesB);
  44. await this.deque.pushFront(bytesA);
  45. await this.deque.pushBack(bytesC);
  46. this.content = [ bytesA, bytesB, bytesC ];
  47. });
  48. it('getters', async function () {
  49. expect(await this.deque.empty()).to.be.equal(false);
  50. expect(await this.deque.length()).to.be.bignumber.equal(this.content.length.toString());
  51. expect(await this.deque.front()).to.be.equal(this.content[0]);
  52. expect(await this.deque.back()).to.be.equal(this.content[this.content.length - 1]);
  53. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  54. });
  55. it('out of bounds access', async function () {
  56. await expectRevert(this.deque.at(this.content.length), 'OutOfBounds()');
  57. });
  58. describe('push', function () {
  59. it('front', async function () {
  60. await this.deque.pushFront(bytesD);
  61. this.content.unshift(bytesD); // add element at the beginning
  62. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  63. });
  64. it('back', async function () {
  65. await this.deque.pushBack(bytesD);
  66. this.content.push(bytesD); // add element at the end
  67. expect(await getContent(this.deque)).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. expectEvent(await this.deque.popFront(), 'OperationResult', { value });
  74. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  75. });
  76. it('back', async function () {
  77. const value = this.content.pop(); // remove last element
  78. expectEvent(await this.deque.popBack(), 'OperationResult', { value });
  79. expect(await getContent(this.deque)).to.have.ordered.members(this.content);
  80. });
  81. });
  82. it('clear', async function () {
  83. await this.deque.clear();
  84. expect(await this.deque.empty()).to.be.equal(true);
  85. expect(await getContent(this.deque)).to.have.ordered.members([]);
  86. });
  87. });
  88. });