lambdaTests.ml 23.4 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
  "Test CDuce.lambda.identity_applied failed",
75
76
  "Apply(PolyAbstraction(Dummy,Dummy,,,,Sel(Env(1),(`$A -> `$A),{ { `$A = 
Int
77
 } }),Env(1)),Const(2))",
78
  "(fun f x : 'A : 'A -> x)[{A/Int}].2";
Pietro Abate's avatar
Pietro Abate committed
79
80
];;

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

84
85
86
87
88
(* 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
89
90
91
92
93
94
95
    let lambdaexpr,lsize = Compile.compile_expr env texpr in
    Format.printf "Input : %s\n" str;
    Format.printf "Lambda : %s\n" (Printer.lambda_to_string lambdaexpr);
    let evalexpr = Eval.expr lambdaexpr lsize in
    let v = Printer.value_to_string evalexpr in
    Format.printf "Eval : %s\n\n" v;
    v
96
97
98
99
100
101
  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
102
103
      Printf.eprintf "File %s, line %d, characters %d-%d:\n" (Loc.file_name loc)
	l cbegin cend; raise exn
104
    | e -> Printf.eprintf "Runtime error.\n"; raise e
105

106
let tests_eval = "CDuce runtime tests (Typed -> Lambda -> Value)" >:::
107
108
  [
    "abstr" >:: ( fun test_ctxt ->
109
110
111
      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");
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
      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");
127
      assert_equal ~msg:"Test CDuce.runtime.abstr.simple failed"
128
	~printer:(fun x -> x) "Abstraction((Int,Int),Mono)"
129
	(run_test_eval "fun f x : Int : Int -> 2");
130
      assert_equal ~msg:"Test CDuce.runtime.abstr.medium failed"
131
	~printer:(fun x -> x)
132
	"Abstraction((Int,[ Char* ] -> [ Int Char* ]),Mono)"
133
	(run_test_eval "fun f x : Int y : String : (Int*String) -> x,y");
134
135
136
137
    );

    "apply" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.apply.simple failed"
138
	~printer:(fun x -> x) "2"
139
	(run_test_eval "(fun f x : Int : Int -> x).2");
140
      assert_equal ~msg:"Test CDuce.runtime.apply.simple_pair failed"
141
	~printer:(fun x -> x) "(3,2,Mono)"
142
	(run_test_eval "(fun f x : (Int*Int) : (Int*Int) -> x).(3,2)");
143
      assert_equal ~msg:"Test CDuce.runtime.apply.medium failed"
144
	~printer:(fun x -> x) "(2,3,Mono)"
145
	(run_test_eval "((fun f x : Int y : Int : (Int*Int) -> x,y).2).3");
146
147
148
    );

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

294
295
296
297
298
299
300
301
302
    "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");
    );

303
304
    "match" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.match.simple failed"
305
	~printer:(fun x -> x) "1"
306
	(run_test_eval "match 1 : Int with | 1 -> 1");
307
      assert_equal ~msg:"Test CDuce.runtime.match.simple_var failed"
308
	~printer:(fun x -> x) "2"
309
	(run_test_eval "(fun f x : Int : Int ->
310
                      match x : Int with | _ : Int -> x).2");
311
      assert_equal ~msg:"Test CDuce.runtime.match.medium failed"
312
	~printer:(fun x -> x) "2"
313
	(run_test_eval "(fun f x : Int : Int ->
314
                      match x : Int with | 1 -> 3 | _ : Int -> x).2");
315
      assert_equal ~msg:"Test CDuce.runtime.match.rec failed"
316
	~printer:(fun x -> x) "3"
317
	(run_test_eval "(fun f x : Int : Int ->
318
                      match x : Int with | 1 -> 3 | _ : Int -> f.1).2");
319
320
321
322
      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");
323
324
325
326
    );

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

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

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

418
419
    "union_precise" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.union_precise.identity failed"
420
	~printer:(fun x -> x) "Abstraction((Int,Int),([ Char* ],[ Char* ]),Mono)"
421
422
423
	(run_test_eval "fun ((Int -> Int) & (String -> String))
                          | x : (Int | String) -> x");
      assert_equal ~msg:"Test CDuce.runtime.union_precise.match failed"
424
	~printer:(fun x -> x) "Abstraction((Int,Int),([ Char* ],[ Char* ]),Mono)"
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
	(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\"");
    );

440
    "poly" >:: ( fun test_ctxt ->
Julien Lopez's avatar
Julien Lopez committed
441
442
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_pure failed"
	~printer:(fun x -> x)
443
	"Abstraction((`$A,`$A),Sel(1,(`$A -> `$A),Id))"
444
	(run_test_eval "fun f x : 'A : 'A -> x");
445

446
      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
447
	~printer:(fun x -> x)
448
449
	"Abstraction((`$A,`$A),Sel(1,(`$A -> `$A),{ { `$A = Int } ,{ `$A = [ Char* ]
 } }))"
450
	(run_test_eval "(fun f x : 'A : 'A -> x)[{A/Int},{A/String}]");
451

452
453
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
454
	(run_test_eval "((fun f x : 'A : 'A -> x)[{A/Int},{A/String}]).2");
455

456
457
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
458
	(run_test_eval "((fun f x : 'A : 'A -> x)[{A/Int}]).2");
459

Julien Lopez's avatar
Julien Lopez committed
460
461
      assert_equal ~msg:"Test CDuce.runtime.poly.tail failed"
	~printer:(fun x -> x)
462
      "Abstraction(([ `$A* ],[ `$A* ]),Sel(1,([ `$A* ] -> [ `$A* ]),Id))"
463
464
	(run_test_eval "fun tail x : ['A] : ['A] -> match x : ['A] with
                     | (_ : 'A) :: (rest : ['A]) -> rest");
465

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),Mono),Mono),Mono)"
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;
479
        tests_eval
Pietro Abate's avatar
Pietro Abate committed
480
481
482
      ]
  )
;;