|
| 1 | +import { countingSort } from './counting-sort' |
| 2 | + |
| 3 | +const showLogs = process.env.SHOW_LOGS === '1' |
| 4 | + |
| 5 | +describe('countingSort', () => { |
| 6 | + describe('non-negative integers in the input array', () => { |
| 7 | + it('correctly sorts an unsorted array', () => { |
| 8 | + const arrayToSort = [3, 6, 2, 8, 9, 1, 4, 5, 7] |
| 9 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 10 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 11 | + expectedResult |
| 12 | + ) |
| 13 | + }) |
| 14 | + |
| 15 | + it('correctly sorts a mostly sorted array', () => { |
| 16 | + const arrayToSort = [1, 2, 9, 3, 4, 5, 6, 7, 8] |
| 17 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 18 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 19 | + expectedResult |
| 20 | + ) |
| 21 | + }) |
| 22 | + |
| 23 | + it('correctly sorts an inversely sorted array', () => { |
| 24 | + const arrayToSort = [9, 8, 7, 6, 5, 4, 3, 2, 1] |
| 25 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 26 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 27 | + expectedResult |
| 28 | + ) |
| 29 | + }) |
| 30 | + |
| 31 | + it('correctly returns an already sorted array', () => { |
| 32 | + const arrayToSort = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 33 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 34 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 35 | + expectedResult |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('can handle an array with duplicates', () => { |
| 40 | + const arrayToSort = [3, 6, 2, 8, 4, 4, 4, 4, 7] |
| 41 | + const expectedResult = [2, 3, 4, 4, 4, 4, 6, 7, 8] |
| 42 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 43 | + expectedResult |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + it('can handle an array with only one item', () => { |
| 48 | + const arrayToSort = [1] |
| 49 | + const expectedResult = [1] |
| 50 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 51 | + expectedResult |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it('can handle an empty array', () => { |
| 56 | + const arrayToSort = [] |
| 57 | + const expectedResult = [] |
| 58 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 59 | + expectedResult |
| 60 | + ) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('negative integers in the input array', () => { |
| 65 | + it('correctly sorts an unsorted array', () => { |
| 66 | + const arrayToSort = [3, 6, 2, 8, -2, 9, 1, 4, -1, 5, 10, 0, 7] |
| 67 | + const expectedResult = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 68 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 69 | + expectedResult |
| 70 | + ) |
| 71 | + }) |
| 72 | + |
| 73 | + it('correctly sorts a mostly sorted array', () => { |
| 74 | + const arrayToSort = [-2, -1, 0, 1, 2, 9, 3, 4, 5, 6, 7, 8, 10] |
| 75 | + const expectedResult = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 76 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 77 | + expectedResult |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + it('correctly sorts an inversely sorted array', () => { |
| 82 | + const arrayToSort = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2] |
| 83 | + const expectedResult = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 84 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 85 | + expectedResult |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + it('correctly returns an already sorted array', () => { |
| 90 | + const arrayToSort = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 91 | + const expectedResult = [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 92 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 93 | + expectedResult |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + it('can handle an array with duplicates', () => { |
| 98 | + const arrayToSort = [3, 6, -1, -1, 2, 8, 4, 4, 10, 4, -1, 4, 7] |
| 99 | + const expectedResult = [-1, -1, -1, 2, 3, 4, 4, 4, 4, 6, 7, 8, 10] |
| 100 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 101 | + expectedResult |
| 102 | + ) |
| 103 | + }) |
| 104 | + |
| 105 | + it('can handle an array with only one item', () => { |
| 106 | + const arrayToSort = [-1] |
| 107 | + const expectedResult = [-1] |
| 108 | + expect(countingSort(arrayToSort, undefined, undefined, showLogs)).toEqual( |
| 109 | + expectedResult |
| 110 | + ) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + describe('min and max value arguments', () => { |
| 115 | + it('uses the provided minValue and maxValue arguments when provided', () => { |
| 116 | + const arrayToSort = [3, 6, 2, 8, 9, 1, 4, 5, 7] |
| 117 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 118 | + expect(countingSort(arrayToSort, 1, 9, showLogs)).toEqual(expectedResult) |
| 119 | + }) |
| 120 | + |
| 121 | + it('calculates the min and max if only the minValue argument is provided', () => { |
| 122 | + const arrayToSort = [3, 6, 2, 8, 9, 1, 4, 5, 7] |
| 123 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 124 | + expect(countingSort(arrayToSort, 1, undefined, showLogs)).toEqual( |
| 125 | + expectedResult |
| 126 | + ) |
| 127 | + }) |
| 128 | + |
| 129 | + it('calculates the min and max if only the maxValue argument is provided', () => { |
| 130 | + const arrayToSort = [3, 6, 2, 8, 9, 1, 4, 5, 7] |
| 131 | + const expectedResult = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 132 | + expect(countingSort(arrayToSort, undefined, 9, showLogs)).toEqual( |
| 133 | + expectedResult |
| 134 | + ) |
| 135 | + }) |
| 136 | + }) |
| 137 | +}) |
0 commit comments