README

moonbitlang/quickcheck/gen does not have a README file

#
Gen

pub struct Gen[T] {
// private fields
}
fn Gen::Gen(gen : (Int,
RandomState
) -> T) -> Gen[T]

The Gen type represents a generator of values of type T.

#
Gen::ap

fn[T, U] Gen::ap(self : Gen[(T) -> U], v : Gen[T]) -> Gen[U]

Applicative Functor instance for Gen[T]

#
Gen::array_with_size

fn[T] Gen::array_with_size(self : Gen[T], size : Int) -> Gen[Array[T]]

Lift a per-element generator to a generator of arrays with exactly size elements. Each element is drawn independently from the same sample tree.

test {
let g = pure(0).array_with_size(4)
@debug.assert_eq(g.sample(), [0, 0, 0, 0])
}

#
Gen::bind

fn[T, U] Gen::bind(self : Gen[T], f : (T) -> Gen[U]) -> Gen[U]

Monad instance for Gen[T]

test {
// Use bind to sequence generators: first pick `n`, then draw an
// `n`-element list of the fixed value 0.
let g = pure(3).bind(n => pure(0).list_with_size(n))
debug_inspect(
g.sample(),
content=(
#|<List: [0, 0, 0]>
),
)
}

#
Gen::fmap

fn[T, U] Gen::fmap(self : Gen[T], f : (T) -> U) -> Gen[U]

Functor instance for Gen[T] (fmap)

test {
let doubled = pure(21).fmap(x => x * 2)
assert_eq(doubled.sample(), 42)
}

#
Gen::join

fn[T] Gen::join(self : Gen[Gen[T]]) -> Gen[T]

Monadic join: collapse Gen[Gen[T]] to Gen[T] by running the outer generator to produce an inner generator, then running the inner one.

#
Gen::list_with_size

fn[T] Gen::list_with_size(gen : Gen[T], size : Int) -> Gen[
List
[T]]

Lift a per-element generator to a generator of lists with exactly size elements (fixed length, independent draws).

test {
let g = pure(42).list_with_size(3)
debug_inspect(
g.sample(),
content=(
#|<List: [42, 42, 42]>
),
)
}

#
Gen::resize

fn[T] Gen::resize(self : Gen[T], size : Int) -> Gen[T]

Resize a generator to a specific value

#
Gen::run

fn[T] Gen::run(self : Gen[T], i : Int, rs :
RandomState
) -> T

Run a generator with a size and random state

#
Gen::sample

fn[T] Gen::sample(self : Gen[T], size? : Int, seed? : UInt64) -> T

Generate a value from a generator

#
Gen::samples

fn[T] Gen::samples(self : Gen[T], size? : Int, seed? : UInt64) -> Array[T]

Generate an array of samples from a generator

#
Gen::scale

fn[T] Gen::scale(self : Gen[T], f : (Int) -> Int) -> Gen[T]

Adjust the size parameter of a generator

#
Gen::spawn

Spawn a new generator from an arbitrary instance

#
Gen::such_that

fn[T] Gen::such_that(self : Gen[T], pred : (T) -> Bool) -> Gen[T]

Generate a value that satisfies a predicate

#
Gen::such_that_maybe

fn[T] Gen::such_that_maybe(self : Gen[T], pred : (T) -> Bool) -> Gen[T?]

Attempt to generate a value that satisfies a predicate If failures reach the maximum size, return None

#
backtrack

fn[T] backtrack(gs : Array[(UInt, Gen[T?])]) -> Gen[T?]

Tries weighted optional generators without replacement until one succeeds.

#
char_range

fn char_range(lo : Char, hi : Char) -> Gen[Char]

Generate char within given range [lo, hi]

#
flatten_array

fn[T] flatten_array(arr : Array[Gen[T]]) -> Gen[Array[T]]

Generate an array of elements from individual generators

#
flatten_list

Generate a list of elements from individual generators

#
flatten_option

fn[T] flatten_option(opt : Gen[T]?) -> Gen[T?]

Generate an option from an optional generator

#
flatten_result

fn[T, E] flatten_result(res : Result[Gen[T], E]) -> Gen[Result[T, E]]

Generate a result of a generator or return the pure error

#
frequency

