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
35 type state = Term | EMetasenv | ETerm | EType | EReduced
37 (* regtest file format
38 * < cic term in concrete syntax >
39 * separator (* see sep above *)
40 * < expected metasenv after disambiguation (CicMetaSubst.ppmetasenv) >
41 * separator (* see sep above *)
42 * < expected cic term after disambiguation (CicPp.ppterm) >
43 * separator (* see sep above *)
44 * < expected cic type as per type_of (CicPp.ppterm) >
45 * separator (* see sep above *)
46 * < expected reduced cic term as (CicPp.ppterm) >
50 term: string; (* raw cic term *)
51 emetasenv : string; (* expected metasenv *)
52 eterm: string; (* expected term *)
53 etype: string; (* expected type *)
54 ereduced: string; (* expected reduced term *)
57 let print_test test fname =
58 let oc = open_out fname in
62 sprintf "%s (* METASENV after disambiguation *)\n" rawsep;
64 sprintf "%s (* TERM after disambiguation *)\n" rawsep;
66 sprintf "%s (* TYPE_OF the disambiguated term *)\n" rawsep;
68 sprintf "%s (* REDUCED disambiguated term *)\n" rawsep;
73 let term = Buffer.create 1024 in (* raw term *)
74 let emetasenv = Buffer.create 1024 in (* expected metasenv *)
75 let eterm = Buffer.create 1024 in (* raw expected term *)
76 let etype = Buffer.create 1024 in (* raw expected type *)
77 let ereduced = Buffer.create 1024 in (* raw expected reducted term *)
78 let state = ref Term in
81 | Term -> state := EMetasenv
82 | EMetasenv -> state := ETerm
83 | ETerm -> state := EType
84 | EType -> state := EReduced
85 | EReduced -> assert false
87 let buffer_of_state = function
88 | Term -> term | EMetasenv -> emetasenv | ETerm -> eterm | EType -> etype
89 | EReduced -> ereduced
91 let clear_buffers () =
92 List.iter Buffer.clear [ term; emetasenv; eterm; etype; ereduced ]
97 let ic = open_in fname in
100 let line = input_line ic in
102 | l when Pcre.pmatch ~rex:sep l -> bump_state ()
103 | l -> Buffer.add_string (buffer_of_state !state) (line ^ "\n")
105 with End_of_file -> ());
106 { term = Buffer.contents term;
107 emetasenv = Buffer.contents emetasenv;
108 eterm = Buffer.contents eterm;
109 etype = Buffer.contents etype;
110 ereduced = Buffer.contents ereduced }
112 let as_expected expected found = (* ignores "term" field *)
113 let outcome = ref true in
114 if expected.eterm <> found.eterm then begin
115 print_endline "Term mismatch";
116 print_endline " expected:";
117 print_endline (" " ^ expected.eterm);
118 print_endline " found:";
119 print_endline (" " ^ found.eterm);
122 if expected.emetasenv <> found.emetasenv then begin
123 print_endline "Metasenv mismatch";
124 print_endline " expected:";
125 print_endline (" " ^ expected.emetasenv);
126 print_endline " found:";
127 print_endline (" " ^ found.emetasenv);
130 if expected.etype <> found.etype then begin
131 print_endline "Type mismatch";
132 print_endline " expected:";
133 print_endline (" " ^ expected.etype);
134 print_endline " found:";
135 print_endline (" " ^ found.etype);
138 if expected.ereduced <> found.ereduced then begin
139 print_endline "Reduced term mismatch";
140 print_endline " expected:";
141 print_endline (" " ^ expected.ereduced);
142 print_endline " found:";
143 print_endline (" " ^ found.ereduced);
148 let test_this raw_term =
149 let empty_context = [] in
150 let (metasenv, cic_term) = BatchParser.parse raw_term in
153 CicPp.ppterm (CicTypeChecker.type_of_aux' metasenv empty_context cic_term)
159 CicPp.ppterm (CicReduction.whd empty_context cic_term)
164 term = raw_term; (* useless *)
165 emetasenv = CicMetaSubst.ppmetasenv metasenv [] ^ "\n";
166 eterm = CicPp.ppterm cic_term ^ "\n";
167 etype = etype ^ "\n";
168 ereduced = ereduced ^ "\n";
171 let dump_environment filename =
173 let oc = open_out filename in
174 CicEnvironment.dump_to_channel oc;
178 ("DUMP_ENVIRONMENT FAILURE, uncaught excecption " ^
179 Printexc.to_string exc) ;
183 let restore_environment filename =
184 if Sys.file_exists filename then
187 let ic = open_in filename in
188 CicEnvironment.restore_from_channel ic;
192 ("RESTORE_ENVIRONMENT FAILURE, uncaught excecption " ^
193 Printexc.to_string exc) ;
197 CicEnvironment.empty ()
200 let main generate dump fnames =
204 print_endline "[ Gen mode ]";
207 let test_fname = fname ^ ".test" in
208 let env_fname = fname ^ ".env" in
209 print_endline (sprintf "Generating regtest %s -> %s\n ..."
211 let raw_term = (parse_regtest fname).term in
212 let result = test_this raw_term in
213 print_test result test_fname ;
214 if dump then dump_environment env_fname ;
219 print_endline "[ Regtest mode ]";
220 let (ok, nok) = (ref 0, ref []) in
223 let env_fname = fname ^ ".env" in
224 let test_fname = fname ^ ".test" in
225 restore_environment env_fname ;
226 let time = Unix.gettimeofday () in
227 print ("Processing " ^ fname ^":\t") ;
230 let expected = parse_regtest test_fname in
231 let actual = test_this expected.term in
232 if dump then dump_environment env_fname ;
233 if as_expected expected actual then
236 (nok := fname :: !nok ; false)
237 with e -> (nok := fname :: !nok ; false)
239 let timediff = Unix.gettimeofday () -. time in
240 print (sprintf "done in %f seconds\t" timediff) ;
241 print_endline (if is_ok then "
\e[01;32m[ OK ]
\e[00m" else "
\e[01;31m[ FAILED ]
\e[00m")
243 print_endline "*** Summary ***";
244 print_endline (sprintf "Succeeded: %d" !ok);
245 print_endline (sprintf "Failed: %d" (List.length !nok));
246 List.iter (fun fname -> print_endline (sprintf " %s failed :-(" fname))
252 let fnames = ref [] in
253 let gen = ref false in
254 let dump = ref false in
255 let nodump = ref false in
256 let usage = "regtest [OPTION] ... test1 ..." in
258 ["-gen", Arg.Set gen, "generate the tests; implies -dump (unless -nodump is specified)" ;
259 "--gen", Arg.Set gen, "generate the tests; implies -dump (unless -nodump is specified)" ;
260 "-dump", Arg.Set dump, "dumps the final environment" ;
261 "--dump", Arg.Set dump, "dumps the final environment" ;
262 "-nodump", Arg.Set dump, "do not dump the final environment" ;
263 "--nodump", Arg.Set dump, "do not dump the final environment" ]
265 Arg.parse spec (function filename -> fnames := filename::!fnames ) usage ;
267 Arg.usage spec (Sys.argv.(0) ^ ": missing argument test. You must provide at least one test file.\n" ^ usage) ;
268 main !gen ((!gen || !dump) && (not !nodump)) !fnames