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