qcheck/generator
This module provides functions for generating values of various types as well as functions for creating new generators from existing ones.
Types
Functions
pub fn apply(
f f: Generator(fn(a) -> b),
generator x: Generator(a),
) -> Generator(b)
apply(f, x) applies a function generator, f, and an argument generator,
x, into a result generator.
pub fn bind(
generator generator: Generator(a),
f f: fn(a) -> Generator(b),
) -> Generator(b)
bind(generator, f) generates a value of type a with generator, then
passes that value to f, which uses it to generate values of type b.
pub fn char() -> Generator(String)
char() generates characters with a bias towards printable ASCII
characters, while still hitting some edge cases.
pub fn char_alpha() -> Generator(String)
char_alpha() generates alphabetic (ASCII) characters.
pub fn char_alpha_numeric() -> Generator(String)
char_alpha_numeric() generates alphanumeric (ASCII) characters.
pub fn char_digit() -> Generator(String)
char_digit() generates digits from 0 to 9, inclusive.
pub fn char_from_list(chars: List(String)) -> Generator(String)
char_from_list(chars) generates characters from the given list of
characters.
pub fn char_lowercase() -> Generator(String)
char_lowercase() generates lowercase (ASCII) letters.
pub fn char_print() -> Generator(String)
char_print() generates printable ASCII characters, with a bias towards
alphanumeric characters.
pub fn char_print_uniform() -> Generator(String)
char_print_uniform() generates printable ASCII characters.
pub fn char_uniform() -> Generator(String)
char_uniform() generates characters uniformly distributed across the
default range.
pub fn char_uniform_inclusive(
low low: Int,
high high: Int,
) -> Generator(String)
char_uniform_inclusive(low, high) generates “characters” uniformly
distributed between low and high, inclusive. Here, “characters” are
strings of a single codepoint.
Note: this function is slightly weird in that it takes the integer
representation of the range of codepoints, not the strings themselves.
This behavior will likely change.
These char_* functions are mainly used for setting up the string
generators.
Shrinks towards a when possible, but won’t go outside of the range.
pub fn char_uppercase() -> Generator(String)
char_uppercase() generates uppercase (ASCII) letters.
pub fn char_whitespace() -> Generator(String)
char_whitespace() generates whitespace (ASCII) characters.
pub fn dict_generic(
key_generator key_generator: Generator(a),
value_generator value_generator: Generator(b),
max_length max_length: Int,
) -> Generator(Dict(a, b))
dict_generic(key_generator, value_generator, max_len) generates dictionaries with keys
from key_generator and values from value_generator with lengths up to max_len.
Shrinks on size then on elements.
pub fn float() -> Generator(Float)
float() generates floats with a bias towards smaller values and shrinks
towards 0.0.
pub fn float_uniform_inclusive(
low: Float,
high: Float,
) -> Generator(Float)
pub fn from_generators(
generators: List(Generator(a)),
) -> Generator(a)
from_generators(generators) chooses a generator from a list of generators
weighted uniformly, then chooses a value from that generator.
pub fn from_weighted_generators(
generators: List(#(Float, Generator(a))),
) -> Generator(a)
from_generators(generators) chooses a generator from a list of generators
weighted by the given weigths, then chooses a value from that generator.
pub fn generate_tree(
generator: Generator(a),
seed: Seed,
) -> #(Tree(a), Seed)
generate(gen, seed) generates a value of type a and its shrinks using the generator gen.
You should not use this function directly. It is for internal use only.
pub fn int_uniform() -> Generator(Int)
int_uniform() generates uniformly distributed integers across a large
range and shrinks towards 0.
Note: this generator does not hit interesting or corner cases very often.
pub fn int_uniform_inclusive(
low low: Int,
high high: Int,
) -> Generator(Int)
int_uniform_inclusive(low, high) generates integers uniformly distributed
between low and high, inclusive.
Shrinks towards 0, but won’t shrink outside of the range [low, high].
pub fn list_generic(
element_generator: Generator(a),
min_length min_len: Int,
max_length max_len: Int,
) -> Generator(List(a))
list_generic(element_generator, min_len, max_len) generates lists of
elements from element_generator with lengths between min_len and
max_len, inclusive.
Shrinks first on the number of elements, then on the elements themselves.
pub fn map(
generator generator: Generator(a),
f f: fn(a) -> b,
) -> Generator(b)
map(generator, f) transforms the generator generator by applying f to
each generated value. Shrinks as generator shrinks, but with f applied.
pub fn map2(
f f: fn(a, b) -> c,
g1 g1: Generator(a),
g2 g2: Generator(b),
) -> Generator(c)
map2(f, g1, g2) transforms two generators, g1 and g2, by applying f
to each pair of generated values.
pub fn map3(
f f: fn(a, b, c) -> d,
g1 g1: Generator(a),
g2 g2: Generator(b),
g3 g3: Generator(c),
) -> Generator(d)
map3(f, g1, g2, g3) transforms three generators, g1, g2, and g3, by
applying f to each triple of generated values.
pub fn map4(
f f: fn(a, b, c, d) -> e,
g1 g1: Generator(a),
g2 g2: Generator(b),
g3 g3: Generator(c),
g4 g4: Generator(d),
) -> Generator(e)
map4(f, g1, g2, g3, g4) transforms four generators, g1, g2, g3, and
g4, by applying f to each quadruple of generated values.
pub fn map5(
f f: fn(a, b, c, d, e) -> f,
g1 g1: Generator(a),
g2 g2: Generator(b),
g3 g3: Generator(c),
g4 g4: Generator(d),
g5 g5: Generator(e),
) -> Generator(f)
map5(f, g1, g2, g3, g4, g5) transforms five generators, g1, g2, g3,
g4, and g5, by applying f to each quintuple of generated values.
pub fn map6(
f f: fn(a, b, c, d, e, f) -> g,
g1 g1: Generator(a),
g2 g2: Generator(b),
g3 g3: Generator(c),
g4 g4: Generator(d),
g5 g5: Generator(e),
g6 g6: Generator(f),
) -> Generator(g)
map6(f, g1, g2, g3, g4, g5, g6) transforms six generators, g1, g2, g3,
g4, g5, and g6, by applying f to each sextuple of generated values.
pub fn nil() -> Generator(Nil)
nil() is the Nil generator. It always returns Nil and does not shrink.
pub fn option(generator: Generator(a)) -> Generator(Option(a))
option(gen) is an Option generator that uses gen to generate Some
values. Shrinks towards None then towards shrinks of gen.
pub fn return(a: a) -> Generator(a)
return(a) creates a generator that always returns a and does not shrink.
pub fn set_generic(
element_generator: Generator(a),
max_length max_len: Int,
) -> Generator(Set(a))
set_generic(element_generator, max_len) generates sets of elements from
element_generator.
Shrinks first on the number of elements, then on the elements themselves.
pub fn small_positive_or_zero_int() -> Generator(Int)
small_positive_or_zero_int() generates small integers well suited for
modeling the sizes of sized elements like lists or strings.
Smaller numbers are more likely than larger numbers.
Shrinks towards 0.
pub fn small_strictly_positive_int() -> Generator(Int)
small_strictly_positive_int() generates small integers strictly greater
than 0.
pub fn string() -> Generator(String)
`string() generates strings with the default character generator and the default length generator.
pub fn string_from(
char_generator: Generator(String),
) -> Generator(String)
string_from(char_generator) generates strings from the given character generator
using the default length generator.
pub fn string_generic(
char_generator: Generator(String),
length_generator: Generator(Int),
) -> Generator(String)
string_generic(char_generator, length_generator) generates strings with
characters from char_generator and lengths from length_generator.
pub fn string_non_empty() -> Generator(String)
string_non_empty() generates non-empty strings with the default character
generator and the default length generator.
pub fn string_non_empty_from(
char_generator: Generator(String),
) -> Generator(String)
string_non_empty_from(char_generator) generates non-empty strings from the given
character generator using the default length generator.
pub fn string_with_length(length: Int) -> Generator(String)
string_with_length(length) generates strings of the given length with the
default character generator.
pub fn string_with_length_from(
generator: Generator(String),
length: Int,
) -> Generator(String)
string_with_length_from(gen, length) generates strings of the given
length from the given generator.
pub fn tuple2(
g1: Generator(a),
g2: Generator(b),
) -> Generator(#(a, b))
tuple2(g1, g2) generates a tuple of two values, one each from generators
g1 and g2.
pub fn tuple3(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
) -> Generator(#(a, b, c))
tuple3(g1, g2, g3) generates a tuple of three values, one each from
generators g1, g2, and g3.
pub fn tuple4(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
) -> Generator(#(a, b, c, d))
tuple4(g1, g2, g3, g4) generates a tuple of four values, one each from
generators g1, g2, g3, and g4.
pub fn tuple5(
g1: Generator(a),
g2: Generator(b),
g3: Generator(c),
g4: Generator(d),
g5: Generator(e),
) -> Generator(#(a, b, c, d, e))
tuple5(g1, g2, g3, g4, g5) generates a tuple of five values, one each from
generators g1, g2, g3, g4, and g5.