Skip to content
← The curriculum

Invariants over examples

An example tests one input. A property tests the rule, and the generator finds the input you would not have chosen.

Every example-based test picks an input by hand. That input comes from your imagination, which is the same imagination that wrote the bug.

A property test states a rule that should hold for *all* inputs, and lets a generator go looking for a counterexample. You do not choose the input; you describe what must remain true whatever it is.

The rules worth knowing: round-trip — decode of encode is the original. Idempotence — doing it twice is the same as doing it once. Commutativity — order does not matter where it should not. Invariants — a sorted list has the same length and the same elements. Oracle — the fast implementation agrees with the obvious slow one.

1 of 3