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
57 changes: 57 additions & 0 deletions Maths/test/ParityOutlier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,60 @@ describe('Testing parityOutlier function', () => {
expect(parityOutlier([1, 3, 5, 7, 2, 4, 6, 8])).toBe(null)
})
})

describe('parityOutlier contract & invariant checks (Precondition near line 9 of Maths/ParityOutlier.js)', () => {
test('Smallest valid input ([1, 2])', () => {
const input = [1, 2]
expect(parityOutlier(input)).toBe(1)
})

test('Ordinary input ([1, 2, 16, -8848, 5126])', () => {
const input = [1, 2, 16, -8848, 5126]
expect(parityOutlier(input)).toBe(1)
})

test('Preserves input array immutability', () => {
const input = [1, 2, 16, -8848, 5126]
const copy = [...input]
parityOutlier(input)
expect(input).toEqual(copy)
})

// Seeded LCG generator for 100 bounded valid property cases
function createLCG(seed = 12345) {
let state = seed
return function () {
state = (state * 1664525 + 1013904223) % 4294967296
return state / 4294967296
}
}

test('Property test: 100 generated bounded valid inputs satisfy parity outlier invariant', () => {
const rand = createLCG(42)

for (let i = 0; i < 100; i++) {
const isOutlierOdd = rand() > 0.5
const length = 3 + Math.floor(rand() * 20)
const outlierIndex = Math.floor(rand() * length)

const arr = []
for (let j = 0; j < length; j++) {
if (j === outlierIndex) {
const val = Math.floor(rand() * 1000) * 2 + (isOutlierOdd ? 1 : 0)
arr.push(val)
} else {
const val = Math.floor(rand() * 1000) * 2 + (isOutlierOdd ? 0 : 1)
arr.push(val)
}
}

const expectedOutlier = arr[outlierIndex]
const actualOutlier = parityOutlier(arr)

expect(actualOutlier).toBe(expectedOutlier)
const actualParity = Math.abs(actualOutlier % 2)
const expectedParity = isOutlierOdd ? 1 : 0
expect(actualParity).toBe(expectedParity)
}
})
})
41 changes: 41 additions & 0 deletions work/R50-T024/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import subprocess
import json
import sys

def run_checks():
work_dir = "work/R50-T024"
os.makedirs(work_dir, exist_ok=True)

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)

cmd = ["npx", "vitest", "run", "Maths/test/ParityOutlier.test.js"]
res = subprocess.run(cmd, capture_output=True, text=True)

if res.returncode != 0:
cmd = ["npx", "vitest", "run", "Maths/tests/ParityOutlier.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-T024",
"precondition_line": "Maths/ParityOutlier.js:9",
"smallest_valid_input": {"input": [1, 2], "expected": 1},
"ordinary_input": {"input": [1, 2, 16, -8848, 5126], "expected": 1},
"property_tests_run": 100
}, f, indent=2)
print("All checks passed.")
else:
print("Tests failed!")
sys.exit(1)

if __name__ == "__main__":
run_checks()
23 changes: 23 additions & 0 deletions work/R50-T024/fixture_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"status": "PASSED",
"task": "R50-T024",
"precondition_line": "Maths/ParityOutlier.js:9",
"smallest_valid_input": {
"input": [
1,
2
],
"expected": 1
},
"ordinary_input": {
"input": [
1,
2,
16,
-8848,
5126
],
"expected": 1
},
"property_tests_run": 100
}
11 changes: 11 additions & 0 deletions work/R50-T024/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

✓ Maths/test/ParityOutlier.test.js (8 tests) 3ms

 Test Files  1 passed (1)
 Tests  8 passed (8)
 Start at  08:46:44
 Duration  78ms (import 28%, worker 26%, tests 25%, transform 22%, environment 1%)


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