|
| 1 | +export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; |
| 2 | +export const reverseArr = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; |
| 3 | +export const notSortedArr = [15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19]; |
| 4 | +export const equalArr = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; |
| 5 | +export const negativeArr = [-1, 0, 5, -10, 20, 13, -7, 3, 2, -3]; |
| 6 | +export const negativeArrSorted = [-10, -7, -3, -1, 0, 2, 3, 5, 13, 20]; |
| 7 | + |
| 8 | +export class SortTester { |
| 9 | + static testSort(SortingClass) { |
| 10 | + const sorter = new SortingClass(); |
| 11 | + |
| 12 | + expect(sorter.sort([])).toEqual([]); |
| 13 | + expect(sorter.sort([1])).toEqual([1]); |
| 14 | + expect(sorter.sort([1, 2])).toEqual([1, 2]); |
| 15 | + expect(sorter.sort([2, 1])).toEqual([1, 2]); |
| 16 | + expect(sorter.sort([3, 4, 2, 1, 0, 0, 4, 3, 4, 2])).toEqual([0, 0, 1, 2, 2, 3, 3, 4, 4, 4]); |
| 17 | + expect(sorter.sort(sortedArr)).toEqual(sortedArr); |
| 18 | + expect(sorter.sort(reverseArr)).toEqual(sortedArr); |
| 19 | + expect(sorter.sort(notSortedArr)).toEqual(sortedArr); |
| 20 | + expect(sorter.sort(equalArr)).toEqual(equalArr); |
| 21 | + } |
| 22 | + |
| 23 | + static testNegativeNumbersSort(SortingClass) { |
| 24 | + const sorter = new SortingClass(); |
| 25 | + expect(sorter.sort(negativeArr)).toEqual(negativeArrSorted); |
| 26 | + } |
| 27 | + |
| 28 | + static testSortWithCustomComparator(SortingClass) { |
| 29 | + const callbacks = { |
| 30 | + compareCallback: (a, b) => { |
| 31 | + if (a.length === b.length) { |
| 32 | + return 0; |
| 33 | + } |
| 34 | + return a.length < b.length ? -1 : 1; |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + const sorter = new SortingClass(callbacks); |
| 39 | + |
| 40 | + expect(sorter.sort([''])).toEqual(['']); |
| 41 | + expect(sorter.sort(['a'])).toEqual(['a']); |
| 42 | + expect(sorter.sort(['aa', 'a'])).toEqual(['a', 'aa']); |
| 43 | + expect(sorter.sort(['aa', 'q', 'bbbb', 'ccc'])).toEqual(['q', 'aa', 'ccc', 'bbbb']); |
| 44 | + expect(sorter.sort(['aa', 'aa'])).toEqual(['aa', 'aa']); |
| 45 | + } |
| 46 | + |
| 47 | + static testSortStability(SortingClass) { |
| 48 | + const callbacks = { |
| 49 | + compareCallback: (a, b) => { |
| 50 | + if (a.length === b.length) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + return a.length < b.length ? -1 : 1; |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + const sorter = new SortingClass(callbacks); |
| 58 | + |
| 59 | + expect(sorter.sort(['bb', 'aa', 'c'])).toEqual(['c', 'bb', 'aa']); |
| 60 | + expect(sorter.sort(['aa', 'q', 'a', 'bbbb', 'ccc'])).toEqual(['q', 'a', 'aa', 'ccc', 'bbbb']); |
| 61 | + } |
| 62 | + |
| 63 | + static testAlgorithmTimeComplexity(SortingClass, arrayToBeSorted, numberOfVisits) { |
| 64 | + const visitingCallback = jest.fn(); |
| 65 | + const callbacks = { visitingCallback }; |
| 66 | + const sorter = new SortingClass(callbacks); |
| 67 | + |
| 68 | + sorter.sort(arrayToBeSorted); |
| 69 | + |
| 70 | + expect(visitingCallback).toHaveBeenCalledTimes(numberOfVisits); |
| 71 | + } |
| 72 | +} |
0 commit comments