Library
Module
Module type
Parameter
Class
Class type
type 'a t = Random.State.t -> 'a
A generator of arbitrary values of type 'a
val return : 'a -> 'a t
Return always the same value (e.g. 4
)
val int : int -> int t
Any integer between 0 (inclusive) and the given higher bound (exclusive)
val int_range : start:int -> stop:int -> int t
val small_int : int t
Ints lower than 100
split_int gen
generates a number n
from gen
, and returns i, j
where i + j = n
val bool : bool t
Arbitrary boolean
val char : char t
A (printable) char
val alpha : char t
Alphabetic char
val float : float -> float t
Random float
val string : string t
Random strings of small length
List of arbitrary length. Default len
is between 0 and 10.
val among : 'a list -> 'a t
Choose an element among those of the list
val among_array : 'a array -> 'a t
Choose in the array
val shuffle : 'a array -> unit t
Shuffle the array in place
Recursive arbitrary values. The optional value max
defines the maximal depth, if needed (default 15). base
is the base case.
Recursive values of at most given random depth
val fix_fuel : 'a recursive_case list -> int -> 'a option t
fix_fuel l fuel
consumes fuel
in recursive subcases. The idea is that l
contains one or more recursive builders, such that every f
in l
is given a function f' : int -> 'a list t
, to call with an integer n
so as to obtain n
recursive subcases. The function f'
MUST be called exactly once per case f
in l
..
Example:
type tree = Node of tree * tree | Leaf of int;;
let leaf_ x = Leaf x;;
let node_ x y = Node (x,y);;
let rand_tree =
fix_fuel [
`Base (small_int >|= leaf_);
`Rec (fun self ->
self 2 >>= function [x;y] ->
return (node_ x y))
];;
generate (rand_tree 20);; (* generate trees with 20 nodes *)
val pure : 'a -> 'a t
val generate : ?n:int -> ?rand:Random.State.t -> 'a t -> 'a list
Generate n
random values of the given type