Commit | Line | Data |
---|---|---|
9df282a0 | 1 | import { expect } from 'expect' |
f8d5d8fd | 2 | |
9df282a0 | 3 | import { |
3a502712 | 4 | FixedPriorityQueue, |
9df282a0 | 5 | } from '../lib/fixed-priority-queue.cjs' |
097dea68 | 6 | import { defaultQueueSize } from '../lib/utility-types.cjs' |
f8d5d8fd | 7 | |
9df282a0 JB |
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 | ) | |
097dea68 | 16 | const fixedPriorityQueue = new FixedPriorityQueue() |
9df282a0 JB |
17 | expect(fixedPriorityQueue.start).toBe(0) |
18 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
19 | expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array) |
20 | expect(fixedPriorityQueue.capacity).toBe(defaultQueueSize) | |
9df282a0 | 21 | }) |
f8d5d8fd | 22 | |
9df282a0 JB |
23 | it('Verify enqueue() behavior', () => { |
24 | const queueSize = 5 | |
097dea68 | 25 | const fixedPriorityQueue = new FixedPriorityQueue(queueSize) |
9df282a0 JB |
26 | let rtSize = fixedPriorityQueue.enqueue(1) |
27 | expect(fixedPriorityQueue.start).toBe(0) | |
28 | expect(fixedPriorityQueue.size).toBe(1) | |
9df282a0 JB |
29 | expect(rtSize).toBe(fixedPriorityQueue.size) |
30 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
3a502712 | 31 | { data: 1, priority: 0 }, |
9df282a0 JB |
32 | ]) |
33 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
34 | rtSize = fixedPriorityQueue.enqueue(2) | |
35 | expect(fixedPriorityQueue.start).toBe(0) | |
36 | expect(fixedPriorityQueue.size).toBe(2) | |
9df282a0 JB |
37 | expect(rtSize).toBe(fixedPriorityQueue.size) |
38 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
39 | { data: 1, priority: 0 }, | |
3a502712 | 40 | { data: 2, priority: 0 }, |
9df282a0 JB |
41 | ]) |
42 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
43 | rtSize = fixedPriorityQueue.enqueue(3) | |
44 | expect(fixedPriorityQueue.start).toBe(0) | |
45 | expect(fixedPriorityQueue.size).toBe(3) | |
9df282a0 JB |
46 | expect(rtSize).toBe(fixedPriorityQueue.size) |
47 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
48 | { data: 1, priority: 0 }, | |
49 | { data: 2, priority: 0 }, | |
3a502712 | 50 | { data: 3, priority: 0 }, |
9df282a0 JB |
51 | ]) |
52 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
53 | rtSize = fixedPriorityQueue.enqueue(3, -1) | |
54 | expect(fixedPriorityQueue.start).toBe(0) | |
55 | expect(fixedPriorityQueue.size).toBe(4) | |
9df282a0 JB |
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 }, | |
3a502712 | 61 | { data: 3, priority: 0 }, |
9df282a0 JB |
62 | ]) |
63 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
64 | rtSize = fixedPriorityQueue.enqueue(1, 1) | |
65 | expect(fixedPriorityQueue.start).toBe(0) | |
66 | expect(fixedPriorityQueue.size).toBe(5) | |
9df282a0 JB |
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 }, | |
3a502712 | 73 | { data: 1, priority: 1 }, |
9df282a0 JB |
74 | ]) |
75 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
76 | expect(() => fixedPriorityQueue.enqueue(4)).toThrow( | |
77 | new Error('Priority queue is full') | |
78 | ) | |
79 | }) | |
f8d5d8fd | 80 | |
6caee856 | 81 | it('Verify get() behavior', () => { |
097dea68 | 82 | const fixedPriorityQueue = new FixedPriorityQueue() |
6caee856 JB |
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) | |
90 | }) | |
91 | ||
9df282a0 JB |
92 | it('Verify dequeue() behavior', () => { |
93 | const queueSize = 5 | |
097dea68 | 94 | const fixedPriorityQueue = new FixedPriorityQueue(queueSize) |
9df282a0 JB |
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) | |
9df282a0 JB |
100 | expect(fixedPriorityQueue.capacity).toBe(queueSize) |
101 | let rtItem = fixedPriorityQueue.dequeue() | |
102 | expect(fixedPriorityQueue.start).toBe(1) | |
103 | expect(fixedPriorityQueue.size).toBe(2) | |
9df282a0 JB |
104 | expect(rtItem).toBe(2) |
105 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
106 | { data: 2, priority: -1 }, | |
107 | { data: 1, priority: 0 }, | |
3a502712 | 108 | { data: 3, priority: 0 }, |
9df282a0 JB |
109 | ]) |
110 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
111 | rtItem = fixedPriorityQueue.dequeue() | |
112 | expect(fixedPriorityQueue.start).toBe(2) | |
113 | expect(fixedPriorityQueue.size).toBe(1) | |
9df282a0 JB |
114 | expect(rtItem).toBe(1) |
115 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
116 | { data: 2, priority: -1 }, | |
117 | { data: 1, priority: 0 }, | |
3a502712 | 118 | { data: 3, priority: 0 }, |
9df282a0 JB |
119 | ]) |
120 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
121 | rtItem = fixedPriorityQueue.dequeue() | |
122 | expect(fixedPriorityQueue.start).toBe(3) | |
123 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
124 | expect(rtItem).toBe(3) |
125 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
126 | { data: 2, priority: -1 }, | |
127 | { data: 1, priority: 0 }, | |
3a502712 | 128 | { data: 3, priority: 0 }, |
9df282a0 JB |
129 | ]) |
130 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
131 | rtItem = fixedPriorityQueue.dequeue() | |
132 | expect(fixedPriorityQueue.start).toBe(3) | |
133 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
134 | expect(rtItem).toBe(undefined) |
135 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
136 | { data: 2, priority: -1 }, | |
137 | { data: 1, priority: 0 }, | |
3a502712 | 138 | { data: 3, priority: 0 }, |
9df282a0 JB |
139 | ]) |
140 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
141 | }) | |
f8d5d8fd | 142 | |
9df282a0 JB |
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) | |
151 | ++i | |
152 | } | |
153 | fixedPriorityQueue.dequeue() | |
154 | i = fixedPriorityQueue.start + 1 | |
155 | for (const value of fixedPriorityQueue) { | |
156 | expect(value).toBe(i) | |
157 | ++i | |
158 | } | |
159 | }) | |
f8d5d8fd | 160 | |
9df282a0 JB |
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) | |
168 | }) | |
169 | ||
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) | |
179 | }) | |
180 | ||
181 | it('Verify clear() behavior', () => { | |
182 | const fixedPriorityQueue = new FixedPriorityQueue() | |
183 | fixedPriorityQueue.start = 1 | |
184 | fixedPriorityQueue.size = 2 | |
9df282a0 JB |
185 | fixedPriorityQueue.nodeArray = [ |
186 | { data: 2, priority: 0 }, | |
3a502712 | 187 | { data: 3, priority: 0 }, |
9df282a0 JB |
188 | ] |
189 | fixedPriorityQueue.clear() | |
190 | expect(fixedPriorityQueue.start).toBe(0) | |
191 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
192 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ |
193 | { data: 2, priority: 0 }, | |
3a502712 | 194 | { data: 3, priority: 0 }, |
9df282a0 JB |
195 | ]) |
196 | }) | |
197 | }) |