Back to Repositories

Testing Permutation Generation with Repetition in javascript-algorithms

This test suite validates the permutateWithRepetitions algorithm implementation, which generates all possible permutations of elements with repetition allowed. The tests verify the algorithm’s ability to handle inputs of varying sizes and ensure correct permutation generation.

Test Coverage Overview

The test suite provides comprehensive coverage of the permutateWithRepetitions function across different input sizes.

  • Tests single element input [‘A’]
  • Validates two-element input [‘A’, ‘B’]
  • Verifies three-element input [‘A’, ‘B’, ‘C’]
  • Confirms four-element input length calculation

Implementation Analysis

The testing approach employs Jest’s describe/it pattern for structured test organization. Each test case progressively increases complexity, validating both specific permutation outputs and mathematical properties of the results.

The implementation uses array comparison and length verification to ensure correct permutation generation across different input sizes.

Technical Details

  • Testing Framework: Jest
  • Test Pattern: Unit Testing
  • Assertion Methods: toEqual for array comparison, toBe for length validation
  • Data Structures: Arrays for input and expected outputs

Best Practices Demonstrated

The test suite exhibits several testing best practices including progressive complexity scaling, explicit expected outputs, and mathematical validation.

  • Systematic test case organization
  • Complete output validation for smaller inputs
  • Mathematical validation for larger inputs
  • Clear test descriptions

trekhleb/javascript-algorithms

src/algorithms/sets/permutations/__test__/permutateWithRepetitions.test.js

            
import permutateWithRepetitions from '../permutateWithRepetitions';

describe('permutateWithRepetitions', () => {
  it('should permutate string with repetition', () => {
    const permutations1 = permutateWithRepetitions(['A']);
    expect(permutations1).toEqual([
      ['A'],
    ]);

    const permutations2 = permutateWithRepetitions(['A', 'B']);
    expect(permutations2).toEqual([
      ['A', 'A'],
      ['A', 'B'],
      ['B', 'A'],
      ['B', 'B'],
    ]);

    const permutations3 = permutateWithRepetitions(['A', 'B', 'C']);
    expect(permutations3).toEqual([
      ['A', 'A', 'A'],
      ['A', 'A', 'B'],
      ['A', 'A', 'C'],
      ['A', 'B', 'A'],
      ['A', 'B', 'B'],
      ['A', 'B', 'C'],
      ['A', 'C', 'A'],
      ['A', 'C', 'B'],
      ['A', 'C', 'C'],
      ['B', 'A', 'A'],
      ['B', 'A', 'B'],
      ['B', 'A', 'C'],
      ['B', 'B', 'A'],
      ['B', 'B', 'B'],
      ['B', 'B', 'C'],
      ['B', 'C', 'A'],
      ['B', 'C', 'B'],
      ['B', 'C', 'C'],
      ['C', 'A', 'A'],
      ['C', 'A', 'B'],
      ['C', 'A', 'C'],
      ['C', 'B', 'A'],
      ['C', 'B', 'B'],
      ['C', 'B', 'C'],
      ['C', 'C', 'A'],
      ['C', 'C', 'B'],
      ['C', 'C', 'C'],
    ]);

    const permutations4 = permutateWithRepetitions(['A', 'B', 'C', 'D']);
    expect(permutations4.length).toBe(4 * 4 * 4 * 4);
  });
});