"""python3 -I checks/test_judge.py: the judge on the record constructions, a broken one and the format checker.""" import json, os, pathlib, subprocess, sys, tempfile here = pathlib.Path(__file__).resolve().parent.parent def run(*args, cwd): return subprocess.run([sys.executable, "-I", *args], cwd=cwd, capture_output=True, text=True) with tempfile.TemporaryDirectory() as t: route = pathlib.Path(t, "route"); route.mkdir() (route / "rec.json").write_text((here / "constructions/lnzwz_n512.json").read_text()) work = pathlib.Path(t, "work"); work.mkdir() env_judge = str(here / "checks" / "judge") r = subprocess.run([sys.executable, "-c", f"import sys; sys.path.insert(0, {env_judge!r}); sys.argv=['check.py', {str(route)!r}]; exec(open({env_judge + '/check.py'!r}).read())"], cwd=work, capture_output=True, text=True) assert r.returncode == 0, r.stdout + r.stderr assert "best upper bound 0.3808590568145606537807120" in r.stdout, r.stdout assert abs(json.loads((work / "metrics.json").read_text())["score"] - 0.38085905681456065) < 1e-15 assert json.loads((work / "metrics.json").read_text())["score_exact"] == "0.3808590568145606537807120" # improves: Q_L beats Hyra by 3.66e-7 (short of a minimum raised to 1e-6, clear of the real 1e-7) main = pathlib.Path(t, "main"); (main / "constructions").mkdir(parents=True) (main / "constructions" / "hyra.json").write_text((here / "constructions/hyra_n1024.json").read_text()) improves = lambda minimum="1e-7": subprocess.run([sys.executable, "-c", f"import sys; sys.path.insert(0, {env_judge!r}); sys.argv=['improves.py', {str(route)!r}, {str(main)!r}]; exec(open({env_judge + '/improves.py'!r}).read().replace('Fraction(\"1e-7\")', 'Fraction(\"{minimum}\")'))"], cwd=main, capture_output=True, text=True) r = improves("1e-6") assert r.returncode == 1 and "improves on the default branch by 3.66e-7, less than the minimum 1e-6" in r.stdout and "not a new result: the best known is 0.3808564043546676" in r.stdout, r.stdout + r.stderr r = improves() assert r.returncode == 0 and r.stdout.startswith("improves on the default branch by 3.66e-7 (at least 1e-7)"), r.stdout + r.stderr (route / "bad.json").write_text(json.dumps({"values": [2, 0, 0, 0]})) r = subprocess.run([sys.executable, "-c", f"import sys; sys.path.insert(0, {env_judge!r}); sys.argv=['check.py', {str(route)!r}]; exec(open({env_judge + '/check.py'!r}).read())"], cwd=work, capture_output=True, text=True) assert r.returncode == 1 and "not admissible" in r.stdout, r.stdout assert run(str(here / "tools/check_format.py"), str(here / "constructions"), cwd=t).returncode == 0 (route / "shape.json").write_text(json.dumps({"values": []})) assert run(str(here / "tools/check_format.py"), str(route / "shape.json"), cwd=t).returncode == 1 print("ok")