Skip to content

Commit f8d1631

Browse files
committed
feat(R50-T031): add regression tests and JSDoc for change() in CoinChange.js
- Add focused regression tests for the function (Dynamic-Programming/CoinChange.js) covering smallest valid input (amount=0), ordinary input ([1,2,5],5), and domain boundary inputs ([2],3 → 0, [2],2 → 1). - Each assertion names the source precondition line. - Verify array is not mutated (pure function). - Enhance JSDoc on with @example, preconditions, and mutation note. - Add work/R50-T031/check.py runner with evidence.json output. Closes #46
1 parent 5c39e87 commit f8d1631

3 files changed

Lines changed: 172 additions & 2 deletions

File tree

‎Dynamic-Programming/CoinChange.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
/**
2-
* @params {Array} coins
3-
* @params {Number} amount
2+
* @function change
3+
* @description Counts the number of distinct combinations of coins that sum to `amount`.
4+
* Uses a bottom-up dynamic programming approach (unbounded knapsack variant).
5+
* - Precondition (line 7): `combinations` is initialised to length `amount + 1`, all zeros.
6+
* - Precondition (line 8): `combinations[0] = 1` establishes the base case (one way to make 0).
7+
* - The function does NOT mutate the `coins` array.
8+
* - Returns 0 when no combination is possible, 1 when amount === 0.
9+
* @param {number[]} coins - Array of distinct positive coin denominations.
10+
* @param {number} amount - Non-negative target amount.
11+
* @returns {number} Number of distinct combinations that sum to `amount`.
12+
* @example change([1, 2, 5], 5) // => 4
13+
* @example change([2], 3) // => 0 (smallest valid input that yields 0)
14+
* @example change([1], 0) // => 1 (smallest valid input that yields 1)
415
*/
516
export const change = (coins, amount) => {
617
// Create and initialize the storage

‎Dynamic-Programming/tests/CoinChange.test.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
import { change, coinChangeMin } from '../CoinChange'
22

3+
// ---------------------------------------------------------------------------
4+
// R50-T031 Regression suite for `change` — Dynamic-Programming/CoinChange.js
5+
//
6+
// Preconditions cited from source:
7+
// • Line 7: `combinations` array is initialised with zeros, length = amount+1
8+
// • Line 8: `combinations[0] = 1` — base case (one way to make 0)
9+
// • The function does NOT mutate the `coins` input array (verified below)
10+
//
11+
// Expected values are computed independently before examining the
12+
// implementation output.
13+
// ---------------------------------------------------------------------------
14+
15+
describe('change — regression (R50-T031)', () => {
16+
// Smallest supported input: amount = 0
17+
// Independent calculation: combinations[0] is set to 1 on line 8;
18+
// neither inner loop executes → result must be 1.
19+
it('smallest valid input: amount=0 returns 1 (base case, CoinChange.js line 8)', () => {
20+
const coins = [1]
21+
const coinsCopy = [...coins]
22+
const result = change(coins, 0)
23+
expect(result).toBe(1) // expected: 1
24+
// Verify coins array is not mutated (precondition: pure function)
25+
expect(coins).toEqual(coinsCopy)
26+
})
27+
28+
// Ordinary input: coins=[1,2,5], amount=5
29+
// Independent enumeration of combinations:
30+
// {5}, {2+2+1}, {2+1+1+1}, {1+1+1+1+1} → 4 combinations
31+
it('ordinary input: change([1,2,5], 5) === 4', () => {
32+
const result = change([1, 2, 5], 5)
33+
expect(result).toBe(4) // expected: 4
34+
})
35+
36+
// Repeated-value / domain boundary: single coin, amount not divisible
37+
// Independent calculation: 3 is odd; coin=2 only covers even amounts → 0
38+
it('domain boundary: change([2], 3) === 0 (no valid combination)', () => {
39+
const result = change([2], 3)
40+
expect(result).toBe(0) // expected: 0
41+
})
42+
43+
// Repeated-value boundary: single coin, amount exactly equals coin
44+
// Independent calculation: exactly one way → {2}
45+
it('repeated-value boundary: change([2], 2) === 1', () => {
46+
const result = change([2], 2)
47+
expect(result).toBe(1) // expected: 1
48+
})
49+
})
50+
351
test('Base Case 1', () => {
452
const coins = [2, 3, 5]
553
const amount = 0

‎work/R50-T031/check.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
"""
3+
R50-T031 check runner for Dynamic-Programming/CoinChange.js :: change
4+
Runs the repository test suite in non-watch mode and verifies a nonzero
5+
test count is reported. Exits non-zero on any failure.
6+
7+
Preconditions named from source:
8+
- CoinChange.js line 7: combinations array initialised to zeros of length amount+1
9+
- CoinChange.js line 8: combinations[0] = 1 (base case — one way to make 0)
10+
11+
Fixture inputs and independently calculated expected outputs:
12+
Smallest valid input → change([1], 0) == 1
13+
(amount=0, base case: combinations[0] starts at 1, loop body never reached)
14+
Ordinary input → change([1, 2, 5], 5) == 4
15+
(manual enumeration: {5}, {2+2+1}, {2+1+1+1}, {1+1+1+1+1})
16+
"""
17+
18+
import subprocess
19+
import sys
20+
import os
21+
import re
22+
23+
REPO_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
24+
25+
def run_tests():
26+
"""Install deps and run vitest in run (non-watch) mode."""
27+
print("=== Installing dependencies from lockfile ===")
28+
result = subprocess.run(
29+
['npm', 'ci'],
30+
cwd=REPO_ROOT,
31+
capture_output=True,
32+
text=True
33+
)
34+
if result.returncode != 0:
35+
print("npm ci failed:", result.stderr)
36+
sys.exit(1)
37+
38+
print("=== Running vitest (non-watch) ===")
39+
result = subprocess.run(
40+
['npm', 'test', '--', '--run', '--reporter=verbose',
41+
'Dynamic-Programming/tests/CoinChange.test.js'],
42+
cwd=REPO_ROOT,
43+
capture_output=True,
44+
text=True
45+
)
46+
combined = result.stdout + result.stderr
47+
print(combined)
48+
49+
# Verify nonzero test count
50+
match = re.search(r'(\d+)\s+passed', combined)
51+
if not match:
52+
print("FAIL: Could not find passing test count in output.")
53+
sys.exit(1)
54+
count = int(match.group(1))
55+
if count == 0:
56+
print("FAIL: Zero tests passed.")
57+
sys.exit(1)
58+
print(f"OK: {count} test(s) passed.")
59+
60+
if result.returncode != 0:
61+
print("FAIL: Test suite exited non-zero.")
62+
sys.exit(result.returncode)
63+
64+
65+
def save_evidence():
66+
"""Save tool versions and fixture results for review."""
67+
import json, platform, shutil
68+
node_ver = subprocess.run(['node', '--version'], capture_output=True, text=True).stdout.strip()
69+
npm_ver = subprocess.run(['npm', '--version'], capture_output=True, text=True).stdout.strip()
70+
71+
fixtures = [
72+
{
73+
"description": "Smallest valid input (amount=0, precondition: CoinChange.js line 8)",
74+
"coins": [1],
75+
"amount": 0,
76+
"expected": 1,
77+
"note": "combinations[0]=1 is the only element; loops never execute"
78+
},
79+
{
80+
"description": "Ordinary input (coins=[1,2,5], amount=5)",
81+
"coins": [1, 2, 5],
82+
"amount": 5,
83+
"expected": 4,
84+
"note": "Manual: {5},{2+2+1},{2+1+1+1},{1+1+1+1+1}"
85+
},
86+
{
87+
"description": "Repeated-value boundary: single coin, amount not divisible",
88+
"coins": [2],
89+
"amount": 3,
90+
"expected": 0,
91+
"note": "No way to make 3 with only coin=2"
92+
},
93+
]
94+
evidence = {
95+
"task": "R50-T031",
96+
"source_file": "Dynamic-Programming/CoinChange.js",
97+
"function": "change",
98+
"node_version": node_ver,
99+
"npm_version": npm_ver,
100+
"python_version": platform.python_version(),
101+
"fixtures": fixtures
102+
}
103+
out_path = os.path.join(os.path.dirname(__file__), 'evidence.json')
104+
with open(out_path, 'w') as f:
105+
json.dump(evidence, f, indent=2)
106+
print(f"Evidence saved to {out_path}")
107+
108+
109+
if __name__ == '__main__':
110+
save_evidence()
111+
run_tests()

0 commit comments

Comments
 (0)