Skip to content

Proptastic

class in io.proptastic

class Proptastic

Everything a property says, behind one static import: import static io.proptastic.Proptastic.*;. Size bounds have their own: import static io.proptastic.bounds.Bounds.*;.

@Test void sortKeepsSize() {
    forAll(lists(integers()))
        .example(List.of(0, 0))
        .check(xs -> assertEquals(xs.size(), sort(xs).size()));
}

Constants

DIGITS

static final String DIGITS

The digits 0 to 9, an alphabet for strings().from(…) (decision 96).

HEX_DIGITS

static final String HEX_DIGITS

The digits, then a to f, then A to F: hex in either case.

LOWERCASE

static final String LOWERCASE

The ASCII letters a to z.

UPPERCASE

static final String UPPERCASE

The ASCII letters A to Z.

LETTERS

static final String LETTERS

LOWERCASE, then UPPERCASE.

ALPHANUMERIC

static final String ALPHANUMERIC

LETTERS, then DIGITS.

PRINTABLE_ASCII

static final String PRINTABLE_ASCII

The 95 printable ASCII characters, in the order strings() ranks them: the letters, the digits, the space, then the punctuation.

WHITESPACE

static final String WHITESPACE

ASCII whitespace, the space first: the space, tab, line feed, carriage return, vertical tab and form feed.

Methods

forAll

static <A> Property1<A> forAll(Domain<A> a)

A property of one value drawn from a: forAll(lists(integers())).check(xs -> …). Its examples, settings and check follow it.

forAll

static <A, B> Property2<A, B> forAll(Domain<A> a, Domain<B> b)

A property of two values, one drawn from each domain in turn, which check takes in the same order: forAll(balances(), amounts()).check((balance, amount) -> …).

forAll

static <A, B, C> Property3<A, B, C> forAll(Domain<A> a, Domain<B> b, Domain<C> c)

A property of three values, one drawn from each domain in turn, which check takes in the same order: forAll(balances(), amounts()).check((balance, amount) -> …).

forAll

static <A, B, C, D> Property4<A, B, C, D> forAll(Domain<A> a, Domain<B> b, Domain<C> c, Domain<D> d)

A property of four values, one drawn from each domain in turn, which check takes in the same order: forAll(balances(), amounts()).check((balance, amount) -> …).

forAll

static <A, B, C, D, E> Property5<A, B, C, D, E> forAll(Domain<A> a, Domain<B> b, Domain<C> c, Domain<D> d, Domain<E> e)

A property of five values, one drawn from each domain in turn, which check takes in the same order: forAll(balances(), amounts()).check((balance, amount) -> …).

forAll

static <A, B, C, D, E, F> Property6<A, B, C, D, E, F> forAll(Domain<A> a, Domain<B> b, Domain<C> c, Domain<D> d, Domain<E> e, Domain<F> f)

A property of six values, one drawn from each domain in turn, which check takes in the same order: forAll(balances(), amounts()).check((balance, amount) -> …).

forAll

static <S> StatefulProperty<S> forAll(Steps<S> steps)

A stateful test: runs of steps, every invariant checked after each step.

lean

static Model lean(String name)

A Lean model where models go, models/lean: lean("Trading") runs the program Lake built there for it, trading, its name in lower case with a hyphen between words (TradingDesk is trading-desk). PROPTASTIC_MODELS moves the directory. Build it with lake build before the tests run.

lean

static Model lean(Path project, String executable)

A Lean model, the executable Lake built in project: lean(Path.of("lean"), "trading-oracle"). Build it with lake build before the tests run.

command

static Model command(String... command)

A model in any language: an executable that speaks the model protocol, with its arguments.

java

static Model java(Class<?> main, String... arguments)

A model written in Java: main’s class run as its own process with the tests’ Java and class path, its main calling Model#serve: java(LeapYearModel.class).

spec

static <S> Spec<S> spec(String name, Function<S, Map<String, ?>> variables, Predicate<S> withinConstants)

