Follow a model
Some code is hard to check with examples because it’s fast, not simple: it caches, batches or skips work, and the answer is only wrong after the right run of calls. A model is the same behaviour written to be obviously right, often in one line. proptastic runs random sequences of calls on the code and the model side by side, stops at the first step where they disagree, and shrinks the run to the shortest one that still shows it.
This page does that for a counter that batches its increments and has a bug in the batching. It takes about five minutes, and needs nothing beyond the Quickstart.
1. The code
Section titled “1. The code”BatchedCounter counts, but it adds increments to its total four at a time, so most increments
only touch a small number:
/** Counts, adding increments to the total four at a time. */final class BatchedCounter { private long total; private int pending;
void increment() { pending = pending + 1; if (pending == 4) { pending = 0; total = total + pending; } }
void reset() { total = 0; pending = 0; }
long get() { return total + pending; }}Its example test counts to three, and passes:
@Testvoid countsUpToThree() { var counter = new BatchedCounter(); counter.increment(); counter.increment(); counter.increment(); assertEquals(3, counter.get());}2. The model
Section titled “2. The model”What should a counter do? An increment adds one, a reset clears it. Write that beside the code,
as the state each run starts from: a BatchedCounter, the model’s i, and whether they agree.
/** The counter and its model, side by side. */static final class Counting { final BatchedCounter counter = new BatchedCounter(); long i; // the model: how many increments since the last reset
boolean agree() { return counter.get() == i; }
@Override public String toString() { return "get() = " + counter.get() + ", model i = " + i; }}Then the two steps a test can take, each done on the counter and on the model alike, and what
must hold after every step: they agree, get() is the model’s i.
static Steps<Counting> counting() { return steps(Counting::new) .step( "increment", counting -> { counting.counter.increment(); counting.i = counting.i + 1; }) .step( "reset", counting -> { counting.counter.reset(); counting.i = 0; }) .invariant("get() is the model's i", Counting::agree);}The model is the line counting.i = counting.i + 1: much smaller than the code, and obviously
right. A toString of the state’s own is optional; a failure prints it after the step that broke.
3. Run it
Section titled “3. Run it”@Test@Tag("failing")void theCounterFollowsItsModel() { forAll(counting()).check();}forAll(counting()).check() draws runs of steps, eight on average, takes each on a new counter
and its model, and checks after every step. You don’t choose the sequences: proptastic does.
4. Read it
Section titled “4. Read it”invariant "get() is the model's i" broke after step 4: increment()
theCounterFollowsItsModel failed after 4 tries (seed 0x5eed) shrunk steps: 1. increment() 2. increment() 3. increment() 4. increment() ← get() = 0, model i = 4 pin it: .example( step("increment"), step("increment"), step("increment"), step("increment")) // typed pins come from @StepBuilder rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'BatchedCounterTest.theCounterFollowsItsModel'- The invariant that broke comes first, and the step after which it broke.
- The shrunk steps are the shortest run that still breaks it: four increments. After the
fourth,
get()says 0 and the model says 4. proptastic found a longer random run first and took steps out until none could go. - The pin replays exactly those steps every time. Its note says a step builder would make it
typed,
increment()in place ofstep("increment"); steps by name are enough here.
The example test counted to three, one short of the first batch, so it couldn’t see this.
5. Pin it and fix it
Section titled “5. Pin it and fix it”Paste the pin before .check(), so the four increments run first, every time:
@Test@Tag("failing")void theCounterFollowsItsModelPinned() { var increment = step("increment"); forAll(counting()) .example(increment, increment, increment, increment) // the run it found .check();}Now read increment() again. The batch is cleared before it’s added, so the total gains
nothing. Swap the two lines:
/** Counts, adding increments to the total four at a time. */final class BatchedCounter { private long total; private int pending;
void increment() { pending = pending + 1; if (pending == 4) { total = total + pending; pending = 0; } }
void reset() { total = 0; pending = 0; }
long get() { return total + pending; }}Run the tests again, and both pass: the four increments first, then a hundred random runs, each checked after every step. Keep the example: if a later change brings the bug back, it fails on those four increments first.
What just happened
Section titled “What just happened”You followed a model: a few lines that say what the code should do, run beside the code after every step. The model here lives in the test, in Java. It can also be its own program, in any language, that proptastic runs beside the code, or a design checked by a model checker:
- The counter in TLA+: the same counter, its model a TLA+ spec that every step is checked against.
- The counter in Lean: the same counter, its model a Lean function with a theorem about it.
- Follow a model: a model as its own program, and what a pass is sure of.
- Stateful testing: steps, preconditions and invariants in full.