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