-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority-queue.test.js
More file actions
153 lines (123 loc) · 3.69 KB
/
Copy pathpriority-queue.test.js
File metadata and controls
153 lines (123 loc) · 3.69 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import PriorityQueue from '../src/priority-queue-rewrite';
describe('Priority Queue happy path unit tests', () => {
test('Initialised', () => {
const pq = new PriorityQueue();
expect(pq.store).toStrictEqual(new Map());
expect(pq.length).toEqual(0);
});
test('Add and pop items (same priorities)', () => {
const pq = new PriorityQueue();
pq.add('meow', 1);
pq.add('meow', 1);
pq.pop();
const actual = Object.fromEntries(pq.store);
const expectedStore = { 1: ['meow'] };
expect(actual).toStrictEqual(expectedStore);
expect(pq.length).toEqual(1);
});
test('Add and pop items (different priorities)', () => {
const pq = new PriorityQueue();
pq.add('meow', 1);
pq.add('meow', 2);
pq.pop();
const actual = Object.fromEntries(pq.store);
const expectedStore = { 1: ['meow'], 2: [] };
expect(actual).toStrictEqual(expectedStore);
expect(pq.length).toEqual(1);
});
test('Get all priorities (empty)', () => {
const pq = new PriorityQueue();
const actual = pq.getPriorities();
expect(actual).toEqual([]);
});
test('Get all priorities (populated)', () => {
const pq = new PriorityQueue();
pq.add('meow', 1);
pq.add('woof', 2);
const actual = pq.getPriorities();
expect(actual).toEqual([ 1, 2 ]);
});
test('Use custom iterator', () => {
const pq = new PriorityQueue();
pq.add('meow', 2);
pq.add('woof', 1);
pq.add('bark', 1);
pq.add('hoot #1', 3);
pq.add('hoot #2', 3);
pq.add('hoot #3', 3);
const expectedOrder = [
'hoot #1',
'hoot #2',
'hoot #3',
'meow',
'woof',
'bark',
];
const actualOrder = [];
[...pq].map((entry) => actualOrder.push(entry[1]));
expect(actualOrder).toStrictEqual(expectedOrder);
});
test('Use custom iterator (for..of)', () => {
const pq = new PriorityQueue();
pq.add('meow', 2);
pq.add('woof', 0);
pq.add('bark', 1);
pq.add('hoot #1', 3);
pq.add('last', -666);
pq.add('hoot #2', 3);
pq.add('first', 777);
pq.add('hoot #3', 3);
const expectedOrder = [
'first',
'hoot #1',
'hoot #2',
'hoot #3',
'meow',
'bark',
'woof',
'last',
];
const actualOrder = [];
for (const entry of pq) {
const [ _priority, item ] = entry;
actualOrder.push(item);
}
expect(actualOrder).toStrictEqual(expectedOrder);
});
test('Change priority of an item', () => {
const pq = new PriorityQueue();
expect(pq.peek(777)).toStrictEqual([]);
pq.add('cats', 777);
expect(pq.peek(777)).toStrictEqual(['cats']);
pq.changePriority('cats', 0);
expect(pq.peek(777)).toStrictEqual([]);
expect(pq.peek(0)).toStrictEqual(['cats']);
});
test('Change priority of an item (multiple matching values)', () => {
const pq = new PriorityQueue();
expect(pq.peek(777)).toStrictEqual([]);
pq.add('cats', 777);
pq.add('cats', 777);
expect(pq.peek(777)).toStrictEqual(['cats', 'cats']);
let priorityChanged;
priorityChanged = pq.changePriority('cats', 0);
expect(priorityChanged).toBe(true);
priorityChanged = pq.changePriority('cats', 0);
expect(priorityChanged).toBe(true);
priorityChanged = pq.changePriority('cats', 0);
expect(priorityChanged).toBe(false); // No more cats!
expect(pq.peek(777)).toStrictEqual([]);
expect(pq.peek(0)).toStrictEqual(['cats', 'cats']);
});
});
describe('Priority Queue exceptions', () => {
test('Throw on non-numeric priority', () => {
const pq = new PriorityQueue();
// When jest is expecting a function to throw, you need to pass it
// a reference to a function to run, or else it will resolve (throw)
// too early and fail before it gets to our test condition below!
const actual = () => pq.add('bad priority', '666');
const expected = TypeError('Parameter priority must be an integer');
expect(actual).toThrow(expected);
});
});