Skip to content

Commit 93bfe97

Browse files
committedJul 3, 2018
Add test cases for sorting negative numbers and zeros.
1 parent d82958d commit 93bfe97

File tree

10 files changed

+40
-2
lines changed

10 files changed

+40
-2
lines changed
 

‎src/algorithms/sorting/SortTester.js

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
22
export const reverseArr = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
33
export const notSortedArr = [15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19];
44
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];
57

68
export class SortTester {
79
static testSort(SortingClass) {
@@ -18,6 +20,11 @@ export class SortTester {
1820
expect(sorter.sort(equalArr)).toEqual(equalArr);
1921
}
2022

23+
static testNegativeNumbersSort(SortingClass) {
24+
const sorter = new SortingClass();
25+
expect(sorter.sort(negativeArr)).toEqual(negativeArrSorted);
26+
}
27+
2128
static testSortWithCustomComparator(SortingClass) {
2229
const callbacks = {
2330
compareCallback: (a, b) => {

‎src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describe('BubbleSort', () => {
2626
SortTester.testSortStability(BubbleSort);
2727
});
2828

29+
it('should sort negative numbers', () => {
30+
SortTester.testNegativeNumbersSort(BubbleSort);
31+
});
32+
2933
it('should visit EQUAL array element specified number of times', () => {
3034
SortTester.testAlgorithmTimeComplexity(
3135
BubbleSort,

‎src/algorithms/sorting/counting-sort/CountingSort.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ export default class CountingSort extends Sort {
66
* @param {number} [biggestElement]
77
*/
88
sort(originalArray, biggestElement = 0) {
9-
// Detect biggest element in array in order to build in order to build
10-
// number bucket array later.
9+
// Detect biggest element in array in order to build number bucket array later.
1110
let detectedBiggestElement = biggestElement;
1211
if (!detectedBiggestElement) {
1312
originalArray.forEach((element) => {

‎src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ describe('HeapSort', () => {
2424
SortTester.testSortWithCustomComparator(HeapSort);
2525
});
2626

27+
it('should sort negative numbers', () => {
28+
SortTester.testNegativeNumbersSort(HeapSort);
29+
});
30+
2731
it('should visit EQUAL array element specified number of times', () => {
2832
SortTester.testAlgorithmTimeComplexity(
2933
HeapSort,

‎src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describe('InsertionSort', () => {
2626
SortTester.testSortStability(InsertionSort);
2727
});
2828

29+
it('should sort negative numbers', () => {
30+
SortTester.testNegativeNumbersSort(InsertionSort);
31+
});
32+
2933
it('should visit EQUAL array element specified number of times', () => {
3034
SortTester.testAlgorithmTimeComplexity(
3135
InsertionSort,

‎src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describe('MergeSort', () => {
2626
SortTester.testSortStability(MergeSort);
2727
});
2828

29+
it('should sort negative numbers', () => {
30+
SortTester.testNegativeNumbersSort(MergeSort);
31+
});
32+
2933
it('should visit EQUAL array element specified number of times', () => {
3034
SortTester.testAlgorithmTimeComplexity(
3135
MergeSort,

‎src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describe('QuickSort', () => {
2626
SortTester.testSortStability(QuickSort);
2727
});
2828

29+
it('should sort negative numbers', () => {
30+
SortTester.testNegativeNumbersSort(QuickSort);
31+
});
32+
2933
it('should visit EQUAL array element specified number of times', () => {
3034
SortTester.testAlgorithmTimeComplexity(
3135
QuickSort,

‎src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ describe('QuickSortInPlace', () => {
2222
SortTester.testSortWithCustomComparator(QuickSortInPlace);
2323
});
2424

25+
it('should sort negative numbers', () => {
26+
SortTester.testNegativeNumbersSort(QuickSortInPlace);
27+
});
28+
2529
it('should visit EQUAL array element specified number of times', () => {
2630
SortTester.testAlgorithmTimeComplexity(
2731
QuickSortInPlace,

‎src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ describe('SelectionSort', () => {
2222
SortTester.testSortWithCustomComparator(SelectionSort);
2323
});
2424

25+
it('should sort negative numbers', () => {
26+
SortTester.testNegativeNumbersSort(SelectionSort);
27+
});
28+
2529
it('should visit EQUAL array element specified number of times', () => {
2630
SortTester.testAlgorithmTimeComplexity(
2731
SelectionSort,

‎src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ describe('ShellSort', () => {
2222
SortTester.testSortWithCustomComparator(ShellSort);
2323
});
2424

25+
it('should sort negative numbers', () => {
26+
SortTester.testNegativeNumbersSort(ShellSort);
27+
});
28+
2529
it('should visit EQUAL array element specified number of times', () => {
2630
SortTester.testAlgorithmTimeComplexity(
2731
ShellSort,

0 commit comments

Comments
 (0)
Please sign in to comment.