-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_test_misc.ml
More file actions
233 lines (206 loc) · 5.93 KB
/
Copy pathrun_test_misc.ml
File metadata and controls
233 lines (206 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(***************************************************************************)
(* Lucy-n *)
(* *)
(* *)
(* Authors: Louis Mandel (louis.mandel@lri.fr) *)
(* Florence Plateau (florence.plateau@lri.fr) *)
(* *)
(* Creation date: September 2011 *)
(* *)
(***************************************************************************)
type kind =
| Good
| Bad of int option * regexp
| Warning of regexp
and regexp = string
type test =
{ kind: kind;
compiler: string;
file: string;
cmd: string;
out: string;
err: string;
status: int; }
type ok =
| OK of ok_diagnostic list
| KO of ko_diagnostic list
and ok_diagnostic =
| Bad_retrun_code
| Bad_message
| Warning_instead_of_succes
| Failure_instead_of_warning
and ko_diagnostic =
| Success_instead_of_failure
| Warning_instead_of_failure
| Failure_instead_of_success
| Success_instead_of_warning
(* Utility functions *)
let is_ok ok =
match ok with
| OK _ -> true
| _ -> false
let is_ko ok =
match ok with
| KO _ -> true
| _ -> false
(* Analysis *)
let is_warning test =
if test.status = 0 then
let ch = open_in test.err in
let b =
try let _ = input_char ch in true
with End_of_file -> false
in
close_in ch;
b
else
false
let check_msg msg test =
let is_matching reg line =
try ignore (Str.search_forward reg line 0); true
with Not_found -> false
in
let reg = Str.regexp msg in
let ch = open_in test.err in
let b =
try
while not (is_matching reg (input_line ch)) do () done;
true
with End_of_file -> false
in
close_in ch;
b
let ok_of_test test =
match test.kind, test.status with
| Good, 0 ->
if is_warning test then
OK [ Warning_instead_of_succes ]
else
OK []
| Good, n ->
KO [ Failure_instead_of_success ]
| Bad _, 0 ->
if is_warning test then
KO [ Warning_instead_of_failure ]
else
KO [ Success_instead_of_failure ]
| Bad (code, msg) , n ->
let diag =
let diag = [] in
let diag =
match code with
| Some n' when n <> n' -> Bad_retrun_code :: diag
| _ -> diag
in
if check_msg msg test then diag else Bad_message :: diag
in
OK diag
| Warning msg, 0 ->
if is_warning test then
if check_msg msg test then
OK []
else
OK [ Bad_message ]
else
KO [ Success_instead_of_warning ]
| Warning _, n ->
OK [ Failure_instead_of_warning ]
let total log =
List.fold_left
(fun (nb_ok, nb_bof, nb_ko) test ->
match ok_of_test test with
| OK [] -> nb_ok + 1, nb_bof, nb_ko
| OK _ -> nb_ok, nb_bof + 1, nb_ko
| KO _ -> nb_ok, nb_bof, nb_ko + 1)
(0, 0, 0)
log
(* Output *)
let cat file =
let c = open_in file in
try
while true do
let l = input_line c in
Format.printf "%s\n" l
done
with End_of_file ->
Format.printf "@?";
close_in c
let green s = "\027[32m"^s^"\027[0m"
let red s = "\027[31m"^s^"\027[0m"
let orange s = "\027[34m"^s^"\027[0m"
let string_of_ok_diagnostic diag =
match diag with
| Bad_retrun_code -> "bad return code"
| Bad_message -> "bad error message"
| Failure_instead_of_warning -> "failure instead of warning"
| Warning_instead_of_succes -> "unexpected warning"
let string_of_ok_diagnostics diag_l =
String.concat " and " (List.map string_of_ok_diagnostic diag_l)
let string_of_ok ok =
match ok with
| OK [] -> green "OK"
| OK diag_l -> orange ("Bof (" ^ (string_of_ok_diagnostics diag_l)^")")
| KO [ Success_instead_of_failure ] -> red "KO"
| KO [ Warning_instead_of_failure ] -> red "KO (warning instead of failure)"
| KO [ Failure_instead_of_success ] -> red "KO"
| KO [ Success_instead_of_warning ] -> red "KO"
| KO _ -> assert false
let short_string_of_kind k =
match k with
| Good -> "Good: "
| Bad (_, _) -> "Bad: "
| Warning _ -> "Warning: "
let string_of_kind k =
match k with
| Good -> "Good: "
| Bad (None, msg) -> Format.sprintf "Bad \"%s\": " msg
| Bad (Some n, msg) -> Format.sprintf "Bad %i \"%s\": " n msg
| Warning msg -> Format.sprintf "Warning \"%s\": " msg
let report_test verbose test =
let ok = ok_of_test test in
if verbose >=3 then begin
Format.printf "%s@\n" test.cmd
end;
Format.printf "%s (%s%s):\t%s@\n@?"
test.file
(if verbose >= 1 then
string_of_kind test.kind
else short_string_of_kind test.kind)
test.compiler
(string_of_ok ok);
if (verbose >= 1 && is_ko ok) || verbose >= 2 then begin
(* cat test.out; *)
cat test.err
end
let report_total msg (nb_ok, nb_bof, nb_ko) =
let nb_test = nb_ok + nb_ko + nb_bof in
Format.printf "%s%i/%i" msg (nb_ok + nb_bof) nb_test;
if nb_bof = 0 then
Format.printf "@\n"
else
Format.printf " with %i unexpected behaviors@\n" nb_bof
(* Compilation *)
let compile verbose kind compiler file exts =
let ext =
try List.find (Filename.check_suffix file) exts
with Not_found -> assert false
in
let basename = Filename.chop_suffix (Filename.basename file) ext in
let tempname = Filename.temp_file basename "" in
let out = tempname ^ ".out" in
let err = tempname ^ ".err" in
let cmd =
Format.sprintf "%s %s > %s 2> %s" compiler file out err
in
let status = Sys.command cmd in
let res =
{ kind = kind;
compiler = compiler;
file = file;
cmd = cmd;
out = out;
err = err;
status = status }
in
report_test verbose res;
res