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 | """Self-test for count.py. |
| 3 | |
| 4 | Runs, in order: |
| 5 | * the twelve sample fixtures and the 80-point grid fixture through the CLI; |
| 6 | * a set of adversarial configurations invented here (written to ./adversarial); |
| 7 | * a set of invalid configurations that must be rejected; |
| 8 | * a randomized cross-check of count.py against altcount.py, an independently |
| 9 | written second solver that enumerates pairs instead of triples; |
| 10 | * a check that count.py runs from an unrelated working directory. |
| 11 | |
| 12 | Exit status 0 iff every expectation holds. |
| 13 | """ |
| 14 | |
| 15 | import json |
| 16 | import os |
| 17 | import random |
| 18 | import subprocess |
| 19 | import sys |
| 20 | import time |
| 21 | from math import gcd |
| 22 | |
| 23 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 24 | ROOT = os.path.abspath(os.path.join(HERE, os.pardir, os.pardir, os.pardir, os.pardir)) |
| 25 | COUNT = os.path.join(HERE, "count.py") |
| 26 | ALT = os.path.join(HERE, "altcount.py") |
| 27 | CASE_DIR = os.path.join(HERE, "adversarial") |
| 28 | PY = sys.executable |
| 29 | |
| 30 | FAILURES = [] |
| 31 | |
| 32 | |
| 33 | def run_cli(script, path, cwd=None): |
| 34 | started = time.time() |
| 35 | proc = subprocess.run([PY, "-I", script, path], |
| 36 | capture_output=True, text=True, cwd=cwd) |
| 37 | return proc.returncode, proc.stdout, proc.stderr, time.time() - started |
| 38 | |
| 39 | |
| 40 | def check(ok, label, detail=""): |
| 41 | if not ok: |
| 42 | FAILURES.append("%s: %s" % (label, detail)) |
| 43 | return ok |
| 44 | |
| 45 | |
| 46 | def load_count(exit_code, stdout): |
| 47 | if exit_code != 0: |
| 48 | return None |
| 49 | try: |
| 50 | return json.loads(stdout) |
| 51 | except ValueError: |
| 52 | return None |
| 53 | |
| 54 | |
| 55 | # -------------------------------------------------------------------------- |
| 56 | # 1. fixtures |
| 57 | # -------------------------------------------------------------------------- |
| 58 | |
| 59 | FIXTURE_DIR = os.path.join(ROOT, "routes", "rusirui-repro-verifier", "fixtures") |
| 60 | |
| 61 | # expectation kinds: "match" = exit 0 and computed == claimed, |
| 62 | # "mismatch" = exit 0 and computed != claimed, |
| 63 | # "invalid" = exit 1, nothing on stdout. |
| 64 | FIXTURES = [ |
| 65 | ("samples/accept/collinear", "match"), |
| 66 | ("samples/accept/dedup", "match"), |
| 67 | ("samples/accept/grid", "match"), |
| 68 | ("samples/accept/nonunit", "match"), |
| 69 | ("samples/accept/rational-center", "match"), |
| 70 | ("samples/accept/three", "match"), |
| 71 | ("samples/reject/collinear", "mismatch"), |
| 72 | ("samples/reject/duplicate", "invalid"), |
| 73 | ("samples/reject/float", "invalid"), |
| 74 | ("samples/reject/nearly-unit", "mismatch"), |
| 75 | ("samples/reject/overcount", "mismatch"), |
| 76 | ("samples/reject/undercount", "mismatch"), |
| 77 | ("grid80", "match"), |
| 78 | ] |
| 79 | |
| 80 | |
| 81 | def fixture_report(): |
| 82 | print("== fixtures ==") |
| 83 | for rel, kind in FIXTURES: |
| 84 | path = os.path.join(FIXTURE_DIR, rel, "configuration.json") |
| 85 | with open(path, "rb") as handle: |
| 86 | claimed = json.loads(handle.read()).get("unit_circles", "-") |
| 87 | code, out, err, elapsed = run_cli(COUNT, path) |
| 88 | parsed = load_count(code, out) |
| 89 | computed = parsed["unit_circles"] if parsed else "-" |
| 90 | print("%s | %s | %s | %s (%.2fs)" |
| 91 | % (rel + "/configuration.json", code, computed, claimed, elapsed)) |
| 92 | if kind == "invalid": |
| 93 | check(code == 1, rel, "expected exit 1, got %d" % code) |
| 94 | check(out == "", rel, "expected empty stdout on invalid input") |
| 95 | else: |
| 96 | check(code == 0, rel, "expected exit 0, got %d (%s)" |
| 97 | % (code, err.strip())) |
| 98 | if kind == "match": |
| 99 | check(computed == claimed, rel, |
| 100 | "computed %s != claimed %s" % (computed, claimed)) |
| 101 | else: |
| 102 | check(computed != claimed, rel, |
| 103 | "computed %s equals claimed %s" % (computed, claimed)) |
| 104 | # second implementation must agree with the first, byte for byte |
| 105 | alt_code, alt_out, alt_err, _ = run_cli(ALT, path) |
| 106 | check((alt_code, alt_out) == (code, out), rel, |
| 107 | "altcount disagreed: exit %d vs %d, stdout %r vs %r (%s)" |
| 108 | % (alt_code, code, alt_out, out, alt_err.strip())) |
| 109 | |
| 110 | |
| 111 | # -------------------------------------------------------------------------- |
| 112 | # 2. adversarial cases |
| 113 | # -------------------------------------------------------------------------- |
| 114 | |
| 115 | def rat(num, den=1): |
| 116 | if den < 0: |
| 117 | num, den = -num, -den |
| 118 | g = gcd(abs(num), den) |
| 119 | return (num // g, den // g) |
| 120 | |
| 121 | |
| 122 | def add(left, right): |
| 123 | return rat(left[0] * right[1] + right[0] * left[1], left[1] * right[1]) |
| 124 | |
| 125 | |
| 126 | def text(pair): |
| 127 | return str(pair[0]) if pair[1] == 1 else "%d/%d" % pair |
| 128 | |
| 129 | |
| 130 | def doc_of(points, claimed=None): |
| 131 | """points: [( (xn,xd), (yn,yd) ), ...] -> configuration document. |
| 132 | |
| 133 | Integral coordinates are written as JSON integers and non-integral ones as |
| 134 | "p/q" strings, the only two forms the input schema allows. |
| 135 | """ |
| 136 | def wire(pair): |
| 137 | return pair[0] if pair[1] == 1 else "%d/%d" % pair |
| 138 | |
| 139 | body = [[wire(px), wire(py)] for px, py in points] |
| 140 | document = {"schema": 1, "points": body} |
| 141 | if claimed is not None: |
| 142 | document["unit_circles"] = claimed |
| 143 | return document |
| 144 | |
| 145 | |
| 146 | UNIT_VECTORS = [] |
| 147 | for _u, _v in ((0, 1), (1, 0), (1, 1), (1, 2), (2, 1), (1, 3), (3, 1), |
| 148 | (2, 3), (3, 2), (1, 4), (4, 1), (2, 5), (5, 2)): |
| 149 | _d = _v * _v + _u * _u |
| 150 | UNIT_VECTORS.append(((rat(_v * _v - _u * _u, _d), rat(2 * _u * _v, _d)))) |
| 151 | |
| 152 | |
| 153 | def circle_points(center, vectors): |
| 154 | cx, cy = center |
| 155 | return [(add(cx, vx), add(cy, vy)) for vx, vy in vectors] |
| 156 | |
| 157 | |
| 158 | def adversarial_cases(): |
| 159 | cases = [] |
| 160 | |
| 161 | # (a) eight rational points on one circle; ten cocircular triples must fold |
| 162 | # into a single center, with denominators 5 and 13 forcing L = 65. |
| 163 | pts = [((1, 1), (0, 1)), ((-1, 1), (0, 1)), ((0, 1), (1, 1)), ((0, 1), (-1, 1)), |
| 164 | ((rat(3, 5), rat(4, 5))), ((rat(-3, 5), rat(4, 5))), |
| 165 | ((rat(5, 13), rat(12, 13))), ((rat(-5, 13), rat(12, 13)))] |
| 166 | cases.append(("four_on_one_circle", doc_of(pts, 1), 1, |
| 167 | [((0, 1), (0, 1))])) |
| 168 | |
| 169 | # (b) two points exactly 2 apart: the unit circle through them is centred at |
| 170 | # the midpoint and also carries two more points. |
| 171 | pts = [((0, 1), (0, 1)), ((2, 1), (0, 1)), ((1, 1), (1, 1)), ((1, 1), (-1, 1))] |
| 172 | cases.append(("distance_two", doc_of(pts, 1), 1, [((1, 1), (0, 1))])) |
| 173 | |
| 174 | # (c) same pair at distance 2 but the third point misses: circumcentre |
| 175 | # (1, 3/4), radius 5/4. Must not count. |
| 176 | pts = [((0, 1), (0, 1)), ((2, 1), (0, 1)), ((1, 1), (2, 1))] |
| 177 | cases.append(("distance_two_miss", doc_of(pts, 1), 0, [])) |
| 178 | |
| 179 | # (d) four collinear points. |
| 180 | pts = [((i, 1), (0, 1)) for i in range(4)] |
| 181 | cases.append(("collinear_four", doc_of(pts, 1), 0, [])) |
| 182 | |
| 183 | # (e) (0,0),(1,0) lie on a unit circle only with an irrational centre, so a |
| 184 | # third rational point can never complete one; here the circumradius of |
| 185 | # the triangle is sqrt(2)/2 anyway. |
| 186 | pts = [((0, 1), (0, 1)), ((1, 1), (0, 1)), ((1, 1), (1, 1))] |
| 187 | cases.append(("irrational_center_pair", doc_of(pts, 1), 0, [])) |
| 188 | |
| 189 | # (f) four points on the unit circle centred at (1/3, 2/7); the common |
| 190 | # denominator is 105 and the answer has a non-integral centre. |
| 191 | centre = (rat(1, 3), rat(2, 7)) |
| 192 | pts = circle_points(centre, [UNIT_VECTORS[2], UNIT_VECTORS[5], |
| 193 | UNIT_VECTORS[10], UNIT_VECTORS[8]]) |
| 194 | cases.append(("rational_center_105", doc_of(pts, 1), 1, [centre])) |
| 195 | |
| 196 | # (g) two disjoint unit circles, exercising sort order and dedup across |
| 197 | # many triples. |
| 198 | pts = circle_points(((0, 1), (0, 1)), |
| 199 | [UNIT_VECTORS[0], UNIT_VECTORS[2], |
| 200 | UNIT_VECTORS[4], UNIT_VECTORS[6]]) |
| 201 | pts += circle_points(((10, 1), (0, 1)), |
| 202 | [UNIT_VECTORS[1], UNIT_VECTORS[3], |
| 203 | UNIT_VECTORS[5], UNIT_VECTORS[7]]) |
| 204 | cases.append(("two_circles", doc_of(pts, 2), 2, |
| 205 | [((0, 1), (0, 1)), ((10, 1), (0, 1))])) |
| 206 | |
| 207 | # (h) centres at (-1,0), (0,1) and (1,0) -- three of them, since (-1,1), |
| 208 | # (0,0), (1,1) are also unit apart from (0,1). The printed order must |
| 209 | # be the exact rational ascending order, not string order. |
| 210 | pts = [((-2, 1), (0, 1)), ((-1, 1), (1, 1)), ((0, 1), (0, 1)), |
| 211 | ((2, 1), (0, 1)), ((1, 1), (1, 1))] |
| 212 | cases.append(("sorted_negative", doc_of(pts, 3), 3, |
| 213 | [((-1, 1), (0, 1)), ((0, 1), (1, 1)), ((1, 1), (0, 1))])) |
| 214 | |
| 215 | # (i) a near miss whose radius is 1 + 1.25e-25: a double would round it to |
| 216 | # exactly 1 and count it. Exact arithmetic must say no. |
| 217 | big = 10 ** 12 |
| 218 | pts = [((0, 1), (0, 1)), ((2, 1), (0, 1)), ((1, 1), rat(big + 1, big))] |
| 219 | cases.append(("nearly_unit_tiny", doc_of(pts, 1), 0, [])) |
| 220 | |
| 221 | # (j) the same with v = 1 exactly: a real unit circle through all three. |
| 222 | pts = [((0, 1), (0, 1)), ((2, 1), (0, 1)), ((1, 1), (1, 1))] |
| 223 | cases.append(("exact_unit", doc_of(pts, 1), 1, [((1, 1), (0, 1))])) |
| 224 | |
| 225 | # (k) 80 points of the half-integer 10x8 grid. Scaling by 2 turns them |
| 226 | # into the full 10x8 lattice with radius 2. A pair at distance^2 q has |
| 227 | # a rational centre iff (16-q)q is a square, and among the sums of two |
| 228 | # squares q <= 16 only q = 16 (axis pairs, lattice centre) and q = 8 |
| 229 | # (diagonal pairs, half-integer centre) qualify; a half-integer centre |
| 230 | # meets no lattice point at distance 2 (odd squares are 1 mod 8, so |
| 231 | # their sum is 2 mod 8, never 16). Lattice centres with at least three |
| 232 | # of their four two-step neighbours: 6*4 = 24 interior ones with four |
| 233 | # and 8+8+12+12 = 40 on the two-step edge bands with three, so 64. |
| 234 | pts = [] |
| 235 | for j in range(8): |
| 236 | for i in range(10): |
| 237 | pts.append(((rat(i, 2)), (rat(j, 2)))) |
| 238 | cases.append(("half_grid_80", doc_of(pts, 64), 64, None)) |
| 239 | |
| 240 | # (l) fifteen disjoint unit circles, four points each: 60 points, and all |
| 241 | # fifteen centres must be reported. |
| 242 | pts = [] |
| 243 | centres = [] |
| 244 | for k in range(15): |
| 245 | centre = ((20 * k, 1), (0, 1)) |
| 246 | centres.append(centre) |
| 247 | vecs = [UNIT_VECTORS[(4 * k + t) % len(UNIT_VECTORS)] for t in range(4)] |
| 248 | pts += circle_points(centre, vecs) |
| 249 | cases.append(("many_circles", doc_of(pts, 15), 15, centres)) |
| 250 | |
| 251 | # (m) a unit circle far from the origin, so that the modular pre-filter sees |
| 252 | # residues that differ from the coordinates themselves. |
| 253 | centre = ((10 ** 12, 1), (10 ** 12, 1)) |
| 254 | pts = circle_points(centre, [UNIT_VECTORS[0], UNIT_VECTORS[1], |
| 255 | UNIT_VECTORS[2], UNIT_VECTORS[4]]) |
| 256 | cases.append(("huge_offset_circle", doc_of(pts, 1), 1, [centre])) |
| 257 | |
| 258 | # (n) a unit circle whose points have 32-digit denominators: a true |
| 259 | # positive that the pre-filter must not lose, cross-checked against the |
| 260 | # unfiltered pair-based solver. |
| 261 | a = 10 ** 16 + 1 |
| 262 | b = 10 ** 16 + 3 |
| 263 | d = b * b + a * a |
| 264 | huge_vectors = [(rat(b * b - a * a, d), rat(2 * a * b, d)), |
| 265 | (rat(a * a - b * b, d), rat(2 * a * b, d)), |
| 266 | ((0, 1), (1, 1)), |
| 267 | ((1, 1), (0, 1))] |
| 268 | centre = (rat(1, 3), rat(2, 7)) |
| 269 | pts = circle_points(centre, huge_vectors) |
| 270 | cases.append(("huge_denominator_circle", doc_of(pts, 1), 1, [centre])) |
| 271 | |
| 272 | return cases |
| 273 | |
| 274 | |
| 275 | def adversarial_report(): |
| 276 | print("== adversarial cases ==") |
| 277 | os.makedirs(CASE_DIR, exist_ok=True) |
| 278 | for name, document, expected, centres in adversarial_cases(): |
| 279 | path = os.path.join(CASE_DIR, name + ".json") |
| 280 | with open(path, "w", encoding="utf-8") as handle: |
| 281 | json.dump(document, handle, indent=1) |
| 282 | handle.write("\n") |
| 283 | code, out, err, elapsed = run_cli(COUNT, path) |
| 284 | alt_code, alt_out, alt_err, _ = run_cli(ALT, path) |
| 285 | parsed = load_count(code, out) |
| 286 | computed = parsed["unit_circles"] if parsed else "-" |
| 287 | print("%s | %d | %s | claimed %s (%.2fs)" |
| 288 | % (name, code, computed, document.get("unit_circles"), elapsed)) |
| 289 | check(code == 0, name, "expected exit 0, got %d (%s)" % (code, err.strip())) |
| 290 | check(computed == expected, name, |
| 291 | "expected %s circles, got %s" % (expected, computed)) |
| 292 | check((alt_code, alt_out) == (code, out), name, |
| 293 | "altcount disagreed: %r vs %r" % (alt_out, out)) |
| 294 | if centres is not None and parsed is not None: |
| 295 | reported = [tuple(centre) for centre in parsed["centers"]] |
| 296 | for centre in centres: |
| 297 | want = (text(centre[0]), text(centre[1])) |
| 298 | check(want in reported, name, "missing centre %s" % (want,)) |
| 299 | |
| 300 | |
| 301 | # -------------------------------------------------------------------------- |
| 302 | # 3. invalid inputs |
| 303 | # -------------------------------------------------------------------------- |
| 304 | |
| 305 | def invalid_report(): |
| 306 | print("== invalid inputs ==") |
| 307 | folder = os.path.join(CASE_DIR, "invalid") |
| 308 | os.makedirs(folder, exist_ok=True) |
| 309 | good = [[0, 0], [1, 0], [0, 1]] |
| 310 | cases = [ |
| 311 | ("schema_2", {"schema": 2, "points": good, "unit_circles": 0}), |
| 312 | ("schema_float", {"schema": 1.0, "points": good, "unit_circles": 0}), |
| 313 | ("schema_bool", {"schema": True, "points": good, "unit_circles": 0}), |
| 314 | ("schema_missing", {"points": good, "unit_circles": 0}), |
| 315 | ("n_2", {"schema": 1, "points": [[0, 0], [1, 0]]}), |
| 316 | ("n_81", {"schema": 1, "points": [[i, 0] for i in range(81)]}), |
| 317 | ("duplicate_repr", {"schema": 1, |
| 318 | "points": [["1/2", 0], ["2/4", 0], [0, 1]]}), |
| 319 | ("bool_coord", {"schema": 1, "points": [[True, 0], [1, 0], [0, 1]]}), |
| 320 | ("float_coord", {"schema": 1, "points": [[1.0, 0], [1, 0], [0, 1]]}), |
| 321 | ("string_decimal", {"schema": 1, "points": [["0.5", 0], [1, 0], [0, 1]]}), |
| 322 | ("string_space", {"schema": 1, "points": [[" 1/2", 0], [1, 0], [0, 1]]}), |
| 323 | ("string_integer", {"schema": 1, "points": [["1", 0], [1, 0], [0, 1]]}), |
| 324 | ("zero_denominator", {"schema": 1, "points": [["1/0", 0], [1, 0], [0, 1]]}), |
| 325 | ("negative_denominator", {"schema": 1, |
| 326 | "points": [["1/-2", 0], [1, 0], [0, 1]]}), |
| 327 | ("point_len_3", {"schema": 1, "points": [[1, 2, 3], [1, 0], [0, 1]]}), |
| 328 | ("point_not_pair", {"schema": 1, "points": ["12", [1, 0], [0, 1]]}), |
| 329 | ("points_not_list", {"schema": 1, "points": {"a": 1}}), |
| 330 | ("unicode_digits", {"schema": 1, |
| 331 | "points": [["١/٢", 0], [1, 0], [0, 1]]}), |
| 332 | ] |
| 333 | for name, document in cases: |
| 334 | path = os.path.join(folder, name + ".json") |
| 335 | with open(path, "w", encoding="utf-8") as handle: |
| 336 | json.dump(document, handle) |
| 337 | handle.write("\n") |
| 338 | code, out, err, _ = run_cli(COUNT, path) |
| 339 | alt = run_cli(ALT, path) |
| 340 | print("%s | %d | %s" % (name, code, (err.strip().splitlines() or [""])[0])) |
| 341 | check(code == 1, name, "expected exit 1, got %d" % code) |
| 342 | check(out == "", name, "expected empty stdout") |
| 343 | check((alt[0], alt[1]) == (code, out), name, |
| 344 | "altcount differed: %r" % (alt[2].strip(),)) |
| 345 | |
| 346 | raw_cases = [("float_exponent", '{"schema": 1, "points": [[1e3, 0], [1, 0], [0, 1]]}'), |
| 347 | ("not_object", '[]'), |
| 348 | ("bad_json", '{"schema": 1, "points": ['), |
| 349 | ("empty_file", "")] |
| 350 | for name, body in raw_cases: |
| 351 | path = os.path.join(folder, name + ".json") |
| 352 | with open(path, "w", encoding="utf-8") as handle: |
| 353 | handle.write(body) |
| 354 | code, out, err, _ = run_cli(COUNT, path) |
| 355 | alt = run_cli(ALT, path) |
| 356 | print("%s | %d | %s" % (name, code, (err.strip().splitlines() or [""])[0])) |
| 357 | check(code == 1, name, "expected exit 1, got %d" % code) |
| 358 | check(out == "", name, "expected empty stdout") |
| 359 | check((alt[0], alt[1]) == (code, out), name, "altcount differed") |
| 360 | |
| 361 | missing = os.path.join(folder, "does-not-exist.json") |
| 362 | code, out, err, _ = run_cli(COUNT, missing) |
| 363 | print("missing_file | %d | %s" % (code, (err.strip().splitlines() or [""])[0])) |
| 364 | check(code == 1 and out == "", "missing_file", "expected exit 1, no stdout") |
| 365 | |
| 366 | |
| 367 | # -------------------------------------------------------------------------- |
| 368 | # 4. randomized cross-check against the pair-based solver |
| 369 | # -------------------------------------------------------------------------- |
| 370 | |
| 371 | def randomized_report(): |
| 372 | print("== randomized cross-check (count.py vs altcount.py) ==") |
| 373 | sys.path.insert(0, HERE) |
| 374 | import count as impl |
| 375 | import altcount as altimpl |
| 376 | |
| 377 | rng = random.Random(20261128) |
| 378 | denoms = [1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12] |
| 379 | mismatches = 0 |
| 380 | trials = 0 |
| 381 | |
| 382 | def compare(document, label): |
| 383 | nonlocal mismatches, trials |
| 384 | trials += 1 |
| 385 | try: |
| 386 | _, pts_a = impl.read_points(json.loads(json.dumps(document))) |
| 387 | want = impl.solve(pts_a) |
| 388 | status = "solved" |
| 389 | except impl.Invalid: |
| 390 | want = None |
| 391 | status = "invalid" |
| 392 | try: |
| 393 | _, pts_b = altimpl.read_points(json.loads(json.dumps(document))) |
| 394 | got = altimpl.solve(pts_b) |
| 395 | except altimpl.Invalid: |
| 396 | got = None |
| 397 | if (want is None) != (got is None) or (want is not None and want != got): |
| 398 | mismatches += 1 |
| 399 | print("MISMATCH %s: %r -> %r vs %r" % (label, document, want, got)) |
| 400 | return want, status |
| 401 | |
| 402 | # purely random point sets |
| 403 | for round_index in range(40): |
| 404 | count_points = rng.randint(3, 14) |
| 405 | points = [] |
| 406 | seen = set() |
| 407 | while len(points) < count_points: |
| 408 | px = rat(rng.randint(-25, 25), rng.choice(denoms)) |
| 409 | py = rat(rng.randint(-25, 25), rng.choice(denoms)) |
| 410 | if (px, py) in seen: |
| 411 | continue |
| 412 | seen.add((px, py)) |
| 413 | points.append((px, py)) |
| 414 | compare(doc_of(points), "random-%d" % round_index) |
| 415 | |
| 416 | # constructed unions of unit circles plus noise |
| 417 | constructed_failures = 0 |
| 418 | for round_index in range(20): |
| 419 | points = [] |
| 420 | seen = set() |
| 421 | centres = [] |
| 422 | ks = rng.randint(1, 4) |
| 423 | for _ in range(ks): |
| 424 | centre = (rat(rng.randint(-30, 30), rng.choice(denoms)), |
| 425 | rat(rng.randint(-30, 30), rng.choice(denoms))) |
| 426 | if centre in centres: |
| 427 | continue |
| 428 | centres.append(centre) |
| 429 | for vec in rng.sample(UNIT_VECTORS, rng.randint(3, 5)): |
| 430 | pt = (add(centre[0], vec[0]), add(centre[1], vec[1])) |
| 431 | if pt not in seen: |
| 432 | seen.add(pt) |
| 433 | points.append(pt) |
| 434 | while len(points) < 3: |
| 435 | px = rat(rng.randint(-9, 9), 1) |
| 436 | py = rat(rng.randint(-9, 9), 1) |
| 437 | if (px, py) in seen: |
| 438 | continue |
| 439 | seen.add((px, py)) |
| 440 | points.append((px, py)) |
| 441 | rng.shuffle(points) |
| 442 | document = doc_of(points[:80]) |
| 443 | found, _ = compare(document, "constructed-%d" % round_index) |
| 444 | if found is None: |
| 445 | constructed_failures += 1 |
| 446 | continue |
| 447 | for centre in centres: |
| 448 | if centre not in found: |
| 449 | constructed_failures += 1 |
| 450 | print("MISSING CONSTRUCTED CENTRE %s in %s" |
| 451 | % (centre, [text(c) for c in centre])) |
| 452 | |
| 453 | print("trials: %d, mismatches: %d, constructed failures: %d" |
| 454 | % (trials, mismatches, constructed_failures)) |
| 455 | check(mismatches == 0, "randomized", "%d mismatches" % mismatches) |
| 456 | check(constructed_failures == 0, "constructive", |
| 457 | "%d constructed circles missing" % constructed_failures) |
| 458 | |
| 459 | |
| 460 | # -------------------------------------------------------------------------- |
| 461 | # 5. positional independence |
| 462 | # -------------------------------------------------------------------------- |
| 463 | |
| 464 | def cwd_report(): |
| 465 | print("== working-directory independence ==") |
| 466 | path = os.path.join(FIXTURE_DIR, "samples", "accept", "grid", "configuration.json") |
| 467 | import tempfile |
| 468 | with tempfile.TemporaryDirectory() as scratch: |
| 469 | code, out, err, _ = run_cli(COUNT, path, cwd=scratch) |
| 470 | print("run from %s | exit %d | %s" % ("a temp dir", code, out.strip()[:60])) |
| 471 | check(code == 0 and json.loads(out)["unit_circles"] == 5, |
| 472 | "cwd", "expected exit 0 and 5 circles, got %d %r %r" % (code, out, err)) |
| 473 | |
| 474 | |
| 475 | def main(): |
| 476 | fixture_report() |
| 477 | adversarial_report() |
| 478 | invalid_report() |
| 479 | randomized_report() |
| 480 | cwd_report() |
| 481 | print("== summary ==") |
| 482 | if FAILURES: |
| 483 | print("FAILURES: %d" % len(FAILURES)) |
| 484 | for item in FAILURES: |
| 485 | print(" " + item) |
| 486 | return 1 |
| 487 | print("all checks passed") |
| 488 | return 0 |
| 489 | |
| 490 | |
| 491 | if __name__ == "__main__": |
| 492 | sys.exit(main()) |