Library
Module
Module type
Parameter
Class
Class type
The library takes inspiration from Haskell's QuickCheck library. The rough idea is that the programer describes invariants that values of a certain type need to satisfy ("properties"), as functions from this type to bool. She also needs to desribe how to generate random values of the type, so that the property is tried and checked on a number of random instances.
This explains the organization of this module:
Arbitrary
is used to describe how to generate random values. An 'a Arbitrary.t
is a random generator of values of type 'a.Prop
is used to describe and combine properties. Especially interesting is Prop.(==>)
, that is such that a ==> b
only checks the property b
on a value x
if a x
holds (precondition).PP
describes a few combinators to print values. This is used when a property fails on some instances, to print the failing instances.Then, a few functions are provided to test properties. Optional parameters allow to specify the random generator, the printer for failing cases, the number of instances to generate and test...
Examples:
let test = QCheck.mk_test ~n:1000 QCheck.Arbitrary.(list alpha)
(fun l -> List.rev (List.rev l) = l);;
QCheck.run test;;
let test = QCheck.(
mk_test
~n:10_000 ~size:List.length ~limit:15 ~pp:QCheck.PP.(list int)
QCheck.Arbitrary.(list small_int)
(fun l -> l = List.sort compare l));;
QCheck.run test;;
Arbitrary.fix
:type tree = Int of int | Node of tree list;;
let ar = QCheck.Arbitrary.(fix ~max:10
~base:(map small_int (fun i -> Int i))
(fun t st -> Node (list t st)));;
Arbitrary.generate ~n:20 ar;;
More complex and powerful combinators can be found in Gabriel Scherer's Generator
module. Its documentation can be found here.
module Arbitrary : sig ... end
module PP : sig ... end
QCheck helps you test invariants, or properties. This submodule helps dealing with such properties. A property of the 'a
type is a predicate 'a -> bool
.
If the property doesn't have to hold for all values of type 'a
, you can use Prop.assume
or Prop.assume_lazy
in the body of the property. Doing so will "filter" irrelevant values (the one which do not satisfy the assumption) out (in practice it raises an exception).
module Prop : sig ... end
val check :
?rand:Random.State.t ->
?n:int ->
'a Arbitrary.t ->
'a Prop.t ->
'a result
Check that the property prop
holds on n
random instances of the type 'a, as generated by the arbitrary instance gen
val mk_test :
?n:int ->
?pp:'a PP.t ->
?name:string ->
?size:('a -> int) ->
?limit:int ->
'a Arbitrary.t ->
'a Prop.t ->
test
Construct a test. Optional parameters are the same as for run
.
val run : ?out:out_channel -> ?rand:Random.State.t -> test -> bool
Run a test and print results
type suite = test list
A test suite is a list of tests
val run_tests : ?out:out_channel -> ?rand:Random.State.t -> suite -> bool
Run a suite of tests, and print its results