package alcobar

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file alcobar.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
(* Fix for OCaml 5.0 *)
let () = Random.init 42

type src =
  | Random of Random.State.t
  | Fd of Unix.file_descr
  | Recording of Random.State.t * Buffer.t

type state = {
  chan : src;
  buf : Bytes.t;
  mutable offset : int;
  mutable len : int;
}

type 'a printer = Format.formatter -> 'a -> unit

type 'a strat =
  | Choose of 'a gen list
  | Map : ('f, 'a) gens * 'f -> 'a strat
  | Bind : 'a gen * ('a -> 'b gen) -> 'b strat
  | Option : 'a gen -> 'a option strat
  | List : 'a gen -> 'a list strat
  | List1 : 'a gen -> 'a list strat
  | Array : 'a gen -> 'a array strat
  | Array1 : 'a gen -> 'a array strat
  | Unlazy of 'a gen Lazy.t
  | Primitive of (state -> 'a)
  | Print of 'a printer * 'a gen

and 'a gen = { strategy : 'a strat; small_examples : 'a list }

and ('k, 'res) gens =
  | [] : ('res, 'res) gens
  | ( :: ) : 'a gen * ('k, 'res) gens -> ('a -> 'k, 'res) gens

type nonrec +'a list = 'a list = [] | ( :: ) of 'a * 'a list

let unlazy f = { strategy = Unlazy f; small_examples = [] }

let fix f =
  let rec lazygen = lazy (f (unlazy lazygen)) in
  Lazy.force lazygen

let map (type f) (type a) (gens : (f, a) gens) (f : f) =
  {
    strategy = Map (gens, f);
    small_examples = (match gens with [] -> [ f ] | _ -> []);
  }

let dynamic_bind m f = { strategy = Bind (m, f); small_examples = [] }
let const x = map [] x

let choose gens =
  {
    strategy = Choose gens;
    small_examples = List.map (fun x -> x.small_examples) gens |> List.concat;
  }

let option gen = { strategy = Option gen; small_examples = [ None ] }
let list gen = { strategy = List gen; small_examples = [ [] ] }

let list1 gen =
  {
    strategy = List1 gen;
    small_examples = List.map (fun x -> [ x ]) gen.small_examples;
  }

let array gen = { strategy = Array gen; small_examples = [ [||] ] }

let array1 gen =
  {
    strategy = Array1 gen;
    small_examples = List.map (fun x -> [| x |]) gen.small_examples;
  }

let primitive f ex = { strategy = Primitive f; small_examples = [ ex ] }
let pair gena genb = map [ gena; genb ] (fun a b -> (a, b))

let concat_gen_list sep l =
  match l with
  | h :: t ->
      List.fold_left
        (fun acc e -> map [ acc; sep; e ] (fun acc sep e -> acc ^ sep ^ e))
        h t
  | [] -> const ""

let with_printer pp gen =
  { strategy = Print (pp, gen); small_examples = gen.small_examples }

let result gena genb =
  choose [ map [ gena ] (fun va -> Ok va); map [ genb ] (fun vb -> Error vb) ]

let pp = Format.fprintf
let pp_int ppf n = pp ppf "%d" n
let pp_int32 ppf n = pp ppf "%s" (Int32.to_string n)
let pp_int64 ppf n = pp ppf "%s" (Int64.to_string n)
let pp_float ppf f = pp ppf "%f" f
let pp_bool ppf b = pp ppf "%b" b
let pp_char ppf c = pp ppf "%c" c
let pp_uchar ppf c = pp ppf "U+%04x" (Uchar.to_int c)
let pp_string ppf s = pp ppf "%S" s

(* taken from OCaml stdlib *)
let pp_print_iter ~pp_sep iter pp_v ppf v =
  let is_first = ref true in
  let pp_v v =
    if !is_first then is_first := false else pp_sep ppf ();
    pp_v ppf v
  in
  iter pp_v v

let pp_list pv ppf l =
  pp ppf "@[<hv 1>[%a]@]"
    (pp_print_iter ~pp_sep:(fun ppf () -> pp ppf ";@ ") List.iter pv)
    l

