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