T
- The type of generated objects. Primitive objects (e.g. int, boolean etc.) are represented by their boxed
type (e.g. Integer, Boolean).@API(status=STABLE,
since="1.0")
@CheckReturnValue
public interface Arbitrary<T>
Modifier and Type | Interface and Description |
---|---|
static class |
Arbitrary.ArbitraryFacade |
Modifier and Type | Method and Description |
---|---|
default java.util.Optional<java.util.stream.Stream<T>> |
allValues()
Create optional stream of all possible values this arbitrary could generate.
|
default <A> ArrayArbitrary<T,A> |
array(java.lang.Class<A> arrayClass)
Create a new arbitrary of type
T[] using the existing arbitrary for generating the elements of the array. |
default Arbitrary<java.lang.Object> |
asGeneric() |
default Arbitrary<java.util.List<T>> |
collect(java.util.function.Predicate<java.util.List<T>> until)
Create a new arbitrary of type
List<T> by adding elements of type T until condition until is fulfilled. |
default Arbitrary<T> |
dontShrink()
Create a new arbitrary of type
T that will use the underlying
arbitrary to create the tuple values but will return unshrinkable values. |
default EdgeCases<T> |
edgeCases()
Return an arbitrary's edge cases up to a limit of 1000.
|
default Arbitrary<T> |
edgeCases(java.util.function.Consumer<EdgeCases.Config<T>> configurator)
Experimental interface to change generated edge cases of a specific arbitrary.
|
EdgeCases<T> |
edgeCases(int maxEdgeCases) |
default java.util.Optional<ExhaustiveGenerator<T>> |
exhaustive()
Create the exhaustive generator for an arbitrary using the maximum allowed
number of generated samples.
|
default java.util.Optional<ExhaustiveGenerator<T>> |
exhaustive(long maxNumberOfSamples)
Create the exhaustive generator for an arbitrary.
|
default Arbitrary<T> |
filter(int maxMisses,
java.util.function.Predicate<T> filterPredicate)
Create a new arbitrary of the same type
T that creates and shrinks the original arbitrary but only allows
values that are accepted by the filterPredicate . |
default Arbitrary<T> |
filter(java.util.function.Predicate<T> filterPredicate)
Create a new arbitrary of the same type
T that creates and shrinks the original arbitrary but only allows
values that are accepted by the filterPredicate . |
default Arbitrary<T> |
fixGenSize(int genSize)
Fix the genSize of an arbitrary so that it can no longer be influenced from outside
|
default <U> Arbitrary<U> |
flatMap(java.util.function.Function<T,Arbitrary<U>> mapper)
Create a new arbitrary of type
U that uses the values of the existing arbitrary to create a new arbitrary
using the mapper function. |
default void |
forEachValue(java.util.function.Consumer<? super T> action)
Iterate through each value this arbitrary can generate if - and only if -
exhaustive generation is possible.
|
RandomGenerator<T> |
generator(int genSize)
Create the random generator for an arbitrary.
|
default RandomGenerator<T> |
generator(int genSize,
boolean withEdgeCases)
Create the random generator for an arbitrary with or without edge cases.
|
default RandomGenerator<T> |
generatorWithEmbeddedEdgeCases(int genSize)
Create the random generator for an arbitrary where the embedded generators,
if there are any, also generate edge cases.
|
default Arbitrary<T> |
ignoreException(java.lang.Class<? extends java.lang.Throwable> exceptionType)
Create a new arbitrary of type
T that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception of
type exceptionType during generation. |
default Arbitrary<T> |
ignoreException(int maxThrows,
java.lang.Class<? extends java.lang.Throwable> exceptionType)
Create a new arbitrary of type
T that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception of
type exceptionType during generation. |
default Arbitrary<T> |
ignoreExceptions(java.lang.Class<? extends java.lang.Throwable>... exceptionTypes)
Create a new arbitrary of type
T that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception in
exceptionTypes during generation. |
default Arbitrary<T> |
ignoreExceptions(int maxThrows,
java.lang.Class<? extends java.lang.Throwable>... exceptionTypes)
Create a new arbitrary of type
T that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception in
exceptionTypes during generation. |
default Arbitrary<T> |
injectDuplicates(double duplicateProbability)
Create a new arbitrary of type
Iterable<T> that will
inject duplicates of previously generated values with a probability of duplicateProbability . |
default Arbitrary<T> |
injectNull(double nullProbability)
Create a new arbitrary of the same type but inject null values with a probability of
nullProbability . |
default boolean |
isGeneratorMemoizable() |
default IteratorArbitrary<T> |
iterator()
Create a new arbitrary of type
Iterable<T> using the existing arbitrary for generating the elements of the
stream. |
default ListArbitrary<T> |
list()
Create a new arbitrary of type
List<T> using the existing arbitrary for generating the elements of the list. |
default <U> Arbitrary<U> |
map(java.util.function.Function<T,U> mapper)
Create a new arbitrary of type
U that maps the values of the original arbitrary using the mapper
function. |
default Arbitrary<java.util.Optional<T>> |
optional()
Create a new arbitrary of type
Optional<T> using the existing arbitrary for generating the elements of the
stream. |
default Arbitrary<java.util.Optional<T>> |
optional(double presenceProbability)
Create a new arbitrary of type
Optional<T> using the existing arbitrary for generating the elements of the
stream. |
default T |
sample()
Generate a single sample value using this arbitrary.
|
default java.util.stream.Stream<T> |
sampleStream()
Generate a stream of sample values using this arbitrary.
|
default SetArbitrary<T> |
set()
Create a new arbitrary of type
Set<T> using the existing arbitrary for generating the elements of the set. |
default StreamArbitrary<T> |
stream()
Create a new arbitrary of type
Stream<T> using the existing arbitrary
for generating the elements of the stream. |
default Arbitrary<Tuple.Tuple1<T>> |
tuple1()
Create a new arbitrary of type
Tuple.Tuple1<T> that will use the underlying
arbitrary to create the tuple value; |
default Arbitrary<Tuple.Tuple2<T,T>> |
tuple2()
Create a new arbitrary of type
Tuple.Tuple2<T, T> that will use the underlying
arbitrary to create the tuple values; |
default Arbitrary<Tuple.Tuple3<T,T,T>> |
tuple3()
Create a new arbitrary of type
Tuple.Tuple3<T, T, T> that will use the underlying
arbitrary to create the tuple values; |
default Arbitrary<Tuple.Tuple4<T,T,T,T>> |
tuple4()
Create a new arbitrary of type
Tuple.Tuple4<T, T, T, T> that will use the underlying
arbitrary to create the tuple values; |
default Arbitrary<Tuple.Tuple5<T,T,T,T,T>> |
tuple5()
Create a new arbitrary of type
Tuple.Tuple5<T, T, T, T, T> that will use the underlying
arbitrary to create the tuple values; |
default Arbitrary<T> |
withoutEdgeCases()
Create a new arbitrary of type
T that will not explicitly generate
any edge cases, neither directly or in embedded arbitraries. |
RandomGenerator<T> generator(int genSize)
Starting with version 1.4.0 the returned generator should no longer include edge cases explicitly since those will be injected in generator(int, boolean)
genSize
- a very unspecific configuration parameter that can be used
to influence the configuration and behaviour of a random generator
if and only if the generator wants to be influenced.
Many generators are independent of genSize.
The default value of genSize
is the number of tries configured
for a property. Use fixGenSize(int) to fix
the parameter for a given arbitrary.
@API(status=INTERNAL, since="1.4.0") default RandomGenerator<T> generator(int genSize, boolean withEdgeCases)
Never override this method. Override generator(int) instead.
genSize
- See generator(int) about meaning of this parameterwithEdgeCases
- True if edge cases should be injected into the stream of generated values@API(status=INTERNAL, since="1.4.0") default RandomGenerator<T> generatorWithEmbeddedEdgeCases(int genSize)
Override only if there are any embedded arbitraries / generators, e.g. a container using an element generator
genSize
- See generator(int) about meaning of this parameter@API(status=INTERNAL) default Arbitrary<java.lang.Object> asGeneric()
@API(status=INTERNAL) default java.util.Optional<ExhaustiveGenerator<T>> exhaustive()
@API(status=INTERNAL) default java.util.Optional<ExhaustiveGenerator<T>> exhaustive(long maxNumberOfSamples)
maxNumberOfSamples
this can take a long time.
This method must be overridden in all arbitraries that support exhaustive
generation.maxNumberOfSamples
- The maximum number of samples considered.
If during generation it becomes clear that this
number will be exceeded generation stops.@API(status=INTERNAL) default boolean isGeneratorMemoizable()
@API(status=EXPERIMENTAL, since="1.3.0") default EdgeCases<T> edgeCases()
Never override. Override edgeCases(int) instead.
default java.util.Optional<java.util.stream.Stream<T>> allValues()
@API(status=MAINTAINED, since="1.1.2") default void forEachValue(java.util.function.Consumer<? super T> action)
action
- the consumer function to be invoked for each valuejava.lang.AssertionError
- if exhaustive generation is not possibledefault Arbitrary<T> filter(java.util.function.Predicate<T> filterPredicate)
T
that creates and shrinks the original arbitrary but only allows
values that are accepted by the filterPredicate
.filterPredicate
- The predicate used for filteringTooManyFilterMissesException
- if filtering will fail to come up with a value after 10000 tries@API(status=EXPERIMENTAL, since="1.7.0") default Arbitrary<T> filter(int maxMisses, java.util.function.Predicate<T> filterPredicate)
T
that creates and shrinks the original arbitrary but only allows
values that are accepted by the filterPredicate
.maxMisses
- The max number of misses allowed for filteringfilterPredicate
- The predicate used for filteringTooManyFilterMissesException
- if filtering will fail to come up with a value after maxMisses
triesdefault <U> Arbitrary<U> map(java.util.function.Function<T,U> mapper)
U
that maps the values of the original arbitrary using the mapper
function.U
- type of resulting objectmapper
- the function used to mapdefault <U> Arbitrary<U> flatMap(java.util.function.Function<T,Arbitrary<U>> mapper)
U
that uses the values of the existing arbitrary to create a new arbitrary
using the mapper
function.U
- type of resulting objectmapper
- the function used to map to arbitrarydefault Arbitrary<T> injectNull(double nullProbability)
nullProbability
.nullProbability
- the probability. ≥ 0 and ≤ 1.@API(status=MAINTAINED, since="1.2.0") default Arbitrary<T> fixGenSize(int genSize)
genSize
- The size used in arbitrary instead of the dynamic onedefault ListArbitrary<T> list()
List<T>
using the existing arbitrary for generating the elements of the list.default SetArbitrary<T> set()
Set<T>
using the existing arbitrary for generating the elements of the set.default StreamArbitrary<T> stream()
Stream<T>
using the existing arbitrary
for generating the elements of the stream.default IteratorArbitrary<T> iterator()
Iterable<T>
using the existing arbitrary for generating the elements of the
stream.default <A> ArrayArbitrary<T,A> array(java.lang.Class<A> arrayClass)
T[]
using the existing arbitrary for generating the elements of the array.A
- Type of resulting array classarrayClass
- The arrays class to create, e.g. String[].class
. This is required due to limitations in Java's
reflection capabilities.default Arbitrary<java.util.Optional<T>> optional()
Optional<T>
using the existing arbitrary for generating the elements of the
stream.
The new arbitrary generates Optional.empty()
values with a probability of 0.05
(i.e. 1 in 20).
@API(status=MAINTAINED, since="1.5.4") default Arbitrary<java.util.Optional<T>> optional(double presenceProbability)
Optional<T>
using the existing arbitrary for generating the elements of the
stream.
The new arbitrary generates Optional.empty()
values with a probability of 1 - presenceProbability
.
presenceProbability
- The probability with which a value is present, i.e. not empty@API(status=MAINTAINED, since="1.3.0") default Arbitrary<java.util.List<T>> collect(java.util.function.Predicate<java.util.List<T>> until)
List<T>
by adding elements of type T until condition until
is fulfilled.until
- predicate to check if final condition has been reached@API(status=MAINTAINED, since="1.3.0") default java.util.stream.Stream<T> sampleStream()
The underlying generator is created with size 1000. Outside a property a new instance of Random will be created to feed the generator.
Using this method within a property does not break reproducibility of results, i.e. rerunning it with same seed will also generate the same values.
@API(status=MAINTAINED, since="1.3.0") default T sample()
Some additional things to be aware of:
@API(status=MAINTAINED, since="1.3.0") default Arbitrary<T> injectDuplicates(double duplicateProbability)
Iterable<T>
that will
inject duplicates of previously generated values with a probability of duplicateProbability
.
Shrinking behavior for duplicate values -- if duplication is required for falsification -- is poor, i.e. those duplicate values cannot be shrunk to "smaller" duplicate values.
duplicateProbability
- The probability with which a previous value will be generated@API(status=MAINTAINED, since="1.3.0") default Arbitrary<Tuple.Tuple1<T>> tuple1()
Tuple.Tuple1<T>
that will use the underlying
arbitrary to create the tuple value;@API(status=MAINTAINED, since="1.3.0") default Arbitrary<Tuple.Tuple2<T,T>> tuple2()
Tuple.Tuple2<T, T>
that will use the underlying
arbitrary to create the tuple values;@API(status=MAINTAINED, since="1.3.0") default Arbitrary<Tuple.Tuple3<T,T,T>> tuple3()
Tuple.Tuple3<T, T, T>
that will use the underlying
arbitrary to create the tuple values;@API(status=MAINTAINED, since="1.3.0") default Arbitrary<Tuple.Tuple4<T,T,T,T>> tuple4()
Tuple.Tuple4<T, T, T, T>
that will use the underlying
arbitrary to create the tuple values;@API(status=MAINTAINED, since="1.3.3") default Arbitrary<Tuple.Tuple5<T,T,T,T,T>> tuple5()
Tuple.Tuple5<T, T, T, T, T>
that will use the underlying
arbitrary to create the tuple values;@API(status=MAINTAINED, since="1.3.1") default Arbitrary<T> ignoreException(java.lang.Class<? extends java.lang.Throwable> exceptionType)
T
that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception of
type exceptionType
during generation.exceptionType
- The exception type to ignoreTooManyFilterMissesException
- if more than 10000 exceptions are thrown in a row@API(status=EXPERIMENTAL, since="1.7.3") default Arbitrary<T> ignoreException(int maxThrows, java.lang.Class<? extends java.lang.Throwable> exceptionType)
T
that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception of
type exceptionType
during generation.maxThrows
- The maximum number of subsequent exception throws before generation
is stopped.exceptionType
- The exception type to ignoreTooManyFilterMissesException
- if more than maxThrows
exceptions are thrown in a row@API(status=MAINTAINED, since="1.7.2") default Arbitrary<T> ignoreExceptions(java.lang.Class<? extends java.lang.Throwable>... exceptionTypes)
T
that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception in
exceptionTypes
during generation.
If exceptionTypes
is empty, the original arbitrary is returned.
exceptionTypes
- The exception types to ignoreTooManyFilterMissesException
- if more than 10000 exceptions are thrown in a row@API(status=EXPERIMENTAL, since="1.7.3") default Arbitrary<T> ignoreExceptions(int maxThrows, java.lang.Class<? extends java.lang.Throwable>... exceptionTypes)
T
that will use the underlying
arbitrary to create the tuple values but will ignore any raised exception in
exceptionTypes
during generation.
If exceptionTypes
is empty, the original arbitrary is returned.
maxThrows
- The maximum number of subsequent exception throws before generation
is stopped.exceptionTypes
- The exception types to ignoreTooManyFilterMissesException
- if more than maxThrows
exceptions are thrown in a row@API(status=MAINTAINED, since="1.4.0") default Arbitrary<T> dontShrink()
T
that will use the underlying
arbitrary to create the tuple values but will return unshrinkable values.
This might be necessary if values are being mutated during a property run
and the mutated state would make a shrunk value invalid.
This is a hack to get around a weakness in jqwik's shrinking mechanism
@API(status=MAINTAINED, since="1.8.0") default Arbitrary<T> edgeCases(java.util.function.Consumer<EdgeCases.Config<T>> configurator)
configurator
- A consumer that configures deviating edge cases behaviourEdgeCases.Config
@API(status=MAINTAINED, since="1.8.0") default Arbitrary<T> withoutEdgeCases()
T
that will not explicitly generate
any edge cases, neither directly or in embedded arbitraries.
This is useful if you want to prune selected branches of edge case generation
because they are to costly or generate too many cases.