Back to Repositories

Testing Tower of Hanoi Algorithm Implementation in javascript-algorithms

This test suite validates the implementation of the Tower of Hanoi algorithm in JavaScript, focusing on the correct movement of discs and callback execution. The tests verify the algorithm’s functionality across different numbers of discs while ensuring proper state management.

Test Coverage Overview

The test suite provides comprehensive coverage of the Tower of Hanoi implementation:

  • Tests multiple disc configurations (2, 3, and 6 discs)
  • Validates correct number of moves using 2^n-1 formula
  • Verifies final disc positions and intermediate states
  • Tests callback execution and argument passing

Implementation Analysis

The testing approach utilizes Jest’s mocking capabilities to track move operations and verify algorithm correctness. The implementation leverages Stack data structure and employs jest.fn() for callback verification, ensuring both functional correctness and proper integration with the Stack implementation.

Technical Details

  • Testing Framework: Jest
  • Data Structures: Custom Stack implementation
  • Mocking: jest.fn() for callback tracking
  • Assertions: expect().toHaveBeenCalledTimes(), toEqual(), toBe()
  • Setup: Individual test cases with varying disc counts

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for different scenarios
  • Proper setup and teardown of test data
  • Comprehensive state validation
  • Mock function usage for behavior verification
  • Progressive complexity testing (2 to 6 discs)

trekhleb/javascript-algorithms

src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js

            
import hanoiTower from '../hanoiTower';
import Stack from '../../../../data-structures/stack/Stack';

describe('hanoiTower', () => {
  it('should solve tower of hanoi puzzle with 2 discs', () => {
    const moveCallback = jest.fn();
    const numberOfDiscs = 2;

    const fromPole = new Stack();
    const withPole = new Stack();
    const toPole = new Stack();

    hanoiTower({
      numberOfDiscs,
      moveCallback,
      fromPole,
      withPole,
      toPole,
    });

    expect(moveCallback).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1);

    expect(fromPole.toArray()).toEqual([]);
    expect(toPole.toArray()).toEqual([1, 2]);

    expect(moveCallback.mock.calls[0][0]).toBe(1);
    expect(moveCallback.mock.calls[0][1]).toEqual([1, 2]);
    expect(moveCallback.mock.calls[0][2]).toEqual([]);

    expect(moveCallback.mock.calls[1][0]).toBe(2);
    expect(moveCallback.mock.calls[1][1]).toEqual([2]);
    expect(moveCallback.mock.calls[1][2]).toEqual([]);

    expect(moveCallback.mock.calls[2][0]).toBe(1);
    expect(moveCallback.mock.calls[2][1]).toEqual([1]);
    expect(moveCallback.mock.calls[2][2]).toEqual([2]);
  });

  it('should solve tower of hanoi puzzle with 3 discs', () => {
    const moveCallback = jest.fn();
    const numberOfDiscs = 3;

    hanoiTower({
      numberOfDiscs,
      moveCallback,
    });

    expect(moveCallback).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1);
  });

  it('should solve tower of hanoi puzzle with 6 discs', () => {
    const moveCallback = jest.fn();
    const numberOfDiscs = 6;

    hanoiTower({
      numberOfDiscs,
      moveCallback,
    });

    expect(moveCallback).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1);
  });
});