#!/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 exact_count(points): """Exact count by the circle-equation route, used only to fill in claims. A circle through three points satisfies x^2+y^2+Dx+Ey+F=0; subtracting the first point's equation from the other two gives a 2x2 system for (D,E), and the radius is exactly 1 iff D^2+E^2-4F == 4, with centre (-D/2,-E/2). This is a different formula from every counter in panel/ and from the artifact's circumcentre, so agreement is evidence about the mathematics, not about one shared code path. It is still the same author's code, so the 18 structured cases keep their hand-derived expectations and are not replaced by this. """ centres = set() n = len(points) for i in range(n): x1, y1 = points[i] q1 = x1 * x1 + y1 * y1 for j in range(i + 1, n): x2, y2 = points[j] q2 = x2 * x2 + y2 * y2 dx2, dy2, dq2 = x2 - x1, y2 - y1, q2 - q1 for k in range(j + 1, n): x3, y3 = points[k] q3 = x3 * x3 + y3 * y3 dx3, dy3, dq3 = x3 - x1, y3 - y1, q3 - q1 det = dx2 * dy3 - dx3 * dy2 if det == 0: continue # dx*D + dy*E = -dq for the second and third point, so both numerators # carry a minus; the first version of this function dropped them and # negated D and E, which all six counters and the artifact caught. d = -(dq2 * dy3 - dq3 * dy2) / det e = -(dx2 * dq3 - dx3 * dq2) / det f = -q1 - d * x1 - e * y1 if d * d + e * e - 4 * f != 4: continue centres.add((-d / 2, -e / 2)) return len(centres) 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 = {} structured = [] def case(name, points, claimed, independent): put(cases / name, points, claimed) expect[name] = independent structured.append((name, points, 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 ------------------------------------------------ # Every claim below is computed by exact_count, not guessed and not copied from a # counter, so no fixture carries a false claim even where the point of the case is # agreement. Half the cases are built with circles on purpose, so the corpus is not # a long run of 0 == 0. 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)] def scatter(n): points, seen = [], set() while len(points) < n: point = (rng.choice(values), rng.choice(values)) if point in seen: continue seen.add(point) points.append(point) return points def with_circles(): """k points on a unit circle at a random rational centre, plus 0..3 elsewhere.""" t = Fraction(rng.randint(1, 12), rng.randint(1, 12)) centre = (Fraction(rng.randint(-3, 3), rng.randint(1, 5)), Fraction(rng.randint(-3, 3), rng.randint(1, 5))) k = rng.randint(3, 6) ts = [Fraction(rng.randint(-20, 20), rng.randint(1, 9)) for _ in range(k)] points = [] for s in ts: points.append(((1 - s * s) / (1 + s * s) + centre[0], 2 * s / (1 + s * s) + centre[1])) del t points = list(dict.fromkeys(points)) for _ in range(rng.randint(0, 3)): candidate = (rng.choice(values), rng.choice(values)) if candidate not in points: points.append(candidate) if len(points) < 3: points += scatter(3 - len(points)) return points for trial in range(120): points = with_circles() if trial % 2 == 0 else scatter(rng.randint(3, 12)) count = exact_count(points) case(f"random-{trial:03d}", points, count, count) # exact_count fills in every random claim, so prove it against every hand-derived # expectation before writing anything: an error there would plant false claims. for name, points, independent in structured: got = exact_count(points) if got != independent: raise SystemExit(f"exact_count({name}) = {got}, hand-derived {independent}") # --- 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())