lambdaTests.ml 24.9 KB
Newer Older
1
open OUnit2
Pietro Abate's avatar
Pietro Abate committed
2
3
open Camlp4.PreCast 

Pietro Abate's avatar
Pietro Abate committed
4
(* Typed -> Lambda *)
5
let run_test_compile msg expected totest =
Pietro Abate's avatar
Pietro Abate committed
6
7
8
9
  let aux str =
    try
      let expr = Parse.ExprParser.of_string_no_file str in
      let env, texpr = Compute.to_typed expr in
Pietro Abate's avatar
Pietro Abate committed
10
      Format.printf "Computed Typed -> %s%!@." (Printer.typed_to_string texpr);
Pietro Abate's avatar
Pietro Abate committed
11
12
13
14
15
      let lambdaexpr = Compile.compile env texpr in
      Printer.lambda_to_string lambdaexpr
    with
      | Compute.Error -> exit 3
      | Loc.Exc_located (loc, exn) ->
16
17
18
19
20
	let l = Loc.start_line loc in
	let cbegin = Loc.start_off loc - Loc.start_bol loc in
	let cend = Loc.stop_off loc - Loc.start_bol loc in
	Printf.eprintf "File %s, line %d, characters %d-%d:\n" (Loc.file_name loc) l
	  cbegin cend; raise exn
Pietro Abate's avatar
Pietro Abate committed
21
22
      | e -> Printf.eprintf "Runtime error.\n"; raise e
  in
23
  fun _ -> assert_equal ~msg:msg ~printer:(fun x -> x) expected (aux totest)
Pietro Abate's avatar
Pietro Abate committed
24
25

let tests_poly_abstr = [
26
27
28
  "Test CDuce.lambda.const_abstr failed",
  "Abstraction(Dummy,,,,Sel(,(Int -> Int),{}))",
  "fun f x : Int : Int -> 2";
Pietro Abate's avatar
Pietro Abate committed
29

30
  "Test CDuce.lambda.poly.identity failed",
31
32
33
  "Abstraction(Dummy,,,,Sel(,([ Char* ] | Int -> [ Char* ] | Int),Comp({},{ { `$A = 
Int } ,{ `$A = [ Char* ]
 } })))",
34
  "(fun f x : 'A : 'A -> x) [{A/Int},{A/String}]";
Pietro Abate's avatar
Pietro Abate committed
35
36

  "Test CDuce.runtime.poly.tail failed",
37
38
39
40
41
42
43
  "Abstraction(Dummy,,,,Sel(,([ (`$A & Int | Char | Atom | (Any,Any) |
                             <(Any) (Any)>Any | Arrow)* ] -> [ (`$A & Int |
                                                               Char | 
                                                               Atom |
                                                               (Any,Any) |
                                                               <(Any) (Any)>Any |
                                                               Arrow)* ]),{}))",
44
  "fun tail x : ['A] : ['A] -> match x : ['A] with | (el : 'A) :: (rest : ['A]) -> rest";
Pietro Abate's avatar
Pietro Abate committed
45

46
47
48
49
  "Test CDuce.runtime.poly.pair failed", "Abstraction(Dummy,,,,Sel(,((`$A & Int | Char | Atom | (Any,Any) |
                           <(Any) (Any)>Any | Arrow,`$B & Int | Char | 
                           Atom | (Any,Any) | <(Any) (Any)>Any | Arrow) -> 
`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow),{}))",
50
  "fun pair x : ('A * 'B) : 'A -> match x : ('A * 'B) with | (z : 'A, y : 'B) -> z";
51

52
53
54
55
56
57
58
59
60
  "Test CDuce.runtime.poly.match_abstr failed", "Apply(Match(Abstraction(Dummy,,,,Sel(,(`$A & Int | Char | Atom | (Any,Any) |
                                       <(Any) (Any)>Any | Arrow -> `$A & Int |
                                                                   Char |
                                                                   Atom |
                                                                   (Any,Any) |
                                                                   <(Any) (Any)>Any |
                                                                   Arrow),{})), {accept_chars=false; brs_disp=<disp>; brs_rhs=[| (2, TVar(Local(0),Comp({},{ { `$A = 
