1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 let argc = Array.length Sys.argv
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")
36 type state = Term | EMetasenv | ETerm | EType | EReduced
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) >
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 *)
58 let print_test test fname =
59 let oc = open_out fname in
63 sprintf "%s (* METASENV after disambiguation *)\n" rawsep;
65 sprintf "%s (* TERM after disambiguation *)\n" rawsep;
67 sprintf "%s (* TYPE_OF the disambiguated term *)\n" rawsep;
69 sprintf "%s (* REDUCED disambiguated term *)\n" rawsep;
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
82 | Term -> state := EMetasenv
83 | EMetasenv -> state := ETerm
84 | ETerm -> state := EType
85 | EType -> state := EReduced
86 | EReduced -> assert false
88 let buffer_of_state = function
89 | Term -> term | EMetasenv -> emetasenv | ETerm -> eterm | EType -> etype
90 | EReduced -> ereduced
92 let clear_buffers () =
93 List.iter Buffer.clear [ term; emetasenv; eterm; etype; ereduced ]
98 let ic = open_in fname in
101 let line = input_line ic in
103 | l when Pcre.pmatch ~rex:sep l -> bump_state ()
104 | l -> Buffer.add_string (buffer_of_state !state) (line ^ "\n")
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 }
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
120 (if Sys.file_exists report_fname then Sys.remove report_fname)
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);
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);
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);
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);
157 let test_this raw_term =
158 let empty_context = [] in
159 let (metasenv, cic_term) = BatchParser.parse raw_term in
162 CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv empty_context cic_term)
168 CicPp.ppterm (CicReduction.whd empty_context cic_term)
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";
180 let dump_environment filename =
182 let oc = open_out filename in
183 CicEnvironment.dump_to_channel oc;
187 ("DUMP_ENVIRONMENT FAILURE, uncaught excecption " ^
188 Printexc.to_string exc) ;
192 let restore_environment filename =
193 if Sys.file_exists filename then
196 let ic = open_in filename in
197 CicEnvironment.restore_from_channel ic;
201 ("RESTORE_ENVIRONMENT FAILURE, uncaught excecption " ^
202 Printexc.to_string exc) ;
206 CicEnvironment.empty ()
209 let main generate dump fnames =
213 print_endline "[ Gen mode ]";
216 let test_fname = fname ^ ".test" in
217 let env_fname = fname ^ ".env" in
218 print_endline (sprintf "Generating regtest %s -> %s\n ..."
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 ;
228 print_endline "[ Regtest mode ]";
229 let (ok, nok) = (ref 0, ref []) in
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") ;
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
246 (nok := fname :: !nok ; false)
247 with e -> (nok := fname :: !nok ; false)
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")
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))
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
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" ]
275 Arg.parse spec (function filename -> fnames := filename::!fnames ) usage ;
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