Top Description Methods
io.vavr.test

public Interface Gen<T>

Additional top-level class in compilation unit: GenModule.

Annotations
@FunctionalInterface
Type Parameters
<T>
type of generated objects
Imports
io.vavr.Tuple2, io.vavr.collection.Iterator, .List, .Stream, .Vector, java.util.Objects, .Random, java.util.function.Consumer, .Function, .Predicate

Generators are the building blocks for providing arbitrary objects.

To ease the creation of Arbitraries, Gen is a FunctionalInterface which extends Function<Random, T>.

Gen objects are obtained via one of the methods choose, fail, frequency, of and oneOf.

Given Gen objects may be transformed using one of the methods filter, map and flatMap.

A simple way to obtain an Arbitrary of a Gen is to call Gen#arbitrary(). This will ignore the size hint of Arbitrary.

Author
Daniel Dietrich
See Also
Arbitrary

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

A generated value of type T.
apply
(Random
a random number generator
random
)

Functional interface of this generator.

public default Arbitrary<T>

Returns:

An arbitrary which returns this generator regardless of the provided size hint n
arbitrary
()

Converts this Gen to an Arbitrary

public static Gen<Integer>

Returns:

A new int generator
choose
(int
lower bound
min
,
int
upper bound
max
)

Chooses an int between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.

public static Gen<Long>

Returns:

A new long generator
choose
(long
lower bound
min
,
long
upper bound
max
)

Chooses a long between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.

public static Gen<Double>

Returns:

A new double generator
choose
(double
lower bound
min
,
double
upper bound
max
)

Chooses a double between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.

public static Gen<Character>

Returns:

A new char generator
choose
(char
lower bound
min
,
char
upper bound
max
)

Chooses a char between min and max, bounds inclusive and chars distributed according to the underlying random number generator.

public static Gen<Character>

Returns:

A new array generator
choose
(char...
array with the characters to choose from
characters
)

Chooses a char from all chars in the array

public static <
type of enum constants
T extends Enum<T>
>
Gen<T>

Returns:

A new enum generator
choose
(Class<T>
Enum class
clazz
)

Chooses an enum value from all the enum constants defined in the enumerated type.

public static <
value type
T
>
Gen<T>

Returns:

A new array generator
choose
(T...
array with the values to choose from
values
)

Chooses a value from all values in the array.

public static <
value type
T
>
Gen<T>

Returns:

A new iterable generator
choose
(Iterable<T>
iterable with the values to choose from.
values
)

Chooses a value from all values in the iterable

public static <
Type of values theoretically generated.
T
>
Gen<T>

Returns:

A new generator which always fails with the message "failed"
fail
()

A failing generator which throws a RuntimeException("failed").

public static <
Type of values theoretically generated.
T
>
Gen<T>

Returns:

A new generator which always fails with the given message
fail
(String
Message thrown.
message
)

A failing generator which throws a RuntimeException.

public default Gen<T>

Returns:

A new generator
filter
(Predicate<? super T>
A predicate
predicate
)

Returns a generator based on this generator which produces values that fulfill the given predicate.

public default <
Type of generated objects of the new generator
U
>
Gen<U>

Returns:

A new generator
flatMap
(Function<? super T, ? extends Gen<? extends U>>
A function that maps a generated T to a new generator which generates objects of type U.
mapper
)

Maps generated Ts to Us.

public static <
Type to be generated
T
>
Gen<T>

Returns:

A new T generator
frequency
(Tuple2<Integer, Gen<T>>...
A non-empty array of Tuples (frequency, generator)
generators
)

Chooses one of the given generators according to their frequency.

public static <
Type to be generated
T
>
Gen<T>

Returns:

A new T generator
frequency
(Iterable<Tuple2<Integer, Gen<T>>>
A non-empty traversable of Tuples (frequency, generator)
generators
)

Chooses one of the given generators according to their frequency.

public default Gen<T>

Returns:

A new T generator
intersperse
(Gen<T>
another T generator to accept values from.
other
)