Int
 } }))) |]; brs_stack_pos=0}),Const(3))",
61
  "(match (fun f x : 'A : 'A -> x) : ('A -> 'A) with | y : ('A -> 'A) -> y[{A/Int}]).3";
62
63
64



Pietro Abate's avatar
Pietro Abate committed
65
66
]

Pietro Abate's avatar
Pietro Abate committed
67
let tests_poly_abstr = [
68
  (*
Pietro Abate's avatar
Pietro Abate committed
69
70
  "Test CDuce.lambda.const_abstr failed",
  "Abstraction(Dummy,,,,Sel((Int -> Int),{}))",
71
  "fun f x : 'A : 'A -> 2";
72
  *)
Pietro Abate's avatar
Pietro Abate committed
73
74
75

  "Test CDuce.lambda.const_abstr failed",
  "Abstraction(Dummy,,,,Sel(,(Int -> Int),{}))",
76
  "(fun f x : 'A : 'A -> x)[{A/Int}].2";
Pietro Abate's avatar
Pietro Abate committed
77
78
];;

79
80
let tests_compile = "CDuce compile tests (Typed -> Lambda )" >:::
  List.map (fun (m,e,f) -> f >:: run_test_compile m e f) tests_poly_abstr
81

82
83
84
85
86
87
88
89
90
91
92
93
94
(* Typed -> Lambda -> Value *)
let run_test_eval str =
  try
    let expr = Parse.ExprParser.of_string_no_file str in
    let env, texpr = Compute.to_typed expr in
    let evalexpr = Compile.compile_eval_expr env texpr in
    Printer.value_to_string evalexpr
  with
    | Compute.Error -> exit 3
    | Loc.Exc_located (loc, exn) ->
      let l = Loc.start_line loc in
      let cbegin = Loc.start_off loc - Loc.start_bol loc in
      let cend = Loc.stop_off loc - Loc.start_bol loc in
95
96
      Printf.eprintf "File %s, line %d, characters %d-%d:\n" (Loc.file_name loc)
	l cbegin cend; raise exn
97
    | e -> Printf.eprintf "Runtime error.\n"; raise e
98

