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
(** Copyright 2021-2024, Kakadu and contributors *)

(** SPDX-License-Identifier: CC0-1.0 *)

let gen_identifier =
  QCheck.Gen.(
    map2
      (fun c cs -> String.make 1 c ^ String.concat "" (List.map (String.make 1) cs))
      (oneof_list [ 'a'; 'b'; 'c'; 'f'; 'g'; 'x'; 'y'; 'z' ])
      (list_size (0 -- 3) (oneof_list [ 'a'; 'x'; 'y'; 'z'; '0'; '1' ])))
;;

let gen_small_int = QCheck.Gen.(int_range (-100) 100)
let gen_binop = QCheck.Gen.oneof_list Ast.[ Add; Sub; Mul; Div; Eq; Lt; Gt; Le; Ge ]

let gen_expr =
  QCheck.Gen.(
    sized
    @@ fix (fun self n ->
      if n <= 0
      then
        oneof_weighted
          [ 3, map (fun i -> Ast.Const i) gen_small_int
          ; 2, map (fun v -> Ast.Var v) gen_identifier
          ]
      else
        oneof_weighted
          [ 1, map (fun i -> Ast.Const i) gen_small_int
          ; 1, map (fun v -> Ast.Var v) gen_identifier
          ; 2, map2 (fun v e -> Ast.Abs (v, e)) gen_identifier (self (n / 2))
          ; 2, map2 (fun e1 e2 -> Ast.App (e1, e2)) (self (n / 2)) (self (n / 2))
          ; ( 1
            , map3
                (fun op e1 e2 -> Ast.BinOp (op, e1, e2))
                gen_binop
                (self (n / 2))
                (self (n / 2)) )
          ; ( 1
            , map3
                (fun c t e -> Ast.If (c, t, e))
                (self (n / 2))
                (self (n / 2))
                (self (n / 2)) )
          ; ( 1
            , map3
                (fun v e1 e2 -> Ast.Let (v, e1, e2))
                gen_identifier
                (self (n / 2))
                (self (n / 2)) )
          ]))
;;

let test_print_parse =
  QCheck.Test.make
    ~count:1000
    ~name:"print then parse"
    (QCheck.make gen_expr)
    (fun expr ->
       let printed = Printast.show expr in
       match Parser.parse printed with
       | Ok parsed -> String.equal printed (Printast.show parsed)
       | Error e ->
         QCheck.Test.fail_reportf
           "Parse error for: %s\nError: %a"
           printed
           Parser.pp_error
           e)
;;

let test_parse_valid =
  QCheck.Test.make
    ~count:1
    ~name:"parse valid"
    (QCheck.Gen.oneof_list
       [ "42"; "(1 + 2)"; "x"; "(fun x -> x)"; "let x = 5 in x"; "if 1 then 2 else 3" ]
     |> QCheck.make)
    (fun s ->
      match Parser.parse s with
      | Ok _ -> true
      | Error _ -> QCheck.Test.fail_reportf "Failed: %s" s)
;;

let test_print_const =
  QCheck.Test.make ~count:100 ~name:"print const" (QCheck.make gen_small_int) (fun i ->
    String.equal (Printast.show (Ast.Const i)) (string_of_int i))
;;

let test_print_var =
  QCheck.Test.make ~count:100 ~name:"print var" (QCheck.make gen_identifier) (fun v ->
    String.equal (Printast.show (Ast.Var v)) v)
;;

let test_roundtrip =
  let cases =
    Ast.
      [ Const 42, "42"
      ; Var "x", "x"
      ; BinOp (Add, Const 1, Const 2), "(1 + 2)"
      ; App (Var "f", Const 5), "(f 5)"
      ]
  in
  QCheck.Test.make
    ~count:1
    ~name:"roundtrip"
    (QCheck.Gen.oneof_list cases |> QCheck.make)
    (fun (expr, exp) ->
      String.equal (Printast.show expr) exp
      &&
      match Parser.parse exp with
      | Ok _ -> true
      | Error _ -> false)
;;

let test_pp_verbose =
  QCheck.Test.make ~count:100 ~name:"pp_verbose" (QCheck.make gen_expr) (fun expr ->
    let buf = Buffer.create 256 in
    let fmt = Format.formatter_of_buffer buf in
    Printast.pp_verbose fmt expr;
    Format.pp_print_flush fmt ();
    match Parser.parse (Buffer.contents buf) with
    | Ok _ -> true
    | Error _ -> false)
;;

let test_show_compact =
  QCheck.Test.make ~count:50 ~name:"show compact" (QCheck.make gen_small_int) (fun i ->
    let expr = Ast.Const i in
    String.equal (Printast.show expr) (Printast.show ~compact:true expr))
;;

let test_binop_print =
  QCheck.Test.make ~count:1 ~name:"binop print" (QCheck.make gen_small_int) (fun _ ->
    let ops =
      Ast.
        [ Add, "+"
        ; Sub, "-"
        ; Mul, "*"
        ; Div, "/"
        ; Eq, "="
        ; Lt, "<"
        ; Gt, ">"
        ; Le, "<="
        ; Ge, ">="
        ]
    in
    List.for_all
      (fun (op, sym) ->
        let printed = Printast.show (Ast.BinOp (op, Const 1, Const 2)) in
        String.contains printed (String.get sym 0))
      ops)
;;