let pp_array pv ppf a =
  pp ppf "@[<hv 1>[|%a|]@]"
    (pp_print_iter ~pp_sep:(fun ppf () -> pp ppf ";@ ") Array.iter pv)
    a

let pp_option pv ppf = function
  | None -> Format.fprintf ppf "None"
  | Some x -> Format.fprintf ppf "(Some %a)" pv x

exception BadTest of string
exception FailedTest of unit printer

let guard = function true -> () | false -> raise (BadTest "guard failed")
let bad_test () = raise (BadTest "bad test")
let nonetheless = function None -> bad_test () | Some a -> a

let get_data chan buf off len =
  match chan with
  | Random rand ->
      for i = off to off + len - 1 do
        Bytes.set buf i (Char.chr (Random.State.bits rand land 0xff))
      done;
      len - off
  | Recording (rand, record) ->
      for i = off to off + len - 1 do
        let c = Char.chr (Random.State.bits rand land 0xff) in
        Bytes.set buf i c;
        Buffer.add_char record c
      done;
      len - off
  | Fd ch -> Unix.read ch buf off len

let refill src =
  assert (src.offset <= src.len);
  let remaining = src.len - src.offset in
  (* move remaining data to start of buffer *)
  Bytes.blit src.buf src.offset src.buf 0 remaining;
  src.len <- remaining;
  src.offset <- 0;
  let read =
    get_data src.chan src.buf remaining (Bytes.length src.buf - remaining)
  in
  if read = 0 then raise (BadTest "premature end of file")
  else src.len <- remaining + read

let rec getbytes src n =
  assert (src.offset <= src.len);
  if n > Bytes.length src.buf then failwith "request too big";
  if src.len - src.offset >= n then (
    let off = src.offset in
    src.offset <- src.offset + n;
    off)
  else (
    refill src;
    getbytes src n)

let read_char src =
  let off = getbytes src 1 in
  Bytes.get src.buf off

let read_byte src = Char.code (read_char src)

let read_bool src =
  let n = read_byte src in
  n land 1 = 1

let bool = with_printer pp_bool (primitive read_bool false)
let uint8 = with_printer pp_int (primitive read_byte 0)
let int8 = with_printer pp_int (map [ uint8 ] (fun n -> n - 128))

let read_uint16 src =
  let off = getbytes src 2 in
  Bytes.get_uint16_le src.buf off

let read_int16 src =
  let off = getbytes src 2 in
  Bytes.get_int16_le src.buf off

let uint16 = with_printer pp_int (primitive read_uint16 0)
let int16 = with_printer pp_int (primitive read_int16 0)

let read_int32 src =
  let off = getbytes src 4 in
  Bytes.get_int32_le src.buf off

let read_int64 src =
  let off = getbytes src 8 in
  Bytes.get_int64_le src.buf off

let int32 = with_printer pp_int32 (primitive read_int32 0l)
let int64 = with_printer pp_int64 (primitive read_int64 0L)

let int =
  with_printer pp_int
    (if Sys.word_size <= 32 then map [ int32 ] Int32.to_int
     else map [ int64 ] Int64.to_int)

let float =
  with_printer pp_float
    (primitive
       (fun src ->
         let off = getbytes src 8 in
         let i64 = Bytes.get_int64_le src.buf off in
         Int64.float_of_bits i64)
       0.)

let char = with_printer pp_char (primitive read_char 'a')

(* maybe print as a hexdump? *)
let bytes =
  with_printer pp_string
    (primitive
       (fun src ->
         (* null-terminated, with '\001' as an escape code *)
         let buf = Bytes.make 64 '\255' in
         let rec read_bytes p =
           if p >= Bytes.length buf then p
           else
             match read_char src with
             | '\000' -> p
             | '\001' ->
                 Bytes.set buf p (read_char src);
                 read_bytes (p + 1)
             | c ->
                 Bytes.set buf p c;
                 read_bytes (p + 1)
         in
         let count = read_bytes 0 in
         Bytes.sub_string buf 0 count)
       "")

let bytes_fixed n =
  with_printer pp_string
    (primitive
       (fun src ->
         let off = getbytes src n in
         Bytes.sub_string src.buf off n)
       (String.make n 'a'))