99
let tests_eval = "CDuce runtime tests (Typed -> Lambda -> Value)" >:::
100
101
  [
    "abstr" >:: ( fun test_ctxt ->
102
103
104
      assert_equal ~msg:"Test CDuce.runtime.abstr.let_simple failed"
	~printer:(fun x -> x) "3"
	(run_test_eval "let x : Int = 3 in x : Int");
105
106
107
108
109
110
111
112
113
114
115
116
117
118
      assert_equal ~msg:"Test CDuce.runtime.abstr.let_medium failed"
	~printer:(fun x -> x) "2"
	(run_test_eval "let f : (Int -> Int) = (fun (Int -> Int) | x : Int -> x)
                        in (f.f.2) : Int");
      assert_equal ~msg:"Test CDuce.runtime.abstr.let_nested_simple failed"
	~printer:(fun x -> x) "3"
	(run_test_eval "let f : (Int -> Int) = (fun (Int -> Int) | x : Int -> x + 1)
                        in (let x : Int = f.2
                            in x : Int) : Int");
      assert_equal ~msg:"Test CDuce.runtime.abstr.let_nested_medium failed"
	~printer:(fun x -> x) "4"
	(run_test_eval "let f : (Int -> Int) = (fun (Int -> Int) | x : Int -> x + 1)
                        in (let x : Int = f.2
                            in f.x : Int) : Int");
119
      assert_equal ~msg:"Test CDuce.runtime.abstr.simple failed"
120
	~printer:(fun x -> x) "Abstraction((Int, Int),{})"
121
	(run_test_eval "fun f x : Int : Int -> 2");
122
      assert_equal ~msg:"Test CDuce.runtime.abstr.medium failed"
123
	~printer:(fun x -> x)
124
	"Abstraction((Int, [ Char* ] -> [ Int Char* ]),{})"
125
	(run_test_eval "fun f x : Int y : String : (Int*String) -> x,y");
126
127
128
129
    );

    "apply" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.apply.simple failed"
130
	~printer:(fun x -> x) "2"
131
	(run_test_eval "(fun f x : Int : Int -> x).2");
132
      assert_equal ~msg:"Test CDuce.runtime.apply.simple_pair failed"
133
	~printer:(fun x -> x) "(3, 2, {})"
134
	(run_test_eval "(fun f x : (Int*Int) : (Int*Int) -> x).(3,2)");
135
      assert_equal ~msg:"Test CDuce.runtime.apply.medium failed"
136
	~printer:(fun x -> x) "(2, 3, {})"
137
	(run_test_eval "((fun f x : Int y : Int : (Int*Int) -> x,y).2).3");
138
139
140
    );

    "misc" >:: ( fun test_ctxt ->
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
      assert_equal ~msg:"Test CDuce.runtime.misc.even failed"
	~printer:(fun x -> x)
	"Abstraction((Int, Bool) ,(Any \\ (Int), Any \\ (Int)),{})"
	(run_test_eval "fun ((Int -> Bool) & ((!Int) -> (!Int)))
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
                          | x : (!Int) -> x");
      assert_equal ~msg:"Test CDuce.runtime.misc.even_applied1 failed"
	~printer:(fun x -> x)
	"Atom(false)"
	(run_test_eval "(fun ((Int -> Bool) & ((!Int) -> (!Int)))
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
                          | x : (!Int) -> x).5");
      assert_equal ~msg:"Test CDuce.runtime.misc.even_applied2 failed"
	~printer:(fun x -> x)
	"Atom(true)"
	(run_test_eval "(fun ((Int -> Bool) & ((!Int) -> (!Int)))
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
                          | x : (!Int) -> x).8");
      assert_equal ~msg:"Test CDuce.runtime.misc.even_applied3 failed"
	~printer:(fun x -> x)
167
	"(2, (3, Atom(nil), {}), {})"
168
169
170
171
172
	(run_test_eval "(fun ((Int -> Bool) & ((!Int) -> (!Int)))
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
                          | x : (!Int) -> x).[2; 3]");
173
      assert_equal ~msg:"Test CDuce.runtime.misc.is_int failed"
174
	~printer:(fun x -> x)
175
176
	"Abstraction((Int, Bool) ,(Bool, Bool) ,(Any \\ (Int | Bool), Any \\ (Int | Bool)),{})"
	(run_test_eval "fun ((Int -> Bool) & (Bool -> Bool) & ((!(Int|Bool)) -> (!(Int|Bool))))
177
                          | x : Int -> `true
178
179
                          | x : Bool -> `false
                          | x : (!(Int|Bool)) -> x");
180
181
182
      assert_equal ~msg:"Test CDuce.runtime.misc.is_int_applied1 failed"
	~printer:(fun x -> x)
	"Atom(true)"
183
	(run_test_eval "(fun ((Int -> Bool) & (Bool -> Bool) & ((!(Int|Bool)) -> (!(Int|Bool))))
184
                           | x : Int -> `true
185
186
                           | x : Bool -> `false
                           | x : (!(Int|Bool)) -> x).2");
187
188
189
      assert_equal ~msg:"Test CDuce.runtime.misc.is_int_applied2 failed"
	~printer:(fun x -> x)
	"Atom(false)"
190
	(run_test_eval "(fun ((Int -> Bool) & (Bool -> Bool) & ((!(Int|Bool)) -> (!(Int|Bool))))
191
                           | x : Int -> `true
192
193
                           | x : Bool -> `false
                           | x : (!(Int|Bool)) -> x).`true");
194
195
      assert_equal ~msg:"Test CDuce.runtime.misc.is_int_applied3 failed"
	~printer:(fun x -> x)
196
	"(2, (3, Atom(nil), {}), {})"
197
	(run_test_eval "(fun ((Int -> Bool) & (Bool -> Bool) & ((!(Int|Bool)) -> (!(Int|Bool))))
198
                           | x : Int -> `true
199
200
                           | x : Bool -> `false
                           | x : (!(Int|Bool)) -> x).[2; 3]");
201
      assert_equal ~msg:"Test CDuce.runtime.misc.map failed"
202
	~printer:(fun x -> x)
203
204
205
206
207
208
209
210
	"Abstraction(((`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow) -> 
             (`$B & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | 
             Arrow), [ (`$A & Int | Char | Atom | (Any,Any) |
                       <(Any) (Any)>Any | Arrow)* ] -> [ (`$B & Int | 
                                                         Char | Atom |
                                                         (Any,Any) |
                                                         <(Any) (Any)>Any |
                                                         Arrow)* ]),{})"
211
212
213
214
	(run_test_eval "fun map f : ('A -> 'B) x : ['A] : ['B] ->
                          match x : ['A] with
                            | (el : 'A) :: [] -> f.el
                            | (el : 'A) :: (rest : ['A]) -> ((f.el), ((map.f).rest))");
215
216
      assert_equal ~msg:"Test CDuce.runtime.misc.map_even_simple failed"
	~printer:(fun x -> x)
217
	"(\"hey\", (Atom(false), Atom(nil), {}), {})"
Julien Lopez's avatar
Julien Lopez committed
218
219
220
	(run_test_eval "(fun map f : ('A[{A/Int},{A/Bool}]->'B[{A/Int},{A/Bool}]) x : ['A[{A/Int},{A/Bool}]] : ['B[{A/Int},{A/Bool}]] ->
                          match x : ['A[{A/Int},{A/Bool}]] with
                            | (el : 'A[{A/Int},{A/Bool}]) :: (rest : ['A[{A/Int},{A/Bool}]]) -> ((f.el), ((map.f).rest))
221
                            | [] -> []).(fun ((Int -> Bool) & ((!Int) -> (!Int)))
222
223
224
225
226
227
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
                          | x : (!Int) -> x).[\"hey\"; 3]");
      assert_equal ~msg:"Test CDuce.runtime.misc.map_even_medium failed"
	~printer:(fun x -> x)
228
	"(Atom(true), (\"hey\", (Atom(false), (Atom(true), Atom(nil), {}), {}), {}), {})"
Julien Lopez's avatar
Julien Lopez committed
229
230
231
	(run_test_eval "(fun map f : ('A[{A/Int},{A/Bool}]->'B[{A/Int},{A/Bool}]) x : ['A[{A/Int},{A/Bool}]] : ['B[{A/Int},{A/Bool}]] ->
                          match x : ['A[{A/Int},{A/Bool}]] with
                            | (el : 'A[{A/Int},{A/Bool}]) :: (rest : ['A[{A/Int},{A/Bool}]]) -> ((f.el), ((map.f).rest))
232
                            | [] -> []).(fun ((Int -> Bool) & ((!Int) -> (!Int)))
233
234
235
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
236
                          | x : (!Int) -> x).[4; \"hey\"; 3; 2]");
237
238
239
240
241
242
243
244
245
      assert_equal ~msg:"Test CDuce.runtime.misc.map_even_hard failed"
	~printer:(fun x -> x)
	"(Atom(true), (\"hey\", ((3, (5, Atom(nil), {}), {}), (Atom(true), (Abstraction((
`$C & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow, `$C & Int |
                                                                Char | 
                                                                Atom |
                                                                (Any,Any) |
                                                                <(Any) (Any)>Any |
                                                                Arrow),{}), (Atom(false), Atom(nil), {}), {}), {}), {}), {}), {})"
Julien Lopez's avatar
Julien Lopez committed
246
247
248
	(run_test_eval "(fun map f : ('A[{A/Int},{A/Bool}]->'B[{A/Int},{A/Bool}]) x : ['A[{A/Int},{A/Bool}]] : ['B[{A/Int},{A/Bool}]] ->
                          match x : ['A[{A/Int},{A/Bool}]] with
                            | (el : 'A[{A/Int},{A/Bool}]) :: (rest : ['A[{A/Int},{A/Bool}]]) -> ((f.el), ((map.f).rest))
249
250
251
252
                            | [] -> []).(fun ((Int -> Bool) & ((!Int) -> (!Int)))
                          | x : Int -> (match (x % 2) : Int with
                                          | 0 -> `true
                                          | 1 -> `false)
253
                          | x : (!Int) -> x).[4; \"hey\"; [3; 5]; 2; (fun ('C -> 'C) | x : 'C -> x); 3+4]");
254
255
      assert_equal ~msg:"Test CDuce.runtime.misc.map_is_int_simple failed"
	~printer:(fun x -> x)
256
	"(Atom(false), (Atom(true), Atom(nil), {}), {})"
Julien Lopez's avatar
Julien Lopez committed
257
258
259
	(run_test_eval "(fun map f : ('A[{A/Int},{A/Bool}]->'B[{A/Int},{A/Bool}]) x : ['A[{A/Int},{A/Bool}]] : ['B[{A/Int},{A/Bool}]] ->
                          match x : ['A[{A/Int},{A/Bool}]] with
                            | (el : 'A[{A/Int},{A/Bool}]) :: (rest : ['A[{A/Int},{A/Bool}]]) -> ((f.el), ((map.f).rest))
260
                            | [] -> []).(fun ((Int -> Bool) & (Bool -> Bool) & ((!(Int|Bool)) -> (!(Int|Bool))))
261
262
263
264
265
                          | x : Int -> `true
                          | x : Bool -> `false
                          | x : (!(Int|Bool)) -> x).[`true; 3]");
      assert_equal ~msg:"Test CDuce.runtime.misc.map_is_int_medium failed"
	~printer:(fun x -> x)
266
	"(Atom(false), (Atom(true), (Atom(false), Atom(nil), {}), {}), {})"
Julien Lopez's avatar
Julien Lopez committed
267
268
269
	(run_test_eval "(fun map f : ('A[{A/Int},{A/Bool}]->'B[{A/Int},{A/Bool}]) x : ['A[{A/Int},{A/Bool}]] : ['B[{A/Int},{A/Bool}]] ->
                          match x : ['A[{A/Int},{A/Bool}]] with
                            | (el : 'A[{A/Int},{A/Bool}]) :: (rest : ['A[{A/Int},{A/Bool}]]) -> ((f.el), ((map.f).rest))
270
                            | [] -> []).(fun ((Int -> Bool) & (Bool -> Bool) & ((!(Int|Bool)) -> (!(Int|Bool))))
271
272
273
                          | x : Int -> `true
                          | x : Bool -> `false
                          | x : (!(Int|Bool)) -> x).[`true; 3; `true]");
274
      assert_equal ~msg:"Test CDuce.runtime.misc.firsts failed"
275
	~printer:(fun x -> x)
276
	"Abstraction(((Int,Int), X1 -> X1 where X1 = (Int,Int)),{})"
277
	(run_test_eval "fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
278
279
280
                     match x,y : ((Int*Int)*(Int*Int)) with
                       | (a : Int,_ : Int),(b : Int,_ : Int) -> a,b");
      assert_equal ~msg:"Test CDuce.runtime.misc.firsts_applied failed"
281
	~printer:(fun x -> x) "(5, 1, {})"
282
	(run_test_eval "((fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
283
284
285
                       match x,y : ((Int*Int)*(Int*Int)) with
                         | (a : Int,_ : Int),(b : Int,_ : Int) -> a,b)
                   .(5, 3)).(1, 4)");
Julien Lopez's avatar
Julien Lopez committed
286
      assert_equal ~msg:"Test CDuce.runtime.misc.applier failed"
287
	~printer:(fun x -> x) "Abstraction((Int, Int -> Int -> Int),{})"
288
	(run_test_eval "fun applier x : Int f : (Int->Int) : Int -> f.x");
Julien Lopez's avatar
Julien Lopez committed
289
      assert_equal ~msg:"Test CDuce.runtime.misc.applier_applied failed"
290
	~printer:(fun x -> x) "2"
291
	(run_test_eval "((fun applier x : Int f : (Int->Int) : Int ->
Julien Lopez's avatar
Julien Lopez committed
292
                       f.x).2).(fun g x : Int : Int -> x)");
293
294
    );

295
296
297
298
299
300
301
302
303
    "arith" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.arith.simple failed"
	~printer:(fun x -> x) "5"
	(run_test_eval "2+3");
      assert_equal ~msg:"Test CDuce.runtime.arith.complete failed"
	~printer:(fun x -> x) "1"
	(run_test_eval "2+5*7%2-8/4");
    );

304
305
    "match" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.match.simple failed"
306
	~printer:(fun x -> x) "1"
307
	(run_test_eval "match 1 : Int with | 1 -> 1");
308
      assert_equal ~msg:"Test CDuce.runtime.match.simple_var failed"
309
	~printer:(fun x -> x) "2"
310
	(run_test_eval "(fun f x : Int : Int ->
311
                      match x : Int with | _ : Int -> x).2");
312
      assert_equal ~msg:"Test CDuce.runtime.match.medium failed"
313
	~printer:(fun x -> x) "2"
314
	(run_test_eval "(fun f x : Int : Int ->
315
                      match x : Int with | 1 -> 3 | _ : Int -> x).2");
316
      assert_equal ~msg:"Test CDuce.runtime.match.rec failed"
317
	~printer:(fun x -> x) "3"
318
	(run_test_eval "(fun f x : Int : Int ->
319
                      match x : Int with | 1 -> 3 | _ : Int -> f.1).2");
320
321
322
323
      assert_equal ~msg:"Test CDuce.runtime.match.desugar_if failed"
	~printer:(fun x -> x) "0"
	(run_test_eval "((fun f x : Int y : Int : Int ->
                           match (x = y) : Bool with | `true -> 0 | `false -> 1).2).2");
324
325
326
327
    );

    "string" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.string.simple failed"
