Source file testable.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
type 'a check_result =
| Pass
| Fail of {
expected_str : string;
actual_str : string;
diff : Failure.diff_output option;
}
type 'a t = {
pp : 'a Pp.t;
equal : 'a -> 'a -> bool;
gen : 'a Windtrap_prop.Gen.t option;
check : ('a -> 'a -> 'a check_result) option;
}
let make ~pp ?(equal = ( = )) ?gen ?check () = { pp; equal; gen; check }
let pp t = t.pp
let equal t = t.equal
let gen t = t.gen
let check t = t.check
let to_string t v = Pp.to_string (pp t) v
let with_gen gen t = { t with gen = Some gen }
let unit =
{
pp = (fun ppf () -> Pp.string ppf "()");
equal = ( = );
gen = Some Windtrap_prop.Gen.unit;
check = None;
}
let bool =
{
pp = Pp.bool;
equal = Bool.equal;
gen = Some Windtrap_prop.Gen.bool;
check = None;
}
let int =
{
pp = Pp.int;
equal = Int.equal;
gen = Some Windtrap_prop.Gen.int;
check = None;
}
let small_int =
{
pp = Pp.int;
equal = Int.equal;
gen = Some Windtrap_prop.Gen.small_int;
check = None;
}
let nat =
{
pp = Pp.int;
equal = Int.equal;
gen = Some Windtrap_prop.Gen.nat;
check = None;
}
let int32 =
{
pp = Pp.int32;
equal = Int32.equal;
gen = Some Windtrap_prop.Gen.int32;
check = None;
}
let int64 =
{
pp = Pp.int64;
equal = Int64.equal;
gen = Some Windtrap_prop.Gen.int64;
check = None;
}
let nativeint =
{
pp = (fun ppf n -> Pp.pf ppf "%nd" n);
equal = Nativeint.equal;
gen = Some Windtrap_prop.Gen.nativeint;
check = None;
}
let float eps =
let is_nan f = FP_nan = classify_float f in
{
pp = (fun ppf f -> Pp.pf ppf "%g" f);
equal =
(fun a b -> (is_nan a && is_nan b) || a = b || Float.abs (a -. b) <= eps);
gen = Some Windtrap_prop.Gen.float;
check = None;
}
let float_rel ~rel ~abs =
let is_nan f = FP_nan = classify_float f in
{
pp = (fun ppf f -> Pp.pf ppf "%g" f);
equal =
(fun a b ->
if is_nan a && is_nan b then true
else if a = b then true
else
let diff = Float.abs (a -. b) in
let max_ab = Float.max (Float.abs a) (Float.abs b) in
diff <= abs || diff <= rel *. max_ab);
gen = Some Windtrap_prop.Gen.float;
check = None;
}
let char =
{
pp = (fun ppf c -> Pp.pf ppf "%C" c);
equal = Char.equal;
gen = Some Windtrap_prop.Gen.char;
check = None;
}
let bytes =
{
pp = (fun ppf b -> Pp.pf ppf "%S" (Bytes.to_string b));
equal = Bytes.equal;
gen = Some Windtrap_prop.Gen.bytes;
check = None;
}
let option t =
{
pp =
(fun ppf -> function
| None -> Pp.pf ppf "None" | Some v -> Pp.pf ppf "Some %a" t.pp v);
equal =
(fun a b ->
match (a, b) with
| None, None -> true
| Some a, Some b -> t.equal a b
| _ -> false);
gen = Option.map Windtrap_prop.Gen.option t.gen;
check = None;
}
let result ok_t err_t =
{
pp = Pp.result ~ok:ok_t.pp ~error:err_t.pp;
equal =
(fun a b ->
match (a, b) with
| Ok a, Ok b -> ok_t.equal a b
| Error a, Error b -> err_t.equal a b
| _ -> false);
gen =
(match (ok_t.gen, err_t.gen) with
| Some ok_gen, Some err_gen ->
Some (Windtrap_prop.Gen.result ok_gen err_gen)
| _ -> None);
check = None;
}
let either left_t right_t =
{
pp =
(fun ppf -> function
| Either.Left x -> Pp.pf ppf "Left (%a)" left_t.pp x
| Either.Right x -> Pp.pf ppf "Right (%a)" right_t.pp x);
equal =
(fun a b ->
match (a, b) with
| Either.Left a, Either.Left b -> left_t.equal a b
| Either.Right a, Either.Right b -> right_t.equal a b
| _ -> false);
gen =
(match (left_t.gen, right_t.gen) with
| Some lg, Some rg -> Some (Windtrap_prop.Gen.either lg rg)
| _ -> None);
check = None;
}
let rec equal_list eq a b =
match (a, b) with
| [], [] -> true
| x :: xs, y :: ys -> eq x y && equal_list eq xs ys
| _ -> false
let diff_threshold = 4. /. 5.
let edit_ratio ~num_edits ~len_expected ~len_actual =
let max_len =
Float.max (Float.of_int len_expected) (Float.of_int len_actual)
in
if max_len = 0. then 0. else Float.of_int num_edits /. max_len
let make_highlighted_diff ~pp_elt ~sep ~prefix ~suffix expected actual
expected_indices actual_indices =
let format_parts list indices =
list
|> List.mapi (fun i x ->
let text = Pp.str "%a" pp_elt x in
let highlight = List.mem i indices in
Failure.{ text; highlight })
|> List.fold_left
(fun acc part ->
match acc with
| [] -> [ part ]
| _ -> acc @ [ { Failure.text = sep; highlight = false }; part ])
[]
|> fun parts ->
[ { Failure.text = prefix; highlight = false } ]
@ parts
@ [ { Failure.text = suffix; highlight = false } ]
in
Failure.
{
expected_parts = format_parts expected expected_indices;
actual_parts = format_parts actual actual_indices;
}
let check_list_with_diff ~elt_equal ~pp_elt expected actual =
if List.equal elt_equal expected actual then Pass
else
let edits = Distance.(levenshtein List) ~equal:elt_equal expected actual in
let expected_str = Pp.str "[%a]" (Pp.list ~sep:Pp.semi pp_elt) expected in
let actual_str = Pp.str "[%a]" (Pp.list ~sep:Pp.semi pp_elt) actual in
let ratio =
edit_ratio ~num_edits:(List.length edits)
~len_expected:(List.length expected) ~len_actual:(List.length actual)
in
if ratio > diff_threshold then
Fail { expected_str; actual_str; diff = None }
else
let expected_indices, actual_indices = Distance.edit_indices edits in
let diff =
make_highlighted_diff ~pp_elt ~sep:"; " ~prefix:"[" ~suffix:"]" expected
actual expected_indices actual_indices
in
Fail { expected_str; actual_str; diff = Some diff }
let list t =
let elt_equal = t.equal in
let pp_elt = t.pp in
let check expected actual =
check_list_with_diff ~elt_equal ~pp_elt expected actual
in
{
pp = Pp.brackets (Pp.list ~sep:Pp.semi t.pp);
equal = equal_list t.equal;
gen = Option.map Windtrap_prop.Gen.list t.gen;
check = Some check;
}
let check_array_with_diff ~elt_equal ~pp_elt expected actual =
if
Array.length expected = Array.length actual
&& Array.for_all2 elt_equal expected actual
then Pass
else
let edits = Distance.(levenshtein Array) ~equal:elt_equal expected actual in
let expected_str =
Pp.str "[|%a|]" (Pp.array ~sep:Pp.semi pp_elt) expected
in
let actual_str = Pp.str "[|%a|]" (Pp.array ~sep:Pp.semi pp_elt) actual in
let ratio =
edit_ratio ~num_edits:(List.length edits)
~len_expected:(Array.length expected) ~len_actual:(Array.length actual)
in
if ratio > diff_threshold then
Fail { expected_str; actual_str; diff = None }
else
let expected_indices, actual_indices = Distance.edit_indices edits in
let diff =
make_highlighted_diff ~pp_elt ~sep:"; " ~prefix:"[|" ~suffix:"|]"
(Array.to_list expected) (Array.to_list actual) expected_indices
actual_indices
in
Fail { expected_str; actual_str; diff = Some diff }
let array t =
let elt_equal = t.equal in
let pp_elt = t.pp in
let check expected actual =
check_array_with_diff ~elt_equal ~pp_elt expected actual
in
{
pp = (fun ppf arr -> Pp.pf ppf "[|%a|]" (Pp.array ~sep:Pp.semi t.pp) arr);
equal =
(fun a b -> Array.length a = Array.length b && Array.for_all2 t.equal a b);
gen = Option.map Windtrap_prop.Gen.array t.gen;
check = Some check;
}
let check_string_with_diff expected actual =
if String.equal expected actual then Pass
else
let edits =
Distance.(levenshtein String) ~equal:Char.equal expected actual
in
let expected_str = Pp.str "%S" expected in
let actual_str = Pp.str "%S" actual in
let ratio =
edit_ratio ~num_edits:(List.length edits)
~len_expected:(String.length expected)
~len_actual:(String.length actual)
in
if ratio > diff_threshold then
Fail { expected_str; actual_str; diff = None }
else
let expected_indices, actual_indices = Distance.edit_indices edits in
let format_char_parts chars indices =
chars
|> List.mapi (fun i c ->
Failure.{ text = String.make 1 c; highlight = List.mem i indices })
|> fun parts ->
[ { Failure.text = "\""; highlight = false } ]
@ parts
@ [ { Failure.text = "\""; highlight = false } ]
in
let diff =
Failure.
{
expected_parts =
format_char_parts
(String.to_seq expected |> List.of_seq)
expected_indices;
actual_parts =
format_char_parts
(String.to_seq actual |> List.of_seq)
actual_indices;
}
in
Fail { expected_str; actual_str; diff = Some diff }
let string =
{
pp = (fun ppf s -> Pp.pf ppf "%S" s);
equal = String.equal;
gen = Some Windtrap_prop.Gen.string;
check = Some check_string_with_diff;
}
let pair a_t b_t =
{
pp = Pp.pair a_t.pp b_t.pp;
equal = (fun (a1, b1) (a2, b2) -> a_t.equal a1 a2 && b_t.equal b1 b2);
gen =
(match (a_t.gen, b_t.gen) with
| Some ga, Some gb -> Some (Windtrap_prop.Gen.pair ga gb)
| _ -> None);
check = None;
}
let triple a_t b_t c_t =
{
pp =
(fun ppf (a, b, c) ->
Pp.pf ppf "(@[%a,@ %a,@ %a@])" a_t.pp a b_t.pp b c_t.pp c);
equal =
(fun (a1, b1, c1) (a2, b2, c2) ->
a_t.equal a1 a2 && b_t.equal b1 b2 && c_t.equal c1 c2);
gen =
(match (a_t.gen, b_t.gen, c_t.gen) with
| Some ga, Some gb, Some gc -> Some (Windtrap_prop.Gen.triple ga gb gc)
| _ -> None);
check = None;
}
let quad a_t b_t c_t d_t =
{
pp =
(fun ppf (a, b, c, d) ->
Pp.pf ppf "(@[%a,@ %a,@ %a,@ %a@])" a_t.pp a b_t.pp b c_t.pp c d_t.pp d);
equal =
(fun (a1, b1, c1, d1) (a2, b2, c2, d2) ->
a_t.equal a1 a2 && b_t.equal b1 b2 && c_t.equal c1 c2 && d_t.equal d1 d2);
gen =
(match (a_t.gen, b_t.gen, c_t.gen, d_t.gen) with
| Some ga, Some gb, Some gc, Some gd ->
Some (Windtrap_prop.Gen.quad ga gb gc gd)
| _ -> None);
check = None;
}
let pass =
{
pp = (fun ppf _ -> Pp.string ppf "<pass>");
equal = (fun _ _ -> true);
gen = None;
check = None;
}
let slist t cmp =
let sort = List.sort cmp in
{
pp = Pp.brackets (Pp.list ~sep:Pp.semi t.pp);
equal = (fun a b -> equal_list t.equal (sort a) (sort b));
gen = Option.map Windtrap_prop.Gen.list t.gen;
check = None;
}
let of_equal equal =
{
pp = (fun ppf _ -> Pp.string ppf "<opaque>");
equal;
gen = None;
check = None;
}
let contramap f t =
{
pp = (fun ppf a -> t.pp ppf (f a));
equal = (fun a b -> t.equal (f a) (f b));
gen = None;
check = None;
}
let seq t =
let pp ppf s =
let first = ref true in
Seq.iter
(fun x ->
if !first then first := false else Pp.pf ppf ";@ ";
t.pp ppf x)
s
in
let equal s1 s2 =
let rec loop s1 s2 =
match (s1 (), s2 ()) with
| Seq.Nil, Seq.Nil -> true
| Seq.Cons (x, xs), Seq.Cons (y, ys) -> t.equal x y && loop xs ys
| _ -> false
in
loop s1 s2
in
make ~pp ~equal ()
let lazy_t t =
{
pp = (fun ppf l -> t.pp ppf (Lazy.force l));
equal = (fun a b -> t.equal (Lazy.force a) (Lazy.force b));
gen = None;
check = None;
}