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 | """Tests for the pairwise-chord reproduction of PR #4.""" |
| 2 | from fractions import Fraction |
| 3 | import json |
| 4 | from pathlib import Path |
| 5 | import subprocess |
| 6 | import sys |
| 7 | import tempfile |
| 8 | import unittest |
| 9 | |
| 10 | from reproduce import rational_sqrt, solve |
| 11 | |
| 12 | |
| 13 | HERE = Path(__file__).resolve().parent |
| 14 | REPO = HERE.parents[1] |
| 15 | PR4_COMMIT = "73e7e691800cc002741c652ea2eab171320bf988" |
| 16 | PR4_ROUTE = "routes/rusirui-rational-verifier" |
| 17 | |
| 18 | |
| 19 | def git_text(*args): |
| 20 | result = subprocess.run( |
| 21 | ["git", *args], cwd=REPO, check=True, capture_output=True, text=True |
| 22 | ) |
| 23 | return result.stdout |
| 24 | |
| 25 | |
| 26 | def upstream_json(path): |
| 27 | return json.loads(git_text("show", f"{PR4_COMMIT}:{path}")) |
| 28 | |
| 29 | |
| 30 | def fixture_paths(outcome): |
| 31 | prefix = f"{PR4_ROUTE}/samples/{outcome}" |
| 32 | listing = git_text("ls-tree", "-r", "--name-only", PR4_COMMIT, "--", prefix) |
| 33 | return [path for path in listing.splitlines() if path.endswith("/configuration.json")] |
| 34 | |
| 35 | |
| 36 | class ReproductionTests(unittest.TestCase): |
| 37 | def test_grid_output_matches_proposal(self): |
| 38 | config = upstream_json(f"{PR4_ROUTE}/configuration.json") |
| 39 | expected = upstream_json(f"{PR4_ROUTE}/example-verification.json") |
| 40 | self.assertEqual(solve(config), expected) |
| 41 | |
| 42 | def test_all_accept_fixture_counts(self): |
| 43 | files = fixture_paths("accept") |
| 44 | self.assertEqual(len(files), 6) |
| 45 | for path in files: |
| 46 | with self.subTest(sample=Path(path).parent.name): |
| 47 | data = upstream_json(path) |
| 48 | self.assertEqual(solve(data)["unit_circles"], data["unit_circles"]) |
| 49 | |
| 50 | def test_original_cli_on_all_samples_and_stale_output(self): |
| 51 | verifier = git_text("show", f"{PR4_COMMIT}:{PR4_ROUTE}/verify.py") |
| 52 | for outcome in ("accept", "reject"): |
| 53 | files = fixture_paths(outcome) |
| 54 | self.assertEqual(len(files), 6) |
| 55 | for path in files: |
| 56 | with self.subTest(outcome=outcome, sample=Path(path).parent.name): |
| 57 | raw = git_text("show", f"{PR4_COMMIT}:{path}") |
| 58 | with tempfile.TemporaryDirectory() as folder: |
| 59 | root = Path(folder) |
| 60 | route = root / "route" |
| 61 | route.mkdir() |
| 62 | (route / "configuration.json").write_text(raw, encoding="utf-8") |
| 63 | output = root / "verification.json" |
| 64 | output.write_text("stale", encoding="utf-8") |
| 65 | run = subprocess.run( |
| 66 | [sys.executable, "-", str(route), "--output", str(output)], |
| 67 | input=verifier, |
| 68 | cwd=REPO, |
| 69 | capture_output=True, |
| 70 | text=True, |
| 71 | ) |
| 72 | if outcome == "accept": |
| 73 | self.assertEqual(run.returncode, 0, run.stderr) |
| 74 | actual = json.loads(output.read_text(encoding="utf-8")) |
| 75 | self.assertEqual(actual, solve(json.loads(raw))) |
| 76 | else: |
| 77 | self.assertEqual(run.returncode, 1, run.stderr) |
| 78 | self.assertFalse(output.exists()) |
| 79 | |
| 80 | def test_eighty_point_grid(self): |
| 81 | points = [[x, y] for x in range(10) for y in range(8)] |
| 82 | result = solve({"points": points}) |
| 83 | self.assertEqual(result["n"], 80) |
| 84 | self.assertEqual(result["unit_circles"], 76) |
| 85 | |
| 86 | def test_rational_square_detection(self): |
| 87 | self.assertEqual(rational_sqrt(Fraction(9, 16)), Fraction(3, 4)) |
| 88 | self.assertIsNone(rational_sqrt(Fraction(2, 3))) |
| 89 | |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | unittest.main(verbosity=2) |