]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/regtest.ml
report files are now produced (and removed) during regression testing
[helm.git] / helm / gTopLevel / regtest.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 let argc = Array.length Sys.argv
29
30 let rawsep = "###"
31 let sep = Pcre.regexp (sprintf "^%s" rawsep)
32 let print s = print_string s
33 let print_endline s = print_endline s
34 let print_endline_to_channel ch s = output_string ch (s ^ "\n")
35
36 type state = Term | EMetasenv | ETerm | EType | EReduced
37
38 (* regtest file format
39  *    < cic term in concrete syntax >
40  *    separator (* see sep above *)
41  *    < expected metasenv after disambiguation (CicMetaSubst.ppmetasenv)  >
42  *    separator (* see sep above *)
43  *    < expected cic term after disambiguation (CicPp.ppterm) >
44  *    separator (* see sep above *)
45  *    < expected cic type as per type_of (CicPp.ppterm) >
46  *    separator (* see sep above *)
47  *    < expected reduced cic term as (CicPp.ppterm) >
48  *)
49
50 type regtest = {
51   term: string; (* raw cic term *)
52   emetasenv : string; (* expected metasenv *)
53   eterm: string;  (* expected term *)
54   etype: string;  (* expected type *)
55   ereduced: string; (* expected reduced term *)
56 }
57
58 let print_test test fname =
59   let oc = open_out fname in
60   output_string oc
61     (String.concat ""
62       [ test.term;
63         sprintf "%s (* METASENV after disambiguation  *)\n" rawsep;
64         test.emetasenv;
65         sprintf "%s (* TERM after disambiguation      *)\n" rawsep;
66         test.eterm;
67         sprintf "%s (* TYPE_OF the disambiguated term *)\n" rawsep;
68         test.etype;
69         sprintf "%s (* REDUCED disambiguated term     *)\n" rawsep;
70         test.ereduced ]);
71   close_out oc
72
73 let parse_regtest =
74   let term = Buffer.create 1024 in  (* raw term *)
75   let emetasenv = Buffer.create 1024 in  (* expected metasenv *)
76   let eterm = Buffer.create 1024 in (* raw expected term *)
77   let etype = Buffer.create 1024 in (* raw expected type *)
78   let ereduced = Buffer.create 1024 in (* raw expected reducted term *)
79   let state = ref Term in
80   let bump_state () =
81     match !state with
82     | Term -> state := EMetasenv
83     | EMetasenv -> state := ETerm
84     | ETerm -> state := EType
85     | EType -> state := EReduced
86     | EReduced -> assert false
87   in
88   let buffer_of_state = function
89     | Term ->  term | EMetasenv -> emetasenv | ETerm -> eterm | EType -> etype
90     | EReduced -> ereduced
91   in
92   let clear_buffers () =
93     List.iter Buffer.clear [ term; emetasenv; eterm; etype; ereduced ]
94   in
95   fun fname ->
96     state := Term;
97     clear_buffers ();
98     let ic = open_in fname in
99     (try
100       while true do
101         let line = input_line ic in
102         match line with
103         | l when Pcre.pmatch ~rex:sep l -> bump_state ()
104         | l -> Buffer.add_string (buffer_of_state !state) (line ^ "\n")
105       done
106     with End_of_file -> ());
107     { term = Buffer.contents term;
108       emetasenv = Buffer.contents emetasenv;
109       eterm = Buffer.contents eterm;
110       etype = Buffer.contents etype;
111       ereduced = Buffer.contents ereduced }
112
113 let as_expected report_fname expected found = (* ignores "term" field *)
114   let eterm_ok = expected.eterm = found.eterm in
115   let emetasenv_ok = expected.emetasenv = found.emetasenv in
116   let etype_ok = expected.etype = found.etype in
117   let ereduced_ok = expected.ereduced = found.ereduced in
118   let outcome = eterm_ok && emetasenv_ok && etype_ok && ereduced_ok in
119   if outcome then
120    (if Sys.file_exists report_fname then Sys.remove report_fname)
121   else
122    begin
123     let och = open_out report_fname in
124     let print_endline = print_endline_to_channel och in
125     if not eterm_ok then begin
126       print_endline "### Term mismatch ###";
127       print_endline "# expected:";
128       print_endline ("  " ^ expected.eterm);
129       print_endline "# found:";
130       print_endline ("  " ^ found.eterm);
131     end;
132     if not emetasenv_ok then begin
133       print_endline "### Metasenv mismatch ###";
134       print_endline "# expected:";
135       print_endline ("  " ^ expected.emetasenv);
136       print_endline "# found:";
137       print_endline ("  " ^ found.emetasenv);
138     end;
139     if not etype_ok then begin
140       print_endline "### Type mismatch ###";
141       print_endline "# expected:";
142       print_endline ("  " ^ expected.etype);
143       print_endline "# found:";
144       print_endline ("  " ^ found.etype);
145     end;
146     if expected.ereduced <> found.ereduced then begin
147       print_endline "### Reduced term mismatch ###";
148       print_endline "# expected:";
149       print_endline ("  " ^ expected.ereduced);
150       print_endline "# found:";
151       print_endline ("  " ^ found.ereduced);
152     end;
153     close_out och ;
154    end;
155   outcome
156
157 let test_this raw_term =
158   let empty_context = [] in
159   let (metasenv, cic_term) = BatchParser.parse raw_term in
160   let etype =
161    try
162     CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv empty_context cic_term)
163    with
164     _ -> "MALFORMED"
165   in
166   let ereduced =
167    try
168     CicPp.ppterm (CicReduction.whd empty_context cic_term)
169    with
170     _ -> "MALFORMED"
171   in
172   {
173     term = raw_term;  (* useless *)
174     emetasenv = CicMetaSubst.ppmetasenv metasenv [] ^ "\n";
175     eterm = CicPp.ppterm cic_term ^ "\n";
176     etype = etype ^ "\n";
177     ereduced = ereduced ^ "\n";
178   }
179
180 let dump_environment filename =
181   try
182     let oc = open_out filename in
183     CicEnvironment.dump_to_channel oc;
184     close_out oc
185   with exc ->
186     prerr_endline
187      ("DUMP_ENVIRONMENT FAILURE, uncaught excecption " ^
188        Printexc.to_string exc) ;
189     raise exc
190 ;;
191
192 let restore_environment filename =
193   if Sys.file_exists filename then
194    begin
195     try
196       let ic = open_in filename in
197       CicEnvironment.restore_from_channel ic;
198       close_in ic
199     with exc ->
200       prerr_endline
201        ("RESTORE_ENVIRONMENT FAILURE, uncaught excecption " ^
202          Printexc.to_string exc) ;
203       raise exc
204    end
205   else
206    CicEnvironment.empty ()
207 ;;
208
209 let main generate dump fnames =
210  if generate then
211   begin
212    (* gen mode *)
213    print_endline "[ Gen mode ]";
214    List.iter
215     (function fname ->
216       let test_fname = fname ^ ".test" in
217       let env_fname = fname ^ ".env" in
218       print_endline (sprintf "Generating regtest %s -> %s\n ..."
219         fname test_fname);
220       let raw_term = (parse_regtest fname).term in
221       let result = test_this raw_term in
222       print_test result test_fname ;
223       if dump then dump_environment env_fname ;
224     ) fnames
225   end else
226    begin
227     (* regtest mode *)
228     print_endline "[ Regtest mode ]";
229     let (ok, nok) = (ref 0, ref []) in
230     List.iter
231      (function fname ->
232        let env_fname = fname ^ ".env" in
233        let test_fname = fname ^ ".test" in
234        let report_fname = fname ^ ".report" in
235        restore_environment env_fname ;
236        let time = Unix.gettimeofday () in
237        print ("Processing " ^ fname ^":\t") ;
238        let is_ok = 
239         try
240           let expected = parse_regtest test_fname in
241           let actual = test_this expected.term in
242           if dump then dump_environment env_fname ;
243           if as_expected report_fname expected actual then
244            (incr ok ; true)
245           else
246            (nok := fname :: !nok ; false)
247         with e -> (nok := fname :: !nok ; false)
248        in
249         let timediff = Unix.gettimeofday () -. time in
250          print (sprintf "done in %f seconds\t" timediff) ;
251          print_endline (if is_ok then "\e[01;32m[   OK   ]\e[00m" else "\e[01;31m[ FAILED ]\e[00m")
252      ) fnames ;
253     print_endline "*** Summary ***";
254     print_endline (sprintf "Succeeded: %d" !ok);
255     print_endline (sprintf "Failed: %d" (List.length !nok));
256     List.iter (fun fname -> print_endline (sprintf "  %s failed :-(" fname))
257       (List.rev !nok)
258   end
259 ;;
260
261 let _ =
262  let fnames = ref [] in
263  let gen = ref false in
264  let dump = ref false in
265  let nodump = ref false in
266  let usage = "regtest [OPTION] ... test1 ..." in
267  let spec =
268    ["-gen",    Arg.Set gen, "generate the tests; implies -dump (unless -nodump is specified)" ;
269     "--gen",    Arg.Set gen, "generate the tests; implies -dump (unless -nodump is specified)" ;
270     "-dump",   Arg.Set dump, "dumps the final environment" ;
271     "--dump",   Arg.Set dump, "dumps the final environment" ;
272     "-nodump", Arg.Set dump, "do not dump the final environment" ;
273     "--nodump", Arg.Set dump, "do not dump the final environment" ]
274  in
275   Arg.parse spec (function filename -> fnames := filename::!fnames ) usage ;
276   if !fnames = [] then
277    Arg.usage spec (Sys.argv.(0) ^ ": missing argument test. You must provide at least one test file.\n" ^ usage) ;
278   main !gen ((!gen || !dump) && (not !nodump))  !fnames
279 ;;