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 | from fractions import Fraction |
| 17 | import subprocess |
| 18 | import sys |
| 19 | import tempfile |
| 20 | import time |
| 21 | from pathlib import Path |
| 22 | |
| 23 | FAILS = [] |
| 24 | SKIPPED = 0 |
| 25 | MATCHED = 0 |
| 26 | |
| 27 | |
| 28 | def exact(value): |
| 29 | return Fraction(str(value)) |
| 30 | |
| 31 | |
| 32 | def run(script, args, cwd=None, env=None): |
| 33 | start = time.perf_counter() |
| 34 | proc = subprocess.run( |
| 35 | [sys.executable, str(script)] + [str(a) for a in args], |
| 36 | capture_output=True, text=True, cwd=cwd, env=env, timeout=600, |
| 37 | ) |
| 38 | return proc.returncode, proc.stdout.strip(), proc.stderr.strip(), time.perf_counter() - start |
| 39 | |
| 40 | |
| 41 | def check(name, got, want): |
| 42 | global MATCHED |
| 43 | ok = got == want |
| 44 | if ok: |
| 45 | MATCHED += 1 |
| 46 | else: |
| 47 | FAILS.append(name) |
| 48 | print(("OK " if ok else "FAIL ") + name + ("" if ok else f" (got {got!r}, want {want!r})")) |
| 49 | |
| 50 | |
| 51 | def put(folder, data): |
| 52 | path = Path(folder) / "configuration.json" |
| 53 | path.write_text(json.dumps(data), encoding="utf-8") |
| 54 | return path |
| 55 | |
| 56 | |
| 57 | def main(): |
| 58 | if len(sys.argv) != 2: |
| 59 | print(__doc__.strip().splitlines()[2]) |
| 60 | return 2 |
| 61 | artifact = Path(sys.argv[1]).resolve() |
| 62 | verify_py = artifact / "verify.py" |
| 63 | if not verify_py.is_file(): |
| 64 | print(f"no verify.py in {artifact}", file=sys.stderr) |
| 65 | return 2 |
| 66 | grid = [{"schema": 1, "points": [[x, y] for y in range(8) for x in range(10)], "unit_circles": 76}] |
| 67 | |
| 68 | with tempfile.TemporaryDirectory() as tmp: |
| 69 | tmp = Path(tmp) |
| 70 | |
| 71 | # 1. The route's own configuration: PASS n=9, 5 circles, output matches example. |
| 72 | out = tmp / "route.json" |
| 73 | rc, so, se, _ = run(verify_py, [artifact, "--output", out]) |
| 74 | check("route folder exits 0", rc, 0) |
| 75 | check("route folder prints PASS n=9 / 5", (so.startswith("PASS:") and "n=9" in so and "unit circles=5" in so), True) |
| 76 | produced = json.loads(out.read_text()) if out.exists() else None |
| 77 | example = json.loads((artifact / "example-verification.json").read_text()) |
| 78 | check("output equals example-verification.json (parsed)", produced, example) |
| 79 | check("output equals example-verification.json (bytes)", out.read_bytes() if out.exists() else None, |
| 80 | (artifact / "example-verification.json").read_bytes()) |
| 81 | |
| 82 | # 2. The six accept fixtures. |
| 83 | for name in ("collinear", "dedup", "grid", "nonunit", "rational-center", "three"): |
| 84 | folder = artifact / "samples" / "accept" / name |
| 85 | claimed = json.loads((folder / "configuration.json").read_text())["unit_circles"] |
| 86 | out = tmp / f"accept-{name}.json" |
| 87 | rc, so, se, _ = run(verify_py, [folder, "--output", out]) |
| 88 | got = json.loads(out.read_text())["unit_circles"] if out.exists() else None |
| 89 | check(f"accept/{name} exits 0 with {claimed}", (rc, got), (0, claimed)) |
| 90 | |
| 91 | # 3. The six reject fixtures: exit 1 and no output file left behind. |
| 92 | for name in ("collinear", "duplicate", "float", "nearly-unit", "overcount", "undercount"): |
| 93 | folder = artifact / "samples" / "reject" / name |
| 94 | out = tmp / f"reject-{name}.json" |
| 95 | out.write_text('{"stale": "from an earlier run"}') |
| 96 | rc, so, se, _ = run(verify_py, [folder, "--output", out]) |
| 97 | check(f"reject/{name} exits 1 and leaves no output", (rc, out.exists()), (1, False)) |
| 98 | check(f"reject/{name} printed no PASS", so.startswith("PASS"), False) |
| 99 | |
| 100 | # 4. Missing artifact: SKIP, exit 0, no file. |
| 101 | empty = tmp / "empty" |
| 102 | empty.mkdir() |
| 103 | out = tmp / "skip.json" |
| 104 | rc, so, se, _ = run(verify_py, [empty, "--output", out]) |
| 105 | check("missing configuration.json exits 0 with SKIP", (rc, so.startswith("SKIP:"), out.exists()), (0, True, False)) |
| 106 | |
| 107 | # 5. Nonexistent route folder: exit 2. |
| 108 | rc, so, se, _ = run(verify_py, [tmp / "absent", "--output", out]) |
| 109 | check("nonexistent route folder exits 2", rc, 2) |
| 110 | |
| 111 | # 6. Output must differ from input. |
| 112 | rc, so, se, _ = run(verify_py, [artifact, "--output", artifact / "configuration.json"]) |
| 113 | check("output == input exits 2", rc, 2) |
| 114 | check("output == input left configuration.json intact", |
| 115 | json.loads((artifact / "configuration.json").read_text())["schema"], 1) |
| 116 | |
| 117 | # 7. Duplicate JSON key, invalid UTF-8, configuration.json is a directory. |
| 118 | dup = tmp / "dup" |
| 119 | dup.mkdir() |
| 120 | (dup / "configuration.json").write_text('{"schema":1,"schema":1}') |
| 121 | check("duplicate JSON key exits 1", run(verify_py, [dup, "--output", tmp / "x1.json"])[0], 1) |
| 122 | bad = tmp / "badbytes" |
| 123 | bad.mkdir() |
| 124 | (bad / "configuration.json").write_bytes(b"\xff") |
| 125 | check("invalid UTF-8 exits 1", run(verify_py, [bad, "--output", tmp / "x2.json"])[0], 1) |
| 126 | asdir = tmp / "asdir" |
| 127 | (asdir / "configuration.json").mkdir(parents=True) |
| 128 | check("configuration.json as directory exits 1", run(verify_py, [asdir, "--output", tmp / "x3.json"])[0], 1) |
| 129 | |
| 130 | # 8. Caps: 81 points, 33-digit coordinate, file over 128 KiB, count over the pair bound. |
| 131 | many = tmp / "many" |
| 132 | many.mkdir() |
| 133 | put(many, {"schema": 1, "points": [[i, 0] for i in range(81)], "unit_circles": 0}) |
| 134 | check("81 points exits 1", run(verify_py, [many, "--output", tmp / "x4.json"])[0], 1) |
| 135 | wide = tmp / "wide" |
| 136 | wide.mkdir() |
| 137 | put(wide, {"schema": 1, "points": [[("1" + "0" * 32), 0], [0, 1], [-1, 0]], "unit_circles": 1}) |
| 138 | check("33-digit coordinate exits 1", run(verify_py, [wide, "--output", tmp / "x5.json"])[0], 1) |
| 139 | big = tmp / "big" |
| 140 | big.mkdir() |
| 141 | (big / "configuration.json").write_text( |
| 142 | json.dumps({"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1}) + " " * 131072) |
| 143 | check("file over 128 KiB exits 1", run(verify_py, [big, "--output", tmp / "x6.json"])[0], 1) |
| 144 | over = tmp / "over" |
| 145 | over.mkdir() |
| 146 | put(over, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0], [0, -1]], "unit_circles": 5}) |
| 147 | check("count over the pair bound exits 1", run(verify_py, [over, "--output", tmp / "x7.json"])[0], 1) |
| 148 | |
| 149 | # 9. The 80-point 10x8 grid: count 76, and how promptly. |
| 150 | grid_dir = tmp / "grid80" |
| 151 | grid_dir.mkdir() |
| 152 | put(grid_dir, grid[0]) |
| 153 | out = tmp / "grid80.json" |
| 154 | rc, so, se, elapsed = run(verify_py, [grid_dir, "--output", out]) |
| 155 | check("grid80 exits 0", rc, 0) |
| 156 | got = json.loads(out.read_text())["unit_circles"] if out.exists() else None |
| 157 | check("grid80 counts 76", got, 76) |
| 158 | print(f" grid80 wall time: {elapsed:.3f}s") |
| 159 | |
| 160 | # 10. Witnesses: recompute every circle's incident indices from the input, exactly. |
| 161 | # The artifact's own test only asserts >= 3 indices, so the hand claim in issue #5 |
| 162 | # that (1,1) carries four points and the others three was never independently checked. |
| 163 | def witnesses(folder, label): |
| 164 | out_file = tmp / f"witness-{label}.json" |
| 165 | rc, so, se, _ = run(verify_py, [folder, "--output", out_file]) |
| 166 | if rc != 0 or not out_file.exists(): |
| 167 | check(f"witnesses {label} ran", (rc, out_file.exists()), (0, True)) |
| 168 | return None |
| 169 | payload = json.loads(out_file.read_text()) |
| 170 | points = [(exact(p[0]), exact(p[1])) for p in json.loads((folder / "configuration.json").read_text())["points"]] |
| 171 | expected = {} |
| 172 | for circle in payload["circles"]: |
| 173 | cx, cy = exact(circle["center"][0]), exact(circle["center"][1]) |
| 174 | indices = [i for i, (px, py) in enumerate(points) if (px - cx) ** 2 + (py - cy) ** 2 == 1] |
| 175 | expected[f'({cx},{cy})'] = indices |
| 176 | check(f"witnesses {label} {cx},{cy} indices", circle["point_indices"], indices) |
| 177 | check(f"witnesses {label} {cx},{cy} has >= 3 points", len(indices) >= 3, True) |
| 178 | check(f"witnesses {label} every circle accounted for", len(expected), payload["unit_circles"]) |
| 179 | return expected |
| 180 | |
| 181 | grid_witnesses = witnesses(artifact, "grid") |
| 182 | check("grid (1,1) carries four points", grid_witnesses.get("(1,1)") if grid_witnesses else None, [1, 3, 5, 7]) |
| 183 | check("grid (0,1), (1,0), (1,2), (2,1) carry three each", |
| 184 | sorted(len(v) for k, v in grid_witnesses.items() if k != "(1,1)") if grid_witnesses else None, [3, 3, 3, 3]) |
| 185 | rational = witnesses(artifact / "samples" / "accept" / "rational-center", "rational-center") |
| 186 | check("rational-center circle is centred (1/3,2/7)", list(rational) if rational else None, ["(1/3,2/7)"]) |
| 187 | |
| 188 | # 11. The runner as the check would call it (Git POSIX sh on Windows). |
| 189 | sbatch = artifact / "check.sbatch" |
| 190 | shebang = sbatch.read_text().splitlines()[0] |
| 191 | with tempfile.TemporaryDirectory() as outer: |
| 192 | outer = Path(outer) |
| 193 | (outer / "input").mkdir() |
| 194 | target = outer / "input" / "routes" / "r" |
| 195 | target.mkdir(parents=True) |
| 196 | put(target, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1}) |
| 197 | env = dict(os.environ, SS_INPUT=str(outer / "input"), SS_ROUTE_DIR="routes/r") |
| 198 | proc = subprocess.run(["sh", str(sbatch)], capture_output=True, text=True, env=env, cwd=outer) |
| 199 | check("check.sbatch accepts a valid route (exit 0)", proc.returncode, 0) |
| 200 | check("check.sbatch wrote verification.json in its cwd", (outer / "verification.json").is_file(), True) |
| 201 | put(target, {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 0}) |
| 202 | proc = subprocess.run(["sh", str(sbatch)], capture_output=True, text=True, env=env, cwd=outer) |
| 203 | check("check.sbatch rejects a wrong count (exit 1)", proc.returncode, 1) |
| 204 | print(f" check.sbatch shebang: {shebang}") |
| 205 | |
| 206 | # 11. Symlink as configuration.json (skipped where the OS forbids creating one). |
| 207 | with tempfile.TemporaryDirectory() as link_tmp: |
| 208 | link_tmp = Path(link_tmp) |
| 209 | real = link_tmp / "real.json" |
| 210 | real.write_text(json.dumps({"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1})) |
| 211 | route = link_tmp / "route" |
| 212 | route.mkdir() |
| 213 | try: |
| 214 | os.symlink(real, route / "configuration.json") |
| 215 | except OSError as exc: |
| 216 | global SKIPPED |
| 217 | SKIPPED += 1 |
| 218 | print(f"SKIP symlinked configuration.json: cannot create symlink ({exc.__class__.__name__})") |
| 219 | else: |
| 220 | check("symlinked configuration.json exits 1", run(verify_py, [route, "--output", link_tmp / "s.json"])[0], 1) |
| 221 | |
| 222 | print(f"\n{MATCHED} matched, {SKIPPED} skipped, {len(FAILS)} did not match the artifact's documented claim") |
| 223 | return 0 if not FAILS else 1 |
| 224 | |
| 225 | |
| 226 | if __name__ == "__main__": |
| 227 | sys.exit(main()) |