|
| 1 | +export const sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 2 | +export const notSortedArray = [10, 5, 30, -1, 0, 0, 1, 2, -3, 2]; |
| 3 | +export const equalArray = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; |
| 4 | + |
| 5 | +export class SortTester { |
| 6 | + static testSort(SortingClass) { |
| 7 | + const sorter = new SortingClass(); |
| 8 | + |
| 9 | + expect(sorter.sort([])).toEqual([]); |
| 10 | + expect(sorter.sort([1])).toEqual([1]); |
| 11 | + expect(sorter.sort([1, 2])).toEqual([1, 2]); |
| 12 | + expect(sorter.sort([2, 1])).toEqual([1, 2]); |
| 13 | + expect(sorter.sort([1, 1, 1])).toEqual([1, 1, 1]); |
| 14 | + expect(sorter.sort(sortedArray)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
| 15 | + expect(sorter.sort(notSortedArray)).toEqual([-3, -1, 0, 0, 1, 2, 2, 5, 10, 30]); |
| 16 | + expect(sorter.sort(equalArray)).toEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); |
| 17 | + } |
| 18 | + |
| 19 | + static testSortWithCustomComparator(SortingClass) { |
| 20 | + const callbacks = { |
| 21 | + compareCallback: (a, b) => { |
| 22 | + if (a.length === b.length) { |
| 23 | + return 0; |
| 24 | + } |
| 25 | + |
| 26 | + return a.length < b.length ? -1 : 1; |
| 27 | + }, |
| 28 | + }; |
| 29 | + |
| 30 | + const sorter = new SortingClass(callbacks); |
| 31 | + |
| 32 | + expect(sorter.sort([''])).toEqual(['']); |
| 33 | + expect(sorter.sort(['a'])).toEqual(['a']); |
| 34 | + expect(sorter.sort(['aa', 'a'])).toEqual(['a', 'aa']); |
| 35 | + expect(sorter.sort(['bb', 'aa', 'c'])).toEqual(['c', 'bb', 'aa']); |
| 36 | + } |
| 37 | +} |
0 commit comments