Intersperse values from this generator instance with those of another.

public default <
Type of the mapped object
U
>
Gen<U>

Returns:

A new generator
map
(Function<? super T, ? extends U>
A function that maps a generated T to an object of type U.
mapper
)

Maps generated Ts to Us.

public static <
Type of t.
T
>
Gen<T>

Returns:

A new T generator
of
(T
A value.
t
)

A generator which constantly returns t.

public static <T> Gen<T>
of(T seed, Function<? super T, ? extends T> next)

public static <
Type to be generated
T
>
Gen<T>

Returns:

A new T generator
oneOf
(Gen<T>...
A non-empty array of generators
generators
)

Randomly chooses one of the given generators.

public static <
Type to be generated
T
>
Gen<T>

Returns:

A new T generator
oneOf
(Iterable<Gen<T>>
A non-empty Iterable of generators
generators
)

Randomly chooses one of the given generators.

public default Gen<T>
peek(Consumer<? super T> action)

public default <
Type of transformation result
U
>
U

Returns:

An instance of type U
transform
(Function<? super Gen<T>, ? extends U>
A transformation
f
)

Transforms this Gen.

Method Detail

applyback to summary
public T apply(Random random)

Functional interface of this generator.

Parameters
random:Random

a random number generator

Returns:T

A generated value of type T.

arbitraryback to summary
public default Arbitrary<T> arbitrary()

Converts this Gen to an Arbitrary

Returns:Arbitrary<T>

An arbitrary which returns this generator regardless of the provided size hint n

chooseback to summary
public static Gen<Integer> choose(int min, int max)

Chooses an int between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.

Note

min and max are internally swapped if min > max.

Parameters
min:int

lower bound

max:int

upper bound

Returns:Gen<Integer>

A new int generator

chooseback to summary
public static Gen<Long> choose(long min, long max)

Chooses a long between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.

Note

min and max are internally swapped if min > max.

Parameters
min:long

lower bound

max:long

upper bound

Returns:Gen<Long>

A new long generator

chooseback to summary
public static Gen<Double> choose(double min, double max)

Chooses a double between min and max, bounds inclusive and numbers distributed according to the distribution of the underlying random number generator.

Note

min and max are internally swapped if min > max.

Parameters
min:double

lower bound

max:double

upper bound

Returns:Gen<Double>

A new double generator

Exceptions
IllegalArgumentException:
if min or max is infinite, min or max is not a number (NaN)
chooseback to summary
public static Gen<Character> choose(char min, char max)

Chooses a char between min and max, bounds inclusive and chars distributed according to the underlying random number generator.

Note

min and max are internally swapped if min > max.

Parameters
min:char

lower bound

max:char

upper bound

Returns:Gen<Character>

A new char generator

chooseback to summary
public static Gen<Character> choose(char... characters)

Chooses a char from all chars in the array

Parameters
characters:char[]

array with the characters to choose from

Returns:Gen<Character>

A new array generator

chooseback to summary
public static <T extends Enum<T>> Gen<T> choose(Class<T> clazz)

Chooses an enum value from all the enum constants defined in the enumerated type.

Parameters
<T>
type of enum constants
clazz:Class<T>

Enum class

Returns:Gen<T>

A new enum generator

chooseback to summary
public static <T> Gen<T> choose(T... values)

Chooses a value from all values in the array.

Parameters
<T>
value type
values:T[]

array with the values to choose from

Returns:Gen<T>

A new array generator

Annotations
@SafeVarargs
@SuppressWarnings:varargs
chooseback to summary
public static <T> Gen<T> choose(Iterable<T> values)

Chooses a value from all values in the iterable

Parameters
<T>
value type
values:Iterable<T>

iterable with the values to choose from.

Returns:Gen<T>

A new iterable generator

failback to summary
public static <T> Gen<T> fail()

A failing generator which throws a RuntimeException("failed").

Parameters
<T>
Type of values theoretically generated.
Returns:Gen<T>

A new generator which always fails with the message "failed"