A TLA+ spec a stateful test follows, where models go: spec("Reserve", …) reads the state graph TLC dumped with -dump dot,actionlabels from models/tla/Reserve.dot, beside Reserve.tla and the Reserve.cfg TLC checked it by. Then the state’s variables as the spec names them, and whether a state is within the constants TLC checked. The spec must keep a history variable, last. PROPTASTIC_MODELS moves the directory. With no graph there, as for a spec too big for one, the spec is followed by TLC traces: each step the runs took is judged by TLC, at Reserve.cfg’s constants, so the state must give every variable the spec declares.

spec

static <S> Spec<S> spec(String name, Path graph, Function<S, Map<String, ?>> variables, Predicate<S> withinConstants)

Function, java.util.function.Predicate) spec, its graph read from graph rather than where models go.

steps

static <S> Steps<S> steps(Supplier<? extends S> start)

Steps to take on a state, each try on a new one from start: steps(Accounts::new), then .step, .when and .invariant.

steps

static <S, T> Steps<S> steps(Supplier<? extends S> start, Domain<T> steps, Steps.Step1<S, T> apply)

Steps as records: each step one value drawn from steps, such as any(Operation.class) for a sealed interface of records, taken by apply. A failure prints each record as source, and its pin replays them: .example(new Deposit("alice", 1L), …). A precondition is the record’s own: skip the step with assume(…) in apply.

step

static Step step(String name, Object... arguments)

One step for .example(…) on a stateful test, by its name and its arguments: step("deposit", "alice", 1L), as a failure’s pin it: line writes it.

handles

static <T> Handles<T> handles(String name)

Values steps make that later steps use, named for what they are: var accounts = handles("account"). A step keeps one, accounts.keep(bank.open()); as a later step’s argument, .step("deposit", accounts, amounts(), …), they draw one the run kept. A failure prints each by its name and the order it was kept, account1.

fromState

static <S, T> Argument<S, T> fromState(Function<S, Domain<T>> domain)

A step’s argument drawn from a domain the state gives, as it is when the run reaches the step: .step("withdraw", fromState(account -> longs().atLeast(1).atMost(account.balance())), …), where assume(amount <= account.balance()) would throw most tries away. The step’s precondition is checked first, so the domain can rely on it.

handle

static Handle handle(String name, int number)

The numberth value the handles named name kept in a run, from 1, as a step’s argument in .example(…): step("deposit", handle("account", 1), 5L), as a failure’s pin it: line writes it. A step whose handle the replay hasn’t kept is skipped.

first

static FirstSteps first(Object... steps)

The steps taken on one thread before the threads start, first in .example(…) on steps run on threads, as a failure’s pin it: line writes them: .example(first(step("deposit", 100L)), thread(step("withdraw", 60L)), thread(…)).

thread

static ThreadSteps thread(Object... steps)

One thread’s steps for .example(…) on steps run on threads, as a failure’s pin it: line writes them: thread(step("withdraw", 60L), step("deposit", 10L)).

lock

static ReentrantLock lock(String name)

A lock proptastic schedules, named for what it guards, for the state of steps run on threads: new Account(100, lock("account")). Where proptastic runs steps on threads, taking it is a point where another thread may run, and a thread that asks for it while another holds it waits; a deadlock is found and named. Anywhere else it’s a plain java.util.concurrent.locks.ReentrantLock.

record

static void record(@Nullable Object value)

What this step saw, record(account.withdraw(amount)): on threads, the steps one at a time must give the same answer in some order. Anywhere else it does nothing.

assume

static void assume(boolean condition)

Skips this try unless condition holds: it neither passes nor fails.

classify

static void classify(boolean condition, String label)

Counts this try under label if condition holds; a passing run prints the shares.

require

static void require(double percent, boolean condition, String label)

Counts this try under label if condition holds, as classify does, and fails the run when fewer than percent of its tries hold it: require(10, basket.size() >= 20, "a full basket"). It fails only when the tries make sure the share is lower, and draws more tries, up to 16 times as many, while they don’t (decision 45).

target

static void target(double score)

