Skip to content

R50-T001: Lock down the contract of setBit (SetBit.js) [Bit-Manipulation/SetBit.js] - #99

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

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

Conversation

@stavansomaiya

@stavansomaiya stavansomaiya commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Task R50-T001 · task 1 of 10 · Contract regression tests
Issue #1 · Lock down the contract of setBit
Student Stavan Somaiya · URN e26b07f1064
File → function Bit-Manipulation/SetBit.js → setBit (line 29 at the pinned commit, line 42 after this PR)
Pinned source TheAlgorithms/JavaScript @ 5c39e87 · SetBit.js#L29
Branch stavansomaiya:student/e26b07f1064/R50-T001 → S-AI-Projects:master (one commit on 5c39e87)
Outcome No defect found. The implementation is unchanged; this PR adds a JSDoc contract and 6 contract tests.

Summary

Adds focused contract regression tests for setBit in Bit-Manipulation/SetBit.js, plus a concise input/output contract in its JSDoc. Expected values were calculated by hand in binary before running the source, and every precondition is tied to a source line. Return values and argument mutation are tested separately.

Files changed

File Lines What changed
Bit-Manipulation/SetBit.js +13 / −0 JSDoc input/output contract beside setBit (no logic change)
Bit-Manipulation/test/SetBit.test.js +43 / −0 6 new contract tests in describe('setBit contract')
work/R50-T001/ (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: #1

This PR covers only R50-T001. 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

  • Bit-Manipulation/test/SetBit.test.js: 6 new tests. Smallest valid input setBit(0, 0) === 1; ordinary input setBit(5, 1) === 7; repeated value (bit already set) setBit(7, 1) === 7; 32-bit domain boundaries setBit(0, 30) === 1073741824 and setBit(0, 31) === -2147483648; arguments left unchanged.
  • Bit-Manipulation/SetBit.js: JSDoc contract (zero-based position 0..31, signed 32-bit result, idempotent, pure) with two examples. No change to the implementation.
  • work/R50-T001/: check.py runner, evidence.mjs (input / expected / actual / source line), recorded results, tool versions and command log.

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, 10/10 passed
$ npx vitest run Bit-Manipulation/test/SetBit.test.js --reporter=verbose
 RUN  v1.2.1 <clean checkout of student/e26b07f1064/R50-T001>
 ✓ Bit-Manipulation/test/SetBit.test.js > Set bit number 0 in 1:
 ✓ Bit-Manipulation/test/SetBit.test.js > Set bit number 0 in 2:
 ✓ Bit-Manipulation/test/SetBit.test.js > Set bit number 1 in 10:
 ✓ Bit-Manipulation/test/SetBit.test.js > Set bit number 2 in 10:
 ✓ Bit-Manipulation/test/SetBit.test.js > setBit contract > smallest valid input: setBit(0, 0) sets bit 0 of zero
 ✓ Bit-Manipulation/test/SetBit.test.js > setBit contract > ordinary input: setBit(5, 1) turns 0b101 into 0b111
 ✓ Bit-Manipulation/test/SetBit.test.js > setBit contract > repeated value: setting a bit that is already set is a no-op
 ✓ Bit-Manipulation/test/SetBit.test.js > setBit contract > domain boundary: bit 30 is the highest positive bit
 ✓ Bit-Manipulation/test/SetBit.test.js > setBit contract > domain boundary: bit 31 is the 32-bit sign bit
 ✓ Bit-Manipulation/test/SetBit.test.js > setBit contract > returns a new value and leaves its arguments unchanged
 Test Files  1 passed (1)
      Tests  10 passed (10)
Automated check: python3 work/R50-T001/check.py, exit 0
$ python3 work/R50-T001/check.py
[PASS] smallest valid input
       input:    [0, 0]
       expected: 1
       actual:   1
       source:   SetBit.js:36 bitPosition is zero based (0 is smallest)
[PASS] ordinary input (0b101 -> 0b111)
       input:    [5, 1]
       expected: 7
       actual:   7
       source:   SetBit.js:36 bitPosition is zero based
[PASS] repeated value: bit already set
       input:    [7, 1]
       expected: 7
       actual:   7
       source:   SetBit.js:13 1 | 1 -> 1
[PASS] boundary: highest positive bit
       input:    [0, 30]
       expected: 1073741824
       actual:   1073741824
       source:   SetBit.js:6-7 operands are 32-bit integers
[PASS] boundary: sign bit
       input:    [0, 31]
       expected: -2147483648
       actual:   -2147483648
       source:   SetBit.js:6-7 operands are 32-bit integers
[PASS] arguments unchanged after call
       input:    [5, 1]
       expected: [5, 1]
       actual:   [5, 1]
       source:   SetBit.js:42-44 returns a new value only
[KILLED] mutant toggles instead of sets (| replaced by ^): '  return number | (1 << bitPosition)\n' -> '  return number ^ (1 << bitPosition)\n'
       failed:  Set bit number 0 in 1:
       failed:  Set bit number 1 in 10:
       failed:  setBit contract repeated value: setting a bit that is already set is a no-op

focused tests: 10/10 passed  (node_modules/.bin/vitest run Bit-Manipulation/test/SetBit.test.js --reporter=json --outputFile=work/R50-T001/vitest-focused.raw.json)
npm test:      11976/11976 passed  (npm test -- --reporter=json --outputFile=work/R50-T001/vitest-full.raw.json)

R50-T001: all checks passed
(exit 0)

Testing

Automated check: python3 work/R50-T001/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-T001
git checkout FETCH_HEAD
python3 work/R50-T001/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 Bit-Manipulation/test/SetBit.test.js ✅ 10/10 passed
Fixture evidence (input / expected / actual / source line) node work/R50-T001/evidence.mjs ✅ 6/6 rows match
Mutant (temporary copy): toggles instead of sets (| replaced by ^) applied by check.py ✅ caught by repeated value: setting a bit that is already set is a no-op
Repository test script, non-watch npm test (vitest run) ✅ 11976/11976 passed
Code style npm run check-style (Prettier) ✅ passes
Spelling (CI settings) codespell ✅ no new findings

Acceptance criteria → evidence:

# Done when (quoted from #1) Evidence in this PR
1 The checked behavior is tied to setBit in Bit-Manipulation/SetBit.js; every assertion has an explicit expected value. ✅ All 6 new tests (describe('setBit contract')) call setBit directly and compare with literals: 1, 7, 7, 1073741824, -2147483648, and 5 / 1 for the arguments.
2 Use the smallest valid input and one ordinary input for setBit from Bit-Manipulation/SetBit.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 valid setBit(0, 0) → 1; ordinary setBit(5, 1) → 7 (0b101 | 0b010). Both were worked out in binary by hand; the implementation was never edited. Every fixture row in evidence.mjs names its source line: SetBit.js:36 (zero-based position), SetBit.js:6-7 (32-bit operands), SetBit.js:13, SetBit.js:42-44.
3 Existing supported behavior and the public interface are preserved unless a failing regression demonstrates and documents a defect. ✅ Only JSDoc was added; the return line is unchanged. The 4 existing tests pass, and the full suite passes (11976/11976).
4 The tests cover ordinary input, a smallest supported input and a repeated-value or equivalent domain boundary; each reports expected and actual results. ✅ Ordinary (5, 1), smallest (0, 0), repeated value (7, 1) → 7 (bit already set), plus the 32-bit boundaries bit 30 and bit 31. check.py prints input / expected / actual for each row (6/6 match; see the terminal output above).

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

Baseline: every hand-calculated value matched the unmodified implementation, so no defect was found. Sensitivity check: check.py replaces | with ^ (toggle instead of set) in a temporary copy, and the named test repeated value: setting a bit that is already set is a no-op fails on it.

Scope: the PR changes only Bit-Manipulation/SetBit.js, its test file and work/R50-T001/. work/R50-T001/ 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 (this PR)
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

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-T001: Lock down the contract of setBit (SetBit.js) [Bit-Manipulation/SetBit.js]

1 participant