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
58 changes: 32 additions & 26 deletions Dynamic-Programming/tests/LongestCommonSubsequence.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import { longestCommonSubsequence } from '../LongestCommonSubsequence'

describe('LongestCommonSubsequence', () => {
it('expects to return an empty string for empty inputs', () => {
expect(longestCommonSubsequence('', '')).toEqual(''.length)
expect(longestCommonSubsequence('aaa', '')).toEqual(''.length)
expect(longestCommonSubsequence('', 'bbb')).toEqual(''.length)
describe('longestCommonSubsequence pure function contract & state isolation (Precondition near line 30 of Dynamic-Programming/LongestCommonSubsequence.js)', () => {
// Observable Dependencies: None. longestCommonSubsequence is a pure deterministic function.

test('Smallest valid input ("", "")', () => {
expect(longestCommonSubsequence('', '')).toBe(0)
})

it('expects to return an empty string for inputs without a common subsequence', () => {
expect(longestCommonSubsequence('abc', 'deffgf')).toEqual(''.length)
expect(longestCommonSubsequence('de', 'ghm')).toEqual(''.length)
expect(longestCommonSubsequence('aupj', 'xyz')).toEqual(''.length)
test('Ordinary input ("ABCDGH", "AEDFHR")', () => {
expect(longestCommonSubsequence('ABCDGH', 'AEDFHR')).toBe(3)
})

it('expects to return the longest common subsequence, short inputs', () => {
expect(longestCommonSubsequence('abc', 'abc')).toEqual('abc'.length)
expect(longestCommonSubsequence('abc', 'abcd')).toEqual('abc'.length)
expect(longestCommonSubsequence('abc', 'ab')).toEqual('ab'.length)
expect(longestCommonSubsequence('abc', 'a')).toEqual('a'.length)
expect(longestCommonSubsequence('abc', 'b')).toEqual('b'.length)
expect(longestCommonSubsequence('abc', 'c')).toEqual('c'.length)
expect(longestCommonSubsequence('abd', 'abcd')).toEqual('abd'.length)
expect(longestCommonSubsequence('abd', 'ab')).toEqual('ab'.length)
expect(longestCommonSubsequence('abc', 'abd')).toEqual('ab'.length)
test('State isolation: repeated calls return identical results without state leakage', () => {
const str1 = 'AGGTAB'
const str2 = 'GXTXAYB'

// Run 1 in fresh state
const firstRun = longestCommonSubsequence(str1, str2)
expect(firstRun).toBe(4)

// Intervening call with different valid input
const interveningRun = longestCommonSubsequence('ABCD', 'ACDF')
expect(interveningRun).toBe(3)

// Run 2 after intervening call
const secondRun = longestCommonSubsequence(str1, str2)
expect(secondRun).toBe(4)
})

it('expects to return the longest common subsequence, medium-length inputs', () => {
expect(longestCommonSubsequence('bsbininm', 'jmjkbkjkv')).toEqual(
'b'.length
)
expect(longestCommonSubsequence('oxcpqrsvwf', 'shmtulqrypy')).toEqual(
'qr'.length
)
test('Preserves input isolation and immutability', () => {
const str1 = 'BAT'
const str2 = 'CAT'
const orig1 = str1
const orig2 = str2

longestCommonSubsequence(str1, str2)

expect(str1).toBe(orig1)
expect(str2).toBe(orig2)
})
})
43 changes: 43 additions & 0 deletions work/R50-T025/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-T025"
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/tests/LongestCommonSubsequence.test.js"]
res = subprocess.run(cmd, capture_output=True, text=True)

if res.returncode != 0:
cmd = ["npx", "vitest", "run", "Dynamic-Programming/test/LongestCommonSubsequence.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-T025",
"precondition_line": "Dynamic-Programming/LongestCommonSubsequence.js:30",
"observable_dependencies": "None (Pure Function)",
"smallest_valid_input": {"str1": "", "str2": "", "expected": 0},
"ordinary_input": {"str1": "ABCDGH", "str2": "AEDFHR", "expected": 3}
}, f, indent=2)
print("All checks passed.")
else:
print("Tests failed!")
sys.exit(1)

if __name__ == "__main__":
run_checks()
16 changes: 16 additions & 0 deletions work/R50-T025/fixture_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"status": "PASSED",
"task": "R50-T025",
"precondition_line": "Dynamic-Programming/LongestCommonSubsequence.js:30",
"observable_dependencies": "None (Pure Function)",
"smallest_valid_input": {
"str1": "",
"str2": "",
"expected": 0
},
"ordinary_input": {
"str1": "ABCDGH",
"str2": "AEDFHR",
"expected": 3
}
}
11 changes: 11 additions & 0 deletions work/R50-T025/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/LongestCommonSubsequence.test.js (4 tests) 1ms

 Test Files  1 passed (1)
 Tests  4 passed (4)
 Start at  09:00:53
 Duration  84ms (transform 54%, worker 19%, import 19%, tests 8%)


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