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 | |
9df282a0 JB |
86 | it('Verify dequeue() behavior', () => { |
87 | const queueSize = 5 | |
88 | const fixedPriorityQueue = new FixedPriorityQueue(queueSize) | |
89 | fixedPriorityQueue.enqueue(1) | |
90 | fixedPriorityQueue.enqueue(2, -1) | |
91 | fixedPriorityQueue.enqueue(3) | |
92 | expect(fixedPriorityQueue.start).toBe(0) | |
93 | expect(fixedPriorityQueue.size).toBe(3) | |
9df282a0 JB |
94 | expect(fixedPriorityQueue.capacity).toBe(queueSize) |
95 | let rtItem = fixedPriorityQueue.dequeue() | |
96 | expect(fixedPriorityQueue.start).toBe(1) | |
97 | expect(fixedPriorityQueue.size).toBe(2) | |
9df282a0 JB |
98 | expect(rtItem).toBe(2) |
99 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
100 | { data: 2, priority: -1 }, | |
101 | { data: 1, priority: 0 }, | |
102 | { data: 3, priority: 0 } | |
103 | ]) | |
104 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
105 | rtItem = fixedPriorityQueue.dequeue() | |
106 | expect(fixedPriorityQueue.start).toBe(2) | |
107 | expect(fixedPriorityQueue.size).toBe(1) | |
9df282a0 JB |
108 | expect(rtItem).toBe(1) |
109 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
110 | { data: 2, priority: -1 }, | |
111 | { data: 1, priority: 0 }, | |
112 | { data: 3, priority: 0 } | |
113 | ]) | |
114 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
115 | rtItem = fixedPriorityQueue.dequeue() | |
116 | expect(fixedPriorityQueue.start).toBe(3) | |
117 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
118 | expect(rtItem).toBe(3) |
119 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
120 | { data: 2, priority: -1 }, | |
121 | { data: 1, priority: 0 }, | |
122 | { data: 3, priority: 0 } | |
123 | ]) | |
124 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
125 | rtItem = fixedPriorityQueue.dequeue() | |
126 | expect(fixedPriorityQueue.start).toBe(3) | |
127 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
128 | expect(rtItem).toBe(undefined) |
129 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ | |
130 | { data: 2, priority: -1 }, | |
131 | { data: 1, priority: 0 }, | |
132 | { data: 3, priority: 0 } | |
133 | ]) | |
134 | expect(fixedPriorityQueue.capacity).toBe(queueSize) | |
135 | }) | |
f8d5d8fd | 136 | |
9df282a0 JB |
137 | it('Verify iterator behavior', () => { |
138 | const fixedPriorityQueue = new FixedPriorityQueue() | |
139 | fixedPriorityQueue.enqueue(1) | |
140 | fixedPriorityQueue.enqueue(2) | |
141 | fixedPriorityQueue.enqueue(3) | |
142 | let i = fixedPriorityQueue.start + 1 | |
143 | for (const value of fixedPriorityQueue) { | |
144 | expect(value).toBe(i) | |
145 | ++i | |
146 | } | |
147 | fixedPriorityQueue.dequeue() | |
148 | i = fixedPriorityQueue.start + 1 | |
149 | for (const value of fixedPriorityQueue) { | |
150 | expect(value).toBe(i) | |
151 | ++i | |
152 | } | |
153 | }) | |
f8d5d8fd | 154 | |
9df282a0 JB |
155 | it('Verify empty() behavior', () => { |
156 | const fixedPriorityQueue = new FixedPriorityQueue() | |
157 | expect(fixedPriorityQueue.empty()).toBe(true) | |
158 | fixedPriorityQueue.enqueue(1) | |
159 | expect(fixedPriorityQueue.empty()).toBe(false) | |
160 | fixedPriorityQueue.dequeue() | |
161 | expect(fixedPriorityQueue.empty()).toBe(true) | |
162 | }) | |
163 | ||
164 | it('Verify full() behavior', () => { | |
165 | const fixedPriorityQueue = new FixedPriorityQueue(2) | |
166 | expect(fixedPriorityQueue.full()).toBe(false) | |
167 | fixedPriorityQueue.enqueue(1) | |
168 | expect(fixedPriorityQueue.full()).toBe(false) | |
169 | fixedPriorityQueue.enqueue(2) | |
170 | expect(fixedPriorityQueue.full()).toBe(true) | |
171 | fixedPriorityQueue.dequeue() | |
172 | expect(fixedPriorityQueue.full()).toBe(false) | |
173 | }) | |
174 | ||
175 | it('Verify clear() behavior', () => { | |
176 | const fixedPriorityQueue = new FixedPriorityQueue() | |
177 | fixedPriorityQueue.start = 1 | |
178 | fixedPriorityQueue.size = 2 | |
9df282a0 JB |
179 | fixedPriorityQueue.nodeArray = [ |
180 | { data: 2, priority: 0 }, | |
181 | { data: 3, priority: 0 } | |
182 | ] | |
183 | fixedPriorityQueue.clear() | |
184 | expect(fixedPriorityQueue.start).toBe(0) | |
185 | expect(fixedPriorityQueue.size).toBe(0) | |
9df282a0 JB |
186 | expect(fixedPriorityQueue.nodeArray).toMatchObject([ |
187 | { data: 2, priority: 0 }, | |
188 | { data: 3, priority: 0 } | |
189 | ]) | |
190 | }) | |
191 | }) |