Back to Repositories

Testing Array Permutation Algorithm in javascript-algorithms

This test suite validates the permutateWithoutRepetitions algorithm implementation, which generates all possible permutations of an array of elements without repetitions. The tests verify permutation generation for arrays of different sizes and ensure correct factorial-based output lengths.

Test Coverage Overview

The test suite provides comprehensive coverage for permutation generation scenarios:
  • Single element permutation
  • Two-element permutation variations
  • Three and four-element complete permutation sets
  • Duplicate element handling
  • Validation of permutation count against factorial calculations

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for structured test organization. Each test case progressively validates more complex permutations, employing array equality checks and length validations against factorial calculations.

The implementation verifies both the permutation order and total count, ensuring algorithmic correctness.

Technical Details

Testing Framework: Jest
  • Array comparison using toEqual matcher
  • Length validation using toBe matcher
  • Integration with factorial calculation module
  • Structured test case organization

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Progressive complexity in test cases
  • Comprehensive edge case coverage
  • Mathematical validation through factorial calculations
  • Clear test case organization and naming
  • Effective use of Jest matchers for assertions

trekhleb/javascript-algorithms

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

            
import permutateWithoutRepetitions from '../permutateWithoutRepetitions';
import factorial from '../../../math/factorial/factorial';

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

    const permutations2 = permutateWithoutRepetitions(['A', 'B']);
    expect(permutations2.length).toBe(2);
    expect(permutations2).toEqual([
      ['A', 'B'],
      ['B', 'A'],
    ]);

    const permutations6 = permutateWithoutRepetitions(['A', 'A']);
    expect(permutations6.length).toBe(2);
    expect(permutations6).toEqual([
      ['A', 'A'],
      ['A', 'A'],
    ]);

    const permutations3 = permutateWithoutRepetitions(['A', 'B', 'C']);
    expect(permutations3.length).toBe(factorial(3));
    expect(permutations3).toEqual([
      ['A', 'B', 'C'],
      ['B', 'A', 'C'],
      ['B', 'C', 'A'],
      ['A', 'C', 'B'],
      ['C', 'A', 'B'],
      ['C', 'B', 'A'],
    ]);

    const permutations4 = permutateWithoutRepetitions(['A', 'B', 'C', 'D']);
    expect(permutations4.length).toBe(factorial(4));
    expect(permutations4).toEqual([
      ['A', 'B', 'C', 'D'],
      ['B', 'A', 'C', 'D'],
      ['B', 'C', 'A', 'D'],
      ['B', 'C', 'D', 'A'],
      ['A', 'C', 'B', 'D'],
      ['C', 'A', 'B', 'D'],
      ['C', 'B', 'A', 'D'],
      ['C', 'B', 'D', 'A'],
      ['A', 'C', 'D', 'B'],
      ['C', 'A', 'D', 'B'],
      ['C', 'D', 'A', 'B'],
      ['C', 'D', 'B', 'A'],
      ['A', 'B', 'D', 'C'],
      ['B', 'A', 'D', 'C'],
      ['B', 'D', 'A', 'C'],
      ['B', 'D', 'C', 'A'],
      ['A', 'D', 'B', 'C'],
      ['D', 'A', 'B', 'C'],
      ['D', 'B', 'A', 'C'],
      ['D', 'B', 'C', 'A'],
      ['A', 'D', 'C', 'B'],
      ['D', 'A', 'C', 'B'],
      ['D', 'C', 'A', 'B'],
      ['D', 'C', 'B', 'A'],
    ]);

    const permutations5 = permutateWithoutRepetitions(['A', 'B', 'C', 'D', 'E', 'F']);
    expect(permutations5.length).toBe(factorial(6));
  });
});