1 import { expect } from 'expect'
3 import { FixedQueue } from '../lib/fixed-queue.cjs'
4 import { defaultQueueSize } from '../lib/utility-types.cjs'
6 describe('Fixed queue test suite', () => {
7 it('Verify constructor() behavior', () => {
8 expect(() => new FixedQueue('')).toThrow(
9 new TypeError("Invalid fixed queue size: '' is not an integer")
11 expect(() => new FixedQueue(-1)).toThrow(
12 new RangeError('Invalid fixed queue size: -1 < 0')
14 const fixedQueue = new FixedQueue()
15 expect(fixedQueue.start).toBe(0)
16 expect(fixedQueue.size).toBe(0)
17 expect(fixedQueue.nodeArray).toBeInstanceOf(Array)
18 expect(fixedQueue.capacity).toBe(defaultQueueSize)
21 it('Verify enqueue() behavior', () => {
23 const fixedQueue = new FixedQueue(queueSize)
24 let rtSize = fixedQueue.enqueue(1)
25 expect(fixedQueue.start).toBe(0)
26 expect(fixedQueue.size).toBe(1)
27 expect(rtSize).toBe(fixedQueue.size)
28 expect(fixedQueue.nodeArray).toMatchObject([{ data: 1, priority: 0 }])
29 expect(fixedQueue.capacity).toBe(queueSize)
30 rtSize = fixedQueue.enqueue(2)
31 expect(fixedQueue.start).toBe(0)
32 expect(fixedQueue.size).toBe(2)
33 expect(rtSize).toBe(fixedQueue.size)
34 expect(fixedQueue.nodeArray).toMatchObject([
35 { data: 1, priority: 0 },
36 { data: 2, priority: 0 },
38 expect(fixedQueue.capacity).toBe(queueSize)
39 rtSize = fixedQueue.enqueue(3)
40 expect(fixedQueue.start).toBe(0)
41 expect(fixedQueue.size).toBe(3)
42 expect(rtSize).toBe(fixedQueue.size)
43 expect(fixedQueue.nodeArray).toMatchObject([
44 { data: 1, priority: 0 },
45 { data: 2, priority: 0 },
46 { data: 3, priority: 0 },
48 expect(fixedQueue.capacity).toBe(queueSize)
49 rtSize = fixedQueue.enqueue(3, -1)
50 expect(fixedQueue.start).toBe(0)
51 expect(fixedQueue.size).toBe(4)
52 expect(rtSize).toBe(fixedQueue.size)
53 expect(fixedQueue.nodeArray).toMatchObject([
54 { data: 1, priority: 0 },
55 { data: 2, priority: 0 },
56 { data: 3, priority: 0 },
57 { data: 3, priority: -1 },
59 expect(fixedQueue.capacity).toBe(queueSize)
60 rtSize = fixedQueue.enqueue(1, 1)
61 expect(fixedQueue.start).toBe(0)
62 expect(fixedQueue.size).toBe(5)
63 expect(rtSize).toBe(fixedQueue.size)
64 expect(fixedQueue.nodeArray).toMatchObject([
65 { data: 1, priority: 0 },
66 { data: 2, priority: 0 },
67 { data: 3, priority: 0 },
68 { data: 3, priority: -1 },
69 { data: 1, priority: 1 },
71 expect(fixedQueue.capacity).toBe(queueSize)
72 expect(() => fixedQueue.enqueue(4)).toThrow(
73 new Error('Fixed queue is full')
77 it('Verify get() behavior', () => {
78 const fixedQueue = new FixedQueue()
80 fixedQueue.enqueue(2, -1)
82 expect(fixedQueue.get(0)).toBe(1)
83 expect(fixedQueue.get(1)).toBe(2)
84 expect(fixedQueue.get(2)).toBe(3)
85 expect(fixedQueue.get(3)).toBe(undefined)
88 it('Verify dequeue() behavior', () => {
90 const fixedQueue = new FixedQueue(queueSize)
92 fixedQueue.enqueue(2, -1)
94 expect(fixedQueue.start).toBe(0)
95 expect(fixedQueue.size).toBe(3)
96 expect(fixedQueue.capacity).toBe(queueSize)
97 let rtItem = fixedQueue.dequeue()
98 expect(fixedQueue.start).toBe(1)
99 expect(fixedQueue.size).toBe(2)
100 expect(rtItem).toBe(1)
101 expect(fixedQueue.nodeArray).toMatchObject([
102 { data: 1, priority: 0 },
103 { data: 2, priority: -1 },
104 { data: 3, priority: 0 },
106 expect(fixedQueue.capacity).toBe(queueSize)
107 rtItem = fixedQueue.dequeue()
108 expect(fixedQueue.start).toBe(2)
109 expect(fixedQueue.size).toBe(1)
110 expect(rtItem).toBe(2)
111 expect(fixedQueue.nodeArray).toMatchObject([
112 { data: 1, priority: 0 },
113 { data: 2, priority: -1 },
114 { data: 3, priority: 0 },
116 expect(fixedQueue.capacity).toBe(queueSize)
117 rtItem = fixedQueue.dequeue()
118 expect(fixedQueue.start).toBe(3)
119 expect(fixedQueue.size).toBe(0)
120 expect(rtItem).toBe(3)
121 expect(fixedQueue.nodeArray).toMatchObject([
122 { data: 1, priority: 0 },
123 { data: 2, priority: -1 },
124 { data: 3, priority: 0 },
126 expect(fixedQueue.capacity).toBe(queueSize)
127 rtItem = fixedQueue.dequeue()
128 expect(fixedQueue.start).toBe(3)
129 expect(fixedQueue.size).toBe(0)
130 expect(rtItem).toBe(undefined)
131 expect(fixedQueue.nodeArray).toMatchObject([
132 { data: 1, priority: 0 },
133 { data: 2, priority: -1 },
134 { data: 3, priority: 0 },
136 expect(fixedQueue.capacity).toBe(queueSize)
139 it('Verify iterator behavior', () => {
140 const fixedQueue = new FixedQueue()
141 fixedQueue.enqueue(1)
142 fixedQueue.enqueue(2)
143 fixedQueue.enqueue(3)
144 let i = fixedQueue.start + 1
145 for (const value of fixedQueue) {
146 expect(value).toBe(i)
150 i = fixedQueue.start + 1
151 for (const value of fixedQueue) {
152 expect(value).toBe(i)
157 it('Verify empty() behavior', () => {
158 const fixedQueue = new FixedQueue()
159 expect(fixedQueue.empty()).toBe(true)
160 fixedQueue.enqueue(1)
161 expect(fixedQueue.empty()).toBe(false)
163 expect(fixedQueue.empty()).toBe(true)
166 it('Verify full() behavior', () => {
167 const fixedQueue = new FixedQueue(2)
168 expect(fixedQueue.full()).toBe(false)
169 fixedQueue.enqueue(1)
170 expect(fixedQueue.full()).toBe(false)
171 fixedQueue.enqueue(2)
172 expect(fixedQueue.full()).toBe(true)
174 expect(fixedQueue.full()).toBe(false)
177 it('Verify clear() behavior', () => {
178 const fixedQueue = new FixedQueue()
181 fixedQueue.nodeArray = [
182 { data: 2, priority: 0 },
183 { data: 3, priority: 0 },
186 expect(fixedQueue.start).toBe(0)
187 expect(fixedQueue.size).toBe(0)
188 expect(fixedQueue.nodeArray).toMatchObject([
189 { data: 2, priority: 0 },
190 { data: 3, priority: 0 },