1 import { expect } from 'expect'
5 } from '../lib/fixed-priority-queue.cjs'
6 import { defaultQueueSize } from '../lib/utility-types.cjs'
8 describe('Fixed priority queue test suite', () => {
9 it('Verify constructor() behavior', () => {
10 expect(() => new FixedPriorityQueue('')).toThrow(
11 new TypeError("Invalid fixed priority queue size: '' is not an integer")
13 expect(() => new FixedPriorityQueue(-1)).toThrow(
14 new RangeError('Invalid fixed priority queue size: -1 < 0')
16 const fixedPriorityQueue = new FixedPriorityQueue()
17 expect(fixedPriorityQueue.start).toBe(0)
18 expect(fixedPriorityQueue.size).toBe(0)
19 expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array)
20 expect(fixedPriorityQueue.capacity).toBe(defaultQueueSize)
23 it('Verify enqueue() behavior', () => {
25 const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
26 let rtSize = fixedPriorityQueue.enqueue(1)
27 expect(fixedPriorityQueue.start).toBe(0)
28 expect(fixedPriorityQueue.size).toBe(1)
29 expect(rtSize).toBe(fixedPriorityQueue.size)
30 expect(fixedPriorityQueue.nodeArray).toMatchObject([
31 { data: 1, priority: 0 },
33 expect(fixedPriorityQueue.capacity).toBe(queueSize)
34 rtSize = fixedPriorityQueue.enqueue(2)
35 expect(fixedPriorityQueue.start).toBe(0)
36 expect(fixedPriorityQueue.size).toBe(2)
37 expect(rtSize).toBe(fixedPriorityQueue.size)
38 expect(fixedPriorityQueue.nodeArray).toMatchObject([
39 { data: 1, priority: 0 },
40 { data: 2, priority: 0 },
42 expect(fixedPriorityQueue.capacity).toBe(queueSize)
43 rtSize = fixedPriorityQueue.enqueue(3)
44 expect(fixedPriorityQueue.start).toBe(0)
45 expect(fixedPriorityQueue.size).toBe(3)
46 expect(rtSize).toBe(fixedPriorityQueue.size)
47 expect(fixedPriorityQueue.nodeArray).toMatchObject([
48 { data: 1, priority: 0 },
49 { data: 2, priority: 0 },
50 { data: 3, priority: 0 },
52 expect(fixedPriorityQueue.capacity).toBe(queueSize)
53 rtSize = fixedPriorityQueue.enqueue(3, -1)
54 expect(fixedPriorityQueue.start).toBe(0)
55 expect(fixedPriorityQueue.size).toBe(4)
56 expect(rtSize).toBe(fixedPriorityQueue.size)
57 expect(fixedPriorityQueue.nodeArray).toMatchObject([
58 { data: 3, priority: -1 },
59 { data: 1, priority: 0 },
60 { data: 2, priority: 0 },
61 { data: 3, priority: 0 },
63 expect(fixedPriorityQueue.capacity).toBe(queueSize)
64 rtSize = fixedPriorityQueue.enqueue(1, 1)
65 expect(fixedPriorityQueue.start).toBe(0)
66 expect(fixedPriorityQueue.size).toBe(5)
67 expect(rtSize).toBe(fixedPriorityQueue.size)
68 expect(fixedPriorityQueue.nodeArray).toMatchObject([
69 { data: 3, priority: -1 },
70 { data: 1, priority: 0 },
71 { data: 2, priority: 0 },
72 { data: 3, priority: 0 },
73 { data: 1, priority: 1 },
75 expect(fixedPriorityQueue.capacity).toBe(queueSize)
76 expect(() => fixedPriorityQueue.enqueue(4)).toThrow(
77 new Error('Priority queue is full')
81 it('Verify get() behavior', () => {
82 const fixedPriorityQueue = new FixedPriorityQueue()
83 fixedPriorityQueue.enqueue(1)
84 fixedPriorityQueue.enqueue(2, -1)
85 fixedPriorityQueue.enqueue(3)
86 expect(fixedPriorityQueue.get(0)).toBe(2)
87 expect(fixedPriorityQueue.get(1)).toBe(1)
88 expect(fixedPriorityQueue.get(2)).toBe(3)
89 expect(fixedPriorityQueue.get(3)).toBe(undefined)
92 it('Verify dequeue() behavior', () => {
94 const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
95 fixedPriorityQueue.enqueue(1)
96 fixedPriorityQueue.enqueue(2, -1)
97 fixedPriorityQueue.enqueue(3)
98 expect(fixedPriorityQueue.start).toBe(0)
99 expect(fixedPriorityQueue.size).toBe(3)
100 expect(fixedPriorityQueue.capacity).toBe(queueSize)
101 let rtItem = fixedPriorityQueue.dequeue()
102 expect(fixedPriorityQueue.start).toBe(1)
103 expect(fixedPriorityQueue.size).toBe(2)
104 expect(rtItem).toBe(2)
105 expect(fixedPriorityQueue.nodeArray).toMatchObject([
106 { data: 2, priority: -1 },
107 { data: 1, priority: 0 },
108 { data: 3, priority: 0 },
110 expect(fixedPriorityQueue.capacity).toBe(queueSize)
111 rtItem = fixedPriorityQueue.dequeue()
112 expect(fixedPriorityQueue.start).toBe(2)
113 expect(fixedPriorityQueue.size).toBe(1)
114 expect(rtItem).toBe(1)
115 expect(fixedPriorityQueue.nodeArray).toMatchObject([
116 { data: 2, priority: -1 },
117 { data: 1, priority: 0 },
118 { data: 3, priority: 0 },
120 expect(fixedPriorityQueue.capacity).toBe(queueSize)
121 rtItem = fixedPriorityQueue.dequeue()
122 expect(fixedPriorityQueue.start).toBe(3)
123 expect(fixedPriorityQueue.size).toBe(0)
124 expect(rtItem).toBe(3)
125 expect(fixedPriorityQueue.nodeArray).toMatchObject([
126 { data: 2, priority: -1 },
127 { data: 1, priority: 0 },
128 { data: 3, priority: 0 },
130 expect(fixedPriorityQueue.capacity).toBe(queueSize)
131 rtItem = fixedPriorityQueue.dequeue()
132 expect(fixedPriorityQueue.start).toBe(3)
133 expect(fixedPriorityQueue.size).toBe(0)
134 expect(rtItem).toBe(undefined)
135 expect(fixedPriorityQueue.nodeArray).toMatchObject([
136 { data: 2, priority: -1 },
137 { data: 1, priority: 0 },
138 { data: 3, priority: 0 },
140 expect(fixedPriorityQueue.capacity).toBe(queueSize)
143 it('Verify iterator behavior', () => {
144 const fixedPriorityQueue = new FixedPriorityQueue()
145 fixedPriorityQueue.enqueue(1)
146 fixedPriorityQueue.enqueue(2)
147 fixedPriorityQueue.enqueue(3)
148 let i = fixedPriorityQueue.start + 1
149 for (const value of fixedPriorityQueue) {
150 expect(value).toBe(i)
153 fixedPriorityQueue.dequeue()
154 i = fixedPriorityQueue.start + 1
155 for (const value of fixedPriorityQueue) {
156 expect(value).toBe(i)
161 it('Verify empty() behavior', () => {
162 const fixedPriorityQueue = new FixedPriorityQueue()
163 expect(fixedPriorityQueue.empty()).toBe(true)
164 fixedPriorityQueue.enqueue(1)
165 expect(fixedPriorityQueue.empty()).toBe(false)
166 fixedPriorityQueue.dequeue()
167 expect(fixedPriorityQueue.empty()).toBe(true)
170 it('Verify full() behavior', () => {
171 const fixedPriorityQueue = new FixedPriorityQueue(2)
172 expect(fixedPriorityQueue.full()).toBe(false)
173 fixedPriorityQueue.enqueue(1)
174 expect(fixedPriorityQueue.full()).toBe(false)
175 fixedPriorityQueue.enqueue(2)
176 expect(fixedPriorityQueue.full()).toBe(true)
177 fixedPriorityQueue.dequeue()
178 expect(fixedPriorityQueue.full()).toBe(false)
181 it('Verify clear() behavior', () => {
182 const fixedPriorityQueue = new FixedPriorityQueue()
183 fixedPriorityQueue.start = 1
184 fixedPriorityQueue.size = 2
185 fixedPriorityQueue.nodeArray = [
186 { data: 2, priority: 0 },
187 { data: 3, priority: 0 },
189 fixedPriorityQueue.clear()
190 expect(fixedPriorityQueue.start).toBe(0)
191 expect(fixedPriorityQueue.size).toBe(0)
192 expect(fixedPriorityQueue.nodeArray).toMatchObject([
193 { data: 2, priority: 0 },
194 { data: 3, priority: 0 },