Repository navigation
R50-T004: Test an invariant of swap (OddEvenSort.js) [Sorts/OddEvenSort.js] - #102
Open
stavansomaiya wants to merge 1 commit into
Open
stavansomaiya wants to merge 1 commit into
stavansomaiya wants to merge 1 commit into
Conversation
This was referenced Nov 23, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
e26b07f1064Sorts/OddEvenSort.js→swap(line 10 at the pinned commit, line 22 after this PR)5c39e87·OddEvenSort.js#L10stavansomaiya:student/e26b07f1064/R50-T004→S-AI-Projects:master(one commit on5c39e87)Summary
Adds invariant/property tests around
swapinSorts/OddEvenSort.js.Why the invariant holds:
swaponly exchanges two positions, so the multiset of values is preserved.oddEvenSortmoves values only throughswap, and stops only when a full odd pass and a full even pass make no swap, i.e. no adjacent pair is out of order. So the output is (1) ordered, (2) a permutation of the input with the same multiplicities, and (3) the input array itself, sorted in place. The source defines an ascending interface only.Files changed
Sorts/OddEvenSort.jsswapexported; JSDoc stating the invariant (no logic change)Sorts/test/OddEvenSort.test.jsswaptests, 6 fixture tests, 5 property tests over 100 seeded caseswork/R50-T004/(8 files)check.pyrunner,README.mdwrite-up,evidence.mjs,results.jsonand logs; coursework only, kept out of any upstream PRRelated Issue
Fixes: #4
This PR covers only R50-T004. Each of the 10 tasks has its own branch, commit and PR; see All 10 tasks under Additional Notes.
Type of Change
Changes Made
Sorts/test/OddEvenSort.test.js: directswaptests covering the in-place exchange, theundefinedreturn and preserved multiplicities. The fixture covers[3,1,2],[2,2,1],[1,2,3],[3,2,1],[]and[7]. The property run has 100 cases from a fixed seed (mulberry32,20261123, length 0..12, integers -5..5). Ordering, multiplicities and the in-place return are asserted separately. Literal generated cases are pinned, two readable small cases are kept, and any failure reports the shortest failing input.Sorts/OddEvenSort.js:swapexported with JSDoc stating the invariant; JSDoc onoddEvenSortlisting the three properties. No logic change.work/R50-T004/:check.py(runs two deliberately broken temporary copies),evidence.mjsand 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 cifrom the lockfile. The output is copied verbatim; only the temporary checkout path is replaced by a label.Focused tests:
npx vitest run … --reporter=verbose, 17/17 passedAutomated check:
python3 work/R50-T004/check.py, exit 0Testing
Automated check:
python3 work/R50-T004/check.pyinstalls 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 incommands.log.To reproduce:
Recorded results (committed in
results.json; tool versions inversions.txt):npm ci --ignore-scripts --no-audit --no-fundvitest run Sorts/test/OddEvenSort.test.jsnode work/R50-T004/evidence.mjscheck.pyoutput preserves input multiplicitiescheck.pyoutput is orderednpm test(vitest run)npm run check-style(Prettier)codespellAcceptance criteria → evidence:
swapin Sorts/OddEvenSort.js; every assertion has an explicit expected value.swap exchanges two positions in place and returns undefined:swap([3, 1, 2], 0, 1)leaves[1, 3, 2]and returnsundefined. The fixture tests compare with literal arrays, and the generator test pins literal generated cases (case 0 is[4, 0, -1, -2, 0, -1, -5, 1, -4, 0, -3]).[3,1,2]→[1,2,3],[2,2,1]→[1,2,2],[1,2,3]→[1,2,3],[3,2,1]→[1,2,3], smallest[]→[], plus[7]→[7]. Each also asserts that the same array is returned. The source defines ascending order only.swapexported, JSDoc added). The 4 existing tests pass, and the full suite passes (11983/11983).>comparisons at lines 46 and 52. NoNaN, mixed types or holes. Seed20261123(mulberry32). The invariant is justified in the Description above.check.pybreaks the invariant in two temporary copies. Witharr[j] = arr[i]inswap,output preserves input multiplicitiesfails ([2, 1]→[1, 1]). Withsorted = falsedropped from the even pass,output is orderedfails ([3, 1, 2]→[1, 3, 2]).Evidence files:
check.py·README.md(contract, fixture, baseline, findings) ·evidence.mjs·results.json·vitest-focused.jsonChecklist
Additional Notes
The checks fail when the invariant is deliberately broken in a temporary copy. With
arr[j] = arr[i]inswap,output preserves input multiplicitiesfails (smallest case[2, 1]→[1, 1]). Droppingsorted = falsein the even pass makesoutput is orderedfail ([3, 1, 2]→[1, 3, 2]). The generator only produces finite integers, inside the comparable-number domain of the>comparisons, so it never relies on undefined input behaviour.Scope: the PR changes only
Sorts/OddEvenSort.js, its test file andwork/R50-T004/.work/R50-T004/is coursework evidence and would be removed from any later upstream PR.All 10 tasks (one PR each)
R50-T001R50-T002R50-T003R50-T004R50-T005R50-T006R50-T007R50-T008R50-T009R50-T010