1 import { expect } from 'expect'
3 import { FixedPriorityQueue } from '../lib/fixed-priority-queue.cjs'
4 import { defaultQueueSize } from '../lib/utility-types.cjs'
6 describe('Fixed priority queue test suite', () => {
7 it('Verify constructor() behavior', () => {
8 expect(() => new FixedPriorityQueue('')).toThrow(
9 new TypeError("Invalid fixed priority queue size: '' is not an integer")
11 expect(() => new FixedPriorityQueue(-1)).toThrow(
12 new RangeError('Invalid fixed priority queue size: -1 < 0')
14 const fixedPriorityQueue = new FixedPriorityQueue()
15 expect(fixedPriorityQueue.start).toBe(0)
16 expect(fixedPriorityQueue.size).toBe(0)
17 expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array)
18 expect(fixedPriorityQueue.capacity).toBe(defaultQueueSize)
21 it('Verify enqueue() behavior', () => {
23 const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
24 let rtSize = fixedPriorityQueue.enqueue(1)
25 expect(fixedPriorityQueue.start).toBe(0)
26 expect(fixedPriorityQueue.size).toBe(1)
27 expect(rtSize).toBe(fixedPriorityQueue.size)
28 expect(fixedPriorityQueue.nodeArray).toMatchObject([
29 { data: 1, priority: 0 },
31 expect(fixedPriorityQueue.capacity).toBe(queueSize)
32 rtSize = fixedPriorityQueue.enqueue(2)
33 expect(fixedPriorityQueue.start).toBe(0)
34 expect(fixedPriorityQueue.size).toBe(2)
35 expect(rtSize).toBe(fixedPriorityQueue.size)
36 expect(fixedPriorityQueue.nodeArray).toMatchObject([
37 { data: 1, priority: 0 },
38 { data: 2, priority: 0 },
40 expect(fixedPriorityQueue.capacity).toBe(queueSize)
41 rtSize = fixedPriorityQueue.enqueue(3)
42 expect(fixedPriorityQueue.start).toBe(0)
43 expect(fixedPriorityQueue.size).toBe(3)
44 expect(rtSize).toBe(fixedPriorityQueue.size)
45 expect(fixedPriorityQueue.nodeArray).toMatchObject([
46 { data: 1, priority: 0 },
47 { data: 2, priority: 0 },
48 { data: 3, priority: 0 },
50 expect(fixedPriorityQueue.capacity).toBe(queueSize)
51 rtSize = fixedPriorityQueue.enqueue(3, -1)
52 expect(fixedPriorityQueue.start).toBe(0)
53 expect(fixedPriorityQueue.size).toBe(4)
54 expect(rtSize).toBe(fixedPriorityQueue.size)
55 expect(fixedPriorityQueue.nodeArray).toMatchObject([
56 { data: 3, priority: -1 },
57 { data: 1, priority: 0 },
58 { data: 2, priority: 0 },
59 { data: 3, priority: 0 },
61 expect(fixedPriorityQueue.capacity).toBe(queueSize)
62 rtSize = fixedPriorityQueue.enqueue(1, 1)
63 expect(fixedPriorityQueue.start).toBe(0)
64 expect(fixedPriorityQueue.size).toBe(5)
65 expect(rtSize).toBe(fixedPriorityQueue.size)
66 expect(fixedPriorityQueue.nodeArray).toMatchObject([
67 { data: 3, priority: -1 },
68 { data: 1, priority: 0 },
69 { data: 2, priority: 0 },
70 { data: 3, priority: 0 },
71 { data: 1, priority: 1 },
73 expect(fixedPriorityQueue.capacity).toBe(queueSize)
74 expect(() => fixedPriorityQueue.enqueue(4)).toThrow(
75 new Error('Priority queue is full')
79 it('Verify get() behavior', () => {
80 const fixedPriorityQueue = new FixedPriorityQueue()
81 fixedPriorityQueue.enqueue(1)
82 fixedPriorityQueue.enqueue(2, -1)
83 fixedPriorityQueue.enqueue(3)
84 expect(fixedPriorityQueue.get(0)).toBe(2)
85 expect(fixedPriorityQueue.get(1)).toBe(1)
86 expect(fixedPriorityQueue.get(2)).toBe(3)
87 expect(fixedPriorityQueue.get(3)).toBe(undefined)
90 it('Verify dequeue() behavior', () => {
92 const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
93 fixedPriorityQueue.enqueue(1)
94 fixedPriorityQueue.enqueue(2, -1)
95 fixedPriorityQueue.enqueue(3)
96 expect(fixedPriorityQueue.start).toBe(0)
97 expect(fixedPriorityQueue.size).toBe(3)
98 expect(fixedPriorityQueue.capacity).toBe(queueSize)
99 let rtItem = fixedPriorityQueue.dequeue()
100 expect(fixedPriorityQueue.start).toBe(1)
101 expect(fixedPriorityQueue.size).toBe(2)
102 expect(rtItem).toBe(2)
103 expect(fixedPriorityQueue.nodeArray).toMatchObject([
104 { data: 2, priority: -1 },
105 { data: 1, priority: 0 },
106 { data: 3, priority: 0 },
108 expect(fixedPriorityQueue.capacity).toBe(queueSize)
109 rtItem = fixedPriorityQueue.dequeue()
110 expect(fixedPriorityQueue.start).toBe(2)
111 expect(fixedPriorityQueue.size).toBe(1)
112 expect(rtItem).toBe(1)
113 expect(fixedPriorityQueue.nodeArray).toMatchObject([
114 { data: 2, priority: -1 },
115 { data: 1, priority: 0 },
116 { data: 3, priority: 0 },
118 expect(fixedPriorityQueue.capacity).toBe(queueSize)
119 rtItem = fixedPriorityQueue.dequeue()
120 expect(fixedPriorityQueue.start).toBe(3)
121 expect(fixedPriorityQueue.size).toBe(0)
122 expect(rtItem).toBe(3)
123 expect(fixedPriorityQueue.nodeArray).toMatchObject([
124 { data: 2, priority: -1 },
125 { data: 1, priority: 0 },
126 { data: 3, priority: 0 },
128 expect(fixedPriorityQueue.capacity).toBe(queueSize)
129 rtItem = fixedPriorityQueue.dequeue()
130 expect(fixedPriorityQueue.start).toBe(3)
131 expect(fixedPriorityQueue.size).toBe(0)
132 expect(rtItem).toBe(undefined)
133 expect(fixedPriorityQueue.nodeArray).toMatchObject([
134 { data: 2, priority: -1 },
135 { data: 1, priority: 0 },
136 { data: 3, priority: 0 },
138 expect(fixedPriorityQueue.capacity).toBe(queueSize)
141 it('Verify iterator behavior', () => {
142 const fixedPriorityQueue = new FixedPriorityQueue()
143 fixedPriorityQueue.enqueue(1)
144 fixedPriorityQueue.enqueue(2)
145 fixedPriorityQueue.enqueue(3)
146 let i = fixedPriorityQueue.start + 1
147 for (const value of fixedPriorityQueue) {
148 expect(value).toBe(i)
151 fixedPriorityQueue.dequeue()
152 i = fixedPriorityQueue.start + 1
153 for (const value of fixedPriorityQueue) {
154 expect(value).toBe(i)
159 it('Verify empty() behavior', () => {
160 const fixedPriorityQueue = new FixedPriorityQueue()
161 expect(fixedPriorityQueue.empty()).toBe(true)
162 fixedPriorityQueue.enqueue(1)
163 expect(fixedPriorityQueue.empty()).toBe(false)
164 fixedPriorityQueue.dequeue()
165 expect(fixedPriorityQueue.empty()).toBe(true)
168 it('Verify full() behavior', () => {
169 const fixedPriorityQueue = new FixedPriorityQueue(2)
170 expect(fixedPriorityQueue.full()).toBe(false)
171 fixedPriorityQueue.enqueue(1)
172 expect(fixedPriorityQueue.full()).toBe(false)
173 fixedPriorityQueue.enqueue(2)
174 expect(fixedPriorityQueue.full()).toBe(true)
175 fixedPriorityQueue.dequeue()
176 expect(fixedPriorityQueue.full()).toBe(false)
179 it('Verify clear() behavior', () => {
180 const fixedPriorityQueue = new FixedPriorityQueue()
181 fixedPriorityQueue.start = 1
182 fixedPriorityQueue.size = 2
183 fixedPriorityQueue.nodeArray = [
184 { data: 2, priority: 0 },
185 { data: 3, priority: 0 },
187 fixedPriorityQueue.clear()
188 expect(fixedPriorityQueue.start).toBe(0)
189 expect(fixedPriorityQueue.size).toBe(0)
190 expect(fixedPriorityQueue.nodeArray).toMatchObject([
191 { data: 2, priority: 0 },
192 { data: 3, priority: 0 },