This repository was archived by the owner on Apr 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority-queue.test.ts
More file actions
68 lines (63 loc) · 2.29 KB
/
priority-queue.test.ts
File metadata and controls
68 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { PriorityQueue } from ".";
test("size is reported correctly after adding and removing elements", () => {
const maxQueue = new PriorityQueue(true);
expect(maxQueue.size()).toBe(0);
maxQueue.push("A", 1);
expect(maxQueue.size()).toBe(1);
maxQueue.push("B", 2);
expect(maxQueue.size()).toBe(2);
maxQueue.pop();
expect(maxQueue.size()).toBe(1);
maxQueue.pop();
expect(maxQueue.size()).toBe(0);
const minQueue = new PriorityQueue(false);
expect(minQueue.size()).toBe(0);
minQueue.push("A", 1);
expect(minQueue.size()).toBe(1);
minQueue.push("B", 2);
expect(minQueue.size()).toBe(2);
minQueue.pop();
expect(minQueue.size()).toBe(1);
minQueue.pop();
expect(minQueue.size()).toBe(0);
});
test("max queue: added items are in order of priority", () => {
const q = new PriorityQueue(true);
const a = q["_list"]["_array"];
q.push("A", 0);
expect(a.get(0)).toEqual({ value: "A", priority: 0 });
q.push("B", 1);
expect(a.get(0)).toEqual({ value: "B", priority: 1 });
q.push("C", 2);
expect(a.get(0)).toEqual({ value: "C", priority: 2 });
q.push("D", 3);
expect(a.get(0)).toEqual({ value: "D", priority: 3 });
});
test("min queue: added items are in order of priority", () => {
const q = new PriorityQueue(false);
const a = q["_list"]["_array"];
q.push("A", 0);
expect(a.get(0)).toEqual({ value: "A", priority: 0 });
q.push("B", 1);
expect(a.get(0)).toEqual({ value: "A", priority: 0 });
q.push("C", 2);
expect(a.get(0)).toEqual({ value: "A", priority: 0 });
q.push("D", 3);
expect(a.get(0)).toEqual({ value: "A", priority: 0 });
});
test("max queue: popped items come in order of priority", () => {
const q = new PriorityQueue(true);
const alphabet = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
alphabet.forEach((letter, idx) => q.push(letter, idx));
for (let i = alphabet.length - 1; i >= 0; i--) {
expect(q.pop()).toBe(alphabet[i]);
}
});
test("min queue: popped items come in order of priority", () => {
const q = new PriorityQueue(false);
const alphabet = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
alphabet.forEach((letter, idx) => q.push(letter, idx));
for (let i = 0; i < alphabet.length; i++) {
expect(q.pop()).toBe(alphabet[i]);
}
});