fix: fix iteration in fixed queue if start > size
[poolifier.git] / tests / fixed-priority-queue.test.mjs
... / ...
CommitLineData
1// import { expect } from 'expect'
2
3// import {
4// defaultQueueSize,
5// FixedPriorityQueue
6// } from '../lib/fixed-priority-queue.cjs'
7
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")
12// )
13// expect(() => new FixedPriorityQueue(-1)).toThrow(
14// new RangeError('Invalid fixed priority queue size: -1 < 0')
15// )
16// let fixedPriorityQueue = new FixedPriorityQueue()
17// expect(fixedPriorityQueue.start).toBe(0)
18// expect(fixedPriorityQueue.size).toBe(0)
19// expect(fixedPriorityQueue.maxSize).toBe(0)
20// expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array)
21// expect(fixedPriorityQueue.nodeArray.length).toBe(defaultQueueSize)
22// fixedPriorityQueue = new FixedPriorityQueue(2)
23// expect(fixedPriorityQueue.start).toBe(0)
24// expect(fixedPriorityQueue.size).toBe(0)
25// expect(fixedPriorityQueue.maxSize).toBe(0)
26// expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array)
27// expect(fixedPriorityQueue.nodeArray.length).toBe(2)
28// })
29
30// it('Verify enqueue() behavior', () => {
31// const queueSize = 5
32// const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
33// let rtSize = fixedPriorityQueue.enqueue(1)
34// expect(fixedPriorityQueue.start).toBe(0)
35// expect(fixedPriorityQueue.size).toBe(1)
36// expect(fixedPriorityQueue.maxSize).toBe(1)
37// expect(rtSize).toBe(fixedPriorityQueue.size)
38// expect(fixedPriorityQueue.nodeArray).toMatchObject([
39// { data: 1, priority: 0 }
40// ])
41// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
42// rtSize = fixedPriorityQueue.enqueue(2)
43// expect(fixedPriorityQueue.start).toBe(0)
44// expect(fixedPriorityQueue.size).toBe(2)
45// expect(fixedPriorityQueue.maxSize).toBe(2)
46// expect(rtSize).toBe(fixedPriorityQueue.size)
47// expect(fixedPriorityQueue.nodeArray).toMatchObject([
48// { data: 1, priority: 0 },
49// { data: 2, priority: 0 }
50// ])
51// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
52// rtSize = fixedPriorityQueue.enqueue(3)
53// expect(fixedPriorityQueue.start).toBe(0)
54// expect(fixedPriorityQueue.size).toBe(3)
55// expect(fixedPriorityQueue.maxSize).toBe(3)
56// expect(rtSize).toBe(fixedPriorityQueue.size)
57// expect(fixedPriorityQueue.nodeArray).toMatchObject([
58// { data: 1, priority: 0 },
59// { data: 2, priority: 0 },
60// { data: 3, priority: 0 }
61// ])
62// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
63// rtSize = fixedPriorityQueue.enqueue(3, -1)
64// expect(fixedPriorityQueue.start).toBe(0)
65// expect(fixedPriorityQueue.size).toBe(4)
66// expect(fixedPriorityQueue.maxSize).toBe(4)
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// ])
74// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
75// rtSize = fixedPriorityQueue.enqueue(1, 1)
76// expect(fixedPriorityQueue.start).toBe(0)
77// expect(fixedPriorityQueue.size).toBe(5)
78// expect(fixedPriorityQueue.maxSize).toBe(5)
79// expect(rtSize).toBe(fixedPriorityQueue.size)
80// expect(fixedPriorityQueue.nodeArray).toMatchObject([
81// { data: 3, priority: -1 },
82// { data: 1, priority: 0 },
83// { data: 2, priority: 0 },
84// { data: 3, priority: 0 },
85// { data: 1, priority: 1 }
86// ])
87// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
88// expect(() => fixedPriorityQueue.enqueue(4)).toThrow(
89// new Error('Priority queue is full')
90// )
91// })
92
93// it('Verify dequeue() behavior', () => {
94// const queueSize = 5
95// const fixedPriorityQueue = new FixedPriorityQueue(queueSize)
96// fixedPriorityQueue.enqueue(1)
97// fixedPriorityQueue.enqueue(2, -1)
98// fixedPriorityQueue.enqueue(3)
99// expect(fixedPriorityQueue.start).toBe(0)
100// expect(fixedPriorityQueue.size).toBe(3)
101// expect(fixedPriorityQueue.maxSize).toBe(3)
102// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
103// let rtItem = fixedPriorityQueue.dequeue()
104// expect(fixedPriorityQueue.start).toBe(1)
105// expect(fixedPriorityQueue.size).toBe(2)
106// expect(fixedPriorityQueue.maxSize).toBe(3)
107// expect(rtItem).toBe(2)
108// expect(fixedPriorityQueue.nodeArray).toMatchObject([
109// { data: 2, priority: -1 },
110// { data: 1, priority: 0 },
111// { data: 3, priority: 0 }
112// ])
113// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
114// rtItem = fixedPriorityQueue.dequeue()
115// expect(fixedPriorityQueue.start).toBe(2)
116// expect(fixedPriorityQueue.size).toBe(1)
117// expect(fixedPriorityQueue.maxSize).toBe(3)
118// expect(rtItem).toBe(1)
119// expect(fixedPriorityQueue.nodeArray).toMatchObject([
120// { data: 2, priority: -1 },
121// { data: 1, priority: 0 },
122// { data: 3, priority: 0 }
123// ])
124// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
125// rtItem = fixedPriorityQueue.dequeue()
126// expect(fixedPriorityQueue.start).toBe(3)
127// expect(fixedPriorityQueue.size).toBe(0)
128// expect(fixedPriorityQueue.maxSize).toBe(3)
129// expect(rtItem).toBe(3)
130// expect(fixedPriorityQueue.nodeArray).toMatchObject([
131// { data: 2, priority: -1 },
132// { data: 1, priority: 0 },
133// { data: 3, priority: 0 }
134// ])
135// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
136// rtItem = fixedPriorityQueue.dequeue()
137// expect(fixedPriorityQueue.start).toBe(3)
138// expect(fixedPriorityQueue.size).toBe(0)
139// expect(fixedPriorityQueue.maxSize).toBe(3)
140// expect(rtItem).toBe(undefined)
141// expect(fixedPriorityQueue.nodeArray).toMatchObject([
142// { data: 2, priority: -1 },
143// { data: 1, priority: 0 },
144// { data: 3, priority: 0 }
145// ])
146// expect(fixedPriorityQueue.nodeArray.length).toBe(queueSize)
147// })
148
149// it('Verify iterator behavior', () => {
150// const fixedPriorityQueue = new FixedPriorityQueue()
151// fixedPriorityQueue.enqueue(1)
152// fixedPriorityQueue.enqueue(2)
153// fixedPriorityQueue.enqueue(3)
154// let i = fixedPriorityQueue.start + 1
155// for (const value of fixedPriorityQueue) {
156// expect(value).toBe(i)
157// ++i
158// }
159// fixedPriorityQueue.dequeue()
160// i = fixedPriorityQueue.start + 1
161// for (const value of fixedPriorityQueue) {
162// expect(value).toBe(i)
163// ++i
164// }
165// })
166
167// it('Verify clear() behavior', () => {
168// const fixedPriorityQueue = new FixedPriorityQueue()
169// fixedPriorityQueue.start = 1
170// fixedPriorityQueue.size = 2
171// fixedPriorityQueue.maxSize = 2
172// fixedPriorityQueue.nodeArray = [
173// { data: 2, priority: 0 },
174// { data: 3, priority: 0 }
175// ]
176// fixedPriorityQueue.clear()
177// expect(fixedPriorityQueue.start).toBe(0)
178// expect(fixedPriorityQueue.size).toBe(0)
179// expect(fixedPriorityQueue.maxSize).toBe(0)
180// expect(fixedPriorityQueue.nodeArray).toMatchObject([
181// { data: 2, priority: 0 },
182// { data: 3, priority: 0 }
183// ])
184// })
185// })