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 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")
37 type state = Term | EEnv | EMetasenv | ETerm | EType | EReduced
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 ... *)
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 *)
64 let print_test tests fname =
65 let oc = open_out fname in
66 output_string oc (List.hd tests).term;
73 [ sprintf "%s%s INTERPRETATION NUMBER %d %s%s\n" rawsep rawsep !i rawsep rawsep ;
74 sprintf "%s (* disambiguation environment *)\n" rawsep;
76 sprintf "%s (* METASENV after disambiguation *)\n" rawsep;
78 sprintf "%s (* TERM after disambiguation *)\n" rawsep;
80 sprintf "%s (* TYPE_OF the disambiguated term *)\n" rawsep;
82 sprintf "%s (* REDUCED disambiguated term *)\n" rawsep;
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
97 | Term -> state := EEnv
98 | EEnv -> state := EMetasenv
99 | EMetasenv -> state := ETerm
100 | ETerm -> state := EType
101 | EType -> state := EReduced
102 | EReduced -> assert false
104 let buffer_of_state = function
105 | Term -> term | EEnv -> eenv | EMetasenv -> emetasenv | ETerm -> eterm | EType -> etype
106 | EReduced -> ereduced
108 let clear_buffers () =
109 List.iter Buffer.clear [ eenv; emetasenv; eterm; etype; ereduced ]
113 let first = ref true in
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 ;
125 let ic = open_in fname in
128 let line = input_line ic in
130 | l when Pcre.pmatch ~rex:sep2 l ->
131 if !first then first := false else push_res () ;
134 | l when Pcre.pmatch ~rex:sep l -> bump_state ()
135 | l -> Buffer.add_string (buffer_of_state !state) (line ^ "\n")
137 with End_of_file -> ());
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
148 eterm_ok && eenv_ok && emetasenv_ok && etype_ok && ereduced_ok
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);
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);
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);
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);
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);
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 print_interpretation test =
195 print_endline "## Interpretation dump ##";
196 print_endline "# Disambiguation environment:";
197 print_endline test.eenv;
198 print_endline "# Metasenv:";
199 print_endline test.emetasenv;
200 print_endline "# Term:";
201 print_endline test.eterm;
202 print_endline "# Type:";
203 print_endline test.etype;
204 print_endline "# Reduced term:";
205 print_endline test.ereduced;
210 | ex::extl, fo::fotl ->
211 let outcome1 = as_expected_one och ex fo in
212 let outcome2 = aux (extl,fotl) in
215 print_endline "### Too many interpretations found:" ;
216 List.iter print_interpretation found;
219 print_endline "### Too few interpretations found:" ;
220 List.iter print_interpretation expected;
223 let outcome = aux (expected,found) in
224 (if Lazy.lazy_is_val och then close_out (Lazy.force och)) ;
227 let test_this mqi_handle uri_pred raw_term =
228 let empty_context = [] in
230 (function (env, metasenv, cic_term) ->
234 (CicTypeChecker.type_of_aux' metasenv empty_context cic_term)
235 with _ -> "MALFORMED"
239 CicPp.ppterm (CicReduction.whd empty_context cic_term)
240 with _ -> "MALFORMED"
242 { term = raw_term; (* useless *)
243 eenv = DisambiguatingParser.EnvironmentP3.to_string env ^ "\n";
244 emetasenv = CicMetaSubst.ppmetasenv metasenv [] ^ "\n";
245 eterm = CicPp.ppterm cic_term ^ "\n";
246 etype = etype ^ "\n";
247 ereduced = ereduced ^ "\n";
249 ) (BatchParser.parse mqi_handle ~uri_pred raw_term)
251 let dump_environment filename =
253 let oc = open_out filename in
254 CicEnvironment.dump_to_channel oc;
258 ("DUMP_ENVIRONMENT FAILURE, uncaught excecption " ^
259 Printexc.to_string exc) ;
262 let restore_environment filename =
263 if Sys.file_exists filename then
266 let ic = open_in filename in
267 CicEnvironment.restore_from_channel ic;
271 ("RESTORE_ENVIRONMENT FAILURE, uncaught excecption " ^
272 Printexc.to_string exc) ;
276 CicEnvironment.empty ()
278 let main mqi_handle generate dump fnames tryvars prefix varsprefix =
279 let uri_pred = BatchParser.uri_pred_of_conf tryvars ~prefix ~varsprefix in
283 print_endline "[ Gen mode ]";
286 let test_fname = fname ^ ".test" in
287 let env_fname = fname ^ ".env" in
288 print_endline (sprintf "Generating regtest %s -> %s\n ..."
290 let raw_term = (List.hd (parse_regtest fname)).term in
291 let results = test_this mqi_handle uri_pred raw_term in
292 print_test results test_fname ;
293 if dump then dump_environment env_fname ;
298 print_endline "[ Regtest mode ]";
299 let (ok, nok) = (ref 0, ref []) in
302 let env_fname = fname ^ ".env" in
303 let test_fname = fname ^ ".test" in
304 let report_fname = fname ^ ".report" in
305 restore_environment env_fname ;
306 let time = Unix.gettimeofday () in
307 print ("Processing " ^ fname ^":\t") ;
310 let expected = parse_regtest test_fname in
311 let actual = test_this mqi_handle uri_pred (List.hd expected).term in
312 if dump then dump_environment env_fname ;
313 if as_expected report_fname expected actual then
316 (nok := fname :: !nok ; false)
317 with e -> (nok := fname :: !nok ; false)
319 let timediff = Unix.gettimeofday () -. time in
320 print (sprintf "done in %f seconds\t" timediff) ;
323 "
\e[01;32m[ OK ]
\e[00m"
325 "
\e[01;31m[ FAILED ]
\e[00m")
327 print_endline "*** Summary ***";
328 print_endline (sprintf "Succeeded: %d" !ok);
329 print_endline (sprintf "Failed: %d" (List.length !nok));
330 List.iter (fun fname -> print_endline (sprintf " %s failed :-(" fname))
336 Helm_registry.load_from "gTopLevel.conf.xml";
337 HelmLogger.register_log_callback
338 (fun ?(append_NL = true) msg ->
339 (if append_NL then prerr_endline else prerr_string)
340 (HelmLogger.string_of_html_msg msg));
342 let mqi_debug_fun s =
343 HelmLogger.log ~append_NL:true (`Msg (`T s)) in
344 let mqi_handle = MQIConn.init ~log:mqi_debug_fun () in
346 let fnames = ref [] in
347 let gen = ref false in
348 let tryvars = ref false in
349 let dump = ref false in
350 let nodump = ref false in
351 let prefix = ref "" in
352 let varsprefix = ref "###" in
353 let usage = "regtest [OPTION] ... test1 ..." in
355 ["-gen", Arg.Set gen,
356 "generate the tests; implies -dump (unless -nodump is specified)" ;
357 "--gen", Arg.Set gen,
358 "generate the tests; implies -dump (unless -nodump is specified)" ;
359 "-dump", Arg.Set dump, "dump the final environment" ;
360 "--dump", Arg.Set dump, "dump the final environment" ;
361 "-nodump", Arg.Set nodump, "do not dump the final environment" ;
362 "--nodump", Arg.Set nodump, "do not dump the final environment" ;
363 "-vars", Arg.Set tryvars, "try also variables" ;
364 "-novars", Arg.Clear tryvars, "do not try variables (default)" ;
365 "-prefix", Arg.Set_string prefix,
366 "limit object choices to URIs beginning with prefix" ;
367 "--prefix", Arg.Set_string prefix,
368 "limit object choices to URIs beginning with prefix" ;
369 "-varsprefix", Arg.Set_string varsprefix,
370 "limit variable choices to URIs beginning with prefix; overrides -prefix" ;
371 "--varsprefix", Arg.Set_string varsprefix,
372 "limit variable choices to URIs beginning with prefix; overrides -prefix"
375 Arg.parse spec (fun filename -> fnames := filename::!fnames ) usage ;
377 Arg.usage spec (Sys.argv.(0) ^ ": missing argument test. You must provide at least one test file.\n" ^ usage) ;
378 if !varsprefix = "###" then varsprefix := !prefix ;
379 main mqi_handle !gen ((!gen || !dump) && (not !nodump)) !fnames !tryvars !prefix !varsprefix;
380 MQIConn.close mqi_handle