From 41107981fb6a7a243264c9babae6ef0cfda7c4e9 Mon Sep 17 00:00:00 2001 From: Stavan Somaiya Date: Mon, 23 Nov 2026 10:10:24 +0530 Subject: [PATCH] test(KeyPriorityQueue): cross-check getParentPosition against independent references (R50-T003) Fixes: #3 --- Data-Structures/Heap/KeyPriorityQueue.js | 16 +- .../Heap/test/KeyPriorityQueue.test.js | 93 +++- work/R50-T003/README.md | 81 ++++ work/R50-T003/baseline.txt | 2 + work/R50-T003/check.py | 267 +++++++++++ work/R50-T003/commands.log | 16 + work/R50-T003/evidence.mjs | 109 +++++ work/R50-T003/results.json | 444 ++++++++++++++++++ work/R50-T003/task.json | 14 + work/R50-T003/versions.txt | 5 + work/R50-T003/vitest-focused.json | 160 +++++++ 11 files changed, 1205 insertions(+), 2 deletions(-) create mode 100644 work/R50-T003/README.md create mode 100644 work/R50-T003/baseline.txt create mode 100644 work/R50-T003/check.py create mode 100644 work/R50-T003/commands.log create mode 100644 work/R50-T003/evidence.mjs create mode 100644 work/R50-T003/results.json create mode 100644 work/R50-T003/task.json create mode 100644 work/R50-T003/versions.txt create mode 100644 work/R50-T003/vitest-focused.json diff --git a/Data-Structures/Heap/KeyPriorityQueue.js b/Data-Structures/Heap/KeyPriorityQueue.js index 420933396e..b12bd39d38 100644 --- a/Data-Structures/Heap/KeyPriorityQueue.js +++ b/Data-Structures/Heap/KeyPriorityQueue.js @@ -19,6 +19,20 @@ */ // Priority Queue Helper functions + +/** + * Returns the array index of the parent of the node stored at `position`. + * + * Example: getParentPosition(4) === 1, because node 1 has children at + * 2 * 1 + 1 = 3 and 2 * 1 + 2 = 4. Siblings share a parent: + * getParentPosition(3) === getParentPosition(4) === 1. + * + * The root (position 0) has no parent and yields -1, which callers treat as + * "no parent" via their `parentPos >= 0` checks. + * + * @param {number} position non-negative integer index into the heap array + * @returns {number} parent index, or -1 for the root + */ const getParentPosition = (position) => Math.floor((position - 1) / 2) const getChildrenPositions = (position) => [2 * position + 1, 2 * position + 2] @@ -154,4 +168,4 @@ class KeyPriorityQueue { } } -export { KeyPriorityQueue } +export { KeyPriorityQueue, getParentPosition } diff --git a/Data-Structures/Heap/test/KeyPriorityQueue.test.js b/Data-Structures/Heap/test/KeyPriorityQueue.test.js index b9e9ea6202..bb26bb6002 100644 --- a/Data-Structures/Heap/test/KeyPriorityQueue.test.js +++ b/Data-Structures/Heap/test/KeyPriorityQueue.test.js @@ -1,4 +1,4 @@ -import { KeyPriorityQueue } from '../KeyPriorityQueue.js' +import { KeyPriorityQueue, getParentPosition } from '../KeyPriorityQueue.js' describe('Key Priority Queue', () => { describe('Method isEmpty', () => { @@ -124,3 +124,94 @@ describe('Key Priority Queue', () => { }) }) }) + +// Reference comparison for getParentPosition (KeyPriorityQueue.js line 36). +describe('getParentPosition against independent references', () => { + // Reviewed explicit table, written by hand from the heap layout described + // at lines 12-16: children of p are 2p + 1 and 2p + 2, so positions + // (1, 2) -> 0, (3, 4) -> 1, (5, 6) -> 2, ... and the root has no parent (-1). + const expectedParents = [ + -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, + 11 + ] + + test.each(expectedParents.map((parent, position) => [position, parent]))( + 'explicit table: parent of position %i is %i', + (position, parent) => { + expect(getParentPosition(position)).toBe(parent) + } + ) + + // Different method: in 1-based heap numbering the parent of k is k >> 1, + // so the 0-based parent of p is ((p + 1) >> 1) - 1. No division or floor. + test('bit-shift oracle agrees for positions 0..63', () => { + const oracle = (p) => ((p + 1) >> 1) - 1 + const mismatches = [] + for (let p = 0; p < 64; p++) { + if (getParentPosition(p) !== oracle(p)) { + mismatches.push([p, getParentPosition(p), oracle(p)]) + } + } + expect(mismatches).toEqual([]) + }) + + test('representative result: getParentPosition(4) is 1', () => { + expect(getParentPosition(4)).toBe(1) + expect(getParentPosition(3)).toBe(getParentPosition(4)) + }) +}) + +describe('queue order relying on getParentPosition', () => { + // Increasing priorities make the min-heap behave first-in, first-out. + // Each step lists the explicit expected removed key and queue size; a + // plain array (independent FIFO reference) must agree after every step. + test('enqueue A, B, C; remove twice; insert D; drain -> A, B, C, D', () => { + const steps = [ + { op: 'push', key: 'A', priority: 1, size: 1 }, + { op: 'push', key: 'B', priority: 2, size: 2 }, + { op: 'push', key: 'C', priority: 3, size: 3 }, + { op: 'pop', removed: 'A', size: 2 }, + { op: 'pop', removed: 'B', size: 1 }, + { op: 'push', key: 'D', priority: 4, size: 2 }, + { op: 'pop', removed: 'C', size: 1 }, + { op: 'pop', removed: 'D', size: 0 } + ] + const queue = new KeyPriorityQueue() + const reference = [] + expect(queue.isEmpty()).toBe(true) + + const removed = [] + for (const step of steps) { + if (step.op === 'push') { + queue.push(step.key, step.priority) + reference.push(step.key) + } else { + const key = queue.pop() + expect(key).toBe(step.removed) + expect(key).toBe(reference.shift()) + removed.push(key) + } + expect(queue.priorities.size).toBe(step.size) + expect(reference.length).toBe(step.size) + expect(queue.isEmpty()).toBe(step.size === 0) + } + + expect(removed).toEqual(['A', 'B', 'C', 'D']) + expect(queue.contains('A')).toBe(false) + }) + + test('separate instances do not share contents', () => { + const first = new KeyPriorityQueue() + const second = new KeyPriorityQueue() + first.push('A', 1) + expect(first.isEmpty()).toBe(false) + expect(second.isEmpty()).toBe(true) + expect(second.contains('A')).toBe(false) + + second.push('B', 1) + expect(first.pop()).toBe('A') + expect(first.isEmpty()).toBe(true) + expect(second.contains('B')).toBe(true) + expect(second.priorities.size).toBe(1) + }) +}) diff --git a/work/R50-T003/README.md b/work/R50-T003/README.md new file mode 100644 index 0000000000..3529e26776 --- /dev/null +++ b/work/R50-T003/README.md @@ -0,0 +1,81 @@ +# R50-T003 — Cross-check `getParentPosition` against an independent reference + +Source: `Data-Structures/Heap/KeyPriorityQueue.js`, `getParentPosition` (line 36 after this change; line 22 at pinned commit `5c39e87`). + +## Recorded contract (before any change) + +| Aspect | Observation | +| --------------------- | ------------------------------------------------------------------------------------------------- | +| Input | `position`: non-negative integer index into the heap array (layout described at lines 12-16). | +| Result | `Math.floor((position - 1) / 2)`; the root (0) yields `-1`, read as "no parent" (lines 105, 126). | +| Mutation | None. Pure helper. | +| External dependencies | `Math.floor` only. | +| Public interface | `KeyPriorityQueue` unchanged; `getParentPosition` is now also exported so it can be tested. | + +## Oracles (neither calls the function under test) + +1. **Reviewed explicit table** for positions 0..24 (25 inputs), written by hand from "children of + `p` are `2p + 1` and `2p + 2`": `-1, 0, 0, 1, 1, 2, 2, …, 11, 11`. +2. **Different method**: in 1-based heap numbering the parent of `k` is `k >> 1`, so the 0-based + parent of `p` is `((p + 1) >> 1) - 1`. Compared for positions 0..63. + +Result: no mismatch on any of the 89 compared positions. No implementation change was needed. + +## Fixture (queue behaviour that depends on `getParentPosition`) + +Increasing priorities make the min-heap first-in, first-out. After **every** operation the test +checks the explicit expected key and size, and also compares with an independent reference: a +plain JavaScript array used as a FIFO queue (`push` / `shift`). Size is read from the public +`priorities` map. + +| Step | Operation | Expected removed | Expected size | +| ---- | --------- | ---------------- | ------------------------------------------------ | +| 0 | new queue | — | 0 (`isEmpty() === true`) | +| 1 | push A(1) | — | 1 | +| 2 | push B(2) | — | 2 | +| 3 | push C(3) | — | 3 | +| 4 | pop | A | 2 | +| 5 | pop | B | 1 | +| 6 | push D(4) | — | 2 | +| 7 | pop | C | 1 | +| 8 | pop | D | 0 (`isEmpty() === true`, documented empty state) | + +Removal order: A, B, C, D. Separate instances: pushing into or popping from one queue never changes +another (checked in both directions). + +## Sensitivity check (temporary copy only) + +`check.py` copies the source and test into a temp dir and replaces the formula with +`Math.floor(position / 2)`. The minimal reproducible case is `getParentPosition(0)`: expected `-1`, +mutant `0`. The named test `explicit table: parent of position 0 is -1` fails, along with the existing +push/pop tests. The real source is never modified. + +## Observation, not changed (out of scope) + +`pop()` on an empty queue returns `undefined` and stays empty (`isEmpty() === true`), but +`_swap(0, -1)` leaves a stray `"-1"` property on the internal `_heap` array. This is undocumented +behaviour that does not affect `getParentPosition`, so it is only recorded here (`baseline.txt`). + +## How to run + +From the repository root: + +```sh +python3 work/R50-T003/check.py +``` + +`check.py` installs dependencies from `package-lock.json`, runs the focused test file, evaluates the fixture with `evidence.mjs` (input, expected, actual and source line for every case), runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode (`npm test`, which is `vitest run`). It exits non-zero if any assertion or step fails, and records every command it ran in `commands.log`. + +### Recorded run (files in this folder) + +| Step | Command | Result | +| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | +| Install from lockfile | `npm ci --ignore-scripts --no-audit --no-fund` (exit 0) | ok | +| Focused tests | `node_modules/.bin/vitest run Data-Structures/Heap/test/KeyPriorityQueue.test.js --reporter=json --outputFile=work/R50-T003/vitest-focused.raw.json` | 38/38 passed | +| Fixture evidence | `node work/R50-T003/evidence.mjs` | 36/36 rows match | +| Mutant: parent formula off by one (floor(p / 2)) | temporary copy | killed by `explicit table: parent of position 0 is -1` | +| Repository test script | `npm test -- --reporter=json --outputFile=work/R50-T003/vitest-full.raw.json` | 11999/11999 passed | + +Tool versions (`versions.txt`): python 3.12.10; node v26.13.1; npm 11.20.0; platform macOS-27.2.1-arm64-arm-64bit; vitest vitest/1.2.1 darwin-arm64 node-v26.13.1. + +Files: `results.json` (all of the above, machine-readable), `vitest-focused.json` (each focused test and its status), `commands.log`, `versions.txt`. diff --git a/work/R50-T003/baseline.txt b/work/R50-T003/baseline.txt new file mode 100644 index 0000000000..eec3c4ce24 --- /dev/null +++ b/work/R50-T003/baseline.txt @@ -0,0 +1,2 @@ +fixture removal order: A,B,C,D isEmpty: true +pop on empty -> undefined isEmpty after: true heap length: 0 own keys: ["-1"] diff --git a/work/R50-T003/check.py b/work/R50-T003/check.py new file mode 100644 index 0000000000..cc670cea95 --- /dev/null +++ b/work/R50-T003/check.py @@ -0,0 +1,267 @@ +#!/usr/bin/env python3 +"""Focused check runner for R50-T003. + +Run from the repository root: + + python3 work/R50-T003/check.py + +Steps (everything is recorded under work/R50-T003/): + 1. Record tool versions -> versions.txt + 2. Install dependencies from the lockfile -> `npm ci --ignore-scripts` + (set SKIP_INSTALL=1 to reuse an existing node_modules) + 3. Run the focused Vitest file(s) for this task; require a non-zero test + count and zero failures -> vitest-focused.json + 4. Run evidence.mjs: literal fixture inputs against the real source, with + input / expected / actual / source line -> results.json ("fixture") + 5. Run each deliberate mutant in a temporary copy (never in the real + source); require the named regression test to pass on the original and + fail on the mutant -> results.json ("mutants") + 6. Run the repository test script in non-watch mode + (`npm test` = `vitest run`); require a non-zero test count and zero + failures -> results.json ("full_suite") + +Every command actually executed is written to commands.log. The exit status +is non-zero if any step or assertion fails (or a step times out). +""" + +import json +import os +import platform +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parent.parent +CONFIG = json.loads((HERE / 'task.json').read_text()) +TASK = CONFIG['task'] +VITEST = str(ROOT / 'node_modules' / '.bin' / 'vitest') +TIMEOUT_SECONDS = 600 + +failures = [] +log = [] + + +def show(text): + """Hide machine-specific absolute paths in recorded evidence.""" + text = text.replace(str(ROOT) + os.sep, '').replace(str(ROOT), '.') + return text.replace(tempfile.gettempdir(), '$TMPDIR') + + +def run(cmd, cwd=ROOT, check=False): + """Run a command, log it exactly, and never wait forever.""" + shown = show(' '.join(cmd)) + log.append('$ ' + shown) + try: + proc = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True, + timeout=TIMEOUT_SECONDS) + except subprocess.TimeoutExpired: + log.append(f' -> timed out after {TIMEOUT_SECONDS}s') + failures.append(f'timed out: {shown}') + return subprocess.CompletedProcess(cmd, 124, '', 'timeout') + log.append(f' -> exit {proc.returncode}') + if check and proc.returncode != 0: + print(proc.stdout[-4000:]) + print(proc.stderr[-4000:], file=sys.stderr) + return proc + + +def version(cmd): + try: + return run(cmd).stdout.strip().splitlines()[0] + except Exception as exc: # best effort only + return f'unavailable ({exc})' + + +def test_list(report): + return [ + {'name': t.get('fullName') or t.get('title'), 'status': t.get('status')} + for suite in report.get('testResults', []) + for t in suite.get('assertionResults', []) + ] + + +def read_report(proc, out): + if not out.exists(): + print(proc.stdout[-4000:]) + print(proc.stderr[-4000:], file=sys.stderr) + return None + report = json.loads(out.read_text()) + out.unlink() + return report + + +def summary(cmd, proc, report): + return { + 'command': show(' '.join(cmd)), + 'exit': proc.returncode, + 'total': report and report['numTotalTests'], + 'passed': report and report['numPassedTests'], + 'failed': report and report['numFailedTests'] + } + + +def require_green(label, info, report): + if info['exit'] != 0 or not report or report['numTotalTests'] == 0 or \ + report['numFailedTests'] != 0: + failures.append(f'{label} failed: {info}') + if report: + failures.extend(' failed: ' + t['name'] + for t in test_list(report) + if t['status'] == 'failed') + + +def main(): + results = {'task': TASK} + + # 1. Tool versions + versions = { + 'python': platform.python_version(), + 'node': version(['node', '--version']), + 'npm': version(['npm', '--version']), + 'platform': platform.platform() + } + + # 2. Install from the lockfile + if os.environ.get('SKIP_INSTALL') == '1': + results['install'] = 'skipped (SKIP_INSTALL=1)' + else: + cmd = ['npm', 'ci', '--ignore-scripts', '--no-audit', '--no-fund'] + proc = run(cmd, check=True) + results['install'] = {'command': ' '.join(cmd), + 'exit': proc.returncode} + if proc.returncode != 0: + failures.append('npm ci failed') + return finish(results, versions) + versions['vitest'] = version([VITEST, '--version']) + + # 3. Focused tests + out = HERE / 'vitest-focused.raw.json' + cmd = [VITEST, 'run', *CONFIG['focused_tests'], '--reporter=json', + f'--outputFile={out}'] + proc = run(cmd) + focused_report = read_report(proc, out) + results['focused_tests'] = summary(cmd, proc, focused_report) + require_green('focused tests', results['focused_tests'], focused_report) + if focused_report: + (HERE / 'vitest-focused.json').write_text(json.dumps({ + 'command': results['focused_tests']['command'], + 'numTotalTests': focused_report['numTotalTests'], + 'numPassedTests': focused_report['numPassedTests'], + 'numFailedTests': focused_report['numFailedTests'], + 'tests': test_list(focused_report) + }, indent=2) + '\n') + + # 4. Fixture evidence (input / expected / actual / source line) + proc = run(['node', str(HERE / 'evidence.mjs')]) + rows = [] + if proc.returncode != 0: + failures.append('evidence.mjs crashed: ' + show(proc.stderr[-2000:])) + else: + rows = json.loads(proc.stdout) + results['fixture'] = rows + for row in rows: + print(f"[{'PASS' if row['pass'] else 'FAIL'}] {row['name']}") + print(f" input: {json.dumps(row['input'])}") + print(f" expected: {json.dumps(row['expected'])}") + print(f" actual: {json.dumps(row['actual'])}") + if row.get('precondition'): + print(f" source: {row['precondition']}") + if not row['pass']: + failures.append(f"fixture mismatch: {row['name']}") + if not rows: + failures.append('no fixture rows were produced') + + # 5. Deliberate mutants, in temporary copies only + original_tests = test_list(focused_report) if focused_report else [] + mutants = [run_mutant(m, original_tests) for m in CONFIG.get('mutants', [])] + if mutants: + results['mutants'] = mutants + + # 6. Repository test script in non-watch mode: `npm test` (vitest run) + out = HERE / 'vitest-full.raw.json' + cmd = ['npm', 'test', '--', '--reporter=json', f'--outputFile={out}'] + proc = run(cmd) + full_report = read_report(proc, out) + results['full_suite'] = summary(cmd, proc, full_report) + require_green('repository test script', results['full_suite'], full_report) + + return finish(results, versions) + + +def run_mutant(mutant, original_tests): + """Copy the source and its tests into a temp dir, apply one edit there, + and require the named test to pass on the original and fail here.""" + original = (ROOT / mutant['file']).read_text() + named = [t for t in original_tests if mutant['must_fail'] in t['name']] + entry = { + 'name': mutant['name'], + 'file': mutant['file'], + 'edit': {'from': mutant['find'], 'to': mutant['replace']}, + 'named_test': mutant['must_fail'], + 'named_test_on_original': [t['status'] for t in named], + 'killed': False + } + if len(named) != 1 or named[0]['status'] != 'passed': + failures.append(f"mutant {mutant['name']}: named test must match " + f"exactly one passing test on the original") + return entry + if original.count(mutant['find']) != 1: + failures.append(f"mutant {mutant['name']}: edit must match once") + return entry + with tempfile.TemporaryDirectory(prefix=f'{TASK}-mutant-') as tmp: + tmp = Path(tmp) + for rel in [mutant['file'], *mutant['tests']]: + dest = tmp / rel + dest.parent.mkdir(parents=True, exist_ok=True) + shutil.copy(ROOT / rel, dest) + (tmp / mutant['file']).write_text( + original.replace(mutant['find'], mutant['replace'])) + out = tmp / 'mutant-report.json' + cmd = [VITEST, 'run', *mutant['tests'], '--root', str(tmp), + '--globals', '--reporter=json', f'--outputFile={out}'] + proc = run(cmd) + report = read_report(proc, out) + failed = [t['name'] for t in test_list(report) + if t['status'] == 'failed'] if report else [] + entry['exit'] = proc.returncode + entry['failed_tests_on_mutant'] = failed + entry['killed'] = proc.returncode != 0 and any( + mutant['must_fail'] in name for name in failed) + print(f"[{'KILLED' if entry['killed'] else 'SURVIVED'}] mutant " + f"{mutant['name']}: {mutant['find']!r} -> {mutant['replace']!r}") + for name in failed: + print(f' failed: {name}') + if not entry['killed']: + failures.append(f"mutant {mutant['name']} was not detected by " + f"'{mutant['must_fail']}'") + return entry + + +def finish(results, versions): + results['ok'] = not failures + results['failures'] = failures + (HERE / 'versions.txt').write_text( + ''.join(f'{k}: {v}\n' for k, v in versions.items())) + (HERE / 'results.json').write_text(json.dumps(results, indent=2) + '\n') + (HERE / 'commands.log').write_text('\n'.join(log) + '\n') + print() + for key, label in [('focused_tests', 'focused tests'), + ('full_suite', 'npm test')]: + if key in results: + info = results[key] + print(f"{label + ':':15}{info['passed']}/{info['total']} passed" + f" ({info['command']})") + if failures: + print('\nFAILED:') + for item in failures: + print(' - ' + item) + return 1 + print(f'\n{TASK}: all checks passed') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/work/R50-T003/commands.log b/work/R50-T003/commands.log new file mode 100644 index 0000000000..d76e24e2aa --- /dev/null +++ b/work/R50-T003/commands.log @@ -0,0 +1,16 @@ +$ node --version + -> exit 0 +$ npm --version + -> exit 0 +$ npm ci --ignore-scripts --no-audit --no-fund + -> exit 0 +$ node_modules/.bin/vitest --version + -> exit 0 +$ node_modules/.bin/vitest run Data-Structures/Heap/test/KeyPriorityQueue.test.js --reporter=json --outputFile=work/R50-T003/vitest-focused.raw.json + -> exit 0 +$ node work/R50-T003/evidence.mjs + -> exit 0 +$ node_modules/.bin/vitest run Data-Structures/Heap/test/KeyPriorityQueue.test.js --root $TMPDIR/R50-T003-mutant-ul77tw79 --globals --reporter=json --outputFile=$TMPDIR/R50-T003-mutant-ul77tw79/mutant-report.json + -> exit 1 +$ npm test -- --reporter=json --outputFile=work/R50-T003/vitest-full.raw.json + -> exit 0 diff --git a/work/R50-T003/evidence.mjs b/work/R50-T003/evidence.mjs new file mode 100644 index 0000000000..67b40831a2 --- /dev/null +++ b/work/R50-T003/evidence.mjs @@ -0,0 +1,109 @@ +// Evaluates the R50-T003 fixture against the real source and prints JSON rows. +// The oracle never calls getParentPosition: it uses a hand-written table and +// the 1-based bit-shift rule parent(k) = k >> 1. +import { + KeyPriorityQueue, + getParentPosition +} from '../../Data-Structures/Heap/KeyPriorityQueue.js' + +const SRC = 'KeyPriorityQueue.js' +const table = [ + -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11 +] +const rows = table.map((expected, position) => { + const actual = getParentPosition(position) + return { + name: `getParentPosition(${position}) vs explicit table`, + input: position, + expected, + actual, + pass: actual === expected, + precondition: `${SRC}:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)` + } +}) + +const shiftOracle = (p) => ((p + 1) >> 1) - 1 +const range = Array.from({ length: 64 }, (_, p) => p) +const actualRange = range.map(getParentPosition) +const expectedRange = range.map(shiftOracle) +rows.push({ + name: 'getParentPosition(0..63) vs bit-shift oracle', + input: '0..63', + expected: expectedRange, + actual: actualRange, + pass: JSON.stringify(actualRange) === JSON.stringify(expectedRange), + precondition: `${SRC}:36 getParentPosition` +}) + +// Fixture: FIFO via increasing priorities. One row per operation, compared +// with explicit expected values and with a plain-array FIFO reference. +const queue = new KeyPriorityQueue() +const reference = [] +const steps = [ + ['push', 'A', 1, null, 1], + ['push', 'B', 2, null, 2], + ['push', 'C', 3, null, 3], + ['pop', null, null, 'A', 2], + ['pop', null, null, 'B', 1], + ['push', 'D', 4, null, 2], + ['pop', null, null, 'C', 1], + ['pop', null, null, 'D', 0] +] +steps.forEach(([op, key, priority, expectedRemoved, expectedSize], i) => { + let removed = null + let referenceRemoved = null + if (op === 'push') { + queue.push(key, priority) + reference.push(key) + } else { + removed = queue.pop() + referenceRemoved = reference.shift() + } + const actual = { + removed, + size: queue.priorities.size, + referenceRemoved, + referenceSize: reference.length + } + const expected = { + removed: expectedRemoved, + size: expectedSize, + referenceRemoved: expectedRemoved, + referenceSize: expectedSize + } + rows.push({ + name: `fixture step ${i + 1}: ${ + op === 'push' ? `push ${key}(${priority})` : 'pop' + }`, + input: op === 'push' ? [op, key, priority] : [op], + expected, + actual, + pass: JSON.stringify(actual) === JSON.stringify(expected), + precondition: + op === 'push' + ? `${SRC}:59-63 push adds and shifts up` + : `${SRC}:69-75 pop removes the least priority` + }) +}) +rows.push({ + name: 'empty state after drain', + input: 'isEmpty()', + expected: true, + actual: queue.isEmpty(), + pass: queue.isEmpty() === true, + precondition: `${SRC}:50-51 isEmpty` +}) + +const a = new KeyPriorityQueue() +const b = new KeyPriorityQueue() +a.push('A', 1) +rows.push({ + name: 'separate instances do not share contents', + input: "a.push('A', 1); b untouched", + expected: { aEmpty: false, bEmpty: true }, + actual: { aEmpty: a.isEmpty(), bEmpty: b.isEmpty() }, + pass: a.isEmpty() === false && b.isEmpty() === true, + precondition: `${SRC}:41-44 constructor creates a new heap and Map` +}) + +console.log(JSON.stringify(rows)) diff --git a/work/R50-T003/results.json b/work/R50-T003/results.json new file mode 100644 index 0000000000..ffde254a53 --- /dev/null +++ b/work/R50-T003/results.json @@ -0,0 +1,444 @@ +{ + "task": "R50-T003", + "install": { + "command": "npm ci --ignore-scripts --no-audit --no-fund", + "exit": 0 + }, + "focused_tests": { + "command": "node_modules/.bin/vitest run Data-Structures/Heap/test/KeyPriorityQueue.test.js --reporter=json --outputFile=work/R50-T003/vitest-focused.raw.json", + "exit": 0, + "total": 38, + "passed": 38, + "failed": 0 + }, + "fixture": [ + { + "name": "getParentPosition(0) vs explicit table", + "input": 0, + "expected": -1, + "actual": -1, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(1) vs explicit table", + "input": 1, + "expected": 0, + "actual": 0, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(2) vs explicit table", + "input": 2, + "expected": 0, + "actual": 0, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(3) vs explicit table", + "input": 3, + "expected": 1, + "actual": 1, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(4) vs explicit table", + "input": 4, + "expected": 1, + "actual": 1, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(5) vs explicit table", + "input": 5, + "expected": 2, + "actual": 2, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(6) vs explicit table", + "input": 6, + "expected": 2, + "actual": 2, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(7) vs explicit table", + "input": 7, + "expected": 3, + "actual": 3, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(8) vs explicit table", + "input": 8, + "expected": 3, + "actual": 3, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(9) vs explicit table", + "input": 9, + "expected": 4, + "actual": 4, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(10) vs explicit table", + "input": 10, + "expected": 4, + "actual": 4, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(11) vs explicit table", + "input": 11, + "expected": 5, + "actual": 5, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(12) vs explicit table", + "input": 12, + "expected": 5, + "actual": 5, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(13) vs explicit table", + "input": 13, + "expected": 6, + "actual": 6, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(14) vs explicit table", + "input": 14, + "expected": 6, + "actual": 6, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(15) vs explicit table", + "input": 15, + "expected": 7, + "actual": 7, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(16) vs explicit table", + "input": 16, + "expected": 7, + "actual": 7, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(17) vs explicit table", + "input": 17, + "expected": 8, + "actual": 8, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(18) vs explicit table", + "input": 18, + "expected": 8, + "actual": 8, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(19) vs explicit table", + "input": 19, + "expected": 9, + "actual": 9, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(20) vs explicit table", + "input": 20, + "expected": 9, + "actual": 9, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(21) vs explicit table", + "input": 21, + "expected": 10, + "actual": 10, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(22) vs explicit table", + "input": 22, + "expected": 10, + "actual": 10, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(23) vs explicit table", + "input": 23, + "expected": 11, + "actual": 11, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(24) vs explicit table", + "input": 24, + "expected": 11, + "actual": 11, + "pass": true, + "precondition": "KeyPriorityQueue.js:15-16 children 2p+1, 2p+2; parent floor((p-1)/2)" + }, + { + "name": "getParentPosition(0..63) vs bit-shift oracle", + "input": "0..63", + "expected": [ + -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, + 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, + 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, + 29, 29, 30, 30, 31 + ], + "actual": [ + -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, + 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, + 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, + 29, 29, 30, 30, 31 + ], + "pass": true, + "precondition": "KeyPriorityQueue.js:36 getParentPosition" + }, + { + "name": "fixture step 1: push A(1)", + "input": ["push", "A", 1], + "expected": { + "removed": null, + "size": 1, + "referenceRemoved": null, + "referenceSize": 1 + }, + "actual": { + "removed": null, + "size": 1, + "referenceRemoved": null, + "referenceSize": 1 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:59-63 push adds and shifts up" + }, + { + "name": "fixture step 2: push B(2)", + "input": ["push", "B", 2], + "expected": { + "removed": null, + "size": 2, + "referenceRemoved": null, + "referenceSize": 2 + }, + "actual": { + "removed": null, + "size": 2, + "referenceRemoved": null, + "referenceSize": 2 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:59-63 push adds and shifts up" + }, + { + "name": "fixture step 3: push C(3)", + "input": ["push", "C", 3], + "expected": { + "removed": null, + "size": 3, + "referenceRemoved": null, + "referenceSize": 3 + }, + "actual": { + "removed": null, + "size": 3, + "referenceRemoved": null, + "referenceSize": 3 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:59-63 push adds and shifts up" + }, + { + "name": "fixture step 4: pop", + "input": ["pop"], + "expected": { + "removed": "A", + "size": 2, + "referenceRemoved": "A", + "referenceSize": 2 + }, + "actual": { + "removed": "A", + "size": 2, + "referenceRemoved": "A", + "referenceSize": 2 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:69-75 pop removes the least priority" + }, + { + "name": "fixture step 5: pop", + "input": ["pop"], + "expected": { + "removed": "B", + "size": 1, + "referenceRemoved": "B", + "referenceSize": 1 + }, + "actual": { + "removed": "B", + "size": 1, + "referenceRemoved": "B", + "referenceSize": 1 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:69-75 pop removes the least priority" + }, + { + "name": "fixture step 6: push D(4)", + "input": ["push", "D", 4], + "expected": { + "removed": null, + "size": 2, + "referenceRemoved": null, + "referenceSize": 2 + }, + "actual": { + "removed": null, + "size": 2, + "referenceRemoved": null, + "referenceSize": 2 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:59-63 push adds and shifts up" + }, + { + "name": "fixture step 7: pop", + "input": ["pop"], + "expected": { + "removed": "C", + "size": 1, + "referenceRemoved": "C", + "referenceSize": 1 + }, + "actual": { + "removed": "C", + "size": 1, + "referenceRemoved": "C", + "referenceSize": 1 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:69-75 pop removes the least priority" + }, + { + "name": "fixture step 8: pop", + "input": ["pop"], + "expected": { + "removed": "D", + "size": 0, + "referenceRemoved": "D", + "referenceSize": 0 + }, + "actual": { + "removed": "D", + "size": 0, + "referenceRemoved": "D", + "referenceSize": 0 + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:69-75 pop removes the least priority" + }, + { + "name": "empty state after drain", + "input": "isEmpty()", + "expected": true, + "actual": true, + "pass": true, + "precondition": "KeyPriorityQueue.js:50-51 isEmpty" + }, + { + "name": "separate instances do not share contents", + "input": "a.push('A', 1); b untouched", + "expected": { + "aEmpty": false, + "bEmpty": true + }, + "actual": { + "aEmpty": false, + "bEmpty": true + }, + "pass": true, + "precondition": "KeyPriorityQueue.js:41-44 constructor creates a new heap and Map" + } + ], + "mutants": [ + { + "name": "parent formula off by one (floor(p / 2))", + "file": "Data-Structures/Heap/KeyPriorityQueue.js", + "edit": { + "from": "const getParentPosition = (position) => Math.floor((position - 1) / 2)", + "to": "const getParentPosition = (position) => Math.floor(position / 2)" + }, + "named_test": "explicit table: parent of position 0 is -1", + "named_test_on_original": ["passed"], + "killed": true, + "exit": 1, + "failed_tests_on_mutant": [ + " Key Priority Queue Methods push and pop Test Case 1", + " Key Priority Queue Methods push and pop Test Case 2", + " Key Priority Queue Methods push and pop Test Case 3", + " Key Priority Queue Method update Update with change in position", + " getParentPosition against independent references explicit table: parent of position 0 is -1", + " getParentPosition against independent references explicit table: parent of position 2 is 0", + " getParentPosition against independent references explicit table: parent of position 4 is 1", + " getParentPosition against independent references explicit table: parent of position 6 is 2", + " getParentPosition against independent references explicit table: parent of position 8 is 3", + " getParentPosition against independent references explicit table: parent of position 10 is 4", + " getParentPosition against independent references explicit table: parent of position 12 is 5", + " getParentPosition against independent references explicit table: parent of position 14 is 6", + " getParentPosition against independent references explicit table: parent of position 16 is 7", + " getParentPosition against independent references explicit table: parent of position 18 is 8", + " getParentPosition against independent references explicit table: parent of position 20 is 9", + " getParentPosition against independent references explicit table: parent of position 22 is 10", + " getParentPosition against independent references explicit table: parent of position 24 is 11", + " getParentPosition against independent references bit-shift oracle agrees for positions 0..63", + " getParentPosition against independent references representative result: getParentPosition(4) is 1" + ] + } + ], + "full_suite": { + "command": "npm test -- --reporter=json --outputFile=work/R50-T003/vitest-full.raw.json", + "exit": 0, + "total": 11999, + "passed": 11999, + "failed": 0 + }, + "ok": true, + "failures": [] +} diff --git a/work/R50-T003/task.json b/work/R50-T003/task.json new file mode 100644 index 0000000000..30400713e7 --- /dev/null +++ b/work/R50-T003/task.json @@ -0,0 +1,14 @@ +{ + "task": "R50-T003", + "focused_tests": ["Data-Structures/Heap/test/KeyPriorityQueue.test.js"], + "mutants": [ + { + "name": "parent formula off by one (floor(p / 2))", + "file": "Data-Structures/Heap/KeyPriorityQueue.js", + "find": "const getParentPosition = (position) => Math.floor((position - 1) / 2)", + "replace": "const getParentPosition = (position) => Math.floor(position / 2)", + "tests": ["Data-Structures/Heap/test/KeyPriorityQueue.test.js"], + "must_fail": "explicit table: parent of position 0 is -1" + } + ] +} diff --git a/work/R50-T003/versions.txt b/work/R50-T003/versions.txt new file mode 100644 index 0000000000..b405ff9cb4 --- /dev/null +++ b/work/R50-T003/versions.txt @@ -0,0 +1,5 @@ +python: 3.12.10 +node: v26.13.1 +npm: 11.20.0 +platform: macOS-27.2.1-arm64-arm-64bit +vitest: vitest/1.2.1 darwin-arm64 node-v26.13.1 diff --git a/work/R50-T003/vitest-focused.json b/work/R50-T003/vitest-focused.json new file mode 100644 index 0000000000..307de189a8 --- /dev/null +++ b/work/R50-T003/vitest-focused.json @@ -0,0 +1,160 @@ +{ + "command": "node_modules/.bin/vitest run Data-Structures/Heap/test/KeyPriorityQueue.test.js --reporter=json --outputFile=work/R50-T003/vitest-focused.raw.json", + "numTotalTests": 38, + "numPassedTests": 38, + "numFailedTests": 0, + "tests": [ + { + "name": " Key Priority Queue Method isEmpty Check heap is empty", + "status": "passed" + }, + { + "name": " Key Priority Queue Method isEmpty Check heap is not empty", + "status": "passed" + }, + { + "name": " Key Priority Queue Methods push and pop Test Case 1", + "status": "passed" + }, + { + "name": " Key Priority Queue Methods push and pop Test Case 2", + "status": "passed" + }, + { + "name": " Key Priority Queue Methods push and pop Test Case 3", + "status": "passed" + }, + { + "name": " Key Priority Queue Method contains Check heap does not contain element", + "status": "passed" + }, + { + "name": " Key Priority Queue Method contains Check heap contains element", + "status": "passed" + }, + { + "name": " Key Priority Queue Method update Update without change in position", + "status": "passed" + }, + { + "name": " Key Priority Queue Method update Update with change in position", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 0 is -1", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 1 is 0", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 2 is 0", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 3 is 1", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 4 is 1", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 5 is 2", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 6 is 2", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 7 is 3", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 8 is 3", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 9 is 4", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 10 is 4", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 11 is 5", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 12 is 5", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 13 is 6", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 14 is 6", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 15 is 7", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 16 is 7", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 17 is 8", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 18 is 8", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 19 is 9", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 20 is 9", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 21 is 10", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 22 is 10", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 23 is 11", + "status": "passed" + }, + { + "name": " getParentPosition against independent references explicit table: parent of position 24 is 11", + "status": "passed" + }, + { + "name": " getParentPosition against independent references bit-shift oracle agrees for positions 0..63", + "status": "passed" + }, + { + "name": " getParentPosition against independent references representative result: getParentPosition(4) is 1", + "status": "passed" + }, + { + "name": " queue order relying on getParentPosition enqueue A, B, C; remove twice; insert D; drain -> A, B, C, D", + "status": "passed" + }, + { + "name": " queue order relying on getParentPosition separate instances do not share contents", + "status": "passed" + } + ] +}