fn[T] frequency(arr : Array[(UInt, Gen[T])]) -> Gen[T]

Chooses one of the given generators, with a weighted random distribution. @alert unsafe "Panics if the array is empty or total weight is zero"

test {
// 90% chance of "common", 10% chance of "rare". With a fixed seed
// the draws are deterministic — over 10 samples we expect mostly
// "common".
let g = frequency([(9U, pure("common")), (1U, pure("rare"))])
for x in g.samples() {
assert_true(x == "common" || x == "rare")
}
}

#
frequency_list

fn[T] frequency_list(lst :
List
[(UInt, T)]) -> Gen[T]

Chooses one of the given generators, with a weighted random distribution. @alert unsafe "Panics if the list is empty or total weight is zero"

#
int_bound

fn int_bound(bound : Int) -> Gen[Int]

Generates int within given bound [0, bound)

test {
for x in int_bound(100).samples() {
assert_true(x >= 0 && x < 100)
}
}

#
int_range

fn int_range(lo : Int, hi : Int) -> Gen[Int]

Generates int within given range [lo, hi)

test {
let g = int_range(10, 20)
for x in g.samples() {
assert_true(x >= 10 && x < 20)
}
}

#
integer_bound

Generates integer within given bound [0, bound)

#
liftA2

fn[A, B, C] liftA2(f : (A, B) -> C, v : Gen[A], w : Gen[B]) -> Gen[C]

Lift a binary function to generators

#
liftA3

fn[A, B, C, D] liftA3(f : (A, B, C) -> D, v : Gen[A], w : Gen[B], x : Gen[C]) -> Gen[D]

Lift a ternary function to generators

#
liftA4

fn[A, B, C, D, E] liftA4(f : (A, B, C, D) -> E, v : Gen[A], w : Gen[B], x : Gen[C], y : Gen[D]) -> Gen[E]

Lift a quaternary function to generators

#
liftA5

fn[A, B, C, D, E, F] liftA5(f : (A, B, C, D, E) -> F, v : Gen[A], w : Gen[B], x : Gen[C], y : Gen[D], z : Gen[E]) -> Gen[F]

Lift a quinary function to generators

#
liftA6

fn[A, B, C, D, E, F, G] liftA6(ff : (A, B, C, D, E, F) -> G, v : Gen[A], w : Gen[B], x : Gen[C], y : Gen[D], z : Gen[E], u : Gen[F]) -> Gen[G]

Lift a senary function to generators

#
one_of

fn[T] one_of(arr : Array[Gen[T]]) -> Gen[T]

Randomly uses one of the given generators. @alert unsafe "Panics if the array is empty"

test {
let g = one_of([pure(1), pure(2), pure(3)])
for x in g.samples() {
assert_true(x == 1 || x == 2 || x == 3)
}
}

#
one_of_array

fn[T] one_of_array(val : Array[T]) -> Gen[T]

Randomly select one element from an array @alert unsafe "Panics if the array is empty"

#
one_of_list

fn[T] one_of_list(lst :
List
[T]) -> Gen[T]

Randomly uses one of the given generators in list @alert unsafe "Panics if the list is empty"

#
pure

fn[T] pure(val : T) -> Gen[T]

Functor instance for Gen[T] (pure)

test {
// A pure generator ignores size and rng and always yields the value.
assert_eq(pure(42).sample(), 42)
assert_eq(pure("hi").sample(), "hi")
}

#
quad

fn[T, U, V, W] quad(gen1 : Gen[T], gen2 : Gen[U], gen3 : Gen[V], gen4 : Gen[W]) -> Gen[(T, U, V, W)]

Create quad generator from four generators

#
sized

fn[T] sized(f : (Int) -> Gen[T]) -> Gen[T]

Create sized generators

#
triple

fn[T, U, V] triple(gen1 : Gen[T], gen2 : Gen[U], gen3 : Gen[V]) -> Gen[(T, U, V)]

Create triple generator from three generators

#
tuple

fn[T, U] tuple(gen1 : Gen[T], gen2 : Gen[U]) -> Gen[(T, U)]

Create tuple generator from two generators

test {
let g : Gen[(Int, String)] = tuple(pure(7), pure("x"))
@debug.assert_eq(g.sample(), (7, "x"))
}

Source Files