Skip to content

Every value

A random pass has checked the inputs it drew. When the draws have only a few values, a side, a count from 1 to 10, a flag, it can check every one of them instead, and then a pass is a proof over that domain.

Where the draws have few values, run each once in place of random tries, and a pass has checked them all:

ExhaustivelyTest.java
@Test
void aFeeIsNeverMoreThanALotEach() {
forAll(any(Side.class), integers().atLeast(1).atMost(10), booleans())
.exhaustively()
.check((side, lots, rush) -> assertTrue(fee(side, lots, rush) <= lots));
}
aFeeIsNeverMoreThanALotEach passed every value, all 40

That’s every side, every lot count from 1 to 10 and both flags: 40 values, the simplest first. A counterexample is the first value that fails, shrunk as any other. Here the fee should be waived for an urgent buy of up to 2 lots, but the code says lots < 2:

ExhaustivelyTest.java
@Test
void anUrgentBuyOfUpToTwoLotsIsFree() {
forAll(integers().atLeast(1).atMost(10))
.exhaustively()
.check(lots -> assertTrue(lots > 2 || fee(Side.BUY, lots, true) == 0));
}
Terminal window
expected: <true> but was: <false>
anUrgentBuyOfUpToTwoLotsIsFree failed after 2 tries (seed 0x5eed)
shrunk counterexample: lots = 2
pin it: .example(2)
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'ExhaustivelyTest.anUrgentBuyOfUpToTwoLotsIsFree'

Draws with more than 100,000 values, such as a list, a string or an unbounded number, are refused: bound them, or draw them at random. A filter counts the values it rejects too, so a strict one makes a domain too large.

A random pass whose draws have no more values than it tried says so, as a hint:

ExhaustivelyTest.java
@Test
void aFeeIsNeverMoreThanALot() {
forAll(integers().atLeast(1).atMost(10), booleans())
.check((lots, rush) -> assertTrue(fee(Side.SELL, lots, rush) <= lots));
}
Terminal window
aFeeIsNeverMoreThanALot passed 100 tries · this draws 20 values: .exhaustively() checks them all

The hint never changes what runs: .exhaustively() is yours to add.

A draw with no bound, or a list or a string, has far too many values. .exhaustively() counts them first, and refuses past 100,000, before running any:

ExhaustivelyTest.java
@Test
void aFeeIsNeverMoreThanALotForAnyCount() {
forAll(integers().atLeast(1), booleans())
.exhaustively()
.check((lots, rush) -> assertTrue(fee(Side.SELL, lots, rush) <= lots));
}
aFeeIsNeverMoreThanALotForAnyCount: .exhaustively() runs every value, but its draws have more than 100,000: bound them, or draw them at random

Privacy