From 9421d71f10cc8412ab2ba9d20fbe318885bd95e3 Mon Sep 17 00:00:00 2001 From: tarun dhoka Date: Wed, 11 Nov 2026 08:11:50 +0530 Subject: [PATCH] test: cross-check rodCut contract for Issue #23 --- Dynamic-Programming/tests/RodCutting.test.js | 59 +++++++++++++++----- work/R50-T023/check.py | 43 ++++++++++++++ work/R50-T023/fixture_results.json | 15 +++++ work/R50-T023/test_output.log | 11 ++++ work/R50-T023/versions.json | 4 ++ 5 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 work/R50-T023/check.py create mode 100644 work/R50-T023/fixture_results.json create mode 100644 work/R50-T023/test_output.log create mode 100644 work/R50-T023/versions.json diff --git a/Dynamic-Programming/tests/RodCutting.test.js b/Dynamic-Programming/tests/RodCutting.test.js index 349ea29978..9495e4ff65 100644 --- a/Dynamic-Programming/tests/RodCutting.test.js +++ b/Dynamic-Programming/tests/RodCutting.test.js @@ -1,21 +1,52 @@ import { rodCut } from '../RodCutting' -test('Test Case 1', () => { - expect(rodCut([1, 5, 8, 9, 10, 17, 17, 20], 8)).toBe(22) -}) +describe('rodCut contract & oracle cross-checks (Precondition established near line 6 of Dynamic-Programming/RodCutting.js)', () => { + // Required Fixtures: Smallest valid input [1], n=1 and ordinary input [1, 5, 8, 9, 10, 17, 17, 20], n=8 + test('Smallest valid input (prices = [1], n = 1)', () => { + const prices = [1] + expect(rodCut(prices, 1)).toBe(1) + }) -test('Test Case 2', () => { - expect(rodCut([1, 5, 4, 2, 1, 11, 19, 12], 8)).toBe(20) -}) + test('Ordinary input (prices = [1, 5, 8, 9, 10, 17, 17, 20], n = 8)', () => { + const prices = [1, 5, 8, 9, 10, 17, 17, 20] + expect(rodCut(prices, 8)).toBe(22) + }) -test('Test Case 3', () => { - expect(rodCut([1, 2, 1], 3)).toBe(3) -}) + test('Preserves input array immutability', () => { + const prices = [1, 5, 8, 9, 10, 17, 17, 20] + const pricesCopy = [...prices] + rodCut(prices, 8) + expect(prices).toEqual(pricesCopy) + }) -test('Test Case 4', () => { - expect(rodCut([5, 4, 3, 2, 1], 5)).toBe(25) -}) + // 20-Domain Explicit Reference Cross-Check Table + const domainTable = [ + { prices: [1], n: 1, expected: 1 }, + { prices: [2], n: 1, expected: 2 }, + { prices: [1, 5], n: 2, expected: 5 }, + { prices: [3, 5], n: 2, expected: 6 }, + { prices: [1, 5, 8], n: 3, expected: 8 }, + { prices: [3, 2, 1], n: 3, expected: 9 }, + { prices: [1, 2, 1], n: 3, expected: 3 }, + { prices: [1, 5, 8, 9], n: 4, expected: 10 }, + { prices: [2, 5, 7, 8], n: 4, expected: 10 }, + { prices: [5, 4, 3, 2, 1], n: 5, expected: 25 }, + { prices: [1, 5, 8, 9, 10], n: 5, expected: 13 }, + { prices: [1, 5, 8, 9, 10, 17], n: 6, expected: 17 }, + { prices: [2, 3, 7, 8, 10, 12], n: 6, expected: 14 }, + { prices: [1, 5, 8, 9, 10, 17, 17], n: 7, expected: 18 }, + { prices: [3, 5, 8, 9, 10, 17, 18], n: 7, expected: 21 }, + { prices: [1, 5, 4, 2, 1, 11, 19, 12], n: 8, expected: 20 }, + { prices: [1, 5, 8, 9, 10, 17, 17, 20], n: 8, expected: 22 }, + { prices: [3, 5, 8, 8, 10, 16, 14, 19], n: 8, expected: 24 }, + { prices: [1, 5, 8, 9, 10, 17, 17, 20, 24], n: 9, expected: 25 }, + { prices: [2, 5, 8, 9, 10, 17, 17, 20, 24, 30], n: 10, expected: 30 } + ] -test('Test Case 5', () => { - expect(rodCut([3, 5, 8, 8, 10, 16, 14, 19], 8)).toBe(24) + test.each(domainTable)( + 'rodCut($prices, $n) matches expected $expected', + ({ prices, n, expected }) => { + expect(rodCut(prices, n)).toBe(expected) + } + ) }) diff --git a/work/R50-T023/check.py b/work/R50-T023/check.py new file mode 100644 index 0000000000..e70731c115 --- /dev/null +++ b/work/R50-T023/check.py @@ -0,0 +1,43 @@ +import os +import subprocess +import json +import sys + +def run_checks(): + work_dir = "work/R50-T023" + os.makedirs(work_dir, exist_ok=True) + + # 1. Record tool versions + node_ver = subprocess.check_output(["node", "-v"]).decode().strip() + npm_ver = subprocess.check_output(["npm", "-v"]).decode().strip() + with open(f"{work_dir}/versions.json", "w") as f: + json.dump({"node": node_ver, "npm": npm_ver}, f, indent=2) + + # 2. Run Vitest in non-watch mode + cmd = ["npx", "vitest", "run", "Dynamic-Programming/test/RodCutting.test.js"] + res = subprocess.run(cmd, capture_output=True, text=True) + + # Fallback to tests/ path if test/ path was not targeted + if res.returncode != 0: + cmd = ["npx", "vitest", "run", "Dynamic-Programming/tests/RodCutting.test.js"] + res = subprocess.run(cmd, capture_output=True, text=True) + + with open(f"{work_dir}/test_output.log", "w") as f: + f.write(res.stdout + "\n" + res.stderr) + + if res.returncode == 0: + with open(f"{work_dir}/fixture_results.json", "w") as f: + json.dump({ + "status": "PASSED", + "task": "R50-T023", + "precondition_line": "Dynamic-Programming/RodCutting.js:6", + "smallest_valid_input": {"prices": [1], "n": 1, "expected": 1}, + "ordinary_input": {"prices": [1, 5, 8, 9, 10, 17, 17, 20], "n": 8, "expected": 22} + }, f, indent=2) + print("All checks passed.") + else: + print("Tests failed!") + sys.exit(1) + +if __name__ == "__main__": + run_checks() diff --git a/work/R50-T023/fixture_results.json b/work/R50-T023/fixture_results.json new file mode 100644 index 0000000000..654c84a5ee --- /dev/null +++ b/work/R50-T023/fixture_results.json @@ -0,0 +1,15 @@ +{ + "status": "PASSED", + "task": "R50-T023", + "precondition_line": "Dynamic-Programming/RodCutting.js:6", + "smallest_valid_input": { + "prices": [1], + "n": 1, + "expected": 1 + }, + "ordinary_input": { + "prices": [1, 5, 8, 9, 10, 17, 17, 20], + "n": 8, + "expected": 22 + } +} diff --git a/work/R50-T023/test_output.log b/work/R50-T023/test_output.log new file mode 100644 index 0000000000..ae8560254c --- /dev/null +++ b/work/R50-T023/test_output.log @@ -0,0 +1,11 @@ + + RUN  v5.1.0 /Users/fabulous_tarun8/Library/Mobile Documents/com~apple~CloudDocs/Studies/s&ai/diwaliHomework/TheAlgorithms-JavaScript + + ✓ Dynamic-Programming/tests/RodCutting.test.js (23 tests) 2ms + + Test Files  1 passed (1) + Tests  23 passed (23) + Start at  08:11:35 + Duration  86ms (transform 47%, import 25%, worker 16%, tests 12%) + + diff --git a/work/R50-T023/versions.json b/work/R50-T023/versions.json new file mode 100644 index 0000000000..6eb3953a49 --- /dev/null +++ b/work/R50-T023/versions.json @@ -0,0 +1,4 @@ +{ + "node": "v26.12.1", + "npm": "11.20.0" +}