| ISSUE 447/456 · COMPUTER SCIENCE |
~4 MIN |
FOUNDATION / COMPUTER SCIENCE · READING DATE 2027-12-22 · DECEMBER 2027 — SYSTEMS THINKING AND COSMIC SCALE
How to test more cases than you can write by hand
START HERE
A programmer can check code that puts a list in order by giving it one list and comparing the result with an expected list. Property-based testing checks general rules across many generated inputs rather than relying only on individually chosen examples. An invariant is a rule that should remain true across the cases the test is meant to cover.
|
/ WHY NOW
A single demonstration that [3, 1, 2] becomes [1, 2, 3] leaves many possible bugs hidden. Empty lists, repeated values and negative numbers can exercise different paths. Generating varied cases makes those paths easier to challenge systematically.
|
/ THE IDEA
First state what the program promises. A correct ascending sort returns values in nondecreasing order and preserves every input value with its number of occurrences. Neither rule alone is enough: a function returning an empty list is perfectly ordered, and a function returning its input preserves values without sorting them. A test generator constructs inputs from a specified domain, meaning the permitted range and kinds of values. When a rule fails, tools can shrink the failure: they search for a smaller input that still demonstrates the defect. The small example helps a person understand the cause.
THE FORMAL IDEA
ordered(result) AND counts(result) = counts(input)
| ordered means each item is no greater than the next; counts records how many times each value appears. | | AND requires both conditions. This is a logical property, not arithmetic. The generated domain must match the values the sorting function claims to support. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Input [3, 1, 2] produces [1, 2, 3]: both rules pass. Change the input to [3, 1, 1, 2]. A buggy implementation that discards duplicates produces [1, 2, 3]. Ordering still passes, but the count of 1 falls from two to one, so preservation fails.
|
The changed input exposes a bug the original example could not see. Checking only a set of distinct values would miss it too, because both lists contain the same distinct numbers. The property must preserve the detail that matters to the promise.
/ SO WHAT?
For a practical project, take one existing example and ask which rule its expected answer illustrates. Generate cases around that rule, especially boundaries and repeated values. Keep known examples as well: generated testing extends human judgement, but does not remove the need to state the intended behaviour precisely.
ONE CAVEAT |
| A passing run is not a proof over every possible input. Generators may neglect important cases, and an incorrect property can approve incorrect software. Randomised or stateful programs also need carefully defined repeatability and comparison rules. |
KEEP THIS
When a program promises a general rule, varied generated inputs can reveal violations that a handful of successful examples never exercise.
|
NEXT: What stops a star from collapsing under its own weight?
|