Skip to content

Commit

Permalink
Add test cases for sorting negative numbers and zeros.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jul 3, 2018
1 parent d82958d commit 93bfe97
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/algorithms/sorting/SortTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/algorithms/sorting/counting-sort/CountingSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 4 additions & 0 deletions src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 93bfe97

Please sign in to comment.