package qcheck

  1. Overview
  2. Docs
Legend:
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 (--) : int -> int -> int t

Infix synonym for int_range

val small_int : int t

Ints lower than 100

val split_int : int t -> (int * int) t

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

val string_len : int t -> string t

String of random length

val map : 'a t -> ('a -> 'b) -> 'b t

Transform an arbitrary into another

val map' : ('a -> 'b) -> 'a t -> 'b t
  • since 0.3
val list : ?len:int t -> 'a t -> 'a list t

List of arbitrary length. Default len is between 0 and 10.

val opt : 'a t -> 'a option t

May return a value, or None

val pair : 'a t -> 'b t -> ('a * 'b) t
val triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
val quad : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t
val list_repeat : int -> 'a t -> 'a list t

Lists of given length exactly

val array : ?len:int t -> 'a t -> 'a array t

Random array of random length

val array_repeat : int -> 'a t -> 'a array t

Random array of given length

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

  • since 0.3
val choose : 'a t list -> 'a t

Choice among a list generators

val (|||) : 'a t -> 'a t -> 'a t

Choice among two generators. a ||| b is the same as choose [a;b].

val fix : ?max:int -> base:'a t -> ('a t -> 'a t) -> 'a t

Recursive arbitrary values. The optional value max defines the maximal depth, if needed (default 15). base is the base case.

val fix_depth : depth:int t -> base:'a t -> ('a t -> 'a t) -> 'a t

Recursive values of at most given random depth

val fail_fix : unit -> 'a

Function used to indicate that, in fix_fuel, a sub-case is impossible to fulfill.

  • since 0.4
type 'a recursive_case = [
  1. | `Base of 'a t
    (*

    base case, no fuel

    *)
  2. | `Base_fuel of int -> 'a t
    (*

    base case, using fuel for its own purpose

    *)
  3. | `Rec of (int -> 'a list t) -> 'a t
    (*

    recursive case. must call the function exactly once

    *)
  4. | `Rec_fuel of (int -> 'a list t) -> int -> 'a t
    (*

    the function is given self and max and shall call self i exactly once for some i <= max.

    *)
  5. | `Rec1 of 'a t -> 'a t
    (*

    recursive case with exactly one subcase

    *)
  6. | `Rec2 of 'a t -> 'a t -> 'a t
    (*

    recursive case with exactly two subcases

    *)
]

What is a recursive case for a fueled fixpoint?

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..

  • since 0.3

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 *)

type tree' = Node2 of tree' * tree' | Node1 of tree' | Leaf' of int;;

let leaf' x = Leaf' x ;;
let node2 x y = Node2(x,y) ;;
let node1 x = Node1 x;;

(* alternative with [`Rec2] *)
let rand_tree' =
  fix_fuel [
     `Base (small_int >|= leaf');
     `Rec1 (fun self -> self >|= node1);
     `Rec2 (fun self1 self2 -> pure node2 <*> self1 <*> self2)
    ];;

generate ~n:1 (rand_tree' 20);;
type ('a, 'state) general_recursive_case = [
  1. | `Base of 'state -> 'a t
  2. | `Base_fuel of int -> 'state -> 'a t
  3. | `Rec of (int -> ('state -> 'a) list t) -> 'state -> 'a t
  4. | `Rec_fuel of (int -> ('state -> 'a) list t) -> int -> 'state -> 'a t
  5. | `Rec1 of ('state -> 'a t) -> 'state -> 'a t
  6. | `Rec2 of ('state -> 'a t) -> ('state -> 'a t) -> 'state -> 'a t
]

What is a recursive case for a general fueled fixpoint?

val fix_fuel_gen : ('a, 'state) general_recursive_case list -> int -> 'state -> 'a option t

fix_fuel_gen l state fuel consumes fuel in recursive subcases, similar to what fix_fuel l fuel would do, but more general because a "state" is passed bottom-up. In recursive subcases, is that l contains one or more recursive builders, such that every f in l is given a function f' : int -> ('state -> '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..

  • since 0.4
val lift : ('a -> 'b) -> 'a t -> 'b t
val lift2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val lift3 : ('a -> 'b -> 'c -> 'd) -> 'a t -> 'b t -> 'c t -> 'd t
val lift4 : ('a -> 'b -> 'c -> 'd -> 'e) -> 'a t -> 'b t -> 'c t -> 'd t -> 'e t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

Monadic bind

val (>|=) : 'a t -> ('a -> 'b) -> 'b t
  • since 0.3
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
  • since 0.3
val pure : 'a -> 'a t
  • since 0.3
val retry : 'a option t -> 'a t

Generate until a Some value is returned

val generate : ?n:int -> ?rand:Random.State.t -> 'a t -> 'a list

Generate n random values of the given type

OCaml

Innovation. Community. Security.