|
| 1 | +const MaxHeap = require('.'); |
| 2 | + |
| 3 | +describe('MaxHeap', () => { |
| 4 | + it('Should be a class', () => { |
| 5 | + expect(typeof MaxHeap.prototype.constructor).toEqual('function'); |
| 6 | + }); |
| 7 | + |
| 8 | + it('Should create an instance of MaxHeap', () => { |
| 9 | + const mh = new MaxHeap(); |
| 10 | + expect(mh instanceof MaxHeap).toEqual(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it('Should add an element to the MaxHeap', () => { |
| 14 | + const mh = new MaxHeap(); |
| 15 | + mh.add(10); |
| 16 | + expect(mh.getMax()).toEqual(10); |
| 17 | + }); |
| 18 | + |
| 19 | + it('Should keep the largest element at the root', () => { |
| 20 | + const mh = new MaxHeap(); |
| 21 | + [12, 5, 34].forEach(el => mh.add(el)); |
| 22 | + expect(mh.getMax()).toEqual(34); |
| 23 | + }); |
| 24 | + |
| 25 | + it('Should retain Heap properties after removal of an element', () => { |
| 26 | + const mh = new MaxHeap(); |
| 27 | + [12, 45, 1, 34].forEach(el => mh.add(el)); |
| 28 | + expect(mh.getMax()).toEqual(45); |
| 29 | + mh.remove(); |
| 30 | + expect(mh.getMax()).toEqual(34); |
| 31 | + }); |
| 32 | + |
| 33 | + it('Should return `null` when heap is empty', () => { |
| 34 | + const mh = new MaxHeap(); |
| 35 | + [1, 34].forEach(el => mh.add(el)); |
| 36 | + expect(mh.getMax()).toEqual(34); |
| 37 | + mh.remove(); |
| 38 | + mh.remove(); |
| 39 | + expect(mh.getMax()).toEqual(null); |
| 40 | + }); |
| 41 | + |
| 42 | + it('Should return the elelment value on `remove()`', () => { |
| 43 | + const mh = new MaxHeap(); |
| 44 | + [1, 34].forEach(el => mh.add(el)); |
| 45 | + expect(mh.getMax()).toEqual(34); |
| 46 | + expect(mh.remove()).toEqual(34); |
| 47 | + expect(mh.remove()).toEqual(1); |
| 48 | + expect(mh.getMax()).toEqual(null); |
| 49 | + }); |
| 50 | +}); |
0 commit comments