]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/test.ml
Added problem failing because of order of step
[fireball-separation.git] / ocaml / test.ml
1 open Lambda4;;
2 open Util;;
3
4 let acaso l =
5     let n = Random.int (List.length l) in
6     List.nth l n
7 ;;
8
9 let acaso2 l1 l2 =
10   let n1 = List.length l1 in
11   let n = Random.int (n1 + List.length l2) in
12   if n >= n1 then List.nth l2 (n - n1) else List.nth l1 n
13 ;;
14
15 (** with bombs and pacmans too *)
16 let acaso3 l1 l2 =
17  if Random.int 10 = 0 then (
18   if Random.int 2 = 0 then "BOMB" else "PAC"
19  ) else
20   let n1 = List.length l1 in
21   let n = Random.int (n1 + List.length l2) in
22   if n >= n1 then List.nth l2 (n - n1) else List.nth l1 n
23 ;;
24
25 (* let rec take l n =
26   if n = 0 then []
27   else match l with
28   | [] -> []
29   | x::xs -> x :: (take xs (n-1))
30 ;; *)
31
32 let gen n vars =
33   let rec take' l n =
34     if n = 0 then [], []
35     else match l with
36     | [] -> [], []
37     | [_] -> assert false
38     | x1::x2::xs -> let a, b = take' xs (n-1) in x1::a,x2::b in
39   let rec aux n inerts lams =
40     if n = 0 then List.hd inerts, take' (Util.sort_uniq (List.tl inerts)) 5
41     else let inerts, lams = if Random.int 2 = 0
42       then inerts, ("(" ^ acaso vars ^ ". " ^ acaso2 inerts lams ^ ")") :: lams
43       else ("(" ^ acaso inerts ^ " " ^ acaso2 inerts lams^ ")") :: inerts, lams
44     in aux (n-1) inerts lams
45   in aux (2*n) vars []
46 ;;
47
48 let gen_pac n vars =
49  let rec take' l n =
50   if n = 0 then [], []
51   else match l with
52   | [] -> [], []
53   | [_] -> assert false
54   | x1::x2::xs -> let a, b = take' xs (n-1) in x1::a,x2::b in
55  let rec aux n inerts lams =
56   if n = 0 then List.hd inerts, take' (Util.sort_uniq (List.tl inerts)) 5
57   else let inerts, lams = if Random.int 2 = 0
58     then inerts, ("(" ^ acaso vars ^ ". " ^ acaso3 inerts lams ^ ")") :: lams
59     else ("(" ^ acaso inerts ^ " " ^ acaso3 inerts lams^ ")") :: inerts, lams
60   in aux (n-1) inerts lams
61  in aux (2*n) vars []
62 ;;
63
64
65 let rec repeat f n =
66   prerr_endline "\n########################### NEW TEST ###########################";
67   f () ;
68   if n > 0 then repeat f (n-1)
69 ;;
70
71 let solve div convs nums label =
72  let p = String.concat "\n" (
73   ("$! randomly generated test " ^ label) ::
74   ("D " ^ div) ::
75   List.map ((^) "C ") convs @
76   List.map (fun s -> "N " ^ s ^ " Z") nums
77  ) in
78  prerr_endline p;
79  match (Lambda4.solve ++ Lambda4.problem_of ++ Parser.problem_of_string) p with
80  | `Uncomplete, `Unseparable _ ->
81     failwith ("Test4.solve: unseparable, and cannot tell if that's right or not.")
82  | _ -> ()
83 ;;
84
85 let main =
86  Random.self_init ();
87  (* num = number of tests to run *)
88  let num = 100 in
89  (* if flag --with-pac active, then more general tests *)
90  let with_pac = List.mem "--with-pac" (Array.to_list Sys.argv) in
91
92  let label, f = if with_pac
93   then (
94    let complex = 10 in
95    let vars = ["x"; "y"; "z"; "v" ; "w"; "a"; "b"; "c"] in
96    "with pacmans & bombs", fun () -> gen_pac complex vars
97   ) else (
98    let complex = 200 in
99    let vars = ["x"; "y"; "z"; "v" ; "w"; "a"; "b"; "c"] in
100    "", fun () -> gen complex vars
101    ) in
102
103  repeat (fun _ ->
104    let div, (conv, nums) = f () in
105    solve div conv nums label
106  ) num;
107
108  prerr_endline "\n---- ALL TESTS COMPLETED ----"
109 ;;