From d29a21f4f559ae467a24d2580a33cc853e314696 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 27 Jan 2013 23:17:41 +0100 Subject: [PATCH] tests module --- tests.ml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests.ml diff --git a/tests.ml b/tests.ml new file mode 100644 index 0000000..11307b5 --- /dev/null +++ b/tests.ml @@ -0,0 +1,28 @@ + +(** {2 Test sequences} *) + +let seq_of_list l = Sequence.from_iter (fun k -> List.iter k l) + +let list_of_seq seq = List.rev (Sequence.fold (fun y x -> x::y) [] seq) + +let seq_of_hashtbl h = + Sequence.from_iter (fun k -> Hashtbl.iter (fun a b -> k (a, b)) h) + +let hashtbl_from_seq seq = + let h = Hashtbl.create 3 in + Sequence.iter (fun (k,v) -> Hashtbl.replace h k v) seq; + h + +(** print a list of items using the printing function *) +let rec pp_list ?(sep=", ") pp_item formatter = function + | x::y::xs -> Format.fprintf formatter "%a%s@,%a" + pp_item x sep (pp_list ~sep:sep pp_item) (y::xs) + | x::[] -> pp_item formatter x + | [] -> () + +let _ = + let l = [0;1;2;3;4;5;6] in + let l' = list_of_seq (Sequence.filter (fun x -> x mod 2 = 0) (seq_of_list l)) in + Format.printf "l=@[[%a]@]; l'=@[[%a]@]@." + (pp_list Format.pp_print_int) l + (pp_list Format.pp_print_int) l'