public final class Gen<A>
extends java.lang.Object
A generator for values of the type of the given type parameter (A
). Generation
of a value accepts a general 'size' argument (integer), a random generator
and
returns an optional value
of the type parameter. Several generators are provided,
allowing various forms of composition of generators.
A user typically creates an arbitrary
to return a generator using the 'combinator methods' below. For example,
suppose a class Person
:
class Person { final int age; final String name; final boolean male; Person(final int age, final String name, final boolean male) { this.age = age; this.name = name; this.male = male; } }
In a case like this one, a user may create a generator over Person
by
invoking the bind(F)
methods — in this case, bind(Gen , Gen , F)
the one
that takes two generator arguments}, since the class has one more than two fields (the bind
method is invoked on a generator adding the extra one to the count as they are composed). The
class fields are of types for which there exist generators (on Arbitrary
so those can be
used to compose a generator for Person
:
static Arbitrary<Person> personArbitrary() { final Gen<Person> personGenerator = arbInteger.gen.bind(arbString().gen, arbBoolean().gen, // compose the generators {int age => {String name => {boolean male => new Person(age, name, male)}}}; return arbitrary(personGenerator); }The example above uses Java 7 closure syntax. Here is the same example using objects instead:
static Arbitrary<Person> personArbitrary() { final Gen<Person> personGenerator = arbInteger.gen.bind(arbString.gen, arbBoolean.gen, // compose the generators new F<Integer, F<String, F<Boolean, Person>>>() { public F<String, F<Boolean, Person>> f(final Integer age) { return new F<String, F<Boolean, Person>>() { public F<Boolean, Person> f(final String name) { return new F<Boolean, Person>() { public Person f(final Boolean male) { return new Person(age, name, male); } }; } }; } }); return arbitrary(personGenerator); }
Modifier and Type | Method and Description |
---|---|
<B> Gen<B> |
apply(Gen<F<A,B>> gf)
Function application within this generator to produce a new generator.
|
<B> Gen<B> |
bind(F<A,Gen<B>> f)
Binds the given function across this generator to produce a new generator.
|
<B,C> Gen<C> |
bind(Gen<B> gb,
F<A,F<B,C>> f)
Binds the given function across this generator and the given generator to produce a new
generator.
|
<B,C,D> Gen<D> |
bind(Gen<B> gb,
Gen<C> gc,
F<A,F<B,F<C,D>>> f)
Binds the given function across this generator and the given generators to produce a new
generator.
|
<B,C,D,E> Gen<E> |
bind(Gen<B> gb,
Gen<C> gc,
Gen<D> gd,
F<A,F<B,F<C,F<D,E>>>> f)
Binds the given function across this generator and the given generators to produce a new
generator.
|
<B,C,D,E,F$> |
bind(Gen<B> gb,
Gen<C> gc,
Gen<D> gd,
Gen<E> ge,
F<A,F<B,F<C,F<D,F<E,F$>>>>> f)
Binds the given function across this generator and the given generators to produce a new
generator.
|
<B,C,D,E,F$,G> |
bind(Gen<B> gb,
Gen<C> gc,
Gen<D> gd,
Gen<E> ge,
Gen<F$> gf,
F<A,F<B,F<C,F<D,F<E,F<F$,G>>>>>> f)
Binds the given function across this generator and the given generators to produce a new
generator.
|
<B,C,D,E,F$,G,H> |
bind(Gen<B> gb,
Gen<C> gc,
Gen<D> gd,
Gen<E> ge,
Gen<F$> gf,
Gen<G> gg,
F<A,F<B,F<C,F<D,F<E,F<F$,F<G,H>>>>>>> f)
Binds the given function across this generator and the given generators to produce a new
generator.
|
<B,C,D,E,F$,G,H,I> |
bind(Gen<B> gb,
Gen<C> gc,
Gen<D> gd,
Gen<E> ge,
Gen<F$> gf,
Gen<G> gg,
Gen<H> gh,
F<A,F<B,F<C,F<D,F<E,F<F$,F<G,F<H,I>>>>>>>> f)
Binds the given function across this generator and the given generators to produce a new
generator.
|
static Gen<java.lang.Double> |
choose(double from,
double to)
Returns a generator that produces values between the given range (inclusive).
|
static Gen<java.lang.Integer> |
choose(int from,
int to)
Returns a generator that produces values between the given range (inclusive).
|
static <A> Gen<A> |
elements(A... as)
Returns a generator that produces values from the given arguments.
|
static <A> Gen<A> |
elemFrequency(List<P2<java.lang.Integer,A>> as)
Returns a generator that produces values from the given frequency and value pairs.
|
static <A> Gen<A> |
fail()
Returns a generator that never returns a value.
|
Gen<A> |
filter(F<A,java.lang.Boolean> f)
Returns a generator that produces values that meet the given predicate.
|
Unit |
foreach(java.lang.Integer i,
Rand r,
F<A,Unit> f)
Executes a side-effect for each generated result using the given arguments.
|
void |
foreachDoEffect(java.lang.Integer i,
Rand r,
Effect1<A> f)
Executes a side-effect for each generated result using the given arguments.
|
static <A> Gen<A> |
frequency(List<P2<java.lang.Integer,Gen<A>>> gs)
Returns a generator that uses values from the given frequency and generator pairs.
|
static <A> Gen<A> |
gen(F<java.lang.Integer,F<Rand,A>> f)
Returns a generator that uses the given function.
|
A |
gen(int i,
Rand r)
Applies the given size and random generator to produce a value.
|
static <A> Gen<A> |
join(Gen<Gen<A>> g)
Joins the generator of generators through a
bind(F) operation. |
static <A> Gen<List<A>> |
listOf(Gen<A> g)
Returns a generator of lists whose values come from the given generator.
|
static <A> Gen<List<A>> |
listOf(Gen<A> g,
int x)
Returns a generator of lists whose values come from the given generator.
|
static <A> Gen<List<A>> |
listOf1(Gen<A> g)
Returns a generator of lists whose values come from the given generator.
|
<B> Gen<B> |
map(F<A,B> f)
Maps the given function across this generator.
|
static <A> Gen<A> |
oneOf(List<Gen<A>> gs)
Returns a generator that produces values from one of the given generators on subsequent
requests.
|
static <A> Gen<A> |
parameterised(F<java.lang.Integer,F<Rand,Gen<A>>> f)
Constructs a generator that can access its construction arguments — size and random
generator.
|
static <A> Gen<List<A>> |
pick(int n,
List<A> as)
Returns a generator of lists that picks the given number of elements from the given list.
|
static <A,B> Gen<F<A,B>> |
promote(F<A,Gen<B>> f)
Promotes the given function to a generator for functions.
|
Gen<A> |
resize(int s)
Resizes this generator with the given size.
|
static <A> Gen<List<A>> |
sequence(List<Gen<A>> gs)
Sequence the given generators through a
bind(F) operation. |
static <A> Gen<List<A>> |
sequenceN(int n,
Gen<A> g)
Sequences the given generator the given number of times through a
bind(F) operation. |
static <A> Gen<A> |
sized(F<java.lang.Integer,Gen<A>> f)
Constructs a generator that can access its size construction arguments.
|
static <A> Gen<List<A>> |
someOf(List<A> as)
Returns a generator of lists that produces some of the values of the given list.
|
static <A> Gen<A> |
value(A a)
Returns a generator that always produces the given value.
|
public A gen(int i, Rand r)
i
- The size to use to produce the value.r
- The random generator to use to produce the value..public <B> Gen<B> map(F<A,B> f)
f
- The function to map across this generator.public Gen<A> filter(F<A,java.lang.Boolean> f)
f
- The predicate to meet for the values produced by the generator.public Unit foreach(java.lang.Integer i, Rand r, F<A,Unit> f)
i
- The size to generate the result to apply the side-effect to.r
- The random generator to generate the result to apply the side-effect to.f
- The side-effect to execute on the generated value.public void foreachDoEffect(java.lang.Integer i, Rand r, Effect1<A> f)
i
- The size to generate the result to apply the side-effect to.r
- The random generator to generate the result to apply the side-effect to.f
- The side-effect to execute on the generated value.public <B> Gen<B> bind(F<A,Gen<B>> f)
f
- The function to bind across this generator.public <B,C> Gen<C> bind(Gen<B> gb, F<A,F<B,C>> f)
gb
- The second generator to bind the given function across.f
- The function to bind across this generator and the given generator.public <B,C,D> Gen<D> bind(Gen<B> gb, Gen<C> gc, F<A,F<B,F<C,D>>> f)
gb
- The second generator to bind the given function across.gc
- The third generator to bind the given function across.f
- The function to bind across this generator and the given generators.public <B,C,D,E> Gen<E> bind(Gen<B> gb, Gen<C> gc, Gen<D> gd, F<A,F<B,F<C,F<D,E>>>> f)
gb
- The second generator to bind the given function across.gc
- The third generator to bind the given function across.gd
- The fourth generator to bind the given function across.f
- The function to bind across this generator and the given generators.public <B,C,D,E,F$> Gen<F$> bind(Gen<B> gb, Gen<C> gc, Gen<D> gd, Gen<E> ge, F<A,F<B,F<C,F<D,F<E,F$>>>>> f)
gb
- The second generator to bind the given function across.gc
- The third generator to bind the given function across.gd
- The fourth generator to bind the given function across.ge
- The fifth generator to bind the given function across.f
- The function to bind across this generator and the given generators.public <B,C,D,E,F$,G> Gen<G> bind(Gen<B> gb, Gen<C> gc, Gen<D> gd, Gen<E> ge, Gen<F$> gf, F<A,F<B,F<C,F<D,F<E,F<F$,G>>>>>> f)
gb
- The second generator to bind the given function across.gc
- The third generator to bind the given function across.gd
- The fourth generator to bind the given function across.ge
- The fifth generator to bind the given function across.gf
- The sixth generator to bind the given function across.f
- The function to bind across this generator and the given generators.public <B,C,D,E,F$,G,H> Gen<H> bind(Gen<B> gb, Gen<C> gc, Gen<D> gd, Gen<E> ge, Gen<F$> gf, Gen<G> gg, F<A,F<B,F<C,F<D,F<E,F<F$,F<G,H>>>>>>> f)
gb
- The second generator to bind the given function across.gc
- The third generator to bind the given function across.gd
- The fourth generator to bind the given function across.ge
- The fifth generator to bind the given function across.gf
- The sixth generator to bind the given function across.gg
- The seventh generator to bind the given function across.f
- The function to bind across this generator and the given generators.public <B,C,D,E,F$,G,H,I> Gen<I> bind(Gen<B> gb, Gen<C> gc, Gen<D> gd, Gen<E> ge, Gen<F$> gf, Gen<G> gg, Gen<H> gh, F<A,F<B,F<C,F<D,F<E,F<F$,F<G,F<H,I>>>>>>>> f)
gb
- The second generator to bind the given function across.gc
- The third generator to bind the given function across.gd
- The fourth generator to bind the given function across.ge
- The fifth generator to bind the given function across.gf
- The sixth generator to bind the given function across.gg
- The seventh generator to bind the given function across.gh
- The eighth generator to bind the given function across.f
- The function to bind across this generator and the given generators.public <B> Gen<B> apply(Gen<F<A,B>> gf)
gf
- The generator over the function to apply to this generator.public Gen<A> resize(int s)
s
- The new size of the generator.public static <A> Gen<A> gen(F<java.lang.Integer,F<Rand,A>> f)
f
- The function to use for this generator.public static <A> Gen<List<A>> sequence(List<Gen<A>> gs)
bind(F)
operation.gs
- The generators to sequence.public static <A> Gen<List<A>> sequenceN(int n, Gen<A> g)
bind(F)
operation.n
- The number of times to sequence the given generator.g
- The generator sequence.public static <A> Gen<A> parameterised(F<java.lang.Integer,F<Rand,Gen<A>>> f)
f
- The function that constructs the generator with its arguments.public static <A> Gen<A> sized(F<java.lang.Integer,Gen<A>> f)
f
- The function that constructs the generator with its size argument.public static <A> Gen<A> value(A a)
a
- The value to always produce.public static Gen<java.lang.Integer> choose(int from, int to)
from
- The value for the generator to produce values from.to
- The value for the generator to produce values from.public static Gen<java.lang.Double> choose(double from, double to)
from
- The value for the generator to produce values from.to
- The value for the generator to produce values from.public static <A> Gen<A> fail()
public static <A> Gen<A> join(Gen<Gen<A>> g)
bind(F)
operation.g
- The generator of generators to join.public static <A> Gen<A> frequency(List<P2<java.lang.Integer,Gen<A>>> gs)
gs
- The pairs of frequency and generator from which to return values in the returned
generator.public static <A> Gen<A> elemFrequency(List<P2<java.lang.Integer,A>> as)
as
- The pairs of frequency and value from which to produce values.public static <A> Gen<A> elements(A... as)
as
- The values that the returned generator may produce.public static <A> Gen<A> oneOf(List<Gen<A>> gs)
gs
- The list of generators to produce a value from.public static <A> Gen<List<A>> listOf(Gen<A> g, int x)
g
- The generator to produce values from for the returned generator.x
- An adjuster of size to apply to the given generator when producing values.public static <A> Gen<List<A>> listOf(Gen<A> g)
g
- The generator to produce values from for the returned generator.public static <A> Gen<List<A>> listOf1(Gen<A> g)
g
- The generator to produce values from for the returned generator.public static <A> Gen<List<A>> pick(int n, List<A> as)
n
- The number of elements to pick from the given list.as
- The list from which to pick elements.public static <A> Gen<List<A>> someOf(List<A> as)
as
- The list from which to pick values.