Generator[T: T]¶
A Generator is capable of generating random values of a certain type T
given a source of Randomness
and knows how to shrink or simplify values of that type.
When testing a property against one or more given Generators
those generators' generate
methods are being called many times
to generate sample values that are then used to validate the property.
When a failing sample is found, the ponycheck engine is trying to find a
smaller or more simple sample by shrinking
it.
If the generator did not provide any shrinked samples
as a result of generate
, its shrink
method is called
to obtain simpler results. Ponycheck obtains more shrunken samples until
the property is not failing anymore.
The last failing sample, which is considered the most simple one,
is then reported to the user.
class box Generator[T: T] is
GenObj[T] box
Implements¶
- GenObj[T] box
Constructors¶
create¶
new ref create(
gen: GenObj[T] box)
: Generator[T] ref^
Parameters¶
- gen: GenObj[T] box
Returns¶
- Generator[T] ref^
Public Functions¶
generate¶
Let this generator generate a value
given a source of Randomness
.
Also allow for returning a value and pre-generated shrink results
as a ValueAndShrink[T]
instance, a tuple of (T^, Seq[T])
.
This helps propagating shrink results through all kinds of Generator
combinators like filter
, map
and flatMap
.
If implementing a custom Generator
based on another one,
a Generator Combinator, you should use shrunken values
returned by generate
to also return shrunken values based on them
If generating an example value is costly, it might be more efficient to simply return the generated value and only shrink in big steps or do no shrinking at all. If generating values is lightweight, shrunken values should also be returned.
fun box generate(
rnd: Randomness ref)
: (T^ | (T^ , Iterator[T^] ref)) ?
Parameters¶
- rnd: Randomness ref
Returns¶
- (T^ | (T^ , Iterator[T^] ref)) ?
shrink¶
Simplify the given value.
As the returned value can also be iso
, it needs to be consumed and returned
It is preffered to already return a ValueAndShrink
from generate
.
fun box shrink(
t: T)
: (T^ , Iterator[T^] ref)
Parameters¶
- t: T
Returns¶
- (T^ , Iterator[T^] ref)
generate_value¶
fun box generate_value(
rnd: Randomness ref)
: T^ ?
Parameters¶
- rnd: Randomness ref
Returns¶
- T^ ?
generate_and_shrink¶
fun box generate_and_shrink(
rnd: Randomness ref)
: (T^ , Iterator[T^] ref) ?
Parameters¶
- rnd: Randomness ref
Returns¶
- (T^ , Iterator[T^] ref) ?
filter¶
apply predicate
to the values generated by this Generator
and only values for which predicate
returns true
.
Example:
let even_i32s =
Generators.i32()
.filter(
{(t) => (t, ((t % 2) == 0)) })
fun box filter(
predicate: {(T): (T^, Bool)}[T] box)
: Generator[T] box
Parameters¶
- predicate: {(T): (T^, Bool)}[T] box
Returns¶
- Generator[T] box
map[U: U]¶
apply function fn
to each value of this iterator
and yield the results.
Example:
let single_code_point_string_gen =
Generators.u32()
.map[String]({(u) => String.from_utf32(u) })
fun box map[U: U](
fn: {(T): U^}[T, U] box)
: Generator[U] box
Parameters¶
- fn: {(T): U^}[T, U] box
Returns¶
- Generator[U] box
flat_map[U: U]¶
For each value of this generator create a generator that is then combined.
fun box flat_map[U: U](
fn: {(T): Generator[U]}[T, U] box)
: Generator[U] box
Parameters¶
- fn: {(T): Generator[U]}[T, U] box
Returns¶
- Generator[U] box
union[U: U]¶
Create a generator that produces the value of this generator or the other with the same probability, returning a union type of this generator and the other one.
fun box union[U: U](
other: Generator[U] box)
: Generator[(T | U)] box
Parameters¶
- other: Generator[U] box
Returns¶
- Generator[(T | U)] box
iter¶
fun box iter(
rnd: Randomness ref)
: Iterator[(T^ | (T^ , Iterator[T^] ref))] ref^
Parameters¶
- rnd: Randomness ref
Returns¶
value_iter¶
fun box value_iter(
rnd: Randomness ref)
: Iterator[T^] ref
Parameters¶
- rnd: Randomness ref
Returns¶
- Iterator[T^] ref
value_and_shrink_iter¶
fun box value_and_shrink_iter(
rnd: Randomness ref)
: Iterator[(T^ , Iterator[T^] ref)] ref
Parameters¶
- rnd: Randomness ref