Skip to content

R50-T010: Strengthen the tests of OpenKnightTour (KnightTour.js) [Backtracking/KnightTour.js] - #108

Open
stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T010
Open

stavansomaiya wants to merge 1 commit into
S-AI-Projects:masterfrom
stavansomaiya:student/e26b07f1064/R50-T010

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T010 · task 10 of 10 · Mutation-resistant tests
Issue #10 · Strengthen the tests of OpenKnightTour
Student Stavan Somaiya · URN e26b07f1064
File → function Backtracking/KnightTour.js → OpenKnightTour (line 3 at the pinned commit, line 3 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · KnightTour.js#L3
Branch stavansomaiya:student/e26b07f1064/R50-T010 → S-AI-Projects:master (one commit on 5c39e87)
Outcome Two plausible one-line mutants are caught by named tests. The implementation is unchanged (comment only).

Summary

Adds mutation-resistant regressions for OpenKnightTour in Backtracking/KnightTour.js. Plausible one-line mistakes, applied only in temporary copies, pass the old tests on small inputs but fail the new ones for a semantic reason. The original passes them all.

Files changed

File Lines What changed
Backtracking/KnightTour.js +5 / −1 comment documenting the protected backtracking condition (no logic change)
Backtracking/tests/KnightTour.test.js +92 / −0 5 invariant tests with an independent tour checker
work/R50-T010/ (8 files) evidence check.py runner, README.md write-up, evidence.mjs, results.json and logs; coursework only, kept out of any upstream PR

Related Issue

Fixes: #10

This PR covers only R50-T010. Each of the 10 tasks has its own branch, commit and PR; see All 10 tasks under Additional Notes.

Type of Change

  • Bug Fix
  • New Feature
  • UI/UX Improvement
  • Performance Improvement
  • Tests
  • Documentation
  • Refactoring / Maintenance
  • CI/CD

Changes Made

  • Backtracking/tests/KnightTour.test.js: an independent tour checker (1..n² each exactly once, every step a knight move; it calls no OpenKnightTour method). Tests cover n = 1 (smallest), the 3×3 failed search leaving an all-zero board, the 5×5 solution being a valid open tour, getMoves keeping moves onto row 0 / column 0 (reduced to getMoves([1, 2]) on 3×3), and two fresh instances both producing the same explicit board.
  • Backtracking/KnightTour.js: a comment documenting the protected backtracking condition. No logic change, no mutant, no test-only branch.
  • work/R50-T010/: check.py (runs both mutants), evidence.mjs and results.

Screenshots / Recordings

There is no UI, so the recording is the terminal output of a fresh run on 23 Nov 2026. The run used a clean checkout of this branch with a new npm ci from the lockfile. The output is copied verbatim; only the temporary checkout path is replaced by a label.

Focused tests: npx vitest run … --reporter=verbose, 7/7 passed
$ npx vitest run Backtracking/tests/KnightTour.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T010>
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour > OpenKnightTour(5)
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour > OpenKnightTour(2)
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour invariants > smallest board 1x1: no tour found, board untouched
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour invariants > failed search on 3x3 leaves the board all zeros
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour invariants > 5x5 solution is a valid open tour using 1..25 once each
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour invariants > getMoves keeps moves onto row 0 and column 0
 ✓ Backtracking/tests/KnightTour.test.js > OpenKnightTour invariants > fresh equivalent instances give identical results 396ms
 Test Files  1 passed (1)
      Tests  7 passed (7)
Automated check: python3 work/R50-T010/check.py, exit 0
$ python3 work/R50-T010/check.py
[PASS] original: smallest board n = 1
       input:    1
       expected: {"solved": false, "board": [[0]], "tourProblems": null}
       actual:   {"solved": false, "board": [[0]], "tourProblems": null}
       source:   KnightTour.js:38 start square is not numbered (curr = 0)
[PASS] original: n = 3 failed search leaves zeros
       input:    3
       expected: {"solved": false, "board": [[0, 0, 0], [0, 0, 0], [0, 0, 0]], "tourProblems": null}
       actual:   {"solved": false, "board": [[0, 0, 0], [0, 0, 0], [0, 0, 0]], "tourProblems": null}
       source:   KnightTour.js:58 backtracking reset
[PASS] original: n = 5 is a valid tour
       input:    5
       expected: {"solved": true, "tourProblems": 0}
       actual:   {"solved": true, "tourProblems": 0}
       source:   KnightTour.js:24-26 in-bounds moves only; :58 reset
[PASS] original: getMoves([1, 2]) on 3x3
       input:    [3, [1, 2]]
       expected: [[2, 0], [0, 0]]
       actual:   [[2, 0], [0, 0]]
       source:   KnightTour.js:25 y >= 0 keeps row 0
[PASS] mutant (no reset): n = 3 board
       input:    3
       expected: {"differsFrom": [[0, 0, 0], [0, 0, 0], [0, 0, 0]]}
       actual:   [[2, 5, 8], [7, 0, 3], [4, 1, 6]]
       source:   KnightTour.js:58 removed
[PASS] mutant (no reset): n = 5 tour problems
       input:    5
       expected: {"differsFrom": 0}
       actual:   11
       source:   KnightTour.js:58 removed
[PASS] mutant (y > 0): getMoves([1, 2]) on 3x3
       input:    [3, [1, 2]]
       expected: {"differsFrom": [[2, 0], [0, 0]]}
       actual:   [[2, 0]]
       source:   KnightTour.js:25 changed
[KILLED] mutant backtracking reset removed: '        this.board[y][x] = 0\n' -> ''
       failed:  OpenKnightTour OpenKnightTour(5)
       failed:  OpenKnightTour invariants failed search on 3x3 leaves the board all zeros
       failed:  OpenKnightTour invariants 5x5 solution is a valid open tour using 1..25 once each
       failed:  OpenKnightTour invariants fresh equivalent instances give identical results
[KILLED] mutant lower row bound off by one (y > 0): '([y, x]) => y >= 0 && y < this.size' -> '([y, x]) => y > 0 && y < this.size'
       failed:  OpenKnightTour OpenKnightTour(5)
       failed:  OpenKnightTour invariants 5x5 solution is a valid open tour using 1..25 once each
       failed:  OpenKnightTour invariants getMoves keeps moves onto row 0 and column 0
       failed:  OpenKnightTour invariants fresh equivalent instances give identical results

focused tests: 7/7 passed  (node_modules/.bin/vitest run Backtracking/tests/KnightTour.test.js --reporter=json --outputFile=work/R50-T010/vitest-focused.raw.json)
npm test:      11975/11975 passed  (npm test -- --reporter=json --outputFile=work/R50-T010/vitest-full.raw.json)

R50-T010: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T010/check.py installs from the lockfile and runs the focused tests. It then prints input / expected / actual for every fixture row, runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode. It exits non-zero if any assertion or step fails, and records every command in commands.log.

To reproduce:

git fetch https://github.com/stavansomaiya/TheAlgorithms-JavaScript.git student/e26b07f1064/R50-T010
git checkout FETCH_HEAD
python3 work/R50-T010/check.py

Recorded results (committed in results.json; tool versions in versions.txt):

Check Command Result
Install from lockfile npm ci --ignore-scripts --no-audit --no-fund ✅ exit 0
Focused tests vitest run Backtracking/tests/KnightTour.test.js ✅ 7/7 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T010/evidence.mjs ✅ 7/7 rows match
Mutant (temporary copy): backtracking reset removed applied by check.py ✅ caught by failed search on 3x3 leaves the board all zeros
Mutant (temporary copy): lower row bound off by one (y > 0) applied by check.py ✅ caught by getMoves keeps moves onto row 0 and column 0
Repository test script, non-watch npm test (vitest run) ✅ 11975/11975 passed
Code style npm run check-style (Prettier) ✅ passes
Spelling (CI settings) codespell ✅ no new findings

Acceptance criteria → evidence:

# Done when (quoted from #10) Evidence in this PR
1 The checked behavior is tied to OpenKnightTour in Backtracking/KnightTour.js; every assertion has an explicit expected value. ✅ Assertions use literals: false, [[0]], an all-zero 3×3 board, [[2, 0], [0, 0]], and the explicit 5×5 board [[19,4,15,10,25],…].
2 Use the smallest valid input and one ordinary input for OpenKnightTour from Backtracking/KnightTour.js. Write the literal inputs and independently calculated expected outputs in the tests before editing the implementation. The check must name the source line establishing each precondition. ✅ Smallest: n = 1 gives solve() === false and board [[0]], because the start square is never numbered (line 38). Ordinary: n = 5 gives true with a valid open tour, verified by an independent checker. Preconditions cite lines 12-21 (move order), 25 (bounds) and 58 (reset).
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ Only a comment was added; no logic change. The 2 existing tests pass, and the full suite passes (11975/11975).
4 The original passes and the mutant fails the same named assertion for a semantic reason. ✅ Both mutants pass on the original and fail the named assertion for a semantic reason. Without the reset, failed search on 3x3 leaves the board all zeros fails (board [[2,5,8],[7,0,3],[4,1,6]]). With y > 0, getMoves keeps moves onto row 0 and column 0 fails ([[2,0]] instead of [[2,0],[0,0]]).
5 The final implementation contains no mutation or test-only branch. ✅ The only change to KnightTour.js is a comment (lines 53-57). The mutants exist only in temporary copies made by check.py.

Evidence files: check.py · README.md (contract, fixture, baseline, findings) · evidence.mjs · results.json · vitest-focused.json

Checklist

  • I have tested my changes locally.
  • I have added or updated tests, if needed.
  • I have updated documentation, if needed.
  • I have followed the project's coding guidelines.
  • I have run the relevant tests and checks.
  • I have addressed previous review feedback, if applicable. (No review feedback yet.)

Additional Notes

Removing the backtracking reset (line 58) leaves [[2,5,8],[7,0,3],[4,1,6]] on the 3×3 board and fails failed search on 3x3 leaves the board all zeros; on 5×5 it even returns true with repeated numbers. y >= 0 → y > 0 (line 25) fails getMoves keeps moves onto row 0 and column 0. An equivalent mutant (x <= this.size) was identified and rejected. Possible follow-up: n = 1 returns false because the start square is never numbered (line 38); this is kept as-is because fixing it would change every pinned 5×5 board.

Scope: the PR changes only Backtracking/KnightTour.js, its test file and work/R50-T010/. work/R50-T010/ is coursework evidence and would be removed from any later upstream PR.

All 10 tasks (one PR each)
Task Issue Branch Pull request
R50-T001: Lock down the contract of setBit #1 R50-T001 #99
R50-T002: Define the supported boundaries of checkKeywordValidity #2 R50-T002 #100
R50-T003: Cross-check getParentPosition #3 R50-T003 #101
R50-T004: Test an invariant of swap #4 R50-T004 #102
R50-T005: Make verification repeatable for newGeneration #5 R50-T005 #103
R50-T006: Simplify one responsibility in countLetters #6 R50-T006 #104
R50-T007: Publish a runnable example for DisjointSetTreeNode #7 R50-T007 #105
R50-T008: Cover the failure behavior of TrieNode #8 R50-T008 #106
R50-T009: Measure the work done by fibonacci #9 R50-T009 #107
R50-T010: Strengthen the tests of OpenKnightTour #10 R50-T010 #108 (this PR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stavan Somaiya — R50-T010: Strengthen the tests of OpenKnightTour (KnightTour.js) [Backtracking/KnightTour.js]

1 participant