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