|
| 1 | +const Heap = require('./heap-sort') |
| 2 | +const { newArrayOf } = require('../../utils') |
| 3 | +const { StdRandom } = require('../../libs') |
| 4 | + |
| 5 | +describe('Unit Tests: HeapSort', () => { |
| 6 | + describe('Heap.sort()', () => { |
| 7 | + it('should sort an ordered array', () => { |
| 8 | + const orderedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 9 | + const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 10 | + |
| 11 | + Heap.sort(orderedArray) |
| 12 | + |
| 13 | + expect(orderedArray).toEqual(expectedArray) |
| 14 | + }) |
| 15 | + |
| 16 | + it('should sort a reversed array', () => { |
| 17 | + const reversedArray = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] |
| 18 | + const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 19 | + |
| 20 | + Heap.sort(reversedArray) |
| 21 | + |
| 22 | + expect(reversedArray).toEqual(expectedArray) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should sort an unordered array', () => { |
| 26 | + const unorderedArray = [0, 9, 5, 2, 1, 8, 7, 6, 4, 3] |
| 27 | + const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 28 | + |
| 29 | + Heap.sort(unorderedArray) |
| 30 | + |
| 31 | + expect(unorderedArray).toEqual(expectedArray) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should sort an array with all equal values', () => { |
| 35 | + const allEqualArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] |
| 36 | + const expectedArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] |
| 37 | + |
| 38 | + Heap.sort(allEqualArray) |
| 39 | + |
| 40 | + expect(allEqualArray).toEqual(expectedArray) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should sort a small random array', () => { |
| 44 | + const n = CONFIG.SMALL_ARRAY_LENGTH |
| 45 | + const array = newArrayOf(n, () => StdRandom.uniform(n)) |
| 46 | + |
| 47 | + Heap.sort(array) |
| 48 | + |
| 49 | + expect(Heap.isSorted(array)).toBeTrue() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should sort a medium random array', () => { |
| 53 | + const n = CONFIG.MEDIUM_ARRAY_LENGTH |
| 54 | + const array = newArrayOf(n, () => StdRandom.uniform(n)) |
| 55 | + |
| 56 | + Heap.sort(array) |
| 57 | + |
| 58 | + expect(Heap.isSorted(array)).toBeTrue() |
| 59 | + }) |
| 60 | + |
| 61 | + it('should sort a large random array', () => { |
| 62 | + const n = CONFIG.LARGE_ARRAY_LENGTH |
| 63 | + const array = newArrayOf(n, () => StdRandom.uniform(n)) |
| 64 | + |
| 65 | + Heap.sort(array) |
| 66 | + |
| 67 | + expect(Heap.isSorted(array)).toBeTrue() |
| 68 | + }) |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +describe('Explain me: HeapSort', () => { |
| 73 | + it('should perform n exchanges and mutate the array', () => { |
| 74 | + const a = [8, 9, 6, 5, 7, 4] |
| 75 | + const mutations = [[...a]] // initial state |
| 76 | + const originalExchange = Heap.exchange.bind(Heap) |
| 77 | + spyOn(Heap, 'sink').and.callThrough() |
| 78 | + spyOn(Heap, 'exchange').and.callFake((array, i, j) => { |
| 79 | + originalExchange(array, i, j) |
| 80 | + mutations.push([...array]) |
| 81 | + }) |
| 82 | + const expectedExchanges = [ |
| 83 | + [a, 0, 1], // exch 1: 8 <-> 9 : heap construction |
| 84 | + [a, 0, 5], // exch 2: 9 <-> 4 : max goes to the end |
| 85 | + [a, 0, 1], // exch 3: 4 <-> 8 : sink |
| 86 | + [a, 1, 4], // exch 4: 4 <-> 7 : |
| 87 | + [a, 0, 4], // exch 5: 8 <-> 4 : max goes to the end |
| 88 | + [a, 0, 1], // exch 6: 4 <-> 7 : sink |
| 89 | + [a, 1, 3], // exch 7: 4 <-> 5 : |
| 90 | + [a, 0, 3], // exch 8: 7 <-> 4 : max goes to the end |
| 91 | + [a, 0, 2], // exch 9: 4 <-> 6 : sink |
| 92 | + [a, 0, 2], // exch 10: 6 <-> 4 : max goes to the end |
| 93 | + [a, 0, 1], // exch 11: 4 <-> 5 : sink |
| 94 | + [a, 0, 1] // exch 12: 5 <-> 4 : max goes to the end, sink |
| 95 | + ] |
| 96 | + const expectedMutations = [ |
| 97 | + [8, 9, 6, 5, 7, 4], // initial state |
| 98 | + [9, 8, 6, 5, 7, 4], // exch 1 |
| 99 | + [4, 8, 6, 5, 7, 9], // exch 2 |
| 100 | + [8, 4, 6, 5, 7, 9], // exch 3 |
| 101 | + [8, 7, 6, 5, 4, 9], // exch 4 |
| 102 | + [4, 7, 6, 5, 8, 9], // exch 5 |
| 103 | + [7, 4, 6, 5, 8, 9], // exch 6 |
| 104 | + [7, 5, 6, 4, 8, 9], // exch 7 |
| 105 | + [4, 5, 6, 7, 8, 9], // exch 8 |
| 106 | + [6, 5, 4, 7, 8, 9], // exch 9 |
| 107 | + [4, 5, 6, 7, 8, 9], // exch 10 |
| 108 | + [5, 4, 6, 7, 8, 9], // exch 11 |
| 109 | + [4, 5, 6, 7, 8, 9] // exch 12 |
| 110 | + ] |
| 111 | + |
| 112 | + Heap.sort(a) |
| 113 | + |
| 114 | + expect(Heap.exchange.calls.allArgs()).toEqual(expectedExchanges) |
| 115 | + expect(Heap.sink).toHaveBeenCalledTimes(8) |
| 116 | + expect(mutations).toEqual(expectedMutations) |
| 117 | + }) |
| 118 | +}) |
0 commit comments