|
| 1 | +const Insertion = require('./insertion-sort') |
| 2 | +const { newArrayOf } = require('../../utils') |
| 3 | +const { StdRandom } = require('../../libs') |
| 4 | +const { defaultComparator: comparator } = require('../../common') |
| 5 | + |
| 6 | +describe('Unit Tests: Insertion Sort Algorithm', () => { |
| 7 | + beforeEach(() => { |
| 8 | + this.orderedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 9 | + this.reversedArray = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] |
| 10 | + this.unorderedArray = [0, 9, 5, 2, 1, 8, 7, 6, 4, 3] |
| 11 | + this.allEqualArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] |
| 12 | + }) |
| 13 | + |
| 14 | + describe('static less method', () => { |
| 15 | + it('should return true if a is less than b', () => { |
| 16 | + const a = 1 |
| 17 | + const b = 2 |
| 18 | + |
| 19 | + expect(Insertion.less(a, b, comparator)).toBe(true) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should return false if a is greater than b', () => { |
| 23 | + const a = 2 |
| 24 | + const b = 1 |
| 25 | + |
| 26 | + expect(Insertion.less(a, b, comparator)).toBe(false) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should return false if a is equal to b', () => { |
| 30 | + const a = 1 |
| 31 | + const b = 1 |
| 32 | + |
| 33 | + expect(Insertion.less(a, b, comparator)).toBe(false) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should have a defaultComparator function', () => { |
| 37 | + expect(Insertion.less(1, 10)).toBe(true) |
| 38 | + expect(Insertion.less(1, 1)).toBe(false) |
| 39 | + expect(Insertion.less(10, 1)).toBe(false) |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe('static exchange method', () => { |
| 44 | + it('should interchange the values of the two given indexes in the array', () => { |
| 45 | + const array = [5, 6, 7] |
| 46 | + const expectedArray = [7, 6, 5] |
| 47 | + |
| 48 | + Insertion.exchange(array, 0, 2) |
| 49 | + |
| 50 | + expect(array).toEqual(expectedArray) |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('static isSorted method', () => { |
| 55 | + it('should return true if an array is sorted', () => { |
| 56 | + expect(Insertion.isSorted(this.orderedArray)).toBe(true) |
| 57 | + expect(Insertion.isSorted(this.allEqualArray)).toBe(true) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should return false if the array is not sorted', () => { |
| 61 | + expect(Insertion.isSorted(this.unorderedArray)).toBe(false) |
| 62 | + expect(Insertion.isSorted(this.reversedArray)).toBe(false) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should accept a comparator function', () => { |
| 66 | + const orderedArray = ['A', 'B', 'C'] |
| 67 | + const unorderedArray = ['Z', 'X', 'Y'] |
| 68 | + |
| 69 | + expect(Insertion.isSorted(orderedArray, comparator)).toBe(true) |
| 70 | + expect(Insertion.isSorted(unorderedArray, comparator)).toBe(false) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should return true for a sorted big array', () => { |
| 74 | + const n = 1000000 // a million! |
| 75 | + const array = newArrayOf(n, i => i) // from 0 to 999999 |
| 76 | + |
| 77 | + expect(Insertion.isSorted(array)).toBeTrue() |
| 78 | + }) |
| 79 | + |
| 80 | + it('should return false for a random big array', () => { |
| 81 | + const n = 1000000 // a million! |
| 82 | + const array = newArrayOf(n, i => i) // from 0 to 999999 |
| 83 | + array[array.length - 1] = 0 // change last number to be 0 |
| 84 | + |
| 85 | + expect(Insertion.isSorted(array)).toBeFalse() |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe('static show method', () => { |
| 90 | + it('should be a function', () => { |
| 91 | + // I could test the call to StdOut, but I don't want to. |
| 92 | + expect(Insertion.show).toBeInstanceOf(Function) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('static sort method', () => { |
| 97 | + it('should sort a unordered array', () => { |
| 98 | + const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 99 | + |
| 100 | + Insertion.sort(this.unorderedArray) |
| 101 | + |
| 102 | + expect(this.unorderedArray).toEqual(expectedArray) |
| 103 | + }) |
| 104 | + |
| 105 | + it('should sort an ordered array', () => { |
| 106 | + const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 107 | + |
| 108 | + Insertion.sort(this.orderedArray) |
| 109 | + |
| 110 | + expect(this.orderedArray).toEqual(expectedArray) |
| 111 | + }) |
| 112 | + |
| 113 | + it('should sort a reversed array', () => { |
| 114 | + const expectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 115 | + |
| 116 | + Insertion.sort(this.reversedArray) |
| 117 | + |
| 118 | + expect(this.reversedArray).toEqual(expectedArray) |
| 119 | + }) |
| 120 | + |
| 121 | + it('should sort an array with all the values to be equal', () => { |
| 122 | + const expectedArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] |
| 123 | + |
| 124 | + Insertion.sort(this.allEqualArray) |
| 125 | + |
| 126 | + expect(this.allEqualArray).toEqual(expectedArray) |
| 127 | + }) |
| 128 | + |
| 129 | + it('should sort a small random array', () => { |
| 130 | + // considering that Insertion Sort is slow... |
| 131 | + const n = 1000 // a thousand |
| 132 | + const array = newArrayOf(n, () => StdRandom.uniform(n)) |
| 133 | + |
| 134 | + Insertion.sort(array) |
| 135 | + |
| 136 | + expect(Insertion.isSorted(array)).toBeTrue() |
| 137 | + }) |
| 138 | + }) |
| 139 | +}) |
0 commit comments