Scores this try, for the run to climb toward higher scores: target(basket.total()). After the first quarter of the tries, each changes the best-scoring try so far, one to three choices at a time, keeping what scores higher, so a failure that random tries rarely reach, at the high end of a score, can be found (decision 62).

target

static void target(long score)

target for a whole-number score, such as a total in cents.

target

static void target(long score, String label)

String) target for a whole-number score.

target

static void target(double score, String label)

target, for one of several scores a property climbs, each by its label.

integers

static IntegerDomain integers()

Every int; bound it with .atLeast(n), .atMost(n) and the like.

longs

static LongDomain longs()

Every long.

doubles

static DoubleDomain doubles()

Every double, NaN and the infinities among them; .finite() drops those.

dates

static DateDomain dates()

Calendar dates, in 1 January 1900 to 31 December 2100 unless bounded, about one try in five a calendar edge: dates().atLeast(LocalDate.of(2026, 1, 1)).excluding(SATURDAY, SUNDAY). A counterexample shrinks toward Monday 1 January 2001, or the allowed day nearest it.

times

static TimeDomain times()

Times of day, whole seconds most tries and a fraction of a second one try in 8: times().atLeast(LocalTime.of(9, 0)).every(Duration.ofMinutes(15)). A counterexample shrinks toward midnight.

dateTimes

static DateTimeDomain dateTimes()

Dates with times and no zone: a date as dates draws it, then a time on it. A counterexample shrinks toward 2001-01-01T00:00.

instants

static InstantDomain instants()

Moments in UTC, in the dates’ window. A counterexample shrinks toward 2001-01-01T00:00:00Z.

durations

static DurationDomain durations()

Signed lengths of time, edges at zero, a nanosecond, a second, 23 hours, a day and 25 hours, either way. A counterexample shrinks toward zero.

offsets

static Domain<ZoneOffset> offsets()

Fixed offsets from UTC, in whole minutes from -18:00 to +18:00, edges at half and quarter hours and the extremes. A counterexample shrinks toward UTC.

zones

static Domain<ZoneId> zones()

Region zones from a list the contract pins, UTC first: zones with summer time, ones that aren’t a whole hour from UTC, and ones whose rules changed lately. In .inZones(zones()), a zone’s transitions are edges, and a failure names the tz database’s version. A counterexample shrinks toward UTC.

luhnNumbers

static IdentifierDomain luhnNumbers()

Luhn numbers, 12 to 19 digits as a payment card’s, the last the Luhn check digit of the rest. .mistyped() gives ones a digit off. A counterexample shrinks toward 12 digits of zeros.

ibans

static IdentifierDomain ibans()

IBANs from countries whose account numbers carry no check of their own, their two check digits by ISO 13616’s mod 97, with no spaces. .mistyped() gives ones a character off.

isins

static IdentifierDomain isins()

ISINs: a country, nine letters or digits, then the Luhn check digit of the whole, letters as numbers. .mistyped() gives ones a character off.

isbns

static IdentifierDomain isbns()

ISBN-13s, 978 or 979 then nine digits and the check digit, digits weighted 1 and 3, with no hyphens. .mistyped() gives ones a digit off.

decimals

static DecimalDomain decimals()

Exact decimals: a scale up to .places(n)’s most, 10 unless said, then an unscaled value in 64 bits. A counterexample shrinks toward fewer places, then toward zero.

uuids

static Domain<UUID> uuids()

UUIDs of version 4 and 7, valid by construction, the nil and max UUIDs as edges. A counterexample shrinks toward 00000000-0000-4000-8000-000000000000.

floats

static FloatDomain floats()

Every float, by doubles()’s laws in its own range, NaN, the infinities, -0.0f and the smallest subnormal among them; .finite() drops those.

bytes

static BytesDomain bytes()

Byte strings, byte[], each byte 0x00 the simplest; empty, all zero and all 0xFF as edges: bytes().ofSize(atMost(64)).inBase64().

bigIntegers

static BigIntegerDomain bigIntegers()

