more examples in QCheck;

changed the interface of QCheck.Arbitrary.fix so that it
can use several instances of itself (build trees)
This commit is contained in:
Simon Cruanes 2013-09-28 16:22:32 +02:00
parent dd4bb3746e
commit 1bbdc943b4
2 changed files with 26 additions and 9 deletions

View file

@ -109,11 +109,11 @@ module Arbitrary = struct
let fix ?(max=max_int) ~base f =
let rec ar = lazy
(fun depth st ->
if depth >= max || Random.State.bool st
then base st (* base case *)
if depth >= max || Random.State.int st max < depth
then base st (* base case. THe deeper, the more likely. *)
else (* recurse *)
let x = (Lazy.force ar) (depth+1) st in
f x st)
let ar' = Lazy.force ar (depth+1) in
f ar' st)
in
Lazy.force ar 0

View file

@ -29,14 +29,31 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(** Examples:
- Not all lists are sorted:
[QCheck.run ~n:10 ~pp:QCheck.PP.(list int) QCheck.Arbitrary.(list small_int) (fun l -> l = List.sort compare l);;]
{[QCheck.run ~n:10 ~pp:QCheck.PP.(list int)
QCheck.Arbitrary.(list small_int) (fun l -> l = List.sort compare l);;
]}
- List.rev is involutive:
[QCheck.run ~n:1000 QCheck.Arbitrary.(list alpha) (fun l -> List.rev (List.rev l) = l)]
{[QCheck.run ~n:1000 QCheck.Arbitrary.(list alpha)
(fun l -> List.rev (List.rev l) = l)
]}
- generate a tree using {! 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)));;
ar (Random.State.make_self_init ());;
]}
*)
(** {2 Description of how to generate arbitrary values for some type} *)
module Arbitrary : sig
type 'a t = Random.State.t -> 'a
(** A generator of arbitrary values of type 'a *)
@ -98,11 +115,11 @@ module Arbitrary : sig
val choose : 'a t list -> 'a t
(** Choice among combinations *)
val fix : ?max:int -> base:'a t -> ('a -> 'a t) -> 'a t
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. [base] is the base case. *)
val fix_depth : depth:int t -> base:'a t -> ('a -> 'a t) -> 'a t
val fix_depth : depth:int t -> base:'a t -> ('a t -> 'a t) -> 'a t
(** Recursive values of at most given random depth *)
end