]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/regtest.ml
The regression tests now check also the generated disambiguation environments.
[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 sep2 = Pcre.regexp (sprintf "^%s%s" rawsep rawsep)
33 let print s = print_string s; flush stdout
34 let print_endline s = print_endline s
35 let print_endline_to_channel ch s = output_string ch (s ^ "\n")
36
37 type state = Term | EEnv | EMetasenv | ETerm | EType | EReduced
38
39 (* regtest file format
40  *    < cic term in concrete syntax >
41  *    separatorseparator INTERPRETATION NUMBER <n> separatorseparator
42  *    separator (* see sep above *)
43  *    < expected disambiguating environment (EnvironmentP3.to_string)  >
44  *    separator (* see sep above *)
45  *    < expected metasenv after disambiguation (CicMetaSubst.ppmetasenv)  >
46  *    separator (* see sep above *)
47  *    < expected cic term after disambiguation (CicPp.ppterm) >
48  *    separator (* see sep above *)
49  *    < expected cic type as per type_of (CicPp.ppterm) >
50  *    separator (* see sep above *)
51  *    < expected reduced cic term as (CicPp.ppterm) >
52  *    ... (* repeat from ... INTERPRETATION NUMBER ... *)
53  *)
54
55 type regtest = {
56   term: string; (* raw cic term *)
57   eenv : string; (* disambiguating parsing environment *)
58   emetasenv : string; (* expected metasenv *)
59   eterm: string;  (* expected term *)
60   etype: string;  (* expected type *)
61   ereduced: string; (* expected reduced term *)
62 }
63
64 let print_test tests fname =
65   let oc = open_out fname in
66   output_string oc (List.hd tests).term;
67   let i = ref 0 in
68   List.iter
69    (function test ->
70      incr i ;
71      output_string oc
72        (String.concat ""
73          [ sprintf "%s%s INTERPRETATION NUMBER %d %s%s\n" rawsep rawsep !i rawsep rawsep ;
74            sprintf "%s (* disambiguation environment  *)\n" rawsep;
75            test.eenv;
76            sprintf "%s (* METASENV after disambiguation  *)\n" rawsep;
77            test.emetasenv;
78            sprintf "%s (* TERM after disambiguation      *)\n" rawsep;
79            test.eterm;
80            sprintf "%s (* TYPE_OF the disambiguated term *)\n" rawsep;
81            test.etype;
82            sprintf "%s (* REDUCED disambiguated term     *)\n" rawsep;
83            test.ereduced ])
84    ) tests;
85   close_out oc
86
87 let parse_regtest =
88   let term = Buffer.create 1024 in  (* raw term *)
89   let eenv = Buffer.create 1024 in  (* disambiguating parser environment *)
90   let emetasenv = Buffer.create 1024 in  (* expected metasenv *)
91   let eterm = Buffer.create 1024 in (* raw expected term *)
92   let etype = Buffer.create 1024 in (* raw expected type *)
93   let ereduced = Buffer.create 1024 in (* raw expected reducted term *)
94   let state = ref Term in
95   let bump_state () =
96     match !state with
97     | Term -> state := EEnv
98     | EEnv -> state := EMetasenv
99     | EMetasenv -> state := ETerm
100     | ETerm -> state := EType
101     | EType -> state := EReduced
102     | EReduced -> assert false
103   in
104   let buffer_of_state = function
105     | Term ->  term | EEnv -> eenv | EMetasenv -> emetasenv | ETerm -> eterm | EType -> etype
106     | EReduced -> ereduced
107   in
108   let clear_buffers () =
109     List.iter Buffer.clear [ eenv; emetasenv; eterm; etype; ereduced ]
110   in
111   fun fname ->
112     state := Term;
113     let first = ref true in
114     let res = ref [] in
115     let push_res () =
116      res :=
117       {term = Buffer.contents term;
118        eenv = Buffer.contents eenv;
119        emetasenv = Buffer.contents emetasenv;
120        eterm = Buffer.contents eterm;
121        etype = Buffer.contents etype;
122        ereduced = Buffer.contents ereduced } :: !res ;
123     in
124     Buffer.clear term;
125     let ic = open_in fname in
126     (try
127       while true do
128         let line = input_line ic in
129         match line with
130         | l when Pcre.pmatch ~rex:sep2 l ->
131             if !first then first := false else push_res () ;
132             clear_buffers ();
133             state := Term
134         | l when Pcre.pmatch ~rex:sep l -> bump_state ()
135         | l -> Buffer.add_string (buffer_of_state !state) (line ^ "\n")
136       done
137     with End_of_file -> ());
138     push_res () ;
139     List.rev !res
140
141 let as_expected_one och expected found = (* ignores "term" field *)
142   let eterm_ok = expected.eterm = found.eterm in
143   let eenv_ok = expected.eenv = found.eenv in
144   let emetasenv_ok = expected.emetasenv = found.emetasenv in
145   let etype_ok = expected.etype = found.etype in
146   let ereduced_ok = expected.ereduced = found.ereduced in
147   let outcome =
148    eterm_ok && eenv_ok && emetasenv_ok && etype_ok && ereduced_ok
149   in
150    begin
151     let print_endline s = print_endline_to_channel (Lazy.force och) s in
152     if not eterm_ok then begin
153       print_endline "### Term mismatch ###";
154       print_endline "# expected:";
155       print_endline ("  " ^ expected.eterm);
156       print_endline "# found:";
157       print_endline ("  " ^ found.eterm);
158     end;
159     if not eenv_ok then begin
160       print_endline "### Disambiguation environment mismatch ###";
161       print_endline "# expected:";
162       print_endline ("  " ^ expected.eenv);
163       print_endline "# found:";
164       print_endline ("  " ^ found.eenv);
165     end;
166     if not emetasenv_ok then begin
167       print_endline "### Metasenv mismatch ###";
168       print_endline "# expected:";
169       print_endline ("  " ^ expected.emetasenv);
170       print_endline "# found:";
171       print_endline ("  " ^ found.emetasenv);
172     end;
173     if not etype_ok then begin
174       print_endline "### Type mismatch ###";
175       print_endline "# expected:";
176       print_endline ("  " ^ expected.etype);
177       print_endline "# found:";
178       print_endline ("  " ^ found.etype);
179     end;
180     if expected.ereduced <> found.ereduced then begin
181       print_endline "### Reduced term mismatch ###";
182       print_endline "# expected:";
183       print_endline ("  " ^ expected.ereduced);
184       print_endline "# found:";
185       print_endline ("  " ^ found.ereduced);
186     end;
187    end;
188   outcome
189
190 let as_expected report_fname expected found =
191  (if Sys.file_exists report_fname then Sys.remove report_fname) ;
192  let och = lazy (open_out report_fname) in
193  let print_endline s = print_endline_to_channel (Lazy.force och) s in
194   let rec aux =
195    function
196       [],[] -> true
197     | ex::extl, fo::fotl ->
198        as_expected_one och ex fo &&
199        aux (extl,fotl)
200     | [],found ->
201        print_endline "### Too many interpretations found" ;
202        false
203     | expected,[] ->
204        print_endline "### Too few interpretations found" ;
205        false
206   in
207    let outcome = aux (expected,found) in
208     (if Lazy.lazy_is_val och then close_out (Lazy.force och)) ;
209     outcome
210
211 let test_this mqi_handle uri_pred raw_term =
212   let empty_context = [] in
213   List.map
214    (function (env, metasenv, cic_term) ->
215      let etype =
216       try
217        CicPp.ppterm
218         (CicTypeChecker.type_of_aux' metasenv empty_context cic_term)
219       with _ -> "MALFORMED"
220      in
221      let ereduced =
222       try
223        CicPp.ppterm (CicReduction.whd empty_context cic_term)
224       with _ -> "MALFORMED"
225      in
226      { term = raw_term;  (* useless *)
227        eenv = DisambiguatingParser.EnvironmentP3.to_string env ^ "\n";
228        emetasenv = CicMetaSubst.ppmetasenv metasenv [] ^ "\n";
229        eterm = CicPp.ppterm cic_term ^ "\n";
230        etype = etype ^ "\n";
231        ereduced = ereduced ^ "\n";
232      }
233    ) (BatchParser.parse mqi_handle ~uri_pred raw_term)
234
235 let dump_environment filename =
236   try
237     let oc = open_out filename in
238     CicEnvironment.dump_to_channel oc;
239     close_out oc
240   with exc ->
241     prerr_endline
242      ("DUMP_ENVIRONMENT FAILURE, uncaught excecption " ^
243        Printexc.to_string exc) ;
244     raise exc
245
246 let restore_environment filename =
247   if Sys.file_exists filename then
248    begin
249     try
250       let ic = open_in filename in
251       CicEnvironment.restore_from_channel ic;
252       close_in ic
253     with exc ->
254       prerr_endline
255        ("RESTORE_ENVIRONMENT FAILURE, uncaught excecption " ^
256          Printexc.to_string exc) ;
257       raise exc
258    end
259   else
260    CicEnvironment.empty ()
261
262 let main mqi_handle generate  dump fnames tryvars varsprefix =
263  let uri_pred = BatchParser.uri_pred_of_conf tryvars varsprefix in
264  if generate then
265   begin
266    (* gen mode *)
267    print_endline "[ Gen mode ]";
268    List.iter
269     (function fname ->
270       let test_fname = fname ^ ".test" in
271       let env_fname = fname ^ ".env" in
272       print_endline (sprintf "Generating regtest %s -> %s\n ..."
273         fname test_fname);
274       let raw_term = (List.hd (parse_regtest fname)).term in
275       let results = test_this mqi_handle uri_pred raw_term in
276       print_test results test_fname ;
277       if dump then dump_environment env_fname ;
278     ) fnames
279   end else
280    begin
281     (* regtest mode *)
282     print_endline "[ Regtest mode ]";
283     let (ok, nok) = (ref 0, ref []) in
284     List.iter
285      (function fname ->
286        let env_fname = fname ^ ".env" in
287        let test_fname = fname ^ ".test" in
288        let report_fname = fname ^ ".report" in
289        restore_environment env_fname ;
290        let time = Unix.gettimeofday () in
291        print ("Processing " ^ fname ^":\t") ;
292        let is_ok = 
293         try
294           let expected = parse_regtest test_fname in
295           let actual = test_this mqi_handle uri_pred (List.hd expected).term in
296           if dump then dump_environment env_fname ;
297           if as_expected report_fname expected actual then
298            (incr ok ; true)
299           else
300            (nok := fname :: !nok ; false)
301         with e -> (nok := fname :: !nok ; false)
302        in
303         let timediff = Unix.gettimeofday () -. time in
304          print (sprintf "done in %f seconds\t" timediff) ;
305          print_endline
306           (if is_ok then
307             "\e[01;32m[   OK   ]\e[00m"
308           else
309             "\e[01;31m[ FAILED ]\e[00m")
310      ) fnames ;
311     print_endline "*** Summary ***";
312     print_endline (sprintf "Succeeded: %d" !ok);
313     print_endline (sprintf "Failed: %d" (List.length !nok));
314     List.iter (fun fname -> print_endline (sprintf "  %s failed :-(" fname))
315       (List.rev !nok)
316   end
317
318 let _ =
319
320  Helm_registry.load_from "gTopLevel.conf.xml";
321  HelmLogger.register_log_callback
322   (fun ?(append_NL = true) msg ->
323     (if append_NL then prerr_endline else prerr_string)
324       (HelmLogger.string_of_html_msg msg));
325  
326  let mqi_debug_fun = ignore in
327  let mqi_handle = MQIConn.init ~log:mqi_debug_fun () in
328  
329  let fnames = ref [] in
330  let gen = ref false in
331  let tryvars = ref false in
332  let dump = ref false in
333  let nodump = ref false in
334  let varsprefix = ref "" in
335  let usage = "regtest [OPTION] ... test1 ..." in
336  let spec =
337    ["-gen", Arg.Set gen,
338       "generate the tests; implies -dump (unless -nodump is specified)" ;
339     "--gen", Arg.Set gen,
340       "generate the tests; implies -dump (unless -nodump is specified)" ;
341     "-dump", Arg.Set dump, "dump the final environment" ;
342     "--dump", Arg.Set dump, "dump the final environment" ;
343     "-nodump", Arg.Set nodump, "do not dump the final environment" ;
344     "--nodump", Arg.Set nodump, "do not dump the final environment" ;
345     "-vars", Arg.Set tryvars, "try also variables" ;
346     "-novars", Arg.Clear tryvars, "do not try variables (default)" ;
347     "-varsprefix", Arg.Set_string varsprefix,
348       "limit variable choices to URIs beginning with prefix" ;
349     "--varsprefix", Arg.Set_string varsprefix,
350       "limit variable choices to URIs beginning with prefix" ;
351    ]
352  in
353   Arg.parse spec (fun filename -> fnames := filename::!fnames ) usage ;
354   if !fnames = [] then
355    Arg.usage spec (Sys.argv.(0) ^ ": missing argument test. You must provide at least one test file.\n" ^ usage) ;
356   main mqi_handle !gen ((!gen || !dump) && (not !nodump)) !fnames !tryvars !varsprefix;
357   MQIConn.close mqi_handle