Source file check.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
open Base
type 'a typ =
| E of {pp : Format.formatter -> 'a -> unit; equal : 'a -> 'a -> bool}
| C of {pp : Format.formatter -> 'a -> unit; compare : 'a -> 'a -> int}
let get_pp = function E {pp; _} | C {pp; _} -> pp
let get_equal = function
| E {equal; _} -> equal
| C {compare; _} -> fun a b -> compare a b = 0
let get_compare = function C {compare; _} -> Some compare | E _ -> None
let pp_print_unit fmt () = Format.pp_print_string fmt "()"
let unit = C {pp = pp_print_unit; compare = Unit.compare}
let bool = C {pp = Format.pp_print_bool; compare = Bool.compare}
let pp_print_quoted_char fmt char = Format.fprintf fmt "%C" char
let char = C {pp = pp_print_quoted_char; compare = Char.compare}
let int = C {pp = Format.pp_print_int; compare = Int.compare}
let pp_print_int32 fmt i = Format.pp_print_string fmt (Int32.to_string i)
let int32 = C {pp = pp_print_int32; compare = Int32.compare}
let pp_print_int64 fmt i = Format.pp_print_string fmt (Int64.to_string i)
let int64 = C {pp = pp_print_int64; compare = Int64.compare}
let float = C {pp = Format.pp_print_float; compare = Float.compare}
let json_u =
let pp fmt json = Format.pp_print_string fmt (JSON.encode_u json) in
let equal = JSON.equal_u in
E {pp; equal}
let json =
let pp fmt json = Format.pp_print_string fmt (JSON.encode json) in
let equal = JSON.equal in
E {pp; equal}
let compare_floats_epsilon epsilon a b =
if abs_float (a -. b) <= epsilon then 0 else Float.compare a b
let float_epsilon epsilon =
C {pp = Format.pp_print_float; compare = compare_floats_epsilon epsilon}
let pp_print_quoted_string fmt string =
Format.pp_print_char fmt '"' ;
Format.pp_print_string fmt (String.escaped string) ;
Format.pp_print_char fmt '"'
let string = C {pp = pp_print_quoted_string; compare = String.compare}
let pp_option pp_item fmt = function
| None -> Format.pp_print_string fmt "None"
| Some item ->
Format.pp_print_string fmt "Some " ;
pp_item fmt item
let option = function
| E {pp = pp_item; equal = eq_items} ->
E {pp = pp_option pp_item; equal = Option.equal eq_items}
| C {pp = pp_item; compare = cmp_items} ->
C {pp = pp_option pp_item; compare = Option.compare cmp_items}
let result (type a e) (ok : a typ) (error : e typ) : (a, e) result typ =
let pp fmt = function
| Ok x -> Format.fprintf fmt "@[<hov 2>Ok@ %a@]" (get_pp ok) x
| Error e -> Format.fprintf fmt "@[<hov 2>Error@ %a@]" (get_pp error) e
in
match (ok, error) with
| E _, _ | _, E _ ->
let equal = Result.equal ~ok:(get_equal ok) ~error:(get_equal error) in
E {pp; equal}
| C {compare = cmp_ok; _}, C {compare = cmp_err; _} ->
let compare = Result.compare ~ok:cmp_ok ~error:cmp_err in
C {pp; compare}
let pp_list ?(left = "[") ?(right = "]") pp_item fmt list =
Format.pp_print_string fmt left ;
if list <> [] then (
Format.pp_open_box fmt 1 ;
Format.pp_print_char fmt ' ' ;
let pp_sep fmt () =
Format.pp_print_char fmt ';' ;
Format.pp_print_space fmt ()
in
Format.pp_print_list ~pp_sep pp_item fmt list ;
Format.pp_print_char fmt ' ' ;
Format.pp_close_box fmt ()) ;
Format.pp_print_string fmt right
let rec equal_lists eq_items a b =
match (a, b) with
| [], [] -> true
| [], _ :: _ | _ :: _, [] -> false
| hda :: tla, hdb :: tlb -> eq_items hda hdb && equal_lists eq_items tla tlb
let rec compare_lists cmp_items a b =
match (a, b) with
| [], [] -> 0
| [], _ :: _ -> -1
| _ :: _, [] -> 1
| hda :: tla, hdb :: tlb ->
let c = cmp_items hda hdb in
if c = 0 then compare_lists cmp_items tla tlb else c
let list = function
| E {pp = pp_item; equal = eq_items} ->
E {pp = pp_list pp_item; equal = equal_lists eq_items}
| C {pp = pp_item; compare = cmp_items} ->
C {pp = pp_list pp_item; compare = compare_lists cmp_items}
let pp_array pp_item fmt array =
pp_list ~left:"[|" ~right:"|]" pp_item fmt (Array.to_list array)
let equal_arrays eq_items a b =
let len_a = Array.length a in
let len_b = Array.length b in
len_a = len_b
&&
let rec loop i =
if i >= len_a then true
else if eq_items a.(i) b.(i) then loop (i + 1)
else false
in
loop 0
let compare_arrays cmp_items a b =
let len_a = Array.length a in
let len_b = Array.length b in
let rec loop i =
match (i >= len_a, i >= len_b) with
| true, true ->
0
| true, false ->
-1
| false, true ->
1
| false, false ->
let c = cmp_items a.(i) b.(i) in
if c = 0 then loop (i + 1) else c
in
loop 0
let array = function
| E {pp = pp_item; equal = eq_items} ->
E {pp = pp_array pp_item; equal = equal_arrays eq_items}
| C {pp = pp_item; compare = cmp_items} ->
C {pp = pp_array pp_item; compare = compare_arrays cmp_items}
type _ tuple_item = Item : 'b typ * ('a -> 'b) -> 'a tuple_item
let tuple (type a) (items : a tuple_item list) : a typ =
let pp fmt value =
Format.pp_open_box fmt 1 ;
Format.pp_print_string fmt "(" ;
List.iteri
(fun i (Item (item_type, get_item)) ->
if i > 0 then (
Format.pp_print_char fmt ',' ;
Format.pp_print_space fmt ()) ;
get_pp item_type fmt (get_item value))
items ;
Format.pp_print_string fmt ")" ;
Format.pp_close_box fmt ()
in
let comparable =
List.for_all
(function Item (E _, _) -> false | Item (C _, _) -> true)
items
in
if comparable then
let compare a b =
let rec loop = function
| [] -> 0
| Item (item_type, get_item) :: tail -> (
match item_type with
| E _ -> assert false
| C {compare; _} ->
let c = compare (get_item a) (get_item b) in
if c = 0 then loop tail else c)
in
loop items
in
C {pp; compare}
else
let equal a b =
let rec loop = function
| [] -> true
| Item (item_type, get_item) :: tail ->
get_equal item_type (get_item a) (get_item b) && loop tail
in
loop items
in
E {pp; equal}
let tuple2 a b = tuple [Item (a, fst); Item (b, snd)]
let tuple3 a b c =
tuple
[
Item (a, fun (x, _, _) -> x);
Item (b, fun (_, x, _) -> x);
Item (c, fun (_, _, x) -> x);
]
let tuple4 a b c d =
tuple
[
Item (a, fun (x, _, _, _) -> x);
Item (b, fun (_, x, _, _) -> x);
Item (c, fun (_, _, x, _) -> x);
Item (d, fun (_, _, _, x) -> x);
]
let tuple5 a b c d e =
tuple
[
Item (a, fun (x, _, _, _, _) -> x);
Item (b, fun (_, x, _, _, _) -> x);
Item (c, fun (_, _, x, _, _) -> x);
Item (d, fun (_, _, _, x, _) -> x);
Item (e, fun (_, _, _, _, x) -> x);
]
let tuple8 a1 a2 a3 a4 a5 a6 a7 a8 =
tuple
[
Item (a1, fun (x, _, _, _, _, _, _, _) -> x);
Item (a2, fun (_, x, _, _, _, _, _, _) -> x);
Item (a3, fun (_, _, x, _, _, _, _, _) -> x);
Item (a4, fun (_, _, _, x, _, _, _, _) -> x);
Item (a5, fun (_, _, _, _, x, _, _, _) -> x);
Item (a6, fun (_, _, _, _, _, x, _, _) -> x);
Item (a7, fun (_, _, _, _, _, _, x, _) -> x);
Item (a8, fun (_, _, _, _, _, _, _, x) -> x);
]
let convert encode = function
| E {pp; equal} ->
let pp fmt x = pp fmt (encode x) in
let equal a b = equal (encode a) (encode b) in
E {pp; equal}
| C {pp; compare} ->
let pp fmt x = pp fmt (encode x) in
let compare a b = compare (encode a) (encode b) in
C {pp; compare}
let equalable pp equal = E {pp; equal}
let comparable pp compare = C {pp; compare}
module type EQUALABLE = sig
type t
val pp : Format.formatter -> t -> unit
val equal : t -> t -> bool
end
let equalable_module (type a) (m : (module EQUALABLE with type t = a)) =
let module M = (val m) in
equalable M.pp M.equal
module type COMPARABLE = sig
type t
val pp : Format.formatter -> t -> unit
val compare : t -> t -> int
end
let comparable_module (type a) (m : (module COMPARABLE with type t = a)) =
let module M = (val m) in
comparable M.pp M.compare
let fail ?__LOC__ error_msg pp_a a pp_b b =
let error_msg =
let substitution group =
match Re.Group.get group 0 with
| exception Not_found ->
""
| "%L" -> Format.asprintf "%a" pp_a a
| "%R" -> Format.asprintf "%a" pp_b b
| s -> s
in
Re.replace (Re.compile (Re.Perl.re "%[LR]")) ~f:substitution error_msg
in
Test.fail ?__LOC__ "%s" error_msg
let eq a b ?__LOC__ typ ~error_msg =
if not (get_equal typ a b) then
let pp = get_pp typ in
fail ?__LOC__ error_msg pp a pp b
let neq a b ?__LOC__ typ ~error_msg =
if get_equal typ a b then
let pp = get_pp typ in
fail ?__LOC__ error_msg pp a pp b
let comparison function_name predicate a b ?__LOC__ typ ~error_msg =
match typ with
| E _ ->
let pp = get_pp typ in
let message =
Format.asprintf
"Check.%s used on non-comparable type to compare %a and %a with \
~error_msg = %s"
function_name
pp
a
pp
b
error_msg
in
Test.fail ?__LOC__ "%s" message
| C {pp = _; compare} ->
if not (predicate (compare a b)) then
let pp = get_pp typ in
fail ?__LOC__ error_msg pp a pp b
let lt x = comparison "(<)" (fun c -> c < 0) x
let le x = comparison "(<=)" (fun c -> c <= 0) x
let gt x = comparison "(>)" (fun c -> c > 0) x
let ge x = comparison "(>=)" (fun c -> c >= 0) x
let pp_rex fmt rex = Format.pp_print_string fmt (show_rex rex)
let like a b ~error_msg =
if a =~! b then fail error_msg pp_print_quoted_string a pp_rex b
let not_like a b ~error_msg =
if a =~ b then fail error_msg pp_print_quoted_string a pp_rex b
let list_mem typ ?__LOC__ a l ~error_msg =
let eq = get_equal typ in
if not @@ List.exists (eq a) l then
let pp = get_pp typ in
let pp_list = get_pp (list typ) in
fail ?__LOC__ error_msg pp a pp_list l
let list_not_mem typ ?__LOC__ a l ~error_msg =
let eq = get_equal typ in
if List.exists (eq a) l then
let pp = get_pp typ in
let pp_list = get_pp (list typ) in
fail ?__LOC__ error_msg pp a pp_list l
let pp_exn fmt exn = Format.pp_print_string fmt (Printexc.to_string exn)
let pp_exn_option fmt = function
| None -> Format.pp_print_string fmt "no exception"
| Some exn -> pp_exn fmt exn
let raises ?__LOC__ expected_exn f ~error_msg =
match f () with
| exception exn when exn = expected_exn -> ()
| exception exn ->
fail ?__LOC__ error_msg pp_exn expected_exn pp_exn_option (Some exn)
| _ -> fail ?__LOC__ error_msg pp_exn expected_exn pp_exn_option None
let file_exists ?__LOC__ path =
if not (Sys.file_exists path) then
Test.fail ?__LOC__ "expected that file %s exists" path
let file_not_exists ?__LOC__ path =
if Sys.file_exists path then
Test.fail ?__LOC__ "expected that file %s does not exists" path
let directory_exists ?__LOC__ path =
if not (Sys.file_exists path && Sys.is_directory path) then
Test.fail ?__LOC__ "expected that directory %s exists" path
let directory_not_exists ?__LOC__ path =
if Sys.file_exists path && Sys.is_directory path then
Test.fail ?__LOC__ "expected that directory %s does not exists" path
let is_true ?__LOC__ b ~error_msg =
if not b then Test.fail ?__LOC__ "%s" error_msg
let is_false ?__LOC__ b ~error_msg = is_true ?__LOC__ (not b) ~error_msg
let ( = ) = eq
let ( <> ) = neq
let ( < ) = lt
let ( <= ) = le
let ( > ) = gt
let ( >= ) = ge
let ( =~ ) = like
let ( =~! ) = not_like