Std. Gen
(Cure Standard Library v0.37.2)
View Source
Tiny stateless generator API for property-based testing (v0.19.0).
Generators are plain zero-argument lambdas returning a fresh
value on every call. Randomness is delegated to Erlang's :rand
module. Used in tandem with Std.Test.forall/3 to drive
property-based tests, with optional shrinkers below.
Examples
use Std.Gen
let g = fn(_) -> int_in(1, 100)
Std.Test.forall_default(g, fn(x) -> x > 0) # => :okuse Std.Gen
# Generate a list of 0..10 small ints.
list_int(10, 0, 5) # e.g. [3, 0, 2, 5]
Summary
Callbacks
Shrink(t) produces progressively smaller candidates for a value of
type t, most aggressive first. An empty list means "already minimal".
Functions
A fair Bool: 50/50.
A constant "generator" that always returns the given value.
Uniformly choose an integer in the inclusive range [lo, hi].
Falls back to lo when hi <= lo.
Generate a list of length up to max_len (inclusive), filled
with integers in [lo, hi]. A good default shrink is "shorter
list".
Generate a list of exactly n integers in [lo, hi].
Pick an element uniformly from a non-empty list of integers.
Returns default for an empty list.
Private wrapper over :rand.uniform/1; returns an integer in
[1, n].
Seed the PRNG. Thin wrapper over :rand.seed/2; returns the new
state tuple.
Seed the PRNG with algorithm and seed_value, typed.
Smaller candidates for v, most aggressive first.
Produce a list of "smaller" candidate values for v. The
returned list is ordered from most aggressive shrink (halve to
zero) to least aggressive (subtract one). Empty list means
"already minimal".
Shrink a list by dropping elements. Shortest candidates come first.
Types
Callbacks
interface Shrink(t)
Shrink(t) produces progressively smaller candidates for a value of
type t, most aggressive first. An empty list means "already minimal".
This is an interface rather than a fn shrink(v: t) -> List(t), and the
difference is not cosmetic. No parametric inhabitant of ∀t. t -> List(t)
can inspect its argument — the free theorem forces
shrink(g x) == map(g, shrink x) for every g — yet shrinking must
inspect. A typeclass method may dispatch on t, because the dictionary is
a real argument. Applying shrink at a type with no implementation is now
a compile error instead of a silent [].
Functions
fn bool() -> Bool
A fair Bool: 50/50.
fn constant(v: t) -> t
A constant "generator" that always returns the given value.
fn int_in(lo: Int, hi: Int) -> Int
Uniformly choose an integer in the inclusive range [lo, hi].
Falls back to lo when hi <= lo.
fn list_int(max_len: Int, lo: Int, hi: Int) -> List(Int)
Generate a list of length up to max_len (inclusive), filled
with integers in [lo, hi]. A good default shrink is "shorter
list".
fn list_of_int(n: Int, lo: Int, hi: Int) -> List(Int)
Generate a list of exactly n integers in [lo, hi].
fn one_of(list: List(Int), default: Int) -> Int
Pick an element uniformly from a non-empty list of integers.
Returns default for an empty list.
fn rand_uniform(n: Int) -> Int
Private wrapper over :rand.uniform/1; returns an integer in
[1, n].
fn random_algorithm_atom(algorithm: RandomAlgorithm) -> Atom
fn raw_seed(_alg: Atom, _seed: Tuple) -> Tuple
Seed the PRNG. Thin wrapper over :rand.seed/2; returns the new
state tuple.
fn seed(algorithm: RandomAlgorithm, seed_value: Tuple) -> Tuple
Seed the PRNG with algorithm and seed_value, typed.
The typed counterpart of the raw extern above: converts the
RandomAlgorithm constructor to its BEAM atom and delegates. Returns the
new state tuple.
fn shrink(v: t) -> List(t)
Smaller candidates for v, most aggressive first.
fn shrink_int(v: Int) -> List(Int)
Produce a list of "smaller" candidate values for v. The
returned list is ordered from most aggressive shrink (halve to
zero) to least aggressive (subtract one). Empty list means
"already minimal".
Used by Std.Test.forall_shrunk/3 when a property fails, to
iteratively narrow down the counterexample.
fn shrink_list(xs: List(Int)) -> List(List(Int))
Shrink a list by dropping elements. Shortest candidates come first.