PLAIN/TEXT — ISSUE 447/456 COMPUTER SCIENCE · FOUNDATION / COMPUTER SCIENCE · ~4 MIN READING DATE: 2027-12-22 ARCHIVE: https://plaintext.click/newsletter/ SUBJECT: How to test more cases than you can write by hand 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. / SOURCE TRAIL [1] Hypothesis — Property-based testing https://hypothesis.readthedocs.io/en/latest/ [2] Hypothesis — Generating and shrinking a failing test https://hypothesis.works/articles/anatomy-of-a-test/ NEXT: What stops a star from collapsing under its own weight? --- HARD IDEAS. PLAIN TEXT. You are receiving this because you asked for one clear computer-science or physics idea each day. Unsubscribe: https://plaintext.click/unsubscribe View in browser: https://plaintext.click/newsletter/2027/12/2027-12-22-property-testing-invariants.html From: Plain/Text / Brickston Consulting · 30 N Gould St Ste N, Sheridan, WY 82801 US · knowledge@plaintext.click