lambdaTests.ml 23.5 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
76
77
  "Test CDuce.lambda.identity_applied failed",
  "Apply(Abstraction(Dummy,Dummy,,,,Sel(Env(1),(`$A -> `$A),Comp(Id,{ { `$A = 
Int
 } })),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
89
90
91
92
93
94
95
96
(* 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
97
98
      Printf.eprintf "File %s, line %d, characters %d-%d:\n" (Loc.file_name loc)
	l cbegin cend; raise exn
99
    | e -> Printf.eprintf "Runtime error.\n"; raise e
100

101
let tests_eval = "CDuce runtime tests (Typed -> Lambda -> Value)" >:::
102
103
  [
    "abstr" >:: ( fun test_ctxt ->
104
105
106
      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");
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      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");
121
      assert_equal ~msg:"Test CDuce.runtime.abstr.simple failed"
122
	~printer:(fun x -> x) "Abstraction((Int,Int),Sel(1,(Int -> Int),Id))"
123
	(run_test_eval "fun f x : Int : Int -> 2");
124
      assert_equal ~msg:"Test CDuce.runtime.abstr.medium failed"
125
	~printer:(fun x -> x)
126
127
	"Abstraction((Int,[ Char* ] -> [ Int Char* ]),Sel(1,(Int -> [ Char* ] -> 
                                                           [ Int Char* ]),Id))"
128
	(run_test_eval "fun f x : Int y : String : (Int*String) -> x,y");
129
130
131
132
    );

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

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

290
291
292
293
294
295
296
297
298
    "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");
    );

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

    "string" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.string.simple failed"
323
	~printer:(fun x -> x) "\"The cake is a lie\""
324
	(run_test_eval "\"The cake is a lie\"");
325
326
    );

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

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

416
417
    "union_precise" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.union_precise.identity failed"
418
419
	~printer:(fun x -> x) "Abstraction((Int,Int),([ Char* ],[ Char* ]),Sel(1,(Int -> Int),([ Char* ] -> 
[ Char* ]),Id))"
420
421
422
	(run_test_eval "fun ((Int -> Int) & (String -> String))
                          | x : (Int | String) -> x");
      assert_equal ~msg:"Test CDuce.runtime.union_precise.match failed"
423
424
	~printer:(fun x -> x) "Abstraction((Int,Int),([ Char* ],[ Char* ]),Sel(1,(Int -> Int),([ Char* ] -> 
[ Char* ]),Id))"
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
      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
446
	~printer:(fun x -> x)
447
448
449
450
	"Abstraction((`$A,`$A),Sel(1,(`$A -> `$A),Comp(Id,{ { `$A = Int
 } ,{ `$A = [ Char* ]
 } })))"
	(run_test_eval "(fun f x : 'A : 'A -> x)[{A/Int},{A/String}]");
451
452
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
453
	(run_test_eval "((fun f x : 'A : 'A -> x)[{A/Int},{A/String}]).2");
454
455
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
456
	(run_test_eval "((fun f x : 'A : 'A -> x)[{A/Int}]).2");
Julien Lopez's avatar
Julien Lopez committed
457
458
      assert_equal ~msg:"Test CDuce.runtime.poly.tail failed"
	~printer:(fun x -> x)
459
	"Abstraction(([ `$A* ],[ `$A* ]),Sel(1,([ `$A* ] -> [ `$A* ]),Id))"
460
461
	(run_test_eval "fun tail x : ['A] : ['A] -> match x : ['A] with
                     | (_ : 'A) :: (rest : ['A]) -> rest");
Julien Lopez's avatar
Julien Lopez committed
462
      assert_equal ~msg:"Test CDuce.runtime.poly.tail_applied failed"
463
	~printer:(fun x -> x) "(7,(8,(5,Atom(nil),Id),Id),Id)"
464
465
	(run_test_eval "(fun tail x : ['A] : ['A] -> match x : ['A] with
                     | (_ : 'A) :: (rest : ['A]) -> rest).[3; 7; 8; 5]");
466

467
468
    );

469
470
  ]

Pietro Abate's avatar
Pietro Abate committed
471
472
473
474
let _ =
  run_test_tt_main (
    test_list
      [ tests_compile;
475
        tests_eval
Pietro Abate's avatar
Pietro Abate committed
476
477
478
      ]
  )
;;