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