failback to summary
public static <T> Gen<T> fail(String message)

A failing generator which throws a RuntimeException.

Parameters
<T>
Type of values theoretically generated.
message:String

Message thrown.

Returns:Gen<T>

A new generator which always fails with the given message

filterback to summary
public default Gen<T> filter(Predicate<? super T> predicate)

Returns a generator based on this generator which produces values that fulfill the given predicate.

Parameters
predicate:Predicate<? super T>

A predicate

Returns:Gen<T>

A new generator

flatMapback to summary
public default <U> Gen<U> flatMap(Function<? super T, ? extends Gen<? extends U>> mapper)

Maps generated Ts to Us.

Parameters
<U>
Type of generated objects of the new generator
mapper:Function<? super T, ? extends Gen<? extends U>>

A function that maps a generated T to a new generator which generates objects of type U.

Returns:Gen<U>

A new generator

frequencyback to summary
public static <T> Gen<T> frequency(Tuple2<Integer, Gen<T>>... generators)

Chooses one of the given generators according to their frequency. Only generators with positive frequencies are used in returned generator.

Parameters
<T>
Type to be generated
generators:Tuple2<Integer, Gen<T>>[]

A non-empty array of Tuples (frequency, generator)

Returns:Gen<T>

A new T generator

Annotations
@SuppressWarnings:varargs
@SafeVarargs
Exceptions
NullPointerException:
if generators is null
IllegalArgumentException:
if generators doesn't contain any generator with positive frequency
frequencyback to summary
public static <T> Gen<T> frequency(Iterable<Tuple2<Integer, Gen<T>>> generators)

Chooses one of the given generators according to their frequency. Only generators with positive frequencies ares used in returned generator.

Parameters
<T>
Type to be generated
generators:Iterable<Tuple2<Integer, Gen<T>>>

A non-empty traversable of Tuples (frequency, generator)

Returns:Gen<T>

A new T generator

Exceptions
NullPointerException:
if generators is null
IllegalArgumentException:
if generators doesn't contain any generator with positive frequency
intersperseback to summary
public default Gen<T> intersperse(Gen<T> other)

Intersperse values from this generator instance with those of another.

Parameters
other:Gen<T>

another T generator to accept values from.

Returns:Gen<T>

A new T generator

mapback to summary
public default <U> Gen<U> map(Function<? super T, ? extends U> mapper)

Maps generated Ts to Us.

Parameters
<U>
Type of the mapped object
mapper:Function<? super T, ? extends U>

A function that maps a generated T to an object of type U.

Returns:Gen<U>

A new generator

ofback to summary
public static <T> Gen<T> of(T t)

A generator which constantly returns t.

Parameters
<T>
Type of t.
t:T

A value.

Returns:Gen<T>

A new T generator

ofback to summary
public static <T> Gen<T> of(T seed, Function<? super T, ? extends T> next)
oneOfback to summary
public static <T> Gen<T> oneOf(Gen<T>... generators)

Randomly chooses one of the given generators.

Parameters
<T>
Type to be generated
generators:Gen<T>[]

A non-empty array of generators

Returns:Gen<T>

A new T generator

Annotations
@SafeVarargs
Exceptions
NullPointerException:
if generators is null
IllegalArgumentException:
if generators is empty
oneOfback to summary
public static <T> Gen<T> oneOf(Iterable<Gen<T>> generators)

Randomly chooses one of the given generators.

Parameters
<T>
Type to be generated
generators:Iterable<Gen<T>>

A non-empty Iterable of generators

Returns:Gen<T>

A new T generator

Exceptions
NullPointerException:
if generators is null
IllegalArgumentException:
if generators is empty
peekback to summary
public default Gen<T> peek(Consumer<? super T> action)
transformback to summary
public default <U> U transform(Function<? super Gen<T>, ? extends U> f)

Transforms this Gen.

Parameters
<U>
Type of transformation result
f:Function<? super Gen<T>, ? extends U>

A transformation

Returns:U

An instance of type U

Exceptions
NullPointerException:
if f is null