Testing Knight's Tour Algorithm Implementation in javascript-algorithms
This test suite validates the Knight’s Tour algorithm implementation, which finds a sequence of moves for a knight to visit every square on a chess board exactly once. The tests verify both successful path finding on a 5×5 board and handling of impossible scenarios on smaller boards.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
trekhleb/javascript-algorithms
src/algorithms/uncategorized/knight-tour/__test__/knightTour.test.js
import knightTour from '../knightTour';
describe('knightTour', () => {
it('should not find solution on 3x3 board', () => {
const moves = knightTour(3);
expect(moves.length).toBe(0);
});
it('should find one solution to do knight tour on 5x5 board', () => {
const moves = knightTour(5);
expect(moves.length).toBe(25);
expect(moves).toEqual([
[0, 0],
[1, 2],
[2, 0],
[0, 1],
[1, 3],
[3, 4],
[2, 2],
[4, 1],
[3, 3],
[1, 4],
[0, 2],
[1, 0],
[3, 1],
[4, 3],
[2, 4],
[0, 3],
[1, 1],
[3, 0],
[4, 2],
[2, 1],
[4, 0],
[3, 2],
[4, 4],
[2, 3],
[0, 4],
]);
});
});