AI disclosure: This article and its Python examples were generated entirely by OpenAI. The examples and the deliberate mutation were executed before publication.
An exhaustive loop reports a histogram. Checking the total is easy, but the wrong items can still land in the right number of buckets. Here is a compact example where two useful invariants miss a bug and a second enumeration catches it.
An example with sums modulo 6
Given a set of positions, count the residue of x + y for every ordered pair (x, y), including (x, x). For (0, 1, 3), the counts for residues 0 through 5 are (2, 2, 1, 2, 2, 0). The sum is nine, since there are three choices for each coordinate.
Two quick checks follow from the definition. The total must be the square of the number of positions. In each bucket, subtract the contributions from (x, x). The remainder must be nonnegative and even, because (x, y) and (y, x) contribute together.
Verify the buckets with an independent loop
The implementation walks the full Cartesian product. The second version starts with the diagonal and walks unordered pairs of distinct positions, adding two for each pair. The two versions use different loop structures, which gives them a chance to disagree when one skips or double-counts cases.
from collections import Counter from itertools import combinations def ordered_counts(points, modulus): return Counter((x + y) % modulus for x in points for y in points) def check_invariants(counts, points, modulus): diagonals = Counter((2 * x) % modulus for x in points) assert sum(counts.values()) == len(points) ** 2 for r in range(modulus): assert counts[r] >= diagonals[r] assert (counts[r] - diagonals[r]) % 2 == 0 def unordered_oracle(points, modulus): counts = Counter((2 * x) % modulus for x in points) for x, y in combinations(points, 2): counts[(x + y) % modulus] += 2 return counts checked = 0 for modulus in range(1, 9): for size in range(modulus + 1): for points in combinations(range(modulus), size): counts = ordered_counts(points, modulus) check_invariants(counts, points, modulus) assert counts == unordered_oracle(points, modulus) checked += 1 assert checked == 510 Make a wrong answer that passes the quick checks
The following mutation moves two counts from residue 1 to residue 5. The total and the parity of each bucket stay the same. The second implementation detects the changed buckets.
points = (0, 1, 3) wrong = ordered_counts(points, 6) wrong[1] -= 2 wrong[5] += 2 check_invariants(wrong, points, 6) # Passes assert wrong != unordered_oracle(points, 6) # Detects the mutation I ran the comparison on all 510 subsets of the residue sets for moduli 1 through 8, including empty and full sets. Both implementations agreed on that finite corpus. They still share the formula for the residue, so a bug in that common assumption could affect both. A hand-worked case such as (0, 1, 3) checks that part of the specification as well.
When you test a counting routine, try deliberately moving the right number of items to the wrong place. The tests that catch that mutation tell you what your checks actually guarantee.


