vile / scheme / testdata / generate_integer_folds.py
37 lines · 33 sloc · 1.48 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1"""Generate exact-fold results with Python's arbitrary-precision integers."""
2from pathlib import Path
3import random
4
5rng = random.Random(733819)
6minimum, maximum = -(1 << 63), (1 << 63) - 1
7rows = []
8for _ in range(150):
9 terms = []
10 for _ in range(rng.randrange(1, 24)):
11 value = rng.choice([maximum, rng.randrange(1, maximum), 1, 2])
12 terms += [value, -value]
13 terms += [rng.choice([minimum, maximum, 0, 1, -1, 42])]
14 rng.shuffle(terms)
15 # Preserve the fixture's original spelling; whitespace has no numeric role.
16 rows.append(("(+" + " ".join(" " + str(v) for v in terms) + ")", sum(terms)))
17
18 base = rng.choice([minimum, maximum, 0, 1, -1, 42])
19 terms = []
20 for _ in range(rng.randrange(1, 24)):
21 value = rng.choice([maximum, rng.randrange(1, maximum), 1])
22 terms += [value, -value]
23 rng.shuffle(terms)
24 rows.append(("(- " + " ".join(map(str, [base] + terms)) + ")", base - sum(terms)))
25
26 factors = [rng.choice([minimum, maximum, 0, -1, 1, 2, -2])
27 for _ in range(rng.randrange(3, 40))]
28 product = 1
29 for value in factors:
30 product *= value
31 rows.append(("(* " + " ".join(map(str, factors)) + ")", product))
32
33output = "# Python arbitrary-integer oracle, seed 733819; expression TAB exact result.\n"
34output += "\n".join(source + "\t" + str(value)
35 for source, value in rows) + "\n"
36Path(__file__).with_name("integer_folds.tsv").write_text(output)
37print(f"Generated {len(rows)} exact integer folds")
38