]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/simple_test.ml
ed2c81c8e2325946ca5777b1ed1a151743f7f03a
[fireball-separation.git] / ocaml / simple_test.ml
1 open Simple;;
2 open Util;;
3
4 let with_const =
5  List.mem "--with-const" (Array.to_list Sys.argv)
6  ;;
7
8 let acaso l =
9     let n = Random.int (List.length l) in
10     List.nth l n
11 ;;
12
13 let acaso2 l1 l2 =
14   let n1 = List.length l1 in
15   let n = Random.int (n1 + List.length l2) in
16   if n >= n1 then List.nth l2 (n - n1) else List.nth l1 n
17 ;;
18
19 let gen n vars =
20   let rec take' l n =
21     if n = 0 then []
22     else match l with
23     | [] -> []
24     | [_] -> assert false
25     | x::_::xs -> x :: take' xs (n-1) in
26   let rec aux n inerts lams =
27     if n = 0 then List.hd inerts, take' (Util.sort_uniq (List.tl inerts)) 5
28     else let inerts, lams = if Random.int 2 = 0
29       then inerts, ("(" ^ acaso vars ^ ". " ^ acaso2 inerts lams ^ ")") :: lams
30       else ("(" ^ acaso inerts ^ " " ^ acaso2 inerts lams^ ")") :: inerts, lams
31     in aux (n-1) inerts lams in
32   let inerts = if with_const then "C" :: vars else vars in
33   aux (2*n) inerts []
34 ;;
35
36
37 let rec repeat f n =
38   print_endline "\n########################### NEW TEST ###########################";
39   f n ;
40   if n > 0 then repeat f (n-1)
41 ;;
42
43 let main =
44   Random.self_init ();
45   let num = 1000 in
46   (*let complex = 100 in
47   let no_bound_vars = 10 in*)
48   let file = Filename.temp_file ~temp_dir:"./problems/" "simple.constants.auto." "" in
49   let oc = open_out file in
50   print_endline ("\n\n---- <TESTS> " ^ file) ;
51   repeat (fun x ->
52     let complex = 100 + x / 10 in
53     let no_bound_vars = Random.int 20 + 1 in
54     let vars = Array.to_list
55      (Array.init no_bound_vars (fun x -> "x" ^ string_of_int x)) in
56     let div, convs = gen complex vars in
57     let str = " \nD " ^ div ^ String.concat "\nC " convs ^ "\n" in
58     print_endline str;
59     try
60      (solve ++ problem_of ++ Parser.problem_of_string) ("$" ^ str)
61     with Simple.Fail _ as e ->
62      let str = "$ failing test problem \n# Failed because: " ^ Printexc.to_string e ^ str in
63      Printf.fprintf oc "%s\n" str
64   ) num ;
65   close_out oc;
66   print_endline ("\n\n---- </TESTS> " ^ file);
67   let filesize filename =
68     let ic = open_in filename in
69     let size = in_channel_length ic in
70     close_in ic ; size in
71   if filesize file = 0
72    then (print_endline "---- All tests succeeded."; Sys.remove file )
73    else (print_endline ("---- Not all tests succeeded. See " ^ file))
74 ;;