forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.spec.ts
75 lines (64 loc) · 1.74 KB
/
heap.spec.ts
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
import 'mocha';
import { expect } from 'chai';
import { MinHeap, heapSort } from '../../../src/ts/index';
describe('Heap', () => {
let heap: MinHeap<number>;
beforeEach(() => {
heap = new MinHeap<number>();
});
it('starts empty MinHeap', () => {
expect(heap.size()).to.equal(0);
expect(heap.isEmpty()).to.equal(true);
});
it('inserts values in the MinHeap', () => {
const resultArray = [];
for (let i = 1; i < 10; i++) {
resultArray.push(i);
heap.insert(i);
expect(heap.getAsArray()).to.deep.equal(resultArray);
}
});
it('finds the min value from the MinHeap', () => {
const resultArray = [];
for (let i = 10; i >= 1; i--) {
resultArray.push(i);
heap.insert(i);
expect(heap.findMinimum()).to.equal(i);
}
});
it('performs heapify in the MinHeap', () => {
const resultArray = [];
for (let i = 10; i >= 1; i--) {
resultArray.push(i);
}
expect(heap.heapify(resultArray)).to.deep.equal(resultArray);
});
it('extracts the min value from the MinHeap', () => {
let resultArray = [];
for (let i = 1; i < 10; i++) {
resultArray.push(i);
heap.insert(i);
expect(heap.getAsArray()).to.deep.equal(resultArray);
}
resultArray = [
[],
[2, 4, 3, 8, 5, 6, 7, 9],
[3, 4, 6, 8, 5, 9, 7],
[4, 5, 6, 8, 7, 9],
[5, 7, 6, 8, 9],
[6, 7, 9, 8],
[7, 8, 9],
[8, 9],
[9],
[]
];
for (let i = 1; i < 10; i++) {
expect(heap.extract()).to.equal(i);
expect(heap.getAsArray()).to.deep.equal(resultArray[i]);
}
});
it('Heap Sort', () => {
const array = [3, 2, 5, 6, 1, 7, 8, 9];
expect(heapSort(array)).to.deep.equal([1, 2, 3, 5, 6, 7, 8, 9]);
});
});