let choose_int n state =
  assert (n > 0);
  if n = 1 then 0
  else if n <= 0x100 then read_byte state mod n
  else if n < 0x1000000 then
    Int32.(to_int (abs (rem (read_int32 state) (of_int n))))
  else Int64.(to_int (abs (rem (read_int64 state) (of_int n))))

let range ?(min = 0) n =
  if n <= 0 then
    raise (Invalid_argument "Alcobar.range: argument n must be positive");
  if min < 0 then
    raise
      (Invalid_argument "Alcobar.range: argument min must be positive or null");
  with_printer pp_int (primitive (fun s -> min + choose_int n s) min)

let uchar : Uchar.t gen =
  map
    [ range 0x110000 ]
    (fun x ->
      guard (Uchar.is_valid x);
      Uchar.of_int x)

let uchar = with_printer pp_uchar uchar

let rec sequence = function
  | g :: gs -> map [ g; sequence gs ] (fun x xs -> x :: xs)
  | [] -> const []

let shuffle_arr arr =
  let n = Array.length arr in
  let gs = List.init n (fun i -> range ~min:i (n - i)) in
  map [ sequence gs ] @@ fun js ->
  js
  |> List.iteri (fun i j ->
      let t = arr.(i) in
      arr.(i) <- arr.(j);
      arr.(j) <- t);
  arr

let shuffle l = map [ shuffle_arr (Array.of_list l) ] Array.to_list

exception GenFailed of exn * Printexc.raw_backtrace * unit printer

