Skip to content

Commit 9421d71

Browse files
tarun dhokatarun dhoka
authored andcommitted
test: cross-check rodCut contract for Issue #23
1 parent 5c39e87 commit 9421d71

5 files changed

Lines changed: 118 additions & 14 deletions

File tree

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
11
import { rodCut } from '../RodCutting'
22

3-
test('Test Case 1', () => {
4-
expect(rodCut([1, 5, 8, 9, 10, 17, 17, 20], 8)).toBe(22)
5-
})
3+
describe('rodCut contract & oracle cross-checks (Precondition established near line 6 of Dynamic-Programming/RodCutting.js)', () => {
4+
// Required Fixtures: Smallest valid input [1], n=1 and ordinary input [1, 5, 8, 9, 10, 17, 17, 20], n=8
5+
test('Smallest valid input (prices = [1], n = 1)', () => {
6+
const prices = [1]
7+
expect(rodCut(prices, 1)).toBe(1)
8+
})
69

7-
test('Test Case 2', () => {
8-
expect(rodCut([1, 5, 4, 2, 1, 11, 19, 12], 8)).toBe(20)
9-
})
10+
test('Ordinary input (prices = [1, 5, 8, 9, 10, 17, 17, 20], n = 8)', () => {
11+
const prices = [1, 5, 8, 9, 10, 17, 17, 20]
12+
expect(rodCut(prices, 8)).toBe(22)
13+
})
1014

11-
test('Test Case 3', () => {
12-
expect(rodCut([1, 2, 1], 3)).toBe(3)
13-
})
15+
test('Preserves input array immutability', () => {
16+
const prices = [1, 5, 8, 9, 10, 17, 17, 20]
17+
const pricesCopy = [...prices]
18+
rodCut(prices, 8)
19+
expect(prices).toEqual(pricesCopy)
20+
})
1421

15-
test('Test Case 4', () => {
16-
expect(rodCut([5, 4, 3, 2, 1], 5)).toBe(25)
17-
})
22+
// 20-Domain Explicit Reference Cross-Check Table
23+
const domainTable = [
24+
{ prices: [1], n: 1, expected: 1 },
25+
{ prices: [2], n: 1, expected: 2 },
26+
{ prices: [1, 5], n: 2, expected: 5 },
27+
{ prices: [3, 5], n: 2, expected: 6 },
28+
{ prices: [1, 5, 8], n: 3, expected: 8 },
29+
{ prices: [3, 2, 1], n: 3, expected: 9 },
30+
{ prices: [1, 2, 1], n: 3, expected: 3 },
31+
{ prices: [1, 5, 8, 9], n: 4, expected: 10 },
32+
{ prices: [2, 5, 7, 8], n: 4, expected: 10 },
33+
{ prices: [5, 4, 3, 2, 1], n: 5, expected: 25 },
34+
{ prices: [1, 5, 8, 9, 10], n: 5, expected: 13 },
35+
{ prices: [1, 5, 8, 9, 10, 17], n: 6, expected: 17 },
36+
{ prices: [2, 3, 7, 8, 10, 12], n: 6, expected: 14 },
37+
{ prices: [1, 5, 8, 9, 10, 17, 17], n: 7, expected: 18 },
38+
{ prices: [3, 5, 8, 9, 10, 17, 18], n: 7, expected: 21 },
39+
{ prices: [1, 5, 4, 2, 1, 11, 19, 12], n: 8, expected: 20 },
40+
{ prices: [1, 5, 8, 9, 10, 17, 17, 20], n: 8, expected: 22 },
41+
{ prices: [3, 5, 8, 8, 10, 16, 14, 19], n: 8, expected: 24 },
42+
{ prices: [1, 5, 8, 9, 10, 17, 17, 20, 24], n: 9, expected: 25 },
43+
{ prices: [2, 5, 8, 9, 10, 17, 17, 20, 24, 30], n: 10, expected: 30 }
44+
]
1845

19-
test('Test Case 5', () => {
20-
expect(rodCut([3, 5, 8, 8, 10, 16, 14, 19], 8)).toBe(24)
46+
test.each(domainTable)(
47+
'rodCut($prices, $n) matches expected $expected',
48+
({ prices, n, expected }) => {
49+
expect(rodCut(prices, n)).toBe(expected)
50+
}
51+
)
2152
})

‎work/R50-T023/check.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import subprocess
3+
import json
4+
import sys
5+
6+
def run_checks():
7+
work_dir = "work/R50-T023"
8+
os.makedirs(work_dir, exist_ok=True)
9+
10+
# 1. Record tool versions
11+
node_ver = subprocess.check_output(["node", "-v"]).decode().strip()
12+
npm_ver = subprocess.check_output(["npm", "-v"]).decode().strip()
13+
with open(f"{work_dir}/versions.json", "w") as f:
14+
json.dump({"node": node_ver, "npm": npm_ver}, f, indent=2)
15+
16+
# 2. Run Vitest in non-watch mode
17+
cmd = ["npx", "vitest", "run", "Dynamic-Programming/test/RodCutting.test.js"]
18+
res = subprocess.run(cmd, capture_output=True, text=True)
19+
20+
# Fallback to tests/ path if test/ path was not targeted
21+
if res.returncode != 0:
22+
cmd = ["npx", "vitest", "run", "Dynamic-Programming/tests/RodCutting.test.js"]
23+
res = subprocess.run(cmd, capture_output=True, text=True)
24+
25+
with open(f"{work_dir}/test_output.log", "w") as f:
26+
f.write(res.stdout + "\n" + res.stderr)
27+
28+
if res.returncode == 0:
29+
with open(f"{work_dir}/fixture_results.json", "w") as f:
30+
json.dump({
31+
"status": "PASSED",
32+
"task": "R50-T023",
33+
"precondition_line": "Dynamic-Programming/RodCutting.js:6",
34+
"smallest_valid_input": {"prices": [1], "n": 1, "expected": 1},
35+
"ordinary_input": {"prices": [1, 5, 8, 9, 10, 17, 17, 20], "n": 8, "expected": 22}
36+
}, f, indent=2)
37+
print("All checks passed.")
38+
else:
39+
print("Tests failed!")
40+
sys.exit(1)
41+
42+
if __name__ == "__main__":
43+
run_checks()

‎work/R50-T023/fixture_results.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"status": "PASSED",
3+
"task": "R50-T023",
4+
"precondition_line": "Dynamic-Programming/RodCutting.js:6",
5+
"smallest_valid_input": {
6+
"prices": [1],
7+
"n": 1,
8+
"expected": 1
9+
},
10+
"ordinary_input": {
11+
"prices": [1, 5, 8, 9, 10, 17, 17, 20],
12+
"n": 8,
13+
"expected": 22
14+
}
15+
}

‎work/R50-T023/test_output.log‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
 RUN  v5.1.0 /Users/fabulous_tarun8/Library/Mobile Documents/com~apple~CloudDocs/Studies/s&ai/diwaliHomework/TheAlgorithms-JavaScript
3+
4+
✓ Dynamic-Programming/tests/RodCutting.test.js (23 tests) 2ms
5+
6+
 Test Files  1 passed (1)
7+
 Tests  23 passed (23)
8+
 Start at  08:11:35
9+
 Duration  86ms (transform 47%, import 25%, worker 16%, tests 12%)
10+
11+

‎work/R50-T023/versions.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"node": "v26.12.1",
3+
"npm": "11.20.0"
4+
}

0 commit comments

Comments
 (0)