#!/usr/bin/env python3 """Write a deterministic corpus of configurations for differential testing. Usage: python3 make_corpus.py Layout produced: /cases//configuration.json valid input for a counter /invalid//configuration.json input a strict counter must reject /expect.json hand-derived counts for the structured cases ("null" means: only agreement between counters is required, no independent expected value) The random cases use a fixed seed, so two runs produce identical files. """ import json import random import sys from fractions import Fraction from pathlib import Path def num(value): """Render an exact Fraction as JSON int (integral) or "p/q" string.""" value = Fraction(value) if value.denominator == 1: return int(value) return f"{value.numerator}/{value.denominator}" def put(folder, points, claimed=0, schema=1): folder.mkdir(parents=True, exist_ok=True) data = {"schema": schema, "points": [[num(x), num(y)] for x, y in points], "unit_circles": claimed} (folder / "configuration.json").write_text(json.dumps(data) + "\n") return data def on_unit_circle(k, offset=(0, 0)): """k rational points on the unit circle centred at offset (Pythagorean parametrisation).""" ts = [Fraction(0), Fraction(1, 2), Fraction(1), Fraction(2), Fraction(3), Fraction(-1, 2), Fraction(-1), Fraction(5, 2), Fraction(1, 3), Fraction(3, 5), Fraction(-2, 3)] ts = ts[:k] ox, oy = Fraction(offset[0]), Fraction(offset[1]) return [((1 - t * t) / (1 + t * t) + ox, 2 * t / (1 + t * t) + oy) for t in ts] def on_radius_circle(k, radius, offset=(0, 0)): ox, oy = Fraction(offset[0]), Fraction(offset[1]) return [(radius * x + ox, radius * y + oy) for x, y in on_unit_circle(k)] def main(): if len(sys.argv) != 2: print(__doc__.strip().splitlines()[2], file=sys.stderr) return 2 root = Path(sys.argv[1]) cases = root / "cases" invalid = root / "invalid" expect = {} def case(name, points, claimed, independent): put(cases / name, points, claimed) expect[name] = independent # --- structured cases, expectations derived by hand ------------------------- case("three-on-unit-circle", on_unit_circle(3), 1, 1) case("five-on-unit-circle", on_unit_circle(5), 1, 1) case("eight-on-unit-circle", on_unit_circle(8), 1, 1) case("ten-on-unit-circle", on_unit_circle(10), 1, 1) case("five-on-radius-2", on_radius_circle(5, 2), 0, 0) case("five-on-radius-3-shifted", on_radius_circle(5, 3, (1, -2)), 0, 0) case("three-collinear", [(0, 0), (1, 0), (2, 0)], 0, 0) case("five-collinear", [(i, 3) for i in range(5)], 0, 0) case("grid-2x2", [(x, y) for y in range(2) for x in range(2)], 0, 0) case("grid-3x3", [(x, y) for y in range(3) for x in range(3)], 5, 5) # A k-by-k lattice has (k-2)^2 interior centres with four incident points and # 4(k-2) edge centres with three; corner centres have only two and are not counted. # The exact enumeration finds no centre off the lattice, so these counts are exact: # k=4 gives 4 + 8 = 12 and k=5 gives 9 + 12 = 21. case("grid-4x4", [(x, y) for y in range(4) for x in range(4)], 12, 12) case("grid-5x5", [(x, y) for y in range(5) for x in range(5)], 21, 21) case("grid-10x8", [(x, y) for y in range(8) for x in range(10)], 76, 76) # Two unit circles at centres 0 and 6/5 meet at (3/5, +/-4/5). Adding (1,0) and (11/5,0) # gives four unit circles, one per triple: centres (0,0) and (6/5,0) each carry three of # them, and the mixed triples {(3/5,4/5),(1,0),(11/5,0)} and {(3/5,-4/5),(1,0),(11/5,0)} # lie on the unit circles centred (8/5,4/5) and (8/5,-4/5). Derived by hand, not by code. case("two-crossing-unit-circles", [(Fraction(3, 5), Fraction(4, 5)), (Fraction(3, 5), Fraction(-4, 5)), (1, 0), (Fraction(11, 5), 0)], 4, 4) # Four concyclic plus one off the circle: still one circle, counted once. case("four-concyclic-plus-one", on_unit_circle(4) + [(3, 3)], 1, 1) # A pair exactly distance 2 apart (centres coincide) plus a third point elsewhere. case("tangent-pair-distance-2", [(0, 0), (2, 0), (5, 5)], 0, 0) # Three points on a circle of radius 1 but with rational centre not at the origin. case("shifted-unit-circle", on_unit_circle(4, (Fraction(1, 3), Fraction(2, 7))), 1, 1) # Circumcircle has centre (1/2, 1) and radius sqrt(5)/2, not 1, so no unit circle exists. case("half-centre-nonunit", [(0, 0), (1, 0), (0, 2)], 0, 0) # --- fixed-seed random cases ------------------------------------------------ rng = random.Random(20261128) values = [Fraction(i) for i in range(-3, 4)] + [Fraction(p, q) for q in (2, 3, 5, 7) for p in range(-4, 5)] for trial in range(120): n = rng.randint(3, 12) points = [] seen = set() while len(points) < n: x = rng.choice(values) y = rng.choice(values) if (x, y) in seen: continue seen.add((x, y)) points.append((x, y)) case(f"random-{trial:03d}", points, 0, None) # --- inputs a strict counter must reject ------------------------------------- contract = root / "contract" def bad(name, data, bucket=None): bucket = bucket or invalid bucket.mkdir(parents=True, exist_ok=True) (bucket / name).mkdir(exist_ok=True) (bucket / name / "configuration.json").write_text(json.dumps(data) + "\n") good = {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1} bad("float-coordinate", {**good, "points": [[1.0, 0], [0, 1], [-1, 0]]}) bad("boolean-coordinate", {**good, "points": [[True, 0], [0, 1], [-1, 0]]}) bad("decimal-string", {**good, "points": [["1.0", 0], [0, 1], [-1, 0]]}) bad("exponent-string", {**good, "points": [["1e0", 0], [0, 1], [-1, 0]]}) bad("zero-denominator", {**good, "points": [["1/0", 0], [0, 1], [-1, 0]]}) bad("negative-denominator", {**good, "points": [["1/-2", 0], [0, 1], ([-1, 0])]}) bad("33-digit-numerator", {**good, "points": [["1" + "0" * 32, 0], [0, 1], [-1, 0]]}) bad("duplicate-points", {**good, "points": [[1, 0], ["2/2", 0], [0, 1]]}) bad("two-points", {**good, "points": [[1, 0], [0, 1]]}) bad("eighty-one-points", {**good, "points": [[i, 0] for i in range(81)]}) bad("schema-2", {**good, "schema": 2}) # A counter ignores the claimed count, so only the artifact's input contract rejects this. bad("missing-unit-circles", {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]]}, bucket=contract) bad("point-not-a-pair", {**good, "points": [[1, 0, 0], [0, 1], [-1, 0]]}) (root / "expect.json").write_text(json.dumps(expect, indent=2, sort_keys=True) + "\n") print(f"wrote {len(expect)} valid, {len(list(invalid.iterdir()))} invalid and " f"{len(list(contract.iterdir()))} claim-contract cases to {root}") return 0 if __name__ == "__main__": sys.exit(main())