Unit circles through three of n points
Show that n points in the plane have only o(n²) unit circles passing through three of them; the best construction gives n^{3/2}.

| 1 | #!/usr/bin/env python3 |
| 2 | """Operational probes for the verifier proposed in pull request #4. |
| 3 | |
| 4 | Usage: python3 ops_probe.py <artifact-route-dir> |
| 5 | |
| 6 | <artifact-route-dir> is the folder routes/rusirui-rational-verifier of branch |
| 7 | origin/rusirui-rational-verifier (a checkout of it, not the branch itself). |
| 8 | |
| 9 | Each probe runs that folder's verify.py as a subprocess on a controlled input |
| 10 | and compares the exit code, stdout and the presence of the output file with |
| 11 | what the artifact's README claims. Nothing is imported from the artifact. |
| 12 | Prints one line per probe and exits 0 only if every probe matched. |
| 13 | """ |
| 14 | import json |
| 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | import tempfile |
| 19 | import time |
| 20 | from pathlib import Path |
| 21 | |
| 22 | FAILS = [] |
| 23 | MATCHED = 0 |
| 24 | SKIPPED = 0 |
| 25 | |
| 26 | |
| 27 | def run(script, args, cwd=None, env=None): |
| 28 | start = time.perf_counter() |
| 29 | proc = subprocess.run( |
| 30 | [sys.executable, str(script)] + [str(a) for a in args], |
| 31 | capture_output=True, text=True, cwd=cwd, env=env, timeout=600, |
| 32 | ) |
| 33 | return proc.returncode, proc.stdout.strip(), proc.stderr.strip(), time.perf_counter() - start |
| 34 | |
| 35 | |
| 36 | def check(name, got, want): |
| 37 | global MATCHED |
| 38 | ok = got == want |
| 39 | if ok: |
| 40 | MATCHED += 1 |
| 41 | else: |
| 42 | FAILS.append(name) |
| 43 | print(("OK " if ok else "FAIL ") + name + ("" if ok else f" (got {got!r}, want {want!r})")) |
| 44 | |
| 45 | |
| 46 | def put(folder, data): |
| 47 | path = Path(folder) / "configuration.json" |
| 48 | path.write_text(json.dumps(data), encoding="utf-8") |
| 49 | return path |
| 50 | |
| 51 | |
| 52 | def main(): |
| 53 | if len(sys.argv) != 2: |
| 54 | print(__doc__.strip().splitlines()[2]) |
| 55 | return 2 |
| 56 | artifact = Path(sys.argv[1]).resolve() |
| 57 | verify_py = artifact / "verify.py" |
| 58 | if not verify_py.is_file(): |
| 59 | print(f"no verify.py in {artifact}", file=sys.stderr) |
| 60 | return 2 |
| 61 | grid = [{"schema": 1, "points": [[x, y] for y in range(8) for x in range(10)], "unit_circles": 76}] |
| 62 | |
| 63 | with tempfile.TemporaryDirectory() as tmp: |
| 64 | tmp = Path(tmp) |
| 65 | |
| 66 | # 1. The route's own configuration: PASS n=9, 5 circles, output matches example. |
| 67 | out = tmp / "route.json" |
| 68 | rc, so, se, _ = run(verify_py, [artifact, "--output", out]) |
| 69 | check("route folder exits 0", rc, 0) |
| 70 | check("route folder prints PASS n=9 / 5", (so.startswith("PASS:") and "n=9" in so and "unit circles=5" in so), True) |
| 71 | produced = json.loads(out.read_text()) if out.exists() else None |
| 72 | example = json.loads((artifact / "example-verification.json").read_text()) |
| 73 | check("output equals example-verification.json (parsed)", produced, example) |
| 74 | check("output equals example-verification.json (bytes)", out.read_bytes() if out.exists() else None, |
| 75 | (artifact / "example-verification.json").read_bytes()) |
| 76 | |
| 77 | # 2. The six accept fixtures. |
| 78 | for name in ("collinear", "dedup", "grid", "nonunit", "rational-center", "three"): |
| 79 | folder = artifact / "samples" / "accept" / name |
| 80 | claimed = json.loads((folder / "configuration.json").read_text())["unit_circles"] |
| 81 | out = tmp / f"accept-{name}.json" |
| 82 | rc, so, se, _ = run(verify_py, [folder, "--output", out]) |
| 83 | got = json.loads(out.read_text())["unit_circles"] if out.exists() else None |
| 84 | check(f"accept/{name} exits 0 with {claimed}", (rc, got), (0, claimed)) |
| 85 | |
| 86 | # 3. The six reject fixtures: exit 1 and no output file left behind. |
| 87 | for name in ("collinear", "duplicate", "float", "nearly-unit", "overcount", "undercount"): |
| 88 | folder = artifact / "samples" / "reject" / name |
| 89 | out = tmp / f"reject-{name}.json" |
| 90 | out.write_text('{"stale": "from an earlier run"}') |
| 91 | rc, so, se, _ = run(verify_py, [folder, "--output", out]) |
| 92 | check(f"reject/{name} exits 1 and leaves no output", (rc, out.exists()), (1, False)) |
| 93 | check(f"reject/{name} printed no PASS", so.startswith("PASS"), False) |
| 94 | |
| 95 | # 4. Missing artifact: SKIP, exit 0, no file. |
| 96 | empty = tmp / "empty" |
| 97 | empty.mkdir() |
| 98 | out = tmp / "skip.json" |
| 99 | rc, so, se, _ = run(verify_py, [empty, "--output", out]) |
| 100 | check("missing configuration.json exits 0 with SKIP", (rc, so.startswith("SKIP:"), out.exists()), (0, True, False)) |
| 101 | |
| 102 | # 5. Nonexistent route folder: exit 2. |
| 103 | rc, so, se, _ = run(verify_py, [tmp / "absent", "--output", out]) |
| 104 | check("nonexistent route folder exits 2", rc, 2) |
| 105 | |
| 106 | # 6. Output must differ from input. |
| 107 | rc, so, se, _ = run(verify_py, [artifact, "--output", artifact / "configuration.json"]) |
| 108 | check("output == input exits 2", rc, 2) |
| 109 | check("output == input left configuration.json intact", |
| 110 | json.loads((artifact / "configuration.json").read_text())["schema"], 1) |
| 111 | |
| 112 | # 7. Duplicate JSON key, invalid UTF-8, configuration.json is a directory. |
| 113 | dup = tmp / "dup" |
| 114 | dup.mkdir() |
| 115 | (dup / "configuration.json").write_text('{"schema":1,"schema":1}') |
| 116 | check("duplicate JSON key exits 1", run(verify_py, [dup, "--output", tmp / "x1.json"])[0], 1) |
| 117 | bad = tmp / "badbytes" |
| 118 | bad.mkdir() |
| 119 | (bad / "configuration.json").write_bytes(b"\xff") |
| 120 | check("invalid UTF-8 exits 1", run(verify_py, [bad, "--output", tmp / "x2.json"])[0], 1) |
| 121 | asdir = tmp / "asdir" |
| 122 | (asdir / "configuration.json").mkdir(parents=True) |
| 123 | check("configuration.json as directory exits 1", run(verify_py, [asdir, "--output", tmp / "x3.json"])[0], 1) |
| 124 | |
| 125 | # 8. Caps: 81 points, 33-digit coordinate, file over 128 KiB, count over the pair bound. |
| 126 | many = tmp / "many" |
| 127 | many.mkdir() |
| 128 | put(many, {"schema": 1, "points": [[i, 0] for i in range(81)], "unit_circles": 0}) |
| 129 | check("81 points exits 1", run(verify_py, [many, "--output", tmp / "x4.json"])[0], 1) |
| 130 | wide = tmp / "wide" |
| 131 | wide.mkdir() |
| 132 | put(wide, {"schema": 1, "points": [[("1" + "0" * 32), 0], [0, 1], [-1, 0]], "unit_circles": 1}) |
| 133 | check("33-digit coordinate exits 1", run(verify_py, [wide, "--output", tmp / "x5.json"])[0], 1) |
| 134 | big = tmp / "big" |
| 135 | big.mkdir() |
| 136 | (big / "configuration.json").write_text( |
| 137 | json.dumps({"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1}) + " " * 131072) |
| 138 | check("file over 128 KiB exits 1", run(verify_py, [big, "--output", tmp / "x6.json"])[0], 1) |
| 139 | over = tmp / "over" |
| 140 | over.mkdir() |
| 141 | put(over, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0], [0, -1]], "unit_circles": 5}) |
| 142 | check("count over the pair bound exits 1", run(verify_py, [over, "--output", tmp / "x7.json"])[0], 1) |
| 143 | |
| 144 | # 9. The 80-point 10x8 grid: count 76, and how promptly. |
| 145 | grid_dir = tmp / "grid80" |
| 146 | grid_dir.mkdir() |
| 147 | put(grid_dir, grid[0]) |
| 148 | out = tmp / "grid80.json" |
| 149 | rc, so, se, elapsed = run(verify_py, [grid_dir, "--output", out]) |
| 150 | check("grid80 exits 0", rc, 0) |
| 151 | got = json.loads(out.read_text())["unit_circles"] if out.exists() else None |
| 152 | check("grid80 counts 76", got, 76) |
| 153 | print(f" grid80 wall time: {elapsed:.3f}s") |
| 154 | |
| 155 | # 10. The runner as the check would call it (Git POSIX sh on Windows). |
| 156 | sbatch = artifact / "check.sbatch" |
| 157 | shebang = sbatch.read_text().splitlines()[0] |
| 158 | with tempfile.TemporaryDirectory() as outer: |
| 159 | outer = Path(outer) |
| 160 | (outer / "input").mkdir() |
| 161 | target = outer / "input" / "routes" / "r" |
| 162 | target.mkdir(parents=True) |
| 163 | put(target, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1}) |
| 164 | env = dict(os.environ, SS_INPUT=str(outer / "input"), SS_ROUTE_DIR="routes/r") |
| 165 | proc = subprocess.run(["sh", str(sbatch)], capture_output=True, text=True, env=env, cwd=outer) |
| 166 | check("check.sbatch accepts a valid route (exit 0)", proc.returncode, 0) |
| 167 | check("check.sbatch wrote verification.json in its cwd", (outer / "verification.json").is_file(), True) |
| 168 | put(target, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 0}) |
| 169 | proc = subprocess.run(["sh", str(sbatch)], capture_output=True, text=True, env=env, cwd=outer) |
| 170 | check("check.sbatch rejects a wrong count (exit 1)", proc.returncode, 1) |
| 171 | print(f" check.sbatch shebang: {shebang}") |
| 172 | |
| 173 | # 11. Symlink as configuration.json (skipped where the OS forbids creating one). |
| 174 | with tempfile.TemporaryDirectory() as link_tmp: |
| 175 | link_tmp = Path(link_tmp) |
| 176 | real = link_tmp / "real.json" |
| 177 | real.write_text(json.dumps({"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1})) |
| 178 | route = link_tmp / "route" |
| 179 | route.mkdir() |
| 180 | try: |
| 181 | os.symlink(real, route / "configuration.json") |
| 182 | except OSError as exc: |
| 183 | global SKIPPED |
| 184 | SKIPPED += 1 |
| 185 | print(f"SKIP symlinked configuration.json: cannot create symlink ({exc.__class__.__name__})") |
| 186 | else: |
| 187 | check("symlinked configuration.json exits 1", run(verify_py, [route, "--output", link_tmp / "s.json"])[0], 1) |
| 188 | |
| 189 | print(f"\n{MATCHED} matched, {SKIPPED} skipped, {len(FAILS)} did not match the artifact's documented claim") |
| 190 | return 0 if not FAILS else 1 |
| 191 | |
| 192 | |
| 193 | if __name__ == "__main__": |
| 194 | sys.exit(main()) |