328
	~printer:(fun x -> x) "\"The cake is a lie\""
329
	(run_test_eval "\"The cake is a lie\"");
330
331
    );

Julien Lopez's avatar
Julien Lopez committed
332
333
334
    "list" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.list.simple failed"
	~printer:(fun x -> x) "1"
335
	(run_test_eval "match [1; 2] : [Int] with
336
337
                     | (el : Int) :: (_ : [Int]) -> el
                     | [] -> 3");
Julien Lopez's avatar
Julien Lopez committed
338
339
      assert_equal ~msg:"Test CDuce.runtime.list.simple2 failed"
	~printer:(fun x -> x) "3"
340
341
342
	(run_test_eval "match [] : [Int] with
                     | (el : Int) :: (_ : [Int]) -> el
                     | [] -> 3");
Julien Lopez's avatar
Julien Lopez committed
343
      assert_equal ~msg:"Test CDuce.runtime.list.tail failed"
344
	~printer:(fun x -> x) "Abstraction(([ Int* ], [ Int* ]),{})"
345
	(run_test_eval "fun tail x : [Int] : [Int] -> match x : [Int] with
346
                     | (_ : Int) :: (rest : [Int]) -> rest");
347
      assert_equal ~msg:"Test CDuce.runtime.list.tail.eval failed"
348
	~printer:(fun x -> x) "(2, (5, Atom(nil), {}), {})"
349
	(run_test_eval "(fun tail x : [Int] : [Int] -> match x : [Int] with
350
                     | (_ : Int) :: (rest : [Int]) -> rest).[1; 2; 5]");
351
      assert_equal ~msg:"Test CDuce.runtime.list.last failed"
352
	~printer:(fun x -> x) "7"
353
	(run_test_eval "(fun f x : [Int] : [Int] -> match x : [Int] with
354
                     | (el : Int) :: [] -> el
355
                     | (_ : Int) :: (rest : [Int]) -> f.rest).[1; 2; 5; 4; 8; 7]");
356
357
358
      assert_equal ~msg:"Test CDuce.runtime.list.plusone failed"
	~printer:(fun x -> x) "(2, (3, (6, (5, (9, 8, {}), {}), {}), {}), {})"
	(run_test_eval "(fun f x : [Int] : [Int] -> match x : [Int] with
359
360
                     | (el : Int) :: [] -> el+1
                     | (el : Int) :: (rest : [Int]) -> ((el+1), (f.rest))).[1; 2; 5; 4; 8; 7]");
361
362
363
364
365
      assert_equal ~msg:"Test CDuce.runtime.list.concat failed"
	~printer:(fun x -> x) "(1, (2, (5, (4, (8, (7, (2, (3, (4, Atom(nil), {}), {}), {}), {}), {}), {}), {}), {}), {})"
	(run_test_eval "(fun concat x : [[Int]] : [Int] -> match x : [[Int]] with
                     | (el : [Int]) :: [] -> el
                     | (el : [Int]) :: (rest : [[Int]]) -> (el@(concat.rest))).[[1; 2; 5; 4; 8; 7]; [2; 3; 4]]");
366
367
368
369
370
371
372
      assert_equal ~msg:"Test CDuce.runtime.list.length_easy failed"
	~printer:(fun x -> x) "6"
	(run_test_eval "(fun length x : [Int] : Int -> match x : [Int] with
                     | [] -> 0
                     | (el : Int) :: (rest : [Int]) -> length.rest + 1).[1; 2; 5; 4; 8; 7]");
      assert_equal ~msg:"Test CDuce.runtime.list.length_hard failed"
	~printer:(fun x -> x) "((6, 0, {}), 2, {})"
Julien Lopez's avatar
Julien Lopez committed
373
374
	(run_test_eval "let length : (['A[{A/Int},{A/Bool}]] -> Int) =
                          (fun f l : ['A[{A/Int},{A/Bool}]] : Int -> match l : ['A[{A/Int},{A/Bool}]] with
375
                             | [] -> 0
Julien Lopez's avatar
Julien Lopez committed
376
                             | (el : 'A[{A/Int},{A/Bool}]) :: (rest : ['A[{A/Int},{A/Bool}]]) -> f.rest + 1)
377
378
                        in
                        (length.[1; 2; 5; 4; 8; 7], length.[], length.[`true; 2]) : (Int*Int*Int)");
379
380
      assert_equal ~msg:"Test CDuce.runtime.list.nth failed"
	~printer:(fun x -> x) "5"
Julien Lopez's avatar
Julien Lopez committed
381
382
383
384
	(run_test_eval "(fun nth l : ['A[{A/(Int|Bool)}]] n : Int : 'A[{A/(Int|Bool)}] ->
                           match l : ['A[{A/(Int|Bool)}]] with
                             | (el : 'A[{A/(Int|Bool)}]) :: [] -> el
                             | (el : 'A[{A/(Int|Bool)}]) :: (rest : ['A[{A/(Int|Bool)}]]) ->
385
                               (if n = 0 then el else nth.rest.(n-1))).[1; 2; 5; `true; 2].2");
386
387
388
389
390
391
392
      assert_equal ~msg:"Test CDuce.runtime.list.rev failed"
	~printer:(fun x -> x) "(2, (Atom(true), (5, (2, (1, Atom(nil), {}), {}), {}), {}), {})"
	(run_test_eval "(fun rev l : ['A[{A/(Int|Bool)}]] : ['A[{A/(Int|Bool)}]] ->
                           match l : ['A[{A/(Int|Bool)}]] with
                             | (el : 'A[{A/(Int|Bool)}]) :: [] -> [el]
                             | (el : 'A[{A/(Int|Bool)}]) :: (rest : ['A[{A/(Int|Bool)}]]) ->
                               (rev.rest) @ [el]).[1; 2; 5; `true; 2]");
Julien Lopez's avatar
Julien Lopez committed
393
394
    );

395
396
    "union" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.union.identity failed"
397
	~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
398
	(run_test_eval "fun f x : (Int | String) : (Int | String) -> x");
399
      assert_equal ~msg:"Test CDuce.runtime.union.match failed"
400
	~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
401
	(run_test_eval "fun f x : (Int | String) : (Int | String) ->
402
                     match x : (Int | String) with
403
404
                       | _ : Int -> 2
                       | _ : String -> \"Piece of cake\"");
405
406
      assert_equal ~msg:"Test CDuce.runtime.union.match_applied failed"
	~printer:(fun x -> x) "2"
407
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
408
                      match x : (Int | String) with
409
410
                        | _ : Int -> 2
                        | _ : String -> \"Piece of cake\").5");
411
412
      assert_equal ~msg:"Test CDuce.runtime.union.match_applied2 failed"
	~printer:(fun x -> x) "\"Piece of cake\""
413
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
414
                      match x : (Int | String) with
415
416
                        | _ : Int -> 2
                        | _ : String -> \"Piece of cake\").\"test\"");
417
418
    );

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    "union_precise" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.union_precise.identity failed"
	~printer:(fun x -> x) "Abstraction((Int, Int) ,([ Char* ], [ Char* ]),{})"
	(run_test_eval "fun ((Int -> Int) & (String -> String))
                          | x : (Int | String) -> x");
      assert_equal ~msg:"Test CDuce.runtime.union_precise.match failed"
	~printer:(fun x -> x) "Abstraction((Int, Int) ,([ Char* ], [ Char* ]),{})"
	(run_test_eval "fun ((Int -> Int) & (String -> String))
                          | x : Int -> 2
                          | x : String -> \"Piece of cake\"");
      assert_equal ~msg:"Test CDuce.runtime.union_precise.match_applied failed"
	~printer:(fun x -> x) "2"
	(run_test_eval "(fun ((Int -> Int) & (String -> String))
                           | x : Int -> 2
                           | x : String -> \"Piece of cake\").5");
      assert_equal ~msg:"Test CDuce.runtime.union_precise.match_applied2 failed"
	~printer:(fun x -> x) "\"Piece of cake\""
	(run_test_eval "(fun ((Int -> Int) & (String -> String))
                           | x : Int -> 2
                           | x : String -> \"Piece of cake\").\"test\"");
    );

441
    "poly" >:: ( fun test_ctxt ->
Julien Lopez's avatar
Julien Lopez committed
442
443
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_pure failed"
	~printer:(fun x -> x)
444
445
	"Abstraction((`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow, 
`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow),{})"
446
	(run_test_eval "fun f x : 'A : 'A -> x");
447
      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
448
	~printer:(fun x -> x)
449
	"Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
Julien Lopez's avatar
Julien Lopez committed
450
451
	(run_test_eval "(fun f x : 'A[{A/Int},{A/String}] : 'A[{A/Int},{A/String}] -> x)
                   [{A/Int},{A/String}]");
452
453
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
Julien Lopez's avatar
Julien Lopez committed
454
455
	(run_test_eval "((fun f x : 'A[{A/Int},{A/String}] : 'A[{A/Int},{A/String}] -> x)
                   [{A/Int},{A/String}]).2");
456
457
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
Julien Lopez's avatar
Julien Lopez committed
458
	(run_test_eval "((fun f x : 'A[{A/String}] : 'A[{A/String}] -> x)[{A/String}]).2");
Julien Lopez's avatar
Julien Lopez committed
459
460
      assert_equal ~msg:"Test CDuce.runtime.poly.tail failed"
	~printer:(fun x -> x)
461
462
463
	"Abstraction(([ (`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any |
               Arrow)* ], [ (`$A & Int | Char | Atom | (Any,Any) |
                            <(Any) (Any)>Any | Arrow)* ]),{})"
464
465
	(run_test_eval "fun tail x : ['A] : ['A] -> match x : ['A] with
                     | (_ : 'A) :: (rest : ['A]) -> rest");
Julien Lopez's avatar
Julien Lopez committed
466
      assert_equal ~msg:"Test CDuce.runtime.poly.tail_applied failed"
467
	~printer:(fun x -> x) "(7, (8, (5, Atom(nil), {}), {}), {})"
468
469
	(run_test_eval "(fun tail x : ['A] : ['A] -> match x : ['A] with
                     | (_ : 'A) :: (rest : ['A]) -> rest).[3; 7; 8; 5]");
470

471
472
    );

473
474
  ]

Pietro Abate's avatar
Pietro Abate committed
475
476
477
478
let _ =
  run_test_tt_main (
    test_list
      [ tests_compile;
Pietro Abate's avatar
Pietro Abate committed
479
        (* tests_eval *)
Pietro Abate's avatar
Pietro Abate committed
480
481
482
      ]
  )
;;