mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add test cases for sorting negative numbers and zeros.
This commit is contained in:
parent
d82958dea9
commit
93bfe97e27
@ -2,6 +2,8 @@ export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
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) {
|
||||
@ -18,6 +20,11 @@ export class SortTester {
|
||||
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) => {
|
||||
|
@ -26,6 +26,10 @@ describe('BubbleSort', () => {
|
||||
SortTester.testSortStability(BubbleSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(BubbleSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
BubbleSort,
|
||||
|
@ -6,8 +6,7 @@ export default class CountingSort extends Sort {
|
||||
* @param {number} [biggestElement]
|
||||
*/
|
||||
sort(originalArray, biggestElement = 0) {
|
||||
// Detect biggest element in array in order to build in order to build
|
||||
// number bucket array later.
|
||||
// Detect biggest element in array in order to build number bucket array later.
|
||||
let detectedBiggestElement = biggestElement;
|
||||
if (!detectedBiggestElement) {
|
||||
originalArray.forEach((element) => {
|
||||
|
@ -24,6 +24,10 @@ describe('HeapSort', () => {
|
||||
SortTester.testSortWithCustomComparator(HeapSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(HeapSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
HeapSort,
|
||||
|
@ -26,6 +26,10 @@ describe('InsertionSort', () => {
|
||||
SortTester.testSortStability(InsertionSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(InsertionSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
InsertionSort,
|
||||
|
@ -26,6 +26,10 @@ describe('MergeSort', () => {
|
||||
SortTester.testSortStability(MergeSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(MergeSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
MergeSort,
|
||||
|
@ -26,6 +26,10 @@ describe('QuickSort', () => {
|
||||
SortTester.testSortStability(QuickSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(QuickSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
QuickSort,
|
||||
|
@ -22,6 +22,10 @@ describe('QuickSortInPlace', () => {
|
||||
SortTester.testSortWithCustomComparator(QuickSortInPlace);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(QuickSortInPlace);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
QuickSortInPlace,
|
||||
|
@ -22,6 +22,10 @@ describe('SelectionSort', () => {
|
||||
SortTester.testSortWithCustomComparator(SelectionSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(SelectionSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
SelectionSort,
|
||||
|
@ -22,6 +22,10 @@ describe('ShellSort', () => {
|
||||
SortTester.testSortWithCustomComparator(ShellSort);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(ShellSort);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
ShellSort,
|
||||
|
Loading…
Reference in New Issue
Block a user