let test_if_print =
  QCheck.Test.make ~count:10 ~name:"if print" (QCheck.make gen_small_int) (fun i ->
    let expr = Ast.If (Const i, Const 1, Const 0) in
    let printed = Printast.show expr in
    String.contains printed 'i' && String.contains printed 'f')
;;

let test_let_print =
  QCheck.Test.make ~count:10 ~name:"let print" (QCheck.make gen_identifier) (fun x ->
    let expr = Ast.Let (x, Const 42, Var x) in
    let printed = Printast.show expr in
    String.contains printed 'l'
    && String.contains printed 'e'
    && String.contains printed 't')
;;

let test_letrec_print =
  QCheck.Test.make ~count:20 ~name:"letrec print" (QCheck.make gen_small_int) (fun n ->
    let expr = Ast.LetRec ("f", "n", Var "n", Const n) in
    let printed = Printast.show expr in
    String.contains printed 'r'
    && String.contains printed 'e'
    && String.contains printed 'c')
;;

let test_fix_print =
  QCheck.Test.make ~count:10 ~name:"fix print" (QCheck.make gen_identifier) (fun x ->
    let expr = Ast.Fix (Abs (x, Var x)) in
    let printed = Printast.show expr in
    String.contains printed 'f'
    && String.contains printed 'i'
    && String.contains printed 'x')
;;

let test_prim_print =
  QCheck.Test.make ~count:10 ~name:"prim print" (QCheck.make gen_small_int) (fun i ->
    let expr = Ast.Prim ("println_int", [ Const i ]) in
    let printed = Printast.show expr in
    String.contains printed 'p')
;;

let test_nested_if =
  QCheck.Test.make ~count:15 ~name:"nested if" (QCheck.make gen_small_int) (fun n ->
    let expr = Ast.If (Const n, If (Const 1, Const 2, Const 3), Const 4) in
    match Parser.parse (Printast.show expr) with
    | Ok _ -> true
    | Error _ -> false)
;;

let test_nested_let =
  QCheck.Test.make ~count:15 ~name:"nested let" (QCheck.make gen_identifier) (fun x ->
    let expr = Ast.Let (x, Const 1, Let ("y", Var x, Let ("z", Var "y", Var "z"))) in
    String.contains (Printast.show expr) 'l')
;;

let test_church_encodings =
  QCheck.Test.make ~count:1 ~name:"church encodings" QCheck.unit (fun () ->
    let open Ast in
    let terms =
      [ Abs ("x", Abs ("y", Var "x")) (* true *)
      ; Abs ("x", Abs ("y", Var "y")) (* false *)
      ; Abs ("f", Abs ("x", Var "x")) (* zero *)
      ; Abs ("f", Abs ("x", App (Var "f", Var "x"))) (* one *)
      ; Abs ("f", Abs ("x", App (Var "f", App (Var "f", Var "x")))) (* two *)
      ]
    in
    List.for_all (fun t -> String.length (Printast.show ~compact:true t) > 0) terms)
;;

let test_comparison_ops =
  QCheck.Test.make
    ~count:30
    ~name:"comparison ops"
    QCheck.(pair (make gen_small_int) (make gen_small_int))
    (fun (a, b) ->
      let ops = Ast.[ Eq, "="; Lt, "<"; Gt, ">"; Le, "<="; Ge, ">=" ] in
      List.for_all
        (fun (op, sym) ->
          let printed = Printast.show (BinOp (op, Const a, Const b)) in
          String.contains printed (String.get sym 0))
        ops)
;;

let test_all_binops =
  QCheck.Test.make
    ~count:50
    ~name:"all binops"
    QCheck.(pair (make gen_expr) (make gen_expr))
    (fun (e1, e2) ->
      let ops = Ast.[ Add; Sub; Mul; Div; Eq; Lt; Gt; Le; Ge ] in
      List.for_all (fun op -> String.length (Printast.show (BinOp (op, e1, e2))) > 0) ops)
;;

let test_multi_abs =
  QCheck.Test.make ~count:15 ~name:"multi abs" QCheck.unit (fun () ->
    let open Ast in
    let terms =
      [ Abs ("a", Abs ("b", Abs ("c", Var "a")))
      ; Abs ("a", Abs ("b", Abs ("c", Abs ("d", Var "a"))))
      ]
    in
    List.for_all (fun t -> String.length (Printast.show ~compact:true t) > 0) terms)
;;

let test_prim_multiple =
  QCheck.Test.make ~count:20 ~name:"prim multi" (QCheck.make gen_small_int) (fun n ->
    let prims =
      Ast.
        [ "println_int", [ Const n ]
        ; "print", [ Const n; Var "x" ]
        ; "debug", [ Var "a"; Var "b"; Const 0 ]
        ]
    in
    List.for_all
      (fun (name, args) -> String.length (Printast.show (Prim (name, args))) > 0)
      prims)
;;

let () =
  let suite =
    [ test_print_const
    ; test_print_var
    ; test_roundtrip
    ; test_parse_valid
    ; test_print_parse
    ; test_pp_verbose
    ; test_show_compact
    ; test_binop_print
    ; test_if_print
    ; test_let_print
    ; test_letrec_print
    ; test_fix_print
    ; test_prim_print
    ; test_nested_if
    ; test_nested_let
    ; test_church_encodings
    ; test_comparison_ops
    ; test_all_binops
    ; test_multi_abs
    ; test_prim_multiple
    ]
  in
  QCheck_base_runner.run_tests_main suite
;;