Testing BubbleSort Algorithm Implementation in javascript-algorithms
This test suite comprehensively validates the BubbleSort algorithm implementation using Jest testing framework. It covers basic sorting functionality, custom comparators, stability testing, and performance characteristics through various array configurations.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
trekhleb/javascript-algorithms
src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js
import BubbleSort from '../BubbleSort';
import {
equalArr,
notSortedArr,
reverseArr,
sortedArr,
SortTester,
} from '../../SortTester';
// Complexity constants.
const SORTED_ARRAY_VISITING_COUNT = 20;
const NOT_SORTED_ARRAY_VISITING_COUNT = 189;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209;
const EQUAL_ARRAY_VISITING_COUNT = 20;
describe('BubbleSort', () => {
it('should sort array', () => {
SortTester.testSort(BubbleSort);
});
it('should sort array with custom comparator', () => {
SortTester.testSortWithCustomComparator(BubbleSort);
});
it('should do stable sorting', () => {
SortTester.testSortStability(BubbleSort);
});
it('should sort negative numbers', () => {
SortTester.testNegativeNumbersSort(BubbleSort);
});
it('should visit EQUAL array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BubbleSort,
equalArr,
EQUAL_ARRAY_VISITING_COUNT,
);
});
it('should visit SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BubbleSort,
sortedArr,
SORTED_ARRAY_VISITING_COUNT,
);
});
it('should visit NOT SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BubbleSort,
notSortedArr,
NOT_SORTED_ARRAY_VISITING_COUNT,
);
});
it('should visit REVERSE SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BubbleSort,
reverseArr,
REVERSE_SORTED_ARRAY_VISITING_COUNT,
);
});
});