[Toss-devel-svn] SF.net SVN: toss:[1189] trunk/Toss/Formula/AuxTest.ml
Status: Beta
Brought to you by:
lukaszkaiser
From: <luk...@us...> - 2010-11-21 21:44:29
|
Revision: 1189 http://toss.svn.sourceforge.net/toss/?rev=1189&view=rev Author: lukstafi Date: 2010-11-21 21:44:23 +0000 (Sun, 21 Nov 2010) Log Message: ----------- AuxTest test suite: missing file. Added Paths: ----------- trunk/Toss/Formula/AuxTest.ml Added: trunk/Toss/Formula/AuxTest.ml =================================================================== --- trunk/Toss/Formula/AuxTest.ml (rev 0) +++ trunk/Toss/Formula/AuxTest.ml 2010-11-21 21:44:23 UTC (rev 1189) @@ -0,0 +1,401 @@ +(* Unit tests for auxiliary functions. *) +open OUnit + +let print_alist f l = + String.concat ", " (List.map (fun (k,v) -> k^": "^f v) l) + +let tests = "Aux" >::: [ + "concat_map, map_some" >:: + (fun () -> + let f = function `A -> ["a";"b"] | `B -> ["c"] | `C -> [] + | `D -> ["d";"e"] in + assert_equal ~printer:(String.concat "; ") + ["a";"b";"c";"d";"e"] + (Aux.concat_map f [`A;`B;`C;`D]); + + let f = function `A -> Some "a" | `B -> Some "b" | `C -> None + | `D -> Some "d" in + assert_equal ~printer:(String.concat "; ") + ["a";"b";"d"] + (Aux.map_some f [`A;`B;`C;`D]); + ); + + "map_reduce" >:: + (fun () -> + let mapf = function `A -> "1", ["a";"b"] | `B -> "2", ["c"] + | `C -> "1", [] | `D -> "2", ["d";"e"] in + assert_equal ~msg:"simple test" + ~printer:(print_alist (String.concat ", ")) + ["1", ["a";"b"]; "2", ["d";"e"; "c"]] + (Aux.map_reduce mapf (@) [] [`A;`B;`C;`D]); + + assert_equal ~msg:"histogram" ~printer:(print_alist string_of_int) + ["abra",3; "bra", 1; "cada",2] + (Aux.map_reduce (fun k->k,1) (+) 0 + ["abra"; "cada"; "abra"; "bra"; "cada"; "abra"]); + ); + + "rev_assoc, rev_assoc_all" >:: + (fun () -> + assert_equal ~printer:(fun x -> x) + "G" + (Aux.rev_assoc + ["B","f";"C","B"; "G","T"; "F", "T"] + "T"); + + assert_raises ~msg:"should not find" + Not_found + (fun () -> Aux.rev_assoc + ["B","f";"C","B"; "G","Ts"] + "T"); + + assert_equal ~printer:(String.concat "; ") + ["F"; "G"] + (Aux.rev_assoc_all + ["B","f";"C","B"; "G","T"; "F", "T"] + "T"); + + assert_equal ~printer:(String.concat "; ") + [] + (Aux.rev_assoc_all + ["B","f";"C","B"; "G","Ts"] + "T"); + ); + + + "replace_assoc, pop_assoc" >:: + (fun () -> + assert_equal ~printer:(print_alist (fun x -> x)) + ["B","f";"C","B"; "G","replaced"; "G", "T"] + (Aux.replace_assoc "G" "replaced" + ["B","f";"C","B"; "G","T"; "G", "T"]); + + assert_equal ~printer:(print_alist (fun x -> x)) + ["B","f";"C","B"; "F", "T"; "G","replaced"] + (Aux.replace_assoc "G" "replaced" + ["B","f";"C","B"; "F", "T"]); + + assert_equal + ~printer:(fun (x,y) -> x^" -- "^print_alist (fun e->e) y) + ("T", ["B","f";"C","B"; "G", "T2"]) + (Aux.pop_assoc "G" + ["B","f";"C","B"; "G","T"; "G", "T2"]); + + assert_raises ~msg:"should not find" + Not_found + (fun () -> Aux.pop_assoc "G" + ["B","f";"C","B"; "F","Ts"]); + ); + + "unsome, map_try" >:: + (fun () -> + assert_equal ~printer:(fun x->x) + "x" (Aux.unsome (Some "x")); + + assert_raises ~msg:"None" + (Invalid_argument "unsome") + (fun () -> Aux.unsome None); + + let f = function `A -> "a" | `B -> "b" | `C -> raise Not_found + | `D -> "d" in + assert_equal ~printer:(String.concat "; ") + ["a";"b";"d"] + (Aux.map_try f [`A;`B;`C;`D]); + ); + + "product, all_tuples_for" >:: + (fun () -> + let print_llist l = String.concat "; " + (List.map (String.concat ", ") l) in + + assert_equal ~printer:print_llist + [["a"; "c"; "d"]; ["a"; "c"; "e"]; ["b"; "c"; "d"]; ["b"; "c"; "e"]] + (Aux.product [["a";"b"]; ["c"]; ["d";"e"]]); + + assert_equal ~printer:print_llist + [] (Aux.product [["a";"b"]; []; ["c"]; ["d";"e"]]); + + assert_equal ~printer:print_llist + [["a"; "a"]; ["a"; "b"]; ["a"; "c"]; ["b"; "a"]; ["b"; "b"]; ["b"; "c"]; ["c"; "a"]; ["c"; "b"]; ["c"; "c"]] + (Aux.all_tuples_for [();()] ["a";"b";"c"]); + + assert_equal ~printer:print_llist + [["a"; "a"; "a"]; ["a"; "a"; "b"]; ["a"; "b"; "a"]; ["a"; "b"; "b"]; ["b"; "a"; "a"]; ["b"; "a"; "b"]; ["b"; "b"; "a"]; ["b"; "b"; "b"]] + (Aux.all_tuples_for [();(); ()] ["a";"b"]); + ); + + "list_remove, remove_one" >:: + (fun () -> + assert_equal ~printer:(String.concat "; ") + ["a";"b";"d"] + (Aux.list_remove "c" ["a";"c";"b"; "d"; "c"]); + + assert_equal ~printer:(String.concat "; ") + ["a";"b";"d"; "c"] + (Aux.remove_one "c" ["a";"c";"b"; "d"; "c"]); + ); + + "insert_nth, find_index" >:: + (fun () -> + assert_equal ~printer:(String.concat "; ") + ["a";"c";"e"; "b"; "d"; "c"] + (Aux.insert_nth 2 "e" ["a";"c"; "b"; "d"; "c"]); + + assert_equal ~printer:(String.concat "; ") + ["a";"c"; "b";"e"] + (Aux.insert_nth 3 "e" ["a";"c"; "b"]); + + assert_raises ~msg:"beyond end of list" + Not_found + (fun () -> Aux.insert_nth 3 "e" ["a"; "b"]); + + assert_equal ~printer:string_of_int + 2 + (Aux.find_index "e" ["a";"c"; "e"; "b"; "d"; "c"]); + + assert_equal ~printer:string_of_int + 3 + (Aux.find_index "e" ["a";"c"; "b"; "e"]); + + assert_raises ~msg:"not in list" + Not_found + (fun () -> Aux.find_index "e" ["a"; "b"]); + ); + + "maximal" >:: + (fun () -> + assert_equal ~printer:(String.concat "; ") ~msg:"orto by first char" + ["b1";"a3";"c7"] + (Aux.maximal (fun a b -> a.[0] = b.[0] && a.[1] <= b.[1]) + ["a1";"c2"; "b1"; "a3"; "c7"]); + + assert_equal ~printer:(String.concat "; ") ~msg:"incomparable" + ["a1";"c2"; "b1"; "a3"; "c7"] + (Aux.maximal (fun _ _ -> false) + ["a1";"c2"; "b1"; "a3"; "c7"]); + + assert_equal ~printer:(String.concat "; ") ~msg:"linear" + ["c7"] + (Aux.maximal (<=) + ["a1";"c2"; "b1"; "a3"; "c7"]); + ); + + "unique, take_n" >:: + (fun () -> + assert_equal ~printer:(String.concat "; ") + ["a";"c";"b";"d"] + (Aux.unique (=) ["a";"c";"b"; "d"; "c"]); + + assert_equal ~printer:(String.concat "; ") + ["a";"c";"b"] + (Aux.take_n 3 ["a";"c";"b"; "d"; "c"]); + + assert_equal ~printer:(String.concat "; ") + ["a";"c";"b"] + (Aux.take_n 5 ["a";"c";"b"]); + ); + + "array_map2, array_combine" >:: + (fun () -> + assert_equal ~printer:(print_alist (fun x->x)) + ["a","e";"c","d";"b","c"] + (Array.to_list + (Aux.array_map2 (fun x y->x,y) [|"a";"c";"b"|] [|"e"; "d"; "c"|])); + + assert_raises ~msg:"not equal lengths" + (Invalid_argument "Aux.array_map2") + (fun ()-> + Aux.array_map2 (fun x y->x,y) [|"a";"c";"b"|] [|"e"; "d"|]); + + assert_equal ~printer:(print_alist (fun x->x)) + ["a","e";"c","d";"b","c"] + (Array.to_list + (Aux.array_combine [|"a";"c";"b"|] [|"e"; "d"; "c"|])); + + assert_raises ~msg:"not equal lengths" + (Invalid_argument "Aux.array_map2") + (fun ()-> + Aux.array_combine [|"a";"c";"b"|] [|"e"; "d"|]); + ); + + "array_existsi, array_mem" >:: + (fun () -> + assert_bool "exists 4th" + (Aux.array_existsi (fun i _ -> i = 4) + [|"a1";"c2"; "b1"; "a3"; "c7"|]); + + assert_bool "exists same 4th" + (Aux.array_existsi (fun i x -> i = 4 && x = "c7") + [|"a1";"c2"; "b1"; "a3"; "c7"|]); + + assert_bool "mem c7" + (Aux.array_mem "c7" + [|"a1";"c2"; "b1"; "a3"; "c7"|]); + + ); + + "array_from_assoc, array_map_of_list" >:: + (fun () -> + assert_equal ~printer:(String.concat "; ") + ["a"; "c"; "b"; "e"; "d"; "c"] + (Array.to_list + (Aux.array_from_assoc + [0,"a";1,"c"; 4,"d"; 5,"c";2,"b";3,"e"])); + + assert_equal ~printer:(String.concat "; ") + ["2"; "3"; "4"; "5"] + (Array.to_list + (Aux.array_map_of_list (fun i->string_of_int (i+1)) + [1;2;3;4])); + ); + + + "array_argfind, array_find_all, array_argfind_all" >:: + (fun () -> + assert_equal ~printer:string_of_int + 2 + (Aux.array_argfind (fun e->e="e") + [|"a";"c"; "e"; "b"; "d"; "c"|]); + + assert_equal ~printer:string_of_int + 3 + (Aux.array_argfind (fun e->e="e") + [|"a";"c"; "b"; "e"|]); + + assert_raises ~msg:"not in array" + Not_found + (fun () -> Aux.array_argfind (fun e->e="e") + [|"a"; "b"|]); + + assert_equal ~printer:(String.concat "; ") + ["e"; "e2"; "e3"] + (Aux.array_find_all (fun e->e.[0]='e') + [|"a";"c"; "e"; "b"; "e2"; "d"; "e3"; "c"|]); + + assert_equal ~printer:(String.concat "; ") + ["e0"] + (Aux.array_find_all (fun e->e.[0]='e') + [|"a";"c"; "b"; "e0"|]); + + assert_equal ~printer:(String.concat "; ") + [] + (Aux.array_find_all (fun e->e.[0]='e') + [|"a";"c"; "b"|]); + + assert_equal + ~printer:(fun l->String.concat "; " (List.map string_of_int l)) + [2;4;6] + (Aux.array_argfind_all (fun e->e.[0]='e') + [|"a";"c"; "e"; "b"; "e2"; "d"; "e3"; "c"|]); + + assert_equal + ~printer:(fun l->String.concat "; " (List.map string_of_int l)) + [3] + (Aux.array_argfind_all (fun e->e.[0]='e') + [|"a";"c"; "b"; "e0"|]); + + assert_equal + ~printer:(fun l->String.concat "; " (List.map string_of_int l)) + [] + (Aux.array_argfind_all (fun e->e.[0]='e') + [|"a";"c"; "b"|]); + ); + + "array_for_all, array_for_all2" >:: + (fun () -> + assert_bool "all c" + (Aux.array_for_all (fun x->x.[0]='c') + [|"c1";"c2"; "c1"; "c3"; "c7"|]); + + assert_bool "not all c" + (not (Aux.array_for_all (fun x->x.[0]='c') + [|"a1";"c2"; "b1"; "a3"; "c7"|])); + + assert_bool "all equal" + (Aux.array_for_all2 (fun x y->x=y) + [|"a";"c";"b"|] [|"a"; "c"; "b"|]); + + assert_bool "not all equal" + (not (Aux.array_for_all2 (fun x y->x=y) + [|"a";"c";"b"|] [|"e"; "d"; "c"|])); + + assert_raises ~msg:"not equal lengths" + (Invalid_argument "Aux.array_for_all2") + (fun ()-> + Aux.array_for_all2 (fun x y->x=y) + [|"a";"c";"b"|] [|"e"; "d"|]); + ); + + "partition_choice, partition_map" >:: + (fun () -> + assert_equal ~printer:(fun (x,y)-> + String.concat ", " x^"; "^String.concat ", " y) + (["b";"c";"e"],["a";"d"]) + (Aux.partition_choice + [Aux.Right "a";Aux.Left "b";Aux.Left "c"; + Aux.Right "d";Aux.Left "e"]); + + let f = function `A -> Aux.Right "a" | `B -> Aux.Left "b" + | `C -> Aux.Left "c" + | `D -> Aux.Right "d" | `E -> Aux.Left "e" in + assert_equal ~printer:(fun (x,y)-> + String.concat ", " x^"; "^String.concat ", " y) + (["b";"c";"e"],["a";"d"]) + (Aux.partition_map f [`A;`B;`C;`D; `E]); + ); + + "transpose_lists" >:: + (fun () -> + let print_llist l = String.concat "; " + (List.map (String.concat ", ") l) in + + assert_equal ~printer:print_llist + [["a"; "d"; "f"]; ["b"; "e"; "g"]] + (Aux.transpose_lists [["a";"b"]; ["d";"e"]; ["f";"g"]]); + + assert_equal ~printer:print_llist + [["a"; "d"]; ["b"; "e"]; ["c"; "f"]] + (Aux.transpose_lists [["a";"b"; "c"]; ["d";"e"; "f"]]); + + assert_raises ~msg:"not rectangular" + (Invalid_argument "List.map2") + (fun ()-> + Aux.transpose_lists [["a";"b"; "c"]; ["d";"e"]]); + ); + + + "not_conflicting_name, not_conflicting_names" >:: + (fun () -> + assert_equal ~printer:(fun x->x) + "x1" + (Aux.not_conflicting_name + (Aux.strings_of_list ["x"; "x0"]) "x"); + + assert_equal ~printer:(String.concat "; ") + ["x1"; "x2"; "x3"] + (Aux.not_conflicting_names "x" + (Aux.strings_of_list ["x"; "x0"]) [();();()]); + + assert_equal ~printer:(fun x->x) + "y" + (Aux.not_conflicting_name + (Aux.strings_of_list ["x"; "x0"]) "y"); + + assert_equal ~printer:(String.concat "; ") + ["y0"; "y1"; "y2"] + (Aux.not_conflicting_names "y" + (Aux.strings_of_list ["x"; "x0"]) [();();()]); + ); + +] + +let a = + let file_from_path p = + String.sub p (String.rindex p '/'+1) + (String.length p - String.rindex p '/' - 1) in + let test_fname name = + let fname = file_from_path Sys.executable_name in + String.length fname >= String.length name && + String.sub fname 0 (String.length name) = name in + if test_fname "AuxTest" then + ignore (run_test_tt ~verbose:true tests) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |