add some tests and use hidden feature of qtest!

This commit is contained in:
Simon Cruanes 2015-09-13 21:45:40 +02:00
parent 6c16656da0
commit 47d5e52224
2 changed files with 37 additions and 10 deletions

View file

@ -1,6 +1,19 @@
(* This file is free software, part of containers. See file "license" for more details. *) (* 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} *) (** {1 Hash Tries} *)
type 'a sequence = ('a -> unit) -> unit 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 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 = let get k m =
try Some (get_exn_ k ~h:(hash_ k) m) try Some (get_exn_ k ~h:(hash_ k) m)
with Not_found -> None with Not_found -> None
@ -401,6 +420,12 @@ module Make(Key : KEY)
let add k v m = add_ k v ~h:(hash_ k) m 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 exception LocalExit
let is_empty_arr_ a = let is_empty_arr_ a =
@ -445,6 +470,13 @@ module Make(Key : KEY)
let remove k m = remove_rec_ k ~h:(hash_ k) m 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 update k f m =
let h = hash_ k in let h = hash_ k in
let opt_v = try Some (get_exn_ k ~h m) with Not_found -> None 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 end
(*$R (*$R
let module M = Make(CCInt) in
let m = M.of_list CCList.( (501 -- 1000) @ (500 -- 1) |> map (fun i->i,i)) 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_equal ~printer:CCInt.to_string 1000 (M.cardinal m);
assert_bool "check all get" assert_bool "check all get"

View file

@ -8,6 +8,11 @@
The coefficients 5/2, 3/2 for balancing come from "balancing weight-balanced trees" 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 sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a printer = Format.formatter -> 'a -> unit 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 & ~small:List.length
Q.(list (pair small_int bool)) (fun l -> \ Q.(list (pair small_int bool)) (fun l -> \
let module M = Make(CCInt) in \
let m = M.of_list l in \ let m = M.of_list l in \
M.balanced m) M.balanced m)
Q.(list (pair small_int small_int)) (fun l -> \ Q.(list (pair small_int small_int)) (fun l -> \
let l = CCList.Set.uniq ~eq:(CCFun.compose_binop fst (=)) l in \ 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 \ let m = M.of_list l in \
List.for_all (fun (k,v) -> M.get_exn k m = v) l) List.for_all (fun (k,v) -> M.get_exn k m = v) l)
Q.(list (pair small_int small_int)) (fun l -> \ Q.(list (pair small_int small_int)) (fun l -> \
let l = CCList.Set.uniq ~eq:(CCFun.compose_binop fst (=)) l in \ 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 \ let m = M.of_list l in \
M.cardinal m = List.length l) 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 & ~small:List.length
Q.(list (pair small_int small_int)) (fun l -> \ Q.(list (pair small_int small_int)) (fun l -> \
let module M = Make(CCInt) in \
let m = M.of_list l in \ let m = M.of_list l in \
List.for_all (fun (k,_) -> \ List.for_all (fun (k,_) -> \
M.mem k m && (let m' = M.remove k m in not (M.mem k m'))) l) 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 -> \ Q.(list (pair small_int small_int)) (fun l -> \
let module M = Make(CCInt) in \
let m = M.of_list l in \ let m = M.of_list l in \
List.for_all (fun (k,_) -> let m' = M.remove k m in M.balanced m') l) 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 with Not_found -> None
(*$T (*$T
let module M = Make(CCInt) in \
let m = CCList.(0 -- 1000 |> map (fun i->i,i) |> M.of_list) 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) 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 & ~small:List.length
Q.(list (pair small_int small_int)) ( fun lst -> \ 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 lst = CCList.Set.uniq ~eq:(CCFun.compose_binop fst (=)) lst in \
let m = M.of_list lst in \ let m = M.of_list lst in \
List.for_all (fun (k,v) -> \ 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') (merge f l1 l2') (merge f r1 r2')
(*$R (*$R
let module M = Make(CCInt) in
let m1 = M.of_list [1, 1; 2, 2; 4, 4] 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 m2 = M.of_list [1, 1; 3, 3; 4, 4; 7, 7] in
let m = M.merge (fun k -> CCOpt.map2 (+)) m1 m2 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 & ~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) -> \ 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 eq x y = fst x = fst y in \
let l1 = CCList.Set.uniq ~eq l1 and l2 = CCList.Set.uniq ~eq l2 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 \ let m1 = M.of_list l1 and m2 = M.of_list l2 in \