Back to Repositories

Validating Selection Sort Algorithm Implementation in javascript-algorithms

This test suite validates the implementation of Selection Sort algorithm, focusing on array sorting functionality and algorithmic complexity verification. The tests cover basic sorting operations, custom comparator usage, and performance characteristics across different input scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of Selection Sort implementation:

  • Basic array sorting validation
  • Custom comparator functionality testing
  • Negative numbers handling
  • Time complexity verification across different array types (equal, sorted, unsorted, reverse sorted)
  • Visiting count validation for performance analysis

Implementation Analysis

The testing approach employs Jest’s describe/it pattern for structured test organization. Each test case utilizes a shared SortTester utility to validate sorting behavior and performance metrics. The implementation verifies both functional correctness and algorithmic efficiency through visiting count assertions.

Technical Details

Testing Infrastructure:

  • Framework: Jest
  • Test Utilities: Custom SortTester class
  • Complexity Constants: Predefined visiting counts (209) for different array types
  • Test Data: Pre-configured array variations (sorted, unsorted, reverse, equal)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Modular test organization using describe blocks
  • Consistent test naming conventions
  • Reusable test utilities
  • Performance metric validation
  • Edge case coverage
  • Separation of test data from test logic

trekhleb/javascript-algorithms

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

            
import SelectionSort from '../SelectionSort';
import {
  equalArr,
  notSortedArr,
  reverseArr,
  sortedArr,
  SortTester,
} from '../../SortTester';

// Complexity constants.
const SORTED_ARRAY_VISITING_COUNT = 209;
const NOT_SORTED_ARRAY_VISITING_COUNT = 209;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209;
const EQUAL_ARRAY_VISITING_COUNT = 209;

describe('SelectionSort', () => {
  it('should sort array', () => {
    SortTester.testSort(SelectionSort);
  });

  it('should sort array with custom comparator', () => {
    SortTester.testSortWithCustomComparator(SelectionSort);
  });

  it('should sort negative numbers', () => {
    SortTester.testNegativeNumbersSort(SelectionSort);
  });

  it('should visit EQUAL array element specified number of times', () => {
    SortTester.testAlgorithmTimeComplexity(
      SelectionSort,
      equalArr,
      EQUAL_ARRAY_VISITING_COUNT,
    );
  });

  it('should visit SORTED array element specified number of times', () => {
    SortTester.testAlgorithmTimeComplexity(
      SelectionSort,
      sortedArr,
      SORTED_ARRAY_VISITING_COUNT,
    );
  });

  it('should visit NOT SORTED array element specified number of times', () => {
    SortTester.testAlgorithmTimeComplexity(
      SelectionSort,
      notSortedArr,
      NOT_SORTED_ARRAY_VISITING_COUNT,
    );
  });

  it('should visit REVERSE SORTED array element specified number of times', () => {
    SortTester.testAlgorithmTimeComplexity(
      SelectionSort,
      reverseArr,
      REVERSE_SORTED_ARRAY_VISITING_COUNT,
    );
  });
});