qcheck/generator

This module provides functions for generating values of various types as well as functions for creating new generators from existing ones.

Types

Generator(a) is a random generator for values of type a.

Note: It is likely that this type will become opaque in the future.

pub type Generator(a) {
  Generator(fn(Seed) -> #(Tree(a), Seed))
}

Constructors

  • Generator(fn(Seed) -> #(Tree(a), Seed))

Functions

pub fn apply(
  f: Generator(fn(a) -> b),
  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(a),
  f: fn(a) -> Generator(b),
) -> Generator(b)

bind(gen, f) generates a value of type a with gen, then passes that value to f, which uses it to generate values of type b.

pub fn bool() -> Generator(Bool)

bool() generates booleans and shrinks towards False.

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: Int,
  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 key: Generator(a),
  value value: Generator(b),
  max_length max_length: Int,
) -> Generator(Dict(a, b))

dict_generic(key_gen, value_gen, max_len) generates dictionaries with keys from key_gen and values from value_gen 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 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(
  gen: 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: Int,
  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(
  elt_gen: Generator(a),
  min_length min_len: Int,
  max_length max_len: Int,
) -> Generator(List(a))

list_generic(elt_gen, min_len, max_len) generates lists of elements from elt_gen 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(a),
  f: fn(a) -> b,
) -> Generator(b)

map(gen, f) transforms the generator gen by applying f to each generated value. Shrinks as gen shrinks, but with f applied.

pub fn map2(
  f: fn(a, b) -> c,
  a: Generator(a),
  b: Generator(b),
) -> Generator(c)

map2(f, a, b) transforms two generators, a and b, by applying f to each pair of generated values.

pub fn map3(
  f: fn(a, b, c) -> d,
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
) -> Generator(d)

map3(f, a, b, c) transforms three generators, a, b, and c, by applying f to each triple of generated values.

pub fn map4(
  f: fn(a, b, c, d) -> e,
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
  d: Generator(d),
) -> Generator(e)

map4(f, a, b, c, d) transforms four generators, a, b, c, and d, by applying f to each quadruple of generated values.

pub fn map5(
  f: fn(a, b, c, d, e) -> f,
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
  d: Generator(d),
  e: Generator(e),
) -> Generator(f)

map5(f, a, b, c, d, e) transforms five generators, a, b, c, d, and e, by applying f to each quintuple of generated values.

pub fn map6(
  f: fn(a, b, c, d, e, f) -> g,
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
  d: Generator(d),
  e: Generator(e),
  f_: Generator(f),
) -> Generator(g)

map6(f, a, b, c, d, e, f_) transforms six generators, a, b, c, d, e, and f_, 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(
  elt_gen: Generator(a),
  max_length max_len: Int,
) -> Generator(Set(a))

set_generic(elt_gen, max_len) generates sets of elements from elt_gen.

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_gen: Generator(String),
) -> Generator(String)

string_from(char_gen) generates strings from the given character generator using the default length generator.

pub fn string_generic(
  char_gen: Generator(String),
  length_gen: Generator(Int),
) -> Generator(String)

string_generic(char_gen, length_gen) generates strings with characters from char_gen and lengths from length_gen.

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_gen: Generator(String),
) -> Generator(String)

string_non_empty_from(char_gen) 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(
  gen: 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(
  a: Generator(a),
  b: Generator(b),
) -> Generator(#(a, b))

tuple2(a, b) generates a tuple of two values, one from generator a and one from generator b.

pub fn tuple3(
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
) -> Generator(#(a, b, c))

tuple3(a, b, c) generates a tuple of three values, one each from generators a, b, and c.

pub fn tuple4(
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
  d: Generator(d),
) -> Generator(#(a, b, c, d))

tuple4(a, b, c, d) generates a tuple of four values, one each from generators a, b, c, and d.

pub fn tuple5(
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
  d: Generator(d),
  e: Generator(e),
) -> Generator(#(a, b, c, d, e))

tuple5(a, b, c, d, e) generates a tuple of five values, one each from generators a, b, c, d, and e.

pub fn tuple6(
  a: Generator(a),
  b: Generator(b),
  c: Generator(c),
  d: Generator(d),
  e: Generator(e),
  f: Generator(f),
) -> Generator(#(a, b, c, d, e, f))

tuple6(a, b, c, d, e, f) generates a tuple of six values, one each from generators a, b, c, d, e, and f.

Search Document