let rec generate : type a. int -> state -> a gen -> a * unit printer =
 fun size input gen ->
  if size <= 1 && gen.small_examples <> [] then
    (List.hd gen.small_examples, fun ppf () -> pp ppf "?")
  else
    match gen.strategy with
    | Choose gens ->
        (* FIXME: better distribution? *)
        (* FIXME: choices of size > 255? *)
        let n = choose_int (List.length gens) input in
        let v, pv = generate size input (List.nth gens n) in
        (v, fun ppf () -> pp ppf "#%d %a" n pv ())
    | Map ([], k) -> (k, fun ppf () -> pp ppf "?")
    | Map (gens, f) ->
        let rec len : type k res. int -> (k, res) gens -> int =
         fun acc xs -> match xs with [] -> acc | _ :: xs -> len (1 + acc) xs
        in
        let n = len 0 gens in
        (* the size parameter is (apparently?) meant to ensure that generation
        eventually terminates, by limiting the set of options from which the
        generator might choose once we've gotten deep into a tree.  make sure we
        always mark our passing, even when we've mapped one value into another,
        so we don't blow the stack. *)
        let size = (size - 1) / n in
        let v, pvs = gen_apply size input gens f in
        begin match v with
        | Ok v -> (v, pvs)
        | Error (e, bt) -> raise (GenFailed (e, bt, pvs))
        end
    | Bind (m, f) ->
        let index, pv_index = generate (size - 1) input m in
        let a, pv = generate (size - 1) input (f index) in
        (a, fun ppf () -> pp ppf "(%a) => %a" pv_index () pv ())
    | Option gen ->
        if size < 1 then (None, fun ppf () -> pp ppf "None")
        else if read_bool input then
          let v, pv = generate size input gen in
          (Some v, fun ppf () -> pp ppf "Some (%a)" pv ())
        else (None, fun ppf () -> pp ppf "None")
    | List gen ->
        let elems = generate_list size input gen in
        ( List.map fst elems,
          fun ppf () -> pp_list (fun ppf (_, pv) -> pv ppf ()) ppf elems )
    | List1 gen ->
        let elems = generate_list1 size input gen in
        ( List.map fst elems,
          fun ppf () -> pp_list (fun ppf (_, pv) -> pv ppf ()) ppf elems )
    | Array gen ->
        let elems = generate_list size input gen in
        let elems = Array.of_list elems in
        ( Array.map fst elems,
          fun ppf () -> pp_array (fun ppf (_, pv) -> pv ppf ()) ppf elems )
    | Array1 gen ->
        let elems = generate_list1 size input gen in
        let elems = Array.of_list elems in
        ( Array.map fst elems,
          fun ppf () -> pp_array (fun ppf (_, pv) -> pv ppf ()) ppf elems )
    | Primitive gen -> (gen input, fun ppf () -> pp ppf "?")
    | Unlazy gen -> generate size input (Lazy.force gen)
    | Print (ppv, gen) ->
        let v, _ = generate size input gen in
        (v, fun ppf () -> ppv ppf v)

and generate_list : type a. int -> state -> a gen -> (a * unit printer) list =
 fun size input gen ->
  if size <= 1 then []
  else if read_bool input then generate_list1 size input gen
  else []

and generate_list1 : type a. int -> state -> a gen -> (a * unit printer) list =
 fun size input gen ->
  let ans = generate (size / 2) input gen in
  ans :: generate_list (size / 2) input gen

and gen_apply : type k res.
    int ->
    state ->
    (k, res) gens ->
    k ->
    (res, exn * Printexc.raw_backtrace) result * unit printer =
 fun size state gens f ->
  let rec go : type k res.
      int ->
      state ->
      (k, res) gens ->
      k ->
      (res, exn * Printexc.raw_backtrace) result * unit printer list =
   fun size input gens ->
    match gens with
    | [] -> fun x -> (Ok x, [])
    | g :: gs ->
        fun f ->
          let v, pv = generate size input g in
          let res, pvs =
            match f v with
            | exception (BadTest _ as e) -> raise e
            | exception e -> (Error (e, Printexc.get_raw_backtrace ()), [])
            | fv -> go size input gs fv
          in
          (res, pv :: pvs)
  in
  let v, pvs = go size state gens f in
  let pvs =
   fun ppf () ->
    match pvs with
    | [ pv ] -> pv ppf ()
    | pvs -> pp_list (fun ppf pv -> pv ppf ()) ppf pvs
  in
  (v, pvs)

let fail s = raise (FailedTest (fun ppf () -> pp ppf "%s" s))
let failf format = Format.kasprintf fail format

let check = function
  | true -> ()
  | false -> raise (FailedTest (fun ppf () -> pp ppf "check false"))

let check_eq ?pp:pv ?cmp ?eq a b =
  let pass =
    match (eq, cmp) with
    | Some eq, _ -> eq a b
    | None, Some cmp -> cmp a b = 0
    | None, None -> Stdlib.compare a b = 0
  in
  if pass then ()
  else
    raise
      (FailedTest
         (fun ppf () ->
           match pv with
           | None -> pp ppf "different"
           | Some pv -> pp ppf "@[<hv>%a@ !=@ %a@]" pv a pv b))

let () = Printexc.record_backtrace true

type test =
  | Test : {
      suite : string;
      name : string;
      gens : ('f, unit) gens;
      f : 'f;
    }
      -> test

type test_status =
  | TestPass of unit printer
  | BadInput of string
  | GenFail of exn * Printexc.raw_backtrace * unit printer
  | TestExn of exn * Printexc.raw_backtrace * unit printer
  | TestFail of unit printer * unit printer

let run_once (gens : (_, unit) gens) f state =
  match gen_apply 100 state gens f with
  | Ok (), pvs -> TestPass pvs
  | Error (FailedTest p, _), pvs -> TestFail (p, pvs)
  | Error (e, bt), pvs -> TestExn (e, bt, pvs)
  | exception BadTest s -> BadInput s
  | exception GenFailed (e, bt, pvs) -> GenFail (e, bt, pvs)

let classify_status = function
  | TestPass _ -> `Pass
  | BadInput _ -> `Bad
  | GenFail _ -> `Fail (* slightly dubious... *)
  | TestExn _ | TestFail _ -> `Fail

let print_status ppf status =
  let print_ex ppf (e, bt) =
    pp ppf "%s" (Printexc.to_string e);
    bt |> Printexc.raw_backtrace_to_string |> String.split_on_char '\n'
    |> List.iter (pp ppf "@,%s")
  in
  match status with
  | TestPass pvs ->
      pp ppf "When given the input:@.@[<v 4>@,%a@,@]@.the test passed." pvs ()
  | BadInput s -> pp ppf "The testcase was invalid:@.%s" s
  | GenFail (e, bt, pvs) ->
      pp ppf
        "When given the input:@.@[<4>%a@]@.the testcase generator threw an \
         exception:@.@[<v 4>@,\
         %a@,\
         @]"
        pvs () print_ex (e, bt)
  | TestExn (e, bt, pvs) ->
      pp ppf
        "When given the input:@.@[<v 4>@,\
         %a@,\
         @]@.the test threw an exception:@.@[<v 4>@,\
         %a@,\
         @]"
        pvs () print_ex (e, bt)
  | TestFail (err, pvs) ->
      pp ppf
        "When given the input:@.@[<v 4>@,\
         %a@,\
         @]@.the test failed:@.@[<v 4>@,\
         %a@,\
         @]"
        pvs () err ()

let prng_state_of_seed seed =
  (* try to make this independent of word size *)
  let seed =
    Int64.
      [|
        to_int (logand (of_int 0xffff) seed);
        to_int (logand (of_int 0xffff) (shift_right seed 16));
        to_int (logand (of_int 0xffff) (shift_right seed 32));
        to_int (logand (of_int 0xffff) (shift_right seed 48));
      |]
  in
  Random.State.make seed

let src_of_seed seed = Random (prng_state_of_seed seed)

(* {1 Property-testing runner (via Alcotest)} *)

type config = {
  seed : int64 option;
  repeat : int;
  verbose : bool;
  infinite : bool;
  timeout : int;
  budget : float;
}

exception Timeout

let default_timeout =
  match Sys.getenv_opt "ALCOBAR_TIMEOUT" with
  | Some s -> ( try int_of_string s with _ -> 2)
  | None -> 2

let config_term =
  let open Cmdliner in
  let seed =
    let doc = "The seed (an int64) for the PRNG." in
    Arg.(value & opt (some int64) None & info [ "s"; "seed" ] ~doc)
  in
  let repeat =
    let doc = "The number of times to repeat each test." in
    Arg.(value & opt int 5000 & info [ "r"; "repeat" ] ~doc)
  in
  let verbose =
    let doc = "Print information on each passing test." in
    Arg.(value & flag & info [ "alcobar-verbose" ] ~doc)
  in
  let infinite =
    let doc = "Run until a failure is found." in
    Arg.(value & flag & info [ "i"; "infinite" ] ~doc)
  in
  let timeout =
    let doc =
      "Per-test timeout in seconds (0 to disable). Can also be set via the \
       ALCOBAR_TIMEOUT environment variable."
    in
    Arg.(value & opt int default_timeout & info [ "timeout" ] ~doc)
  in
  let budget =
    let doc =
      "Total time budget per test in seconds (0 to disable). Iteration stops \
       when the budget is exhausted."
    in
    Arg.(value & opt float 2. & info [ "budget" ] ~docv:"SECONDS" ~doc)
  in
  Term.(
    const (fun seed repeat verbose infinite timeout budget ->
        { seed; repeat; verbose; infinite; timeout; budget })
    $ seed $ repeat $ verbose $ infinite $ timeout $ budget)

let with_timeout timeout f =
  if timeout <= 0 then f ()
  else
    let old_handler =
      Sys.signal Sys.sigalrm (Sys.Signal_handle (fun _ -> raise Timeout))
    in
    let old_alarm = Unix.alarm timeout in
    Fun.protect
      ~finally:(fun () ->
        ignore (Unix.alarm old_alarm);
        Sys.set_signal Sys.sigalrm old_handler)
      f

let run_property_test (Test { gens; f; _ }) config =
  let seed =
    match config.seed with Some s -> s | None -> Random.int64 Int64.max_int
  in
  let seedsrc = prng_state_of_seed seed in
  let npass = ref 0 in
  let failure = ref None in
  let max_iter = if config.infinite then max_int else config.repeat in
  let start_time = Unix.gettimeofday () in
  let within_budget () =
    config.budget <= 0. || Unix.gettimeofday () -. start_time < config.budget
  in
  while !npass < max_iter && Option.is_none !failure && within_budget () do
    let s = Random.State.int64 seedsrc Int64.max_int in
    let state =
      { chan = src_of_seed s; buf = Bytes.make 256 '0'; offset = 0; len = 0 }
    in
    let status =
      try with_timeout config.timeout (fun () -> run_once gens f state)
      with Timeout ->
        TestExn
          ( Timeout,
            Printexc.get_raw_backtrace (),
            fun ppf () -> pp ppf "<timeout after %ds>" config.timeout )
    in
    match classify_status status with
    | `Pass ->
        incr npass;
        if config.verbose then Printf.printf "  pass %d\n%!" !npass
    | `Bad -> ()
    | `Fail -> failure := Some status
  done;
  match !failure with
  | None -> ()
  | Some status -> Alcotest.fail (Format.asprintf "%a" print_status status)

let run_with_alcotest name tests =
  let groups = Hashtbl.create 16 in
  List.iter
    (fun (Test { suite; name; _ } as test) ->
      let tc = Alcotest.test_case name `Quick (run_property_test test) in
      let prev = try Hashtbl.find groups suite with Not_found -> [] in
      Hashtbl.replace groups suite (tc :: prev))
    tests;
  let suites =
    Hashtbl.fold (fun group tcs acc -> (group, List.rev tcs) :: acc) groups []
    |> List.sort (fun (a, _) (b, _) -> String.compare a b)
  in
  Alcotest.run_with_args name config_term suites

(* {1 AFL runner} *)

exception TestFailure

let run_afl tests file =
  AflPersistent.run (fun () ->
      let fd = Unix.openfile file [ Unix.O_RDONLY ] 0o000 in
      let state =
        { chan = Fd fd; buf = Bytes.make 256 '0'; offset = 0; len = 0 }
      in
      let status =
        try
          let (Test { gens; f; _ }) =
            List.nth tests (choose_int (List.length tests) state)
          in
          run_once gens f state
        with BadTest s -> BadInput s
      in
      Unix.close fd;
      match classify_status status with
      | `Pass | `Bad -> ()
      | `Fail ->
          Printexc.record_backtrace false;
          raise TestFailure)

let detect_afl_file () =
  let n = Array.length Sys.argv in
  if n >= 2 then
    let last = Sys.argv.(n - 1) in
    if Sys.file_exists last then Some last else None
  else None

let detect_gen_corpus () =
  let n = Array.length Sys.argv in
  let rec find i =
    if i >= n then None
    else if Sys.argv.(i) = "--gen-corpus" && i + 1 < n then
      Some Sys.argv.(i + 1)
    else find (i + 1)
  in
  find 1

let generate_corpus dir tests =
  (try Unix.mkdir dir 0o755 with Unix.Unix_error (Unix.EEXIST, _, _) -> ());
  let seedsrc = prng_state_of_seed 42L in
  let count = ref 0 in
  let seeds_per_test = 5 in
  let max_attempts = 200 in
  List.iter
    (fun (Test { gens; f; _ }) ->
      let good = ref 0 in
      let attempts = ref 0 in
      while !good < seeds_per_test && !attempts < max_attempts do
        incr attempts;
        let s = Random.State.int64 seedsrc Int64.max_int in
        let record = Buffer.create 256 in
        let state =
          {
            chan = Recording (prng_state_of_seed s, record);
            buf = Bytes.make 256 '0';
            offset = 0;
            len = 0;
          }
        in
        let status = run_once gens f state in
        match classify_status status with
        | `Pass ->
            let filename = Printf.sprintf "%s/seed_%03d" dir !count in
            let oc = open_out_bin filename in
            Buffer.output_buffer oc record;
            close_out oc;
            incr count;
            incr good
        | _ -> ()
      done)
    tests;
  Printf.printf "gen-corpus: wrote %d seed files to %s/\n" !count dir

type test_case =
  | TC : { name : string; gens : ('f, unit) gens; f : 'f } -> test_case

let test_case name gens f = TC { name; gens; f }

let run name suites =
  let tests =
    List.concat_map
      (fun (suite_name, tcs) ->
        List.map
          (fun (TC { name; gens; f }) ->
            Test { suite = suite_name; name; gens; f })
          tcs)
      suites
  in
  match detect_gen_corpus () with
  | Some dir -> generate_corpus dir tests
  | None -> (
      match detect_afl_file () with
      | Some file -> run_afl tests file
      | None -> run_with_alcotest name tests)

module Syntax = struct
  let ( let* ) = dynamic_bind
  let ( let+ ) gen map_fn = map [ gen ] map_fn
  let ( and+ ) = pair
end