How values are drawn
A passing property has checked the values it drew, and no others. If you know how proptastic picks them, you know what a pass is worth: which values were checked often, which hardly ever, and which never. This page says how often each domain draws what, and how to change it. Every share on it is measured by a test in the build, so it can’t drift from the library.
One rule under every domain
Section titled “One rule under every domain”A domain doesn’t draw values directly. It draws choices, whole numbers from 0 up to a limit the domain sets, and turns each choice into a value. Choice 0 is always the simplest value: 0, the first of a list of values, an empty list. A random try draws each choice like this:
| How often | The choice |
|---|---|
| 1 try in 16 | the largest |
| 5 in 16 | one of the 4 smallest, evenly |
| 5 in 16 | one of the 256 smallest, evenly |
| 5 in 16 | any, evenly |
So small choices come up most, and the largest one try in 16. Bugs live at the edges, and this is how proptastic gets to them. The shares below all follow from it. Domains and choices says why choices, and what else rests on them.
Here are a hundred inputs of two numbers drawn by this rule, the smallest values at the bottom left. They crowd that corner, and line the top and right edges, where each number is at its largest:
Property tests: a hundred drawn inputs, crowded at the small values and the far edges. One fails, and shrinks to the smallest that still fails.
Numbers
Section titled “Numbers”static Domain<Integer> quantities() { return integers().atLeast(1).atMost(10_000);}Choice 0 is the value nearest 0 that the bounds allow, here 1. The choices after it go outwards,
the positive before the negative: 0, 1, −1, 2, −2 and so on when 0 is in range. So quantities()
draws:
| Value | How often |
|---|---|
| 1 | about 8% |
| 1 to 4 | about a third |
| 1 to 256 | nearly two in three |
| 10,000, the far end | one try in 16 |
| 257 to 9,999 | the rest, evenly |
integers() with no bounds draws 0 about 8% of the time, and a number between −128 and 128 nearly
three times in five. Both its ends are drawn one try in 16: Integer.MIN_VALUE, the far end, and
Integer.MAX_VALUE, the near one. integers().atLeast(-100).atMost(100) draws −100 and 100 each
one try in 16. longs() draws the same way.
doubles() with no bounds draws NaN, Infinity and -Infinity, one try in 16 among them,
where comparisons, Math.max, sorting and equals go wrong. This hand-written maximum skips a
NaN that Math.max would keep:
static double largest(List<Double> numbers) { double largest = Double.NEGATIVE_INFINITY; for (double number : numbers) { if (number > largest) { largest = number; } } return largest;}Checked against the stream’s max(), it fails with numbers = [NaN], pinned as
List.of(Double.NaN). Each is drawn in place of a number drawn first, so a failure that number
shares shrinks to it, and then as any number does; one only NaN or an infinity causes, as above,
is reported as it is.
doubles().finite() leaves them out, and so does any bound.
Otherwise, a try draws one of three kinds, a third each: a whole
number, drawn as integers() would draw it; a whole number and some sixteenths; or any number in
range, each end of it one time in 16. A range with only one whole number in it, such as
doubles().atLeast(0).lessThan(1), draws no whole-number kind: half its tries are sixteenths and
half any number. So it draws 0.0 about 6% of the time, a multiple of 1/16 about half the time,
and the largest double below 1 about 3%.
booleans() draws true half the time.
A choice of values or kinds
Section titled “A choice of values or kinds”elements(…), oneOf(…) and any(…) of an enum make one choice among the values or domains
given, each as likely as the others: elements("AAPL", "MSFT", "NVDA") draws each a third of the
time, and elements(1, 2, …, 10) each a tenth. The first is the simplest, so put the values you
most want checked first: that’s where a failure shrinks to.
Collections and strings
Section titled “Collections and strings”A list draws its minimum number of elements, then flips for each one more: another element seven times in eight, unless an average size is given. With no size given, a list is:
- empty one time in 8;
- about 7 long on average;
- longer than 30 less than one time in 50. The longest, 100, is all but never drawn.
A size bound stops the flips, so the longest size allowed comes up often:
lists(integers()).ofSize(atLeast(1), atMost(5)) draws a list of 5 about 59% of the time. A
minimum above 50 raises the longest to twice the minimum. Sets and maps draw the same way, but
drop a value or key drawn twice, so they come out smaller. A set that can’t reach its minimum
size, because the elements don’t have that many distinct values, discards the try.
A string’s length is drawn the same way, counted in code points. Each character is one of about
50 edge characters one time in four, as Domains says. The rest follow the rule
above, in the order a to z, A to Z, digits, the rest of ASCII, control characters, then
all of Unicode. So about a quarter of all characters are a, b, c or d, and about three in
ten lie past U+FFFF, a surrogate pair in a Java string. Most of those are code points with no
character assigned.
optionals(…) draws an empty Optional one time in four.
map, filter and flatMap
Section titled “map, filter and flatMap”.map(…) changes each value but not how often it comes up.
integers().atLeast(1).atMost(100).map(lots -> lots * 100) draws 100 and 10,000 as often as the
numbers draw 1 and 100: about 8% and 7%.
.filter(…) draws again until a value passes, so the shares are those of the values that pass.
That can surprise. quantities().filter(quantity -> quantity % 100 == 0) draws the same round lots
as the map above, but 10,000 is the far end of quantities(), drawn one try in 16, and few other
draws pass. So about 92% of what the filter draws is 10,000. A filter that rejects 100 values in a
row discards the try. A run that discards more than ten times its number of tries stops with an
error that says what discarded them.
.flatMap(…) draws a value, picks a domain with it, then draws from that domain. A record built
with domain(…) draws each part in turn, each by its own domain, and none depends on another.
How to change what’s drawn
Section titled “How to change what’s drawn”Bounds move both ends: the value nearest 0 becomes the simplest, and each end is drawn one try in 16. Bound a number to what can really happen, and the edges you care about are the ones drawn.
elements(…) draws exactly the values you list. Repeat one to draw it more often:
static Domain<String> sidesMostlyBuying() { return elements("BUY", "BUY", "BUY", "SELL");}This draws "BUY" three times in four.
weighted(…) says the same of whole domains, inside a oneOf(…):
static Domain<String> sidesWeighted() { return oneOf(weighted(3, elements("BUY")), elements("SELL"));}Each part is drawn in proportion to its weight, and a part without one weighs 1, so this too draws
"BUY" three times in four. A failure still shrinks to the first part.
.edges(…) gives a domain edge values of your own, drawn one try in 8 on top of the domain’s
own. A leap-year rule that forgets every 400th year passes 100 tries of integers().atLeast(1) .atMost(3000), as only 7 of its 3,000 years catch it. With the century years as edges it fails:
static Domain<Integer> years() { var years = integers().atLeast(1).atMost(3000); return years.edges(1600, 1700, 1800, 1900, 2000, 2100);}A counterexample found at an edge value shrinks among the edges, toward the first, then leaves them
for the smallest value that fails, an edge or not: this one is found at 1600 or 2000 and reports
year = 400, a year none of the edges is.
oneOf(…) adds values of your own to a domain, such as both sides of a price break:
static Domain<Integer> quantitiesNearTheBreak() { return oneOf(quantities(), elements(999, 1_000, 1_001));}About half the tries draw from the list. Put the domain that covers everything first: a failure shrinks toward the first, so here it still shrinks to 1.
strings().from(…) draws strings from an alphabet, its characters only:
static Domain<String> sequences() { return strings().from("ACGT").ofLength(atLeast(1), atMost(12));}Each character is drawn as often as another, so one given twice is drawn twice as often, and the
first is the simplest: a counterexample shrinks to "A". To draw from words rather than characters, join
a list of them, as the idempotence page does with names.
strings().matching(…) draws strings a regular expression matches in full, in Java’s
syntax, for a format of your own:
static Domain<String> skus() { return strings().matching("[A-Z]{3}-\\d{4}");}Each alternative is as likely as another, a repeat is its least and about 7 more unless it says its
most, and each character is as likely as another in its class. A counterexample shrinks to the first
alternative, the fewest repeats and the simplest characters: a, then the letters, then digits.
Here a stock code is read and written back, and the number loses its leading zeros:
@Testvoid aSkuReadsBackAsWritten() { forAll(skus()).check(sku -> assertEquals(sku, Sku.parse(sku).text()));}expected: <AAA-0000> but was: <AAA-0>
aSkuReadsBackAsWritten failed after 7 tries (seed 0x5eed) shrunk counterexample: sku = "AAA-0000" pin it: .example("AAA-0000") rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'SkuTest.aSkuReadsBackAsWritten'The code it first failed on had a number with a leading zero; it shrinks to the plainest such code,
"AAA-0000". A part of a regex that limits a
match without drawing a character, such as a lookahead (?=…), a back reference \1 or a word
boundary \b, can’t be drawn, so matching(…) refuses it, naming it. The regex sets the length
and the characters, so matching(…) goes on strings() alone.
Sizes: .ofSize(…), .ofLength(…) and .nonEmpty() bound how many elements or characters.
.averageSize(n), and .averageLength(n) for a string, set how many there are on average, within
those bounds:
static Domain<List<Integer>> longerBaskets() { return lists(integers().atLeast(1).atMost(99)).averageSize(20);}These baskets hold 20 items on average, not 7. The chance of one more element is set to give that average, so the size still varies and the shortest is still the simplest, what a counterexample shrinks to. The longest bound still cuts a draw off, so an average near it comes out a little shorter, and a set or map of few distinct values comes out smaller, as above.
To see the sizes a run drew, look at its statistics. Under the counts of what classify and
require labelled, they show how big each sized draw came out, a list, set, map, string or byte
string, and the strings or lists inside it: half the values were at most the p50 size, nine in
ten at most p90, 99 in 100 at most p99. A failure prints them under the counts, and so does a
pass run with PROPTASTIC_STATISTICS=on. Asking for half the longer baskets to hold 20 items is
asking too much:
@Testvoid halfTheLongerBasketsHoldTwenty() { forAll(lists(integers().atLeast(1).atMost(99)).averageSize(20)) .check( basket -> { require(50, basket.size() >= 20, "twenty items or more"); });}halfTheLongerBasketsHoldTwenty covered too little in 1,600 tries (seed 0x5eed) too rare: "twenty items or more" in 575 of 1,600 tries (35.9%), needs 50% rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'RequireTest.halfTheLongerBasketsHoldTwenty' ! draw it more often: weighted(…), .edges(…), averageSize(…) or a domain of its own or check one input you know every run: .example(…), in place of require
classify tries twenty items or more 575 35.9% ███████▎
sizes p50 p90 p99 basket 13 45 89About a third of the baskets held 20 items or more, so the run fails as too rare, once 1,600 tries
make that sure. The sizes say why: half the baskets held 13 items or fewer, though they hold 20 on
average, as the few long ones, a tenth past 45, pull the average up. An average size isn’t a typical size: when every basket must hold
20, say so with .ofSize(atLeast(20), …).
.map(…) rather than .filter(…) keeps the shares you’d expect, as above, and shrinks
exactly. A failure under a filter shrinks past the values the filter rejects, an even number
over 100 to 102, but one that keeps few values, one in ten say, can stop a little short:
integers().filter(n -> n % 10 == 0) can shrink to 150 where 110 fails too, and
integers().map(n -> n * 10) reaches 110.
Change a whole run, not a domain. Three tools work on the run as a whole: targeted tries climb toward the inputs that score highest, every value runs each value of a small domain once, and classify and require show what was drawn and fail a run that rarely draws what matters.