Std.Gen
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) # => :ok
use Std.Gen
# Generate a list of 0..10 small ints.
list_int(10, 0, 5) # e.g. [3, 0, 2, 5]
Functions
-
# fn __group__() -> Atom
Group tag consumed by
Cure.Stdlib.Preload. -
# 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 tolowhenhi <= 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
nintegers in[lo, hi]. -
# fn one_of(list: List(Int), default: Int) -> Int
Pick an element uniformly from a non-empty list of integers. Returns
defaultfor an empty list. -
# fn seed(_alg: Atom, _seed: Tuple) -> Tuple extern
Seed the PRNG. Thin wrapper over
:rand.seed/2; returns the new state tuple. -
# fn shrink(v: T) -> List(T) extern
Default polymorphic shrinker; dispatches on the runtime shape of
vand returns the empty list for types with no defined shrink. -
# fn shrink_int(v: Int) -> List(Int) extern
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/3when a property fails, to iteratively narrow down the counterexample. -
# fn shrink_list(xs: List(Int)) -> List(List(Int)) extern
Shrink a list by dropping elements. Shortest candidates come first.