From 47d5e52224dd99c08fe27bf2565569c6c939b04d Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 13 Sep 2015 21:45:40 +0200 Subject: [PATCH] add some tests and use hidden feature of qtest! --- src/data/CCHashTrie.ml | 33 ++++++++++++++++++++++++++++++++- src/data/CCWBTree.ml | 14 +++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/data/CCHashTrie.ml b/src/data/CCHashTrie.ml index 30600f7f..49e915af 100644 --- a/src/data/CCHashTrie.ml +++ b/src/data/CCHashTrie.ml @@ -1,6 +1,19 @@ (* This file is free software, part of containers. See file "license" for more details. *) +(*$inject + module M = Make(CCInt) ;; + + let _listuniq = + let g, p = Q.(list (pair small_int small_int)) in + let g' st = + let l = g st in + CCList.Set.uniq ~eq:(fun a b -> fst a=fst b) l + in + g', p + ;; +*) + (** {1 Hash Tries} *) type 'a sequence = ('a -> unit) -> unit @@ -336,6 +349,12 @@ module Make(Key : KEY) let get_exn k m = get_exn_ k ~h:(hash_ k) m + (*$Q + _listuniq (fun l -> \ + let m = M.of_list l in \ + List.for_all (fun (x,y) -> M.get_exn x m = y) l) + *) + let get k m = try Some (get_exn_ k ~h:(hash_ k) m) with Not_found -> None @@ -401,6 +420,12 @@ module Make(Key : KEY) let add k v m = add_ k v ~h:(hash_ k) m + (*$Q + _listuniq (fun l -> \ + let m = List.fold_left (fun m (x,y) -> M.add x y m) M.empty l in \ + List.for_all (fun (x,y) -> M.get_exn x m = y) l) + *) + exception LocalExit let is_empty_arr_ a = @@ -445,6 +470,13 @@ module Make(Key : KEY) let remove k m = remove_rec_ k ~h:(hash_ k) m + (*$Q + Q.(list (pair small_int small_int)) (fun l -> \ + let m = M.of_list l in \ + List.for_all \ + (fun (x,_) -> let m' = M.remove x m in not (M.mem x m')) l) + *) + let update k f m = let h = hash_ k in let opt_v = try Some (get_exn_ k ~h m) with Not_found -> None in @@ -553,7 +585,6 @@ module Make(Key : KEY) end (*$R - let module M = Make(CCInt) in let m = M.of_list CCList.( (501 -- 1000) @ (500 -- 1) |> map (fun i->i,i)) in assert_equal ~printer:CCInt.to_string 1000 (M.cardinal m); assert_bool "check all get" diff --git a/src/data/CCWBTree.ml b/src/data/CCWBTree.ml index 1fa45d47..0ca8e3f0 100644 --- a/src/data/CCWBTree.ml +++ b/src/data/CCWBTree.ml @@ -8,6 +8,11 @@ The coefficients 5/2, 3/2 for balancing come from "balancing weight-balanced trees" *) +(*$inject + module M = Make(CCInt) + +*) + type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option type 'a printer = Format.formatter -> 'a -> unit @@ -241,17 +246,14 @@ module MakeFull(K : KEY) : S with type key = K.t = struct (*$Q & ~small:List.length Q.(list (pair small_int bool)) (fun l -> \ - let module M = Make(CCInt) in \ let m = M.of_list l in \ M.balanced m) Q.(list (pair small_int small_int)) (fun l -> \ let l = CCList.Set.uniq ~eq:(CCFun.compose_binop fst (=)) l in \ - let module M = Make(CCInt) in \ let m = M.of_list l in \ List.for_all (fun (k,v) -> M.get_exn k m = v) l) Q.(list (pair small_int small_int)) (fun l -> \ let l = CCList.Set.uniq ~eq:(CCFun.compose_binop fst (=)) l in \ - let module M = Make(CCInt) in \ let m = M.of_list l in \ M.cardinal m = List.length l) *) @@ -298,12 +300,10 @@ module MakeFull(K : KEY) : S with type key = K.t = struct (*$Q & ~small:List.length Q.(list (pair small_int small_int)) (fun l -> \ - let module M = Make(CCInt) in \ let m = M.of_list l in \ List.for_all (fun (k,_) -> \ M.mem k m && (let m' = M.remove k m in not (M.mem k m'))) l) Q.(list (pair small_int small_int)) (fun l -> \ - let module M = Make(CCInt) in \ let m = M.of_list l in \ List.for_all (fun (k,_) -> let m' = M.remove k m in M.balanced m') l) *) @@ -331,7 +331,6 @@ module MakeFull(K : KEY) : S with type key = K.t = struct with Not_found -> None (*$T - let module M = Make(CCInt) in \ let m = CCList.(0 -- 1000 |> map (fun i->i,i) |> M.of_list) in \ List.for_all (fun i -> M.nth_exn i m = (i,i)) CCList.(0--1000) *) @@ -422,7 +421,6 @@ module MakeFull(K : KEY) : S with type key = K.t = struct (*$Q & ~small:List.length Q.(list (pair small_int small_int)) ( fun lst -> \ - let module M = Make(CCInt) in \ let lst = CCList.Set.uniq ~eq:(CCFun.compose_binop fst (=)) lst in \ let m = M.of_list lst in \ List.for_all (fun (k,v) -> \ @@ -459,7 +457,6 @@ module MakeFull(K : KEY) : S with type key = K.t = struct (merge f l1 l2') (merge f r1 r2') (*$R - let module M = Make(CCInt) in let m1 = M.of_list [1, 1; 2, 2; 4, 4] in let m2 = M.of_list [1, 1; 3, 3; 4, 4; 7, 7] in let m = M.merge (fun k -> CCOpt.map2 (+)) m1 m2 in @@ -473,7 +470,6 @@ module MakeFull(K : KEY) : S with type key = K.t = struct (*$Q & ~small:(fun (l1,l2) -> List.length l1 + List.length l2) Q.(let p = list (pair small_int small_int) in pair p p) (fun (l1, l2) -> \ - let module M = Make(CCInt) in \ let eq x y = fst x = fst y in \ let l1 = CCList.Set.uniq ~eq l1 and l2 = CCList.Set.uniq ~eq l2 in \ let m1 = M.of_list l1 and m2 = M.of_list l2 in \