Source file oUnitUtils.ml
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
233
234
235
236
237
238
239
240
241
242
243
(**
* Utilities for OUnit
*
* @author Sylvain Le Gall
*)
let is_blank =
function
| ' ' | '\012' | '\n' | '\r' | '\t' -> true
| _ -> false
let rec trim s =
let strlen = String.length s in
if strlen = 0 then
""
else if is_blank s.[0] then
trim (String.sub s 1 (strlen - 1))
else if is_blank s.[strlen - 1] then
trim (String.sub s 0 (strlen - 1))
else
s
let s =
let buff = Buffer.create (String.length s) in
let idx = ref 0 in
while !idx < String.length s && s.[!idx] != '#' do
Buffer.add_char buff s.[!idx];
incr idx
done;
Buffer.contents buff
let split_lines s =
let rev_lst = ref [] in
let buff = Buffer.create 13 in
let flush () =
rev_lst := Buffer.contents buff :: !rev_lst;
Buffer.clear buff
in
if String.length s > 0 then
begin
String.iter
(function
| '\n' -> flush ()
| c -> Buffer.add_char buff c)
s;
flush ();
List.rev !rev_lst
end
else
[]
let starts_with ~prefix s =
if String.length s >= String.length prefix then
String.sub s 0 (String.length prefix) = prefix
else
false
let start_substr ~prefix s =
if starts_with ~prefix s then begin
let prefix_len = String.length prefix in
true, String.sub s prefix_len (String.length s - prefix_len)
end else begin
false, s
end
let str =
let prefixes =
[
"Raised at ";
"Re-raised at ";
"Raised by primitive operation at ";
"Called from ";
]
in
let rec s prefixes =
match prefixes with
| [] -> None
| prefix :: tl ->
let really_starts, eol = start_substr ~prefix s in
if really_starts then begin
if eol = "unknown location" then
None
else
try
Scanf.sscanf eol "file \"%s@\", line %d, characters %d-%d"
(fun fn line _ _ -> Some (fn, line))
with Scanf.Scan_failure msg ->
None
end else begin
extract_one_line s tl
end
in
List.map
(fun s -> extract_one_line s prefixes)
(split_lines str)
let cmp_float ?(epsilon = 0.00001) a b =
abs_float (a -. b) <= epsilon *. (abs_float a) ||
abs_float (a -. b) <= epsilon *. (abs_float b)
let buff_format_printf f =
let buff = Buffer.create 13 in
let fmt = Format.formatter_of_buffer buff in
f fmt;
Format.pp_print_flush fmt ();
Buffer.contents buff
let mapi f l =
let rec rmapi cnt l =
match l with
| [] ->
[]
| h :: t ->
(f h cnt) :: (rmapi (cnt + 1) t)
in
rmapi 0 l
let fold_lefti f accu l =
let rec rfold_lefti cnt accup l =
match l with
| [] ->
accup
| h::t ->
rfold_lefti (cnt + 1) (f accup h cnt) t
in
rfold_lefti 0 accu l
let now () =
Unix.gettimeofday ()
let time_fun f x =
let begin_time = now () in
let res = f x in
(now () -. begin_time, res)
let date_iso8601 ?(tz=true) timestamp =
let tm = Unix.gmtime timestamp in
let res =
Printf.sprintf
"%04d-%02d-%02dT%02d:%02d:%02d"
(1900 + tm.Unix.tm_year)
(1 + tm.Unix.tm_mon)
tm.Unix.tm_mday
tm.Unix.tm_hour
tm.Unix.tm_min
tm.Unix.tm_sec
in
if tz then
res ^ "+00:00"
else
res
let buildir =
let pwd = Sys.getcwd () in
let dir_exists fn = Sys.file_exists fn && Sys.is_directory fn in
let concat, dirname = Filename.concat, Filename.dirname in
List.find
dir_exists
[
concat pwd "_build";
concat (dirname pwd) "_build";
concat (dirname (dirname pwd)) "_build";
pwd
]
let failwithf fmt =
Printf.ksprintf failwith fmt
let opt f = function Some v -> f v | None -> ()
let fqdn () = (Unix.gethostbyname (Unix.gethostname ())).Unix.h_name
let shardf = Printf.sprintf "%s#%02d" (Unix.gethostname ())
let string_of_process_status =
function
| Unix.WEXITED n ->
Printf.sprintf "Exited with code %d" n
| Unix.WSIGNALED n ->
Printf.sprintf "Killed by signal %d" n
| Unix.WSTOPPED n ->
Printf.sprintf "Stopped by signal %d" n
let make_counter () =
let data = Hashtbl.create 13 in
let all () =
Hashtbl.fold
(fun k v lst -> (k, v) :: lst)
data []
in
let incr k =
let v =
try
Hashtbl.find data k
with Not_found ->
0
in
Hashtbl.replace data k (v + 1)
in
all, incr