Skip to content

Commit 5c843af

Browse files
committed
[node-core-library] Add more heap tests
1 parent 1ec01d1 commit 5c843af

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/node-core-library",
5+
"comment": "",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library"
10+
}

libraries/node-core-library/src/test/MinimumHeap.test.ts

+57-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,68 @@ describe(MinimumHeap.name, () => {
77
it('iterates in sorted order', () => {
88
const comparator: (a: number, b: number) => number = (a: number, b: number) => a - b;
99

10-
const heap: MinimumHeap<number> = new MinimumHeap<number>(comparator);
11-
for (const x of [1, 3, -2, 9, 6, 12, 11, 0, -5, 2, 3, 1, -21]) {
10+
const inputs: number[] = [];
11+
for (let heapSize: number = 1; heapSize < 100; heapSize++) {
12+
const heap: MinimumHeap<number> = new MinimumHeap<number>(comparator);
13+
inputs.length = 0;
14+
for (let i = 0; i < heapSize; i++) {
15+
const x: number = Math.random();
16+
inputs.push(x);
17+
heap.push(x);
18+
}
19+
20+
const iterationResults: number[] = [];
21+
while (heap.size > 0) {
22+
iterationResults.push(heap.poll()!);
23+
}
24+
25+
expect(iterationResults).toEqual(inputs.sort(comparator));
26+
}
27+
});
28+
29+
it('returns all input objects', () => {
30+
const comparator: (a: {}, b: {}) => number = (a: {}, b: {}) => 0;
31+
32+
const heap: MinimumHeap<{}> = new MinimumHeap<{}>(comparator);
33+
const inputs: Set<{}> = new Set([{}, {}, {}, {}, {}, {}]);
34+
for (const x of inputs) {
1235
heap.push(x);
1336
}
1437

15-
const iterationResults: number[] = [];
38+
const iterationResults: Set<{}> = new Set();
1639
while (heap.size > 0) {
17-
iterationResults.push(heap.poll()!);
40+
iterationResults.add(heap.poll()!);
1841
}
1942

20-
expect(iterationResults).toEqual(iterationResults.slice().sort(comparator));
43+
expect(iterationResults.size).toEqual(inputs.size);
44+
});
45+
46+
it('handles interleaved push and poll', () => {
47+
const comparator: (a: {}, b: {}) => number = (a: {}, b: {}) => 0;
48+
49+
const heap: MinimumHeap<{}> = new MinimumHeap<{}>(comparator);
50+
const input1: Set<{}> = new Set();
51+
const input2: Set<{}> = new Set();
52+
for (let heapSize: number = 1; heapSize < 100; heapSize++) {
53+
input1.add({});
54+
input2.add({});
55+
56+
const iterationResults: Set<{}> = new Set();
57+
58+
for (const x of input1) {
59+
heap.push(x);
60+
}
61+
62+
for (const x of input2) {
63+
iterationResults.add(heap.poll()!);
64+
heap.push(x);
65+
}
66+
67+
while (heap.size > 0) {
68+
iterationResults.add(heap.poll()!);
69+
}
70+
71+
expect(iterationResults.size).toEqual(input1.size + input2.size);
72+
}
2173
});
2274
});

0 commit comments

Comments
 (0)