"""Tests for the pairwise-chord reproduction of PR #4.""" from fractions import Fraction import json from pathlib import Path import subprocess import sys import tempfile import unittest from reproduce import rational_sqrt, solve HERE = Path(__file__).resolve().parent REPO = HERE.parents[1] PR4_COMMIT = "73e7e691800cc002741c652ea2eab171320bf988" PR4_ROUTE = "routes/rusirui-rational-verifier" def git_text(*args): result = subprocess.run( ["git", *args], cwd=REPO, check=True, capture_output=True, text=True ) return result.stdout def upstream_json(path): return json.loads(git_text("show", f"{PR4_COMMIT}:{path}")) def fixture_paths(outcome): prefix = f"{PR4_ROUTE}/samples/{outcome}" listing = git_text("ls-tree", "-r", "--name-only", PR4_COMMIT, "--", prefix) return [path for path in listing.splitlines() if path.endswith("/configuration.json")] class ReproductionTests(unittest.TestCase): def test_grid_output_matches_proposal(self): config = upstream_json(f"{PR4_ROUTE}/configuration.json") expected = upstream_json(f"{PR4_ROUTE}/example-verification.json") self.assertEqual(solve(config), expected) def test_all_accept_fixture_counts(self): files = fixture_paths("accept") self.assertEqual(len(files), 6) for path in files: with self.subTest(sample=Path(path).parent.name): data = upstream_json(path) self.assertEqual(solve(data)["unit_circles"], data["unit_circles"]) def test_original_cli_on_all_samples_and_stale_output(self): verifier = git_text("show", f"{PR4_COMMIT}:{PR4_ROUTE}/verify.py") for outcome in ("accept", "reject"): files = fixture_paths(outcome) self.assertEqual(len(files), 6) for path in files: with self.subTest(outcome=outcome, sample=Path(path).parent.name): raw = git_text("show", f"{PR4_COMMIT}:{path}") with tempfile.TemporaryDirectory() as folder: root = Path(folder) route = root / "route" route.mkdir() (route / "configuration.json").write_text(raw, encoding="utf-8") output = root / "verification.json" output.write_text("stale", encoding="utf-8") run = subprocess.run( [sys.executable, "-", str(route), "--output", str(output)], input=verifier, cwd=REPO, capture_output=True, text=True, ) if outcome == "accept": self.assertEqual(run.returncode, 0, run.stderr) actual = json.loads(output.read_text(encoding="utf-8")) self.assertEqual(actual, solve(json.loads(raw))) else: self.assertEqual(run.returncode, 1, run.stderr) self.assertFalse(output.exists()) def test_eighty_point_grid(self): points = [[x, y] for x in range(10) for y in range(8)] result = solve({"points": points}) self.assertEqual(result["n"], 80) self.assertEqual(result["unit_circles"], 76) def test_rational_square_detection(self): self.assertEqual(rational_sqrt(Fraction(9, 16)), Fraction(3, 4)) self.assertIsNone(rational_sqrt(Fraction(2, 3))) if __name__ == "__main__": unittest.main(verbosity=2)