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 with shrink
.
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.
Implements¶
- GenObj[T] box
Constructors¶
create¶
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 flat_map
.
If implementing a custom Generator
based on another one,
with 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.
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 preferred to already return a ValueAndShrink
from generate
.
Parameters¶
- t: T
Returns¶
- (T^ , Iterator[T^] ref)
generate_value¶
Parameters¶
- rnd: Randomness ref
Returns¶
- T^ ?
generate_and_shrink¶
Parameters¶
- rnd: Randomness ref
Returns¶
- (T^ , Iterator[T^] ref) ?
filter¶
Apply predicate
to the values generated by this Generator
and only yields values for which predicate
returns true
.
Example:
Parameters¶
- predicate: {(T): (T^, Bool)}[T] box
Returns¶
- Generator[T] box
map[U: U]¶
Apply fn
to each value of this iterator
and yield the results.
Example:
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.
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.
Parameters¶
- other: Generator[U] box
Returns¶
- Generator[(T | U)] box
iter¶
Parameters¶
- rnd: Randomness ref
Returns¶
value_iter¶
Parameters¶
- rnd: Randomness ref
Returns¶
- Iterator[T^] ref
value_and_shrink_iter¶
Parameters¶
- rnd: Randomness ref