Source file equivalence_tests.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
open! Core
(** Tests that equivalent constructions in [Unstring] produce the same parse results.
Each test declares two functions [lhs] and [rhs] from some testcase type to
['a Unstring.t]. The scaffold generates random testcases, builds both parsers, and
checks they agree on: matching inputs (likely to parse) and random inputs (likely to
be rejected). *)
module Make (M : sig
type testcase [@@deriving quickcheck, sexp_of]
type result [@@deriving compare, sexp_of]
val lhs : testcase -> result Unstring.t
val rhs : testcase -> result Unstring.t
val matching_string_gen : testcase -> string Quickcheck.Generator.t option
end) : sig
type t [@@deriving quickcheck, sexp_of]
val validate : t -> unit
end = struct
type t =
{ testcase : M.testcase
; inputs : ([ `Matching | `Any ] * string) list
}
[@@deriving sexp_of]
let quickcheck_generator =
let open Quickcheck.Generator.Let_syntax in
let%bind testcase = [%quickcheck.generator: M.testcase] in
let%map inputs =
[ M.matching_string_gen testcase
|> Option.map ~f:(Quickcheck.Generator.map ~f:(fun x -> `Matching, x))
; Some
([%quickcheck.generator: string]
|> Quickcheck.Generator.map ~f:(fun x -> `Any, x))
]
|> List.filter_opt
|> Quickcheck.Generator.union
|> Quickcheck.Generator.list_with_length 5
in
{ testcase; inputs }
;;
let quickcheck_observer = Quickcheck.Observer.singleton ()
let quickcheck_shrinker = Quickcheck.Shrinker.empty ()
let validate { testcase; inputs } =
let lhs = M.lhs testcase |> Unstring.compile in
let rhs = M.rhs testcase |> Unstring.compile in
let run t input =
Sequence.take (Unstring.Compiled.run_all_parses t input) 10 |> Sequence.to_list
in
List.iter inputs ~f:(fun (requirement, input) ->
let lhs, rhs = run lhs input, run rhs input in
[%test_eq: M.result Or_error.t list] lhs rhs;
match requirement with
| `Any -> ()
| `Matching ->
assert (List.length lhs > 0))
;;
end
let%expect_test "map2 is associative" =
let module Test =
Make (struct
type testcase = Blueprint.t * Blueprint.t * Blueprint.t
[@@deriving quickcheck, sexp_of]
type result = Blueprint.Parse.t * Blueprint.Parse.t * Blueprint.Parse.t
[@@deriving compare, sexp_of]
let map2 = Unstring.map2
let to_unstring t = Tuple3.map t ~f:Blueprint.to_unstring
let lhs t =
let a, b, c = to_unstring t in
map2 (map2 a b ~f:(fun a b -> a, b)) c ~f:(fun (a, b) c -> a, b, c)
;;
let rhs t =
let a, b, c = to_unstring t in
map2 a (map2 b c ~f:(fun b c -> b, c)) ~f:(fun a (b, c) -> a, b, c)
;;
let matching_string_gen (a, b, c) =
[ Blueprint.matching_string_gen a
; Blueprint.matching_string_gen b
; Blueprint.matching_string_gen c
]
|> Option.all
|> Option.map ~f:String_gen.concat_list
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "map2 left identity: map2 (return ()) t ≡ t" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = Blueprint.Parse.t [@@deriving compare, sexp_of]
let lhs bp =
Unstring.map2 (Unstring.return ()) (Blueprint.to_unstring bp) ~f:(fun () x -> x)
;;
let rhs bp = Blueprint.to_unstring bp
let matching_string_gen bp = Blueprint.matching_string_gen bp
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "map2 right identity: map2 t (return ()) ≡ t" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = Blueprint.Parse.t [@@deriving compare, sexp_of]
let lhs bp =
Unstring.map2 (Blueprint.to_unstring bp) (Unstring.return ()) ~f:(fun x () -> x)
;;
let rhs bp = Blueprint.to_unstring bp
let matching_string_gen bp = Blueprint.matching_string_gen bp
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "map2 naturality: map (map2 a b ~f) ~g ≡ map2 a b ~f:(compose g f)" =
let module Test =
Make (struct
type testcase = Blueprint.t * Blueprint.t [@@deriving quickcheck, sexp_of]
type result = [ `G of [ `F of Blueprint.Parse.t * Blueprint.Parse.t ] ]
[@@deriving compare, sexp_of]
let f a b = `F (a, b)
let g x = `G x
let lhs (a, b) =
Unstring.map2 (Blueprint.to_unstring a) (Blueprint.to_unstring b) ~f
|> Unstring.map ~f:g
;;
let rhs (a, b) =
Unstring.map2 (Blueprint.to_unstring a) (Blueprint.to_unstring b) ~f:(fun a b ->
g (f a b))
;;
let matching_string_gen (a, b) =
[ Blueprint.matching_string_gen a; Blueprint.matching_string_gen b ]
|> Option.all
|> Option.map ~f:String_gen.concat_list
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "functor identity: map t ~f:Fun.id ≡ t" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = Blueprint.Parse.t [@@deriving compare, sexp_of]
let lhs bp = Unstring.map (Blueprint.to_unstring bp) ~f:Fun.id
let rhs bp = Blueprint.to_unstring bp
let matching_string_gen bp = Blueprint.matching_string_gen bp
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "functor composition: map (map t ~f) ~g ≡ map t ~f:(compose g f)" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = [ `G of [ `F of Blueprint.Parse.t ] ] [@@deriving compare, sexp_of]
let f x = `F x
let g x = `G x
let lhs bp = Blueprint.to_unstring bp |> Unstring.map ~f |> Unstring.map ~f:g
let rhs bp = Blueprint.to_unstring bp |> Unstring.map ~f:(fun x -> g (f x))
let matching_string_gen bp = Blueprint.matching_string_gen bp
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "all_unit ts ≡ folding map2" =
let module Test =
Make (struct
type testcase = Blueprint.t list [@@deriving quickcheck, sexp_of]
type result = unit [@@deriving compare, sexp_of]
let lhs bps = Unstring.all_unit (List.map bps ~f:Blueprint.to_unit_unstring)
let rhs bps =
List.fold
(List.map bps ~f:Blueprint.to_unit_unstring)
~init:(Unstring.return ())
~f:(fun acc t -> Unstring.map2 acc t ~f:(fun () () -> ()))
;;
let matching_string_gen bps =
List.map bps ~f:Blueprint.matching_string_gen
|> Option.all
|> Option.map ~f:String_gen.concat_list
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "capture t ≡ capture' t ~f:String.sub" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = string [@@deriving compare, sexp_of]
let lhs bp = Unstring.capture (Blueprint.to_unit_unstring bp)
let rhs bp =
Unstring.capture' (Blueprint.to_unit_unstring bp) ~f:(fun () ~input ~pos ~len ->
String.sub input ~pos ~len)
;;
let matching_string_gen bp = Blueprint.matching_string_gen bp
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "star t ≡ multiple t" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = unit [@@deriving compare, sexp_of]
let lhs bp = Unstring.star (Blueprint.to_unit_unstring bp)
let rhs bp = Unstring.multiple (Blueprint.to_unit_unstring bp)
let matching_string_gen bp =
Blueprint.repeat_matching_string_gen ~sep:None ~bounds:None bp
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "star t ≡ fold t ~init:() ~f:unit" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = unit [@@deriving compare, sexp_of]
let lhs bp = Unstring.star (Blueprint.to_unit_unstring bp)
let rhs bp =
Unstring.fold (Blueprint.to_unit_unstring bp) ~init:() ~f:(fun () () -> ())
;;
let matching_string_gen bp =
Blueprint.repeat_matching_string_gen ~sep:None ~bounds:None bp
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "multiple ~bounds t ≡ fold ~bounds t ~init:() ~f:unit" =
let module Test =
Make (struct
type testcase = Blueprint.t * Blueprint.Bound.t * Blueprint.Bound.t
[@@deriving quickcheck, sexp_of]
type result = unit [@@deriving compare, sexp_of]
let lhs (bp, lo, hi) =
Unstring.multiple ~bounds:(lo, hi) (Blueprint.to_unit_unstring bp)
;;
let rhs (bp, lo, hi) =
Unstring.fold
~bounds:(lo, hi)
(Blueprint.to_unit_unstring bp)
~init:()
~f:(fun () () -> ())
;;
let matching_string_gen (bp, lo, hi) =
Blueprint.repeat_matching_string_gen ~sep:None ~bounds:(Some (lo, hi)) bp
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "list t ≡ fold t + List.rev" =
let module Test =
Make (struct
type testcase = Blueprint.t * Blueprint.Bound.t * Blueprint.Bound.t
[@@deriving quickcheck, sexp_of]
type result = Blueprint.Parse.t list [@@deriving compare, sexp_of]
let lhs (bp, lo, hi) = Unstring.list ~bounds:(lo, hi) (Blueprint.to_unstring bp)
let rhs (bp, lo, hi) =
Unstring.fold ~bounds:(lo, hi) (Blueprint.to_unstring bp) ~init:[] ~f:(fun xs x ->
x :: xs)
|> Unstring.map ~f:List.rev
;;
let matching_string_gen (bp, lo, hi) =
Blueprint.repeat_matching_string_gen ~sep:None ~bounds:(Some (lo, hi)) bp
;;
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let%expect_test "lit s ≡ char-by-char via map2" =
let module Test =
Make (struct
type testcase = string [@@deriving quickcheck, sexp_of]
type result = unit [@@deriving compare, sexp_of]
let lhs s = Unstring.lit s
let rhs s =
List.fold (String.to_list s) ~init:(Unstring.return ()) ~f:(fun acc c ->
Unstring.map2 acc (Unstring.char (Char c)) ~f:(fun () () -> ()))
;;
let matching_string_gen s = Some (Quickcheck.Generator.return s)
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;
let rec shape_to_unit_unstring (t : Unstring.Printable_shape.t) : unit Unstring.t =
match t with
| Literal s -> Unstring.lit s
| Terminal cf -> Unstring.char cf
| Capture t -> shape_to_unit_unstring t
| Union ts -> Unstring.choice (List.map ts ~f:shape_to_unit_unstring)
| Concat ts -> Unstring.all_unit (List.map ts ~f:shape_to_unit_unstring)
| Star t -> Unstring.star (shape_to_unit_unstring t)
;;
let%expect_test "shape round-trip: t ≡ shape(t) reconstructed" =
let module Test =
Make (struct
type testcase = Blueprint.t [@@deriving quickcheck, sexp_of]
type result = unit [@@deriving compare, sexp_of]
let lhs bp = Blueprint.to_unit_unstring bp
let rhs bp =
Blueprint.to_unit_unstring bp
|> Unstring.to_printable_shape
|> shape_to_unit_unstring
;;
let matching_string_gen bp = Blueprint.matching_string_gen bp
end)
in
let%quick_test prop (test : Test.t) = Test.validate test in
[%expect {| |}]
;;