#!/usr/bin/env python3 """Unit tests for verify_job4128.py: a clean run, and negative controls in which a single digested number is corrupted and the checker must fail. Run: .venv/Scripts/python.exe -m unittest outputs/job4128/test_verify_job4128.py """ import copy import json import os import subprocess import sys import unittest HERE = os.path.dirname(os.path.abspath(__file__)) WS = os.path.abspath(os.path.join(HERE, "..", "..")) PY = os.path.join(WS, ".venv", "Scripts", "python.exe") CHECK = os.path.join(HERE, "verify_job4128.py") RESULTS = os.path.join(HERE, "results_job4128.json") def run_check(path): p = subprocess.run([PY, CHECK, "--results", path], capture_output=True, text=True) try: out = json.loads(p.stdout.strip().splitlines()[-1]) except Exception: out = None return p.returncode, out class VerifyJob4128Tests(unittest.TestCase): @classmethod def setUpClass(cls): with open(RESULTS, encoding="utf-8") as fh: cls.base = json.load(fh) def write(self, obj, name): p = os.path.join(HERE, "_tmp_%s.json" % name) with open(p, "w", encoding="utf-8") as fh: json.dump(obj, fh) return p def test_clean_run_passes(self): rc, out = run_check(RESULTS) self.assertEqual(rc, 0, out) self.assertTrue(out["all_pass"]) self.assertGreater(out["checks"], 200) self.assertEqual(out["failed"], []) def _corrupt(self, mutate, name): d = copy.deepcopy(self.base) mutate(d) p = self.write(d, name) rc, out = run_check(p) os.unlink(p) self.assertEqual(rc, 1, "corruption not detected: %s" % name) self.assertTrue(out and out["failed"], name) def test_negative_L_F_first_positive(self): self._corrupt(lambda d: d["minF_profile_w19"].__setitem__("first_L_minF_ge_1", 593), "lf") def test_negative_minF_at_593(self): def m(d): rl = d["minF_profile_w19"]["run_length_pairs"] for i, (start, _) in enumerate(rl): if start <= 593 and (i + 1 == len(rl) or rl[i + 1][0] > 593): rl[i][1] = 1 self._corrupt(m, "minf593") def test_negative_envelope_zero_at_1704(self): def m(d): for s in d["scans"]: if s["mode"] == "envelope" and s["L"] == 1704 and s["W"] == 9699690: s["global_max_loss"] = 1 s["loss_histogram"] = [[0, s["ranked_starts"] - 1], [1, 1]] self._corrupt(m, "env1704") def test_negative_envelope_positive_dropped_at_1705(self): def m(d): for s in d["scans"]: if s["mode"] == "envelope" and s["L"] == 1705 and s["W"] == 9699690: s["global_max_loss"] = 0 s["starts_with_positive_loss"] = 0 s["loss_histogram"] = [[0, s["ranked_starts"]]] self._corrupt(m, "env1705") def test_negative_chordal_nonzero_at_593(self): def m(d): for s in d["scans"]: if s["mode"] == "chordal" and s["L"] == 593 and s["W"] == 9699690: s["global_max_loss"] = 1 s["starts_with_positive_loss"] = 1 s["loss_histogram"] = [[0, s["ranked_starts"] - 1], [1, 1]] self._corrupt(m, "cho593") def test_negative_chordal_zero_at_900(self): def m(d): for s in d["scans"]: if s["mode"] == "chordal" and s["L"] == 900 and s["W"] == 9699690: s["global_max_loss"] = 0 s["starts_with_positive_loss"] = 0 s["loss_histogram"] = [[0, s["ranked_starts"]]] self._corrupt(m, "cho900") def test_negative_independent_witness_removed(self): def m(d): d["independent_all_phase"]["checks"] = [ r for r in d["independent_all_phase"]["checks"] if not (r["a"] == 4186 and r["L"] == 1704)] self._corrupt(m, "indep") def test_negative_control17_Lc(self): def m(d): for s in d["scans"]: if s["mode"] == "chordal" and s["L"] == 463 and s["W"] == 510510: s["global_max_loss"] = 0 s["starts_with_positive_loss"] = 0 s["loss_histogram"] = [[0, s["ranked_starts"]]] self._corrupt(m, "c17lc") def test_negative_histogram_inconsistent(self): self._corrupt(lambda d: d["scans"][0]["loss_histogram"].append([7, 1]), "hist") def test_negative_minF_cross_check(self): def m(d): for s in d["scans"]: if s["mode"] == "envelope" and s["L"] == 1000 and s["W"] == 9699690: s["minimum_first_order"] = 8 self._corrupt(m, "cross") def test_negative_monotone_ladder_break(self): def m(d): for s in d["scans"]: if s["mode"] == "envelope" and s["L"] == 1900 and s["W"] == 9699690: s["global_max_loss"] = 0 s["starts_with_positive_loss"] = 0 s["loss_histogram"] = [[0, s["ranked_starts"]]] self._corrupt(m, "mono") if __name__ == "__main__": unittest.main()