Testing HeapSort Algorithm Implementation in javascript-algorithms
This test suite validates the HeapSort algorithm implementation, focusing on sorting functionality and time complexity verification. The tests ensure the algorithm correctly handles various array types and maintains expected performance characteristics.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
trekhleb/javascript-algorithms
src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js
import HeapSort from '../HeapSort';
import {
equalArr,
notSortedArr,
reverseArr,
sortedArr,
SortTester,
} from '../../SortTester';
// Complexity constants.
// These numbers don't take into account up/dow heapifying of the heap.
// Thus these numbers are higher in reality.
const SORTED_ARRAY_VISITING_COUNT = 40;
const NOT_SORTED_ARRAY_VISITING_COUNT = 40;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 40;
const EQUAL_ARRAY_VISITING_COUNT = 40;
describe('HeapSort', () => {
it('should sort array', () => {
SortTester.testSort(HeapSort);
});
it('should sort array with custom comparator', () => {
SortTester.testSortWithCustomComparator(HeapSort);
});
it('should sort negative numbers', () => {
SortTester.testNegativeNumbersSort(HeapSort);
});
it('should visit EQUAL array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
HeapSort,
equalArr,
EQUAL_ARRAY_VISITING_COUNT,
);
});
it('should visit SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
HeapSort,
sortedArr,
SORTED_ARRAY_VISITING_COUNT,
);
});
it('should visit NOT SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
HeapSort,
notSortedArr,
NOT_SORTED_ARRAY_VISITING_COUNT,
);
});
it('should visit REVERSE SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
HeapSort,
reverseArr,
REVERSE_SORTED_ARRAY_VISITING_COUNT,
);
});
});