Back to Repositories

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

The test suite provides extensive coverage of BubbleSort functionality:

  • Basic array sorting validation
  • Custom comparator implementation testing
  • Sorting stability verification
  • Negative numbers handling
  • Time complexity validation across different array types (equal, sorted, unsorted, reverse)

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. It leverages a shared SortTester utility class for consistent validation across different array scenarios. Time complexity testing is implemented through visit count verification for various array states.

Key testing patterns include:
  • Modular test case organization
  • Constant-based complexity validation
  • Reusable test utilities

Technical Details

Testing Infrastructure:

  • Framework: Jest
  • Test Utilities: Custom SortTester class
  • Array Types: equalArr, notSortedArr, reverseArr, sortedArr
  • Complexity Constants: Defined visit counts for different array states

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Separation of concerns through utility classes
  • Comprehensive edge case coverage
  • Performance validation through complexity testing
  • Consistent test structure and naming conventions
  • Reusable test components

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,
    );
  });
});