Commit | Line | Data |
---|---|---|
9df282a0 | 1 | import { expect } from 'expect' |
f8d5d8fd | 2 | |
9df282a0 JB |
3 | import { |
4 | defaultQueueSize, | |
5 | FixedPriorityQueue | |
6 | } from '../lib/fixed-priority-queue.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 | ) | |
16 | let fixedPriorityQueue = new FixedPriorityQueue() | |
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) | |
21 | fixedPriorityQueue = new FixedPriorityQueue(2) | |
22 | expect(fixedPriorityQueue.start).toBe(0) | |
23 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
24 | expect(fixedPriorityQueue.nodeArray).toBeInstanceOf(Array) |
25 | expect(fixedPriorityQueue.capacity).toBe(2) | |
26 | }) | |
f8d5d8fd | 27 | |
9df282a0 JB |
28 | it('Verify enqueue() behavior', () => { |
29 | const queueSize = 5 | |
30 | const fixedPriorityQueue = new FixedPriorityQueue(queueSize) | |
31 | let rtSize = fixedPriorityQueue.enqueue(1) | |
32 | expect(fixedPriorityQueue.start).toBe(0) | |
33 | expect(fixedPriorityQueue.size).toBe(1) | |
9df282a0 JB |
34 | expect(rtSize).toBe(fixedPriorityQueue.size) |
35 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
36 | { data: 1, priority: 0 } | |
37 | ]) | |
38 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
39 | rtSize = fixedPriorityQueue.enqueue(2) | |
40 | expect(fixedPriorityQueue.start).toBe(0) | |
41 | expect(fixedPriorityQueue.size).toBe(2) | |
9df282a0 JB |
42 | expect(rtSize).toBe(fixedPriorityQueue.size) |
43 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
44 | { data: 1, priority: 0 }, | |
45 | { data: 2, priority: 0 } | |
46 | ]) | |
47 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
48 | rtSize = fixedPriorityQueue.enqueue(3) | |
49 | expect(fixedPriorityQueue.start).toBe(0) | |
50 | expect(fixedPriorityQueue.size).toBe(3) | |
9df282a0 JB |
51 | expect(rtSize).toBe(fixedPriorityQueue.size) |
52 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
53 | { data: 1, priority: 0 }, | |
54 | { data: 2, priority: 0 }, | |
55 | { data: 3, priority: 0 } | |
56 | ]) | |
57 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
58 | rtSize = fixedPriorityQueue.enqueue(3, -1) | |
59 | expect(fixedPriorityQueue.start).toBe(0) | |
60 | expect(fixedPriorityQueue.size).toBe(4) | |
9df282a0 JB |
61 | expect(rtSize).toBe(fixedPriorityQueue.size) |
62 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
63 | { data: 3, priority: -1 }, | |
64 | { data: 1, priority: 0 }, | |
65 | { data: 2, priority: 0 }, | |
66 | { data: 3, priority: 0 } | |
67 | ]) | |
68 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
69 | rtSize = fixedPriorityQueue.enqueue(1, 1) | |
70 | expect(fixedPriorityQueue.start).toBe(0) | |
71 | expect(fixedPriorityQueue.size).toBe(5) | |
9df282a0 JB |
72 | expect(rtSize).toBe(fixedPriorityQueue.size) |
73 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
74 | { data: 3, priority: -1 }, | |
75 | { data: 1, priority: 0 }, | |
76 | { data: 2, priority: 0 }, | |
77 | { data: 3, priority: 0 }, | |
78 | { data: 1, priority: 1 } | |
79 | ]) | |
80 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
81 | expect(() => fixedPriorityQueue.enqueue(4)).toThrow( | |
82 | new Error('Priority queue is full') | |
83 | ) | |
84 | }) | |
f8d5d8fd | 85 | |
6caee856 JB |
86 | it('Verify get() behavior', () => { |
87 | const fixedPriorityQueue = new FixedPriorityQueue() | |
88 | fixedPriorityQueue.enqueue(1) | |
89 | fixedPriorityQueue.enqueue(2, -1) | |
90 | fixedPriorityQueue.enqueue(3) | |
91 | expect(fixedPriorityQueue.get(0)).toBe(2) | |
92 | expect(fixedPriorityQueue.get(1)).toBe(1) | |
93 | expect(fixedPriorityQueue.get(2)).toBe(3) | |
94 | expect(fixedPriorityQueue.get(3)).toBe(undefined) | |
95 | }) | |
96 | ||
9df282a0 JB |
97 | it('Verify dequeue() behavior', () => { |
98 | const queueSize = 5 | |
99 | const fixedPriorityQueue = new FixedPriorityQueue(queueSize) | |
100 | fixedPriorityQueue.enqueue(1) | |
101 | fixedPriorityQueue.enqueue(2, -1) | |
102 | fixedPriorityQueue.enqueue(3) | |
103 | expect(fixedPriorityQueue.start).toBe(0) | |
104 | expect(fixedPriorityQueue.size).toBe(3) | |
9df282a0 JB |
105 | expect(fixedPriorityQueue.capacity).toBe(queueSize) |
106 | let rtItem = fixedPriorityQueue.dequeue() | |
107 | expect(fixedPriorityQueue.start).toBe(1) | |
108 | expect(fixedPriorityQueue.size).toBe(2) | |
9df282a0 JB |
109 | expect(rtItem).toBe(2) |
110 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
111 | { data: 2, priority: -1 }, | |
112 | { data: 1, priority: 0 }, | |
113 | { data: 3, priority: 0 } | |
114 | ]) | |
115 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
116 | rtItem = fixedPriorityQueue.dequeue() | |
117 | expect(fixedPriorityQueue.start).toBe(2) | |
118 | expect(fixedPriorityQueue.size).toBe(1) | |
9df282a0 JB |
119 | expect(rtItem).toBe(1) |
120 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
121 | { data: 2, priority: -1 }, | |
122 | { data: 1, priority: 0 }, | |
123 | { data: 3, priority: 0 } | |
124 | ]) | |
125 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
126 | rtItem = fixedPriorityQueue.dequeue() | |
127 | expect(fixedPriorityQueue.start).toBe(3) | |
128 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
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.capacity).toBe(queueSize) | |
136 | rtItem = fixedPriorityQueue.dequeue() | |
137 | expect(fixedPriorityQueue.start).toBe(3) | |
138 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
139 | expect(rtItem).toBe(undefined) |
140 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
141 | { data: 2, priority: -1 }, | |
142 | { data: 1, priority: 0 }, | |
143 | { data: 3, priority: 0 } | |
144 | ]) | |
145 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
146 | }) | |
f8d5d8fd | 147 | |
9df282a0 JB |
148 | it('Verify iterator behavior', () => { |
149 | const fixedPriorityQueue = new FixedPriorityQueue() | |
150 | fixedPriorityQueue.enqueue(1) | |
151 | fixedPriorityQueue.enqueue(2) | |
152 | fixedPriorityQueue.enqueue(3) | |
153 | let i = fixedPriorityQueue.start + 1 | |
154 | for (const value of fixedPriorityQueue) { | |
155 | expect(value).toBe(i) | |
156 | ++i | |
157 | } | |
158 | fixedPriorityQueue.dequeue() | |
159 | i = fixedPriorityQueue.start + 1 | |
160 | for (const value of fixedPriorityQueue) { | |
161 | expect(value).toBe(i) | |
162 | ++i | |
163 | } | |
164 | }) | |
f8d5d8fd | 165 | |
9df282a0 JB |
166 | it('Verify empty() behavior', () => { |
167 | const fixedPriorityQueue = new FixedPriorityQueue() | |
168 | expect(fixedPriorityQueue.empty()).toBe(true) | |
169 | fixedPriorityQueue.enqueue(1) | |
170 | expect(fixedPriorityQueue.empty()).toBe(false) | |
171 | fixedPriorityQueue.dequeue() | |
172 | expect(fixedPriorityQueue.empty()).toBe(true) | |
173 | }) | |
174 | ||
175 | it('Verify full() behavior', () => { | |
176 | const fixedPriorityQueue = new FixedPriorityQueue(2) | |
177 | expect(fixedPriorityQueue.full()).toBe(false) | |
178 | fixedPriorityQueue.enqueue(1) | |
179 | expect(fixedPriorityQueue.full()).toBe(false) | |
180 | fixedPriorityQueue.enqueue(2) | |
181 | expect(fixedPriorityQueue.full()).toBe(true) | |
182 | fixedPriorityQueue.dequeue() | |
183 | expect(fixedPriorityQueue.full()).toBe(false) | |
184 | }) | |
185 | ||
186 | it('Verify clear() behavior', () => { | |
187 | const fixedPriorityQueue = new FixedPriorityQueue() | |
188 | fixedPriorityQueue.start = 1 | |
189 | fixedPriorityQueue.size = 2 | |
9df282a0 JB |
190 | fixedPriorityQueue.nodeArray = [ |
191 | { data: 2, priority: 0 }, | |
192 | { data: 3, priority: 0 } | |
193 | ] | |
194 | fixedPriorityQueue.clear() | |
195 | expect(fixedPriorityQueue.start).toBe(0) | |
196 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
197 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ |
198 | { data: 2, priority: 0 }, | |
199 | { data: 3, priority: 0 } | |
200 | ]) | |
201 | }) | |
202 | }) |