forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxHeap.test.js
62 lines (51 loc) · 1.59 KB
/
MaxHeap.test.js
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
const MaxHeap = require('.');
describe('MaxHeap', () => {
it('Should be a class', () => {
expect(typeof MaxHeap.prototype.constructor).toEqual('function');
});
const mh = new MaxHeap();
beforeEach(() => {
mh.destroy();
});
it('Should create an instance of MaxHeap', () => {
expect(mh instanceof MaxHeap).toEqual(true);
});
it('Should create a MaxHeap using collection', () => {
const mHBulk = new MaxHeap([1, 3, 21, 9, 101, 0]);
expect(mHBulk.getMax()).toEqual(101);
});
it('Should add an element to the MaxHeap', () => {
mh.add(10);
expect(mh.getMax()).toEqual(10);
});
it('Should keep the largest element at the root', () => {
[12, 5, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(34);
});
it('Should retain Heap properties after removal of an element', () => {
[12, 45, 1, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(45);
mh.remove();
expect(mh.getMax()).toEqual(34);
});
it('Should return `null` when heap is empty', () => {
[1, 34, 43, 54, 123].forEach(el => mh.add(el));
mh.remove();
mh.remove();
mh.remove();
mh.remove();
mh.remove();
mh.remove();
expect(mh.getMax()).toEqual(null);
});
it('Should return the elelment value on `remove()`', () => {
[1, 34].forEach(el => mh.add(el));
expect(mh.getMax()).toEqual(34);
expect(mh.remove()).toEqual(34);
expect(mh.remove()).toEqual(1);
expect(mh.getMax()).toEqual(null);
});
it('Should return `null` on `remove() called on empty heap`', () => {
expect(mh.getMax()).toEqual(null);
});
});