Integers of any size, -2^127 to 2^127-1 unless bounded, written as 64-bit choices: bigIntegers().atLeast(BigInteger.ZERO). A counterexample shrinks toward 0.

booleans

static Domain<Boolean> booleans()

false, then true, each as likely.

strings

static StringDomain strings()

Strings of any code points; .ofLength(…) bounds them.

lists

static <T> ListDomain<T> lists(Domain<T> elements)

Lists of values drawn from elements, shortest first: lists(integers()).ofSize(atMost(5)).

sets

static <T> SetDomain<T> sets(Domain<T> elements)

Sets of distinct values drawn from elements, smallest first.

maps

static <K, V> MapDomain<K, V> maps(Domain<K> keys, Domain<V> values)

Maps of distinct keys drawn from keys, each to a value from values.

optionals

static <T> Domain<Optional<T>> optionals(Domain<T> values)

Empty first, then a value.

elements

static <T> Domain<T> elements(T... values)

One of values, the first the simplest.

elements

static <T> Domain<T> elements(Collection<? extends T> values)

One of values, each as likely, so a value given twice is drawn twice as often; the first the simplest.

oneOf

static <T> Domain<T> oneOf(Domain<? extends T>... domains)

A value from one of domains, each as likely unless weighted weighted; the first the simplest.

weighted

static <T> Domain<T> weighted(int weight, Domain<T> domain)

domain, weighing weight in a oneOf: oneOf(weighted(3, elements(BUY)), elements(SELL)) draws BUY three times in four. A part without a weight weighs 1. The first part is still the simplest, what a counterexample shrinks to.

domain

static <T> Domain<T> domain(Function<Source, ? extends T> body)

A domain written as a lambda: domain(source -> new Trade(source.draw(symbols()), source.draw(quantities()), source.draw(sides()))). Values are drawn in the order written. Prefer the combined form below when the value is a constructor’s arguments, in order.

domain

static <A, R> Domain<R> domain(Domain<A> a, Proptastic.Make1<A, R> make)

A value made from one drawn from each domain, in order: domain(symbols(), quantities(), sides(), Trade::new). Past six, combine records.

domain

static <A, B, R> Domain<R> domain(Domain<A> a, Domain<B> b, Proptastic.Make2<A, B, R> make)

A domain of values made from one drawn from each domain in turn, by make: domain(symbols(), quantities(), Trade::new). It shrinks as its parts do.

domain

static <A, B, C, R> Domain<R> domain(Domain<A> a, Domain<B> b, Domain<C> c, Proptastic.Make3<A, B, C, R> make)

A domain of values made from one drawn from each domain in turn, by make: domain(symbols(), quantities(), Trade::new). It shrinks as its parts do.

domain

static <A, B, C, D, R> Domain<R> domain(Domain<A> a, Domain<B> b, Domain<C> c, Domain<D> d, Proptastic.Make4<A, B, C, D, R> make)

A domain of values made from one drawn from each domain in turn, by make: domain(symbols(), quantities(), Trade::new). It shrinks as its parts do.

domain

static <A, B, C, D, E, R> Domain<R> domain(Domain<A> a, Domain<B> b, Domain<C> c, Domain<D> d, Domain<E> e, Proptastic.Make5<A, B, C, D, E, R> make)

A domain of values made from one drawn from each domain in turn, by make: domain(symbols(), quantities(), Trade::new). It shrinks as its parts do.

domain

static <A, B, C, D, E, F, R> Domain<R> domain(Domain<A> a, Domain<B> b, Domain<C> c, Domain<D> d, Domain<E> e, Domain<F> f, Proptastic.Make6<A, B, C, D, E, F, R> make)

A domain of values made from one drawn from each domain in turn, by make: domain(symbols(), quantities(), Trade::new). It shrinks as its parts do.

any

static <T> AnyDomain<T> any(Class<T> type)

A domain found from the type: primitives, strings, enums, records component by component, and sealed types as one of the subtypes they permit. .with(Trade::quantity, …) gives a component a domain of its own.

Privacy