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,ugraph ) ->
234 (CicTypeChecker.type_of_aux' metasenv empty_context cic_term ugraph)
237 with _ -> "MALFORMED"
241 CicPp.ppterm (CicReduction.whd empty_context cic_term)
242 with _ -> "MALFORMED"
244 { term = raw_term; (* useless *)
245 eenv = DisambiguatingParser.EnvironmentP3.to_string env ^ "\n";
246 emetasenv = CicMetaSubst.ppmetasenv metasenv [] ^ "\n";
247 eterm = CicPp.ppterm cic_term ^ "\n";
248 etype = etype ^ "\n";
249 ereduced = ereduced ^ "\n";
251 ) (BatchParser.parse mqi_handle ~uri_pred raw_term CicUniv.empty_ugraph)
253 let dump_environment filename =
255 let oc = open_out filename in
256 CicEnvironment.dump_to_channel oc;
260 ("DUMP_ENVIRONMENT FAILURE, uncaught excecption " ^
261 Printexc.to_string exc) ;
264 let restore_environment filename =
265 if Sys.file_exists filename then
268 let ic = open_in filename in
269 CicEnvironment.restore_from_channel ic;
273 ("RESTORE_ENVIRONMENT FAILURE, uncaught excecption " ^
274 Printexc.to_string exc) ;
278 CicEnvironment.empty ()
280 let main mqi_handle generate dump fnames tryvars prefix varsprefix =
281 let uri_pred = BatchParser.uri_pred_of_conf tryvars ~prefix ~varsprefix in
285 print_endline "[ Gen mode ]";
288 let test_fname = fname ^ ".test" in
289 let env_fname = fname ^ ".env" in
290 print_endline (sprintf "Generating regtest %s -> %s\n ..."
292 let raw_term = (List.hd (parse_regtest fname)).term in
293 let results = test_this mqi_handle uri_pred raw_term in
294 print_test results test_fname ;
295 if dump then dump_environment env_fname ;
300 print_endline "[ Regtest mode ]";
301 let (ok, nok) = (ref 0, ref []) in
304 let env_fname = fname ^ ".env" in
305 let test_fname = fname ^ ".test" in
306 let report_fname = fname ^ ".report" in
307 restore_environment env_fname ;
308 let time = Unix.gettimeofday () in
309 print ("Processing " ^ fname ^":\t") ;
312 let expected = parse_regtest test_fname in
313 let actual = test_this mqi_handle uri_pred (List.hd expected).term in
314 if dump then dump_environment env_fname ;
315 if as_expected report_fname expected actual then
318 (nok := fname :: !nok ; false)
319 with e -> (nok := fname :: !nok ; false)
321 let timediff = Unix.gettimeofday () -. time in
322 print (sprintf "done in %f seconds\t" timediff) ;
325 "
\e[01;32m[ OK ]
\e[00m"
327 "
\e[01;31m[ FAILED ]
\e[00m")
329 print_endline "*** Summary ***";
330 print_endline (sprintf "Succeeded: %d" !ok);
331 print_endline (sprintf "Failed: %d" (List.length !nok));
332 List.iter (fun fname -> print_endline (sprintf " %s failed :-(" fname))
338 Helm_registry.load_from "gTopLevel.conf.xml";
339 HelmLogger.register_log_callback
340 (fun ?(append_NL = true) msg ->
341 (if append_NL then prerr_endline else prerr_string)
342 (HelmLogger.string_of_html_msg msg));
343 Helm_registry.load_from "gTopLevel.conf.xml";
346 ~host:(Helm_registry.get "db.host")
347 ~user:(Helm_registry.get "db.user")
348 ~database:(Helm_registry.get "db.database")
351 let fnames = ref [] in
352 let gen = ref false in
353 let tryvars = ref false in
354 let dump = ref false in
355 let nodump = ref false in
356 let prefix = ref "" in
357 let varsprefix = ref "###" in
358 let usage = "regtest [OPTION] ... test1 ..." in
360 ["-gen", Arg.Set gen,
361 "generate the tests; implies -dump (unless -nodump is specified)" ;
362 "--gen", Arg.Set gen,
363 "generate the tests; implies -dump (unless -nodump is specified)" ;
364 "-dump", Arg.Set dump, "dump the final environment" ;
365 "--dump", Arg.Set dump, "dump the final environment" ;
366 "-nodump", Arg.Set nodump, "do not dump the final environment" ;
367 "--nodump", Arg.Set nodump, "do not dump the final environment" ;
368 "-vars", Arg.Set tryvars, "try also variables" ;
369 "-novars", Arg.Clear tryvars, "do not try variables (default)" ;
370 "-prefix", Arg.Set_string prefix,
371 "limit object choices to URIs beginning with prefix" ;
372 "--prefix", Arg.Set_string prefix,
373 "limit object choices to URIs beginning with prefix" ;
374 "-varsprefix", Arg.Set_string varsprefix,
375 "limit variable choices to URIs beginning with prefix; overrides -prefix" ;
376 "--varsprefix", Arg.Set_string varsprefix,
377 "limit variable choices to URIs beginning with prefix; overrides -prefix"
380 Arg.parse spec (fun filename -> fnames := filename::!fnames ) usage ;
382 Arg.usage spec (Sys.argv.(0) ^ ": missing argument test. You must provide at least one test file.\n" ^ usage) ;
383 if !varsprefix = "###" then varsprefix := !prefix ;
384 main dbd !gen ((!gen || !dump) && (not !nodump)) !fnames !tryvars !prefix !varsprefix;