From 1bbdc943b4662a757d0f9387e0689a5f059b85b8 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 28 Sep 2013 16:22:32 +0200 Subject: [PATCH] more examples in QCheck; changed the interface of QCheck.Arbitrary.fix so that it can use several instances of itself (build trees) --- qCheck.ml | 8 ++++---- qCheck.mli | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/qCheck.ml b/qCheck.ml index dde3a49f..649bae0f 100644 --- a/qCheck.ml +++ b/qCheck.ml @@ -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 diff --git a/qCheck.mli b/qCheck.mli index 5fef9711..3266c8bb 100644 --- a/qCheck.mli +++ b/qCheck.mli @@ -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