| 1 | const { expect } = require('expect') |
| 2 | const { Queue } = require('../lib/queue') |
| 3 | |
| 4 | describe('Queue test suite', () => { |
| 5 | it('Verify enqueue() behavior', () => { |
| 6 | const queue = new Queue() |
| 7 | let rtSize = queue.enqueue(1) |
| 8 | expect(queue.size).toBe(1) |
| 9 | expect(rtSize).toBe(queue.size) |
| 10 | expect(queue.offset).toBe(0) |
| 11 | expect(queue.maxSize).toBe(1) |
| 12 | expect(queue.items).toStrictEqual([1]) |
| 13 | rtSize = queue.enqueue(2) |
| 14 | expect(queue.size).toBe(2) |
| 15 | expect(rtSize).toBe(queue.size) |
| 16 | expect(queue.offset).toBe(0) |
| 17 | expect(queue.maxSize).toBe(2) |
| 18 | expect(queue.items).toStrictEqual([1, 2]) |
| 19 | rtSize = queue.enqueue(3) |
| 20 | expect(queue.size).toBe(3) |
| 21 | expect(rtSize).toBe(queue.size) |
| 22 | expect(queue.offset).toBe(0) |
| 23 | expect(queue.maxSize).toBe(3) |
| 24 | expect(queue.items).toStrictEqual([1, 2, 3]) |
| 25 | }) |
| 26 | |
| 27 | it('Verify dequeue() behavior', () => { |
| 28 | const queue = new Queue() |
| 29 | queue.enqueue(1) |
| 30 | queue.enqueue(2) |
| 31 | queue.enqueue(3) |
| 32 | let rtItem = queue.dequeue() |
| 33 | expect(queue.size).toBe(2) |
| 34 | expect(rtItem).toBe(1) |
| 35 | expect(queue.offset).toBe(1) |
| 36 | expect(queue.maxSize).toBe(3) |
| 37 | expect(queue.items).toStrictEqual([1, 2, 3]) |
| 38 | rtItem = queue.dequeue() |
| 39 | expect(queue.size).toBe(1) |
| 40 | expect(rtItem).toBe(2) |
| 41 | expect(queue.offset).toBe(0) |
| 42 | expect(queue.maxSize).toBe(3) |
| 43 | expect(queue.items).toStrictEqual([3]) |
| 44 | rtItem = queue.dequeue() |
| 45 | expect(queue.size).toBe(0) |
| 46 | expect(rtItem).toBe(3) |
| 47 | expect(queue.offset).toBe(0) |
| 48 | expect(queue.maxSize).toBe(3) |
| 49 | expect(queue.items).toStrictEqual([]) |
| 50 | }) |
| 51 | |
| 52 | it('Verify clear() behavior', () => { |
| 53 | const queue = new Queue() |
| 54 | queue.enqueue(1) |
| 55 | queue.enqueue(2) |
| 56 | queue.enqueue(3) |
| 57 | expect(queue.size).toBe(3) |
| 58 | expect(queue.maxSize).toBe(3) |
| 59 | queue.clear() |
| 60 | expect(queue.size).toBe(0) |
| 61 | expect(queue.maxSize).toBe(0) |
| 62 | expect(queue.items).toStrictEqual([]) |
| 63 | expect(queue.offset).toBe(0) |
| 64 | }) |
| 65 | |
| 66 | it('Verify iterator behavior', () => { |
| 67 | const queue = new Queue() |
| 68 | queue.enqueue(1) |
| 69 | queue.enqueue(2) |
| 70 | queue.enqueue(3) |
| 71 | let i = 1 |
| 72 | for (const item of queue) { |
| 73 | expect(item).toBe(i) |
| 74 | ++i |
| 75 | } |
| 76 | }) |
| 77 | }) |