Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions Dynamic-Programming/tests/RodCutting.test.js
Original file line number Diff line number Diff line change
@@ -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)
}
)
})
43 changes: 43 additions & 0 deletions work/R50-T023/check.py
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions work/R50-T023/fixture_results.json
Original file line number Diff line number Diff line change
@@ -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
}
}
11 changes: 11 additions & 0 deletions work/R50-T023/test_output.log
Original file line number Diff line number Diff line change
@@ -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%)


4 changes: 4 additions & 0 deletions work/R50-T023/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"node": "v26.12.1",
"npm": "11.20.0"
}