main.ml 11.4 KB
Newer Older
1
open OUnit2
Pietro Abate's avatar
Pietro Abate committed
2
3
4
open Camlp4.PreCast 

(* Typed -> Lamda *)
5
let run_test_compile msg expected totest =
Pietro Abate's avatar
Pietro Abate committed
6
7
8
9
10
11
12
13
14
  let aux str =
    try
      let expr = Parse.ExprParser.of_string_no_file str in
      let env, texpr = Compute.to_typed expr in
      let lambdaexpr = Compile.compile env texpr in
      Printer.lambda_to_string lambdaexpr
    with
      | Compute.Error -> exit 3
      | Loc.Exc_located (loc, exn) ->
15
16
17
18
19
	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
20
21
      | e -> Printf.eprintf "Runtime error.\n"; raise e
  in
22
  fun _ -> assert_equal ~msg:msg ~printer:(fun x -> x) expected (aux totest)
Pietro Abate's avatar
Pietro Abate committed
23
24

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

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

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

44
  "Test CDuce.runtime.poly.pair failed", "",
45
  "fun pair x : ('A{} * 'B{}) : 'A{} -> match x : ('A{} * 'B{}) with | (x : 'A{}, y : 'B{}) -> x";
46

47
48
  "Test CDuce.runtime.poly.match_abstr failed", "",
  "(match (fun f x : 'A{} : 'A{} -> x) : ('A{} -> 'A{}) with | y : ('A{} -> 'A{}) -> y{A/Int}).3";
49
50
51



Pietro Abate's avatar
Pietro Abate committed
52
53
]

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

57
58
59
60
61
62
63
64
65
66
67
68
69
(* 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
70
71
      Printf.eprintf "File %s, line %d, characters %d-%d:\n" (Loc.file_name loc)
	l cbegin cend; raise exn
72
    | e -> Printf.eprintf "Runtime error.\n"; raise e
73

74
let tests_eval = "CDuce runtime tests (Typed -> Lambda -> Value)" >:::
75
76
77
  [
    "abstr" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.abstr.simple failed"
78
	~printer:(fun x -> x) "Abstraction((Int, Int),{})"
79
	(run_test_eval "fun f x : Int : Int -> 2");
80
      assert_equal ~msg:"Test CDuce.runtime.abstr.medium failed"
81
	~printer:(fun x -> x)
82
	"Abstraction((Int, [ Char* ] -> [ Int Char* ]),{})"
83
	(run_test_eval "fun f x : Int y : String : (Int*String) -> x,y");
84
85
86
87
    );

    "apply" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.apply.simple failed"
88
	~printer:(fun x -> x) "2"
89
	(run_test_eval "(fun f x : Int : Int -> x).2");
90
      assert_equal ~msg:"Test CDuce.runtime.apply.simple_pair failed"
91
	~printer:(fun x -> x) "(3, 2, {})"
92
	(run_test_eval "(fun f x : (Int*Int) : (Int*Int) -> x).(3,2)");
93
      assert_equal ~msg:"Test CDuce.runtime.apply.medium failed"
94
	~printer:(fun x -> x) "(2, 3, {})"
95
	(run_test_eval "((fun f x : Int y : Int : (Int*Int) -> x,y).2).3");
96
97
98
    );

    "misc" >:: ( fun test_ctxt ->
99
      assert_equal ~msg:"Test CDuce.runtime.misc.is_int failed"
100
	~printer:(fun x -> x)
101
102
103
104
105
106
	"Abstraction(([ Char* ] | Int, Bool),{})"
	(run_test_eval "fun is_int x : (Int | String) : Bool ->
                          match x : (Int | String) with
                            | x : Int -> `true
                            | x : String -> `false");
      assert_equal ~msg:"Test CDuce.runtime.misc.map failed"
107
	~printer:(fun x -> x)
108
109
110
111
112
	"Abstraction(((Int,Int), X1 -> X1 where X1 = (Int,Int)),{})"
	(run_test_eval "fun map f : ('A{}->'B{}) x : ['A{}] : ['B{}] ->
                          match x : ['A{}] with
                            | (el : 'A{}) :: (rest : ['A{}]) -> [f.el; (map.f).rest]
                            | el : ['A{}] -> f.el");
113
      assert_equal ~msg:"Test CDuce.runtime.misc.firsts failed"
114
	~printer:(fun x -> x)
115
	"Abstraction(((Int,Int), X1 -> X1 where X1 = (Int,Int)),{})"
116
	(run_test_eval "fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
117
118
119
                     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"
120
	~printer:(fun x -> x) "(5, 1, {})"
121
	(run_test_eval "((fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
122
123
124
                       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
125
      assert_equal ~msg:"Test CDuce.runtime.misc.applier failed"
126
	~printer:(fun x -> x) "Abstraction((Int, Int -> Int -> Int),{})"
127
	(run_test_eval "fun applier x : Int f : (Int->Int) : Int -> f.x");
Julien Lopez's avatar
Julien Lopez committed
128
      assert_equal ~msg:"Test CDuce.runtime.misc.applier_applied failed"
129
	~printer:(fun x -> x) "2"
130
	(run_test_eval "((fun applier x : Int f : (Int->Int) : Int ->
Julien Lopez's avatar
Julien Lopez committed
131
                       f.x).2).(fun g x : Int : Int -> x)");
132
133
134
135
    );

    "match" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.match.simple failed"
136
	~printer:(fun x -> x) "1"
137
	(run_test_eval "match 1 : Int with | 1 -> 1 | \"true\" -> \"true\"");
138
      assert_equal ~msg:"Test CDuce.runtime.match.unused_branches failed"
139
	~printer:(fun x -> x) "1"
140
	(run_test_eval "match 1 : Int with
141
142
                     | s : String -> s | b : Bool -> b | i : Int -> i");
      assert_equal ~msg:"Test CDuce.runtime.match.simple_var failed"
143
	~printer:(fun x -> x) "2"
144
	(run_test_eval "(fun f x : Int : Int ->
145
146
                      match x : Int with | y : Int -> x).2");
      assert_equal ~msg:"Test CDuce.runtime.match.medium failed"
147
	~printer:(fun x -> x) "2"
148
	(run_test_eval "(fun f x : Int : Int ->
149
                      match x : Int with | 1 -> 3 | x : Int -> x).2");
150
      assert_equal ~msg:"Test CDuce.runtime.match.rec failed"
151
	~printer:(fun x -> x) "3"
152
	(run_test_eval "(fun f x : Int : Int ->
153
                      match x : Int with | 1 -> 3 | x : Int -> f.1).2");
154
155
156
157
    );

    "string" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.string.simple failed"
158
	~printer:(fun x -> x) "\"The cake is a lie\""
159
	(run_test_eval "\"The cake is a lie\"");
160
161
    );

Julien Lopez's avatar
Julien Lopez committed
162
163
164
    "list" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.list.simple failed"
	~printer:(fun x -> x) "1"
165
	(run_test_eval "match [1; 2] : [Int] with
Julien Lopez's avatar
Julien Lopez committed
166
167
168
169
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.simple2 failed"
	~printer:(fun x -> x) "3"
170
	(run_test_eval "match 2 : Int with
Julien Lopez's avatar
Julien Lopez committed
171
172
173
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.tail failed"
174
	~printer:(fun x -> x) "Abstraction(([ Int* ], [ Int* ]),{})"
175
	(run_test_eval "fun tail x : [Int] : [Int] -> match x : [Int] with
176
177
                     | (el : Int) :: (rest : [Int]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.list.tail.eval failed"
178
	~printer:(fun x -> x) "(2, 5, {})"
179
	(run_test_eval "(fun tail x : [Int] : [Int] -> match x : [Int] with
Julien Lopez's avatar
Julien Lopez committed
180
                     | (el : Int) :: (rest : [Int]) -> rest).[1; 2; 5]");
181
      (* TODO: Fix this test, we need to define [] aka `nil *)
182
      assert_equal ~msg:"Test CDuce.runtime.list.last failed"
183
	~printer:(fun x -> x) "7"
184
	(run_test_eval "(fun f x : [Int] : [Int] -> match x : [Int] with
185
186
                     | (el : Int) :: (rest : [Int]) -> f.rest
                     | el : [Int] -> el).[1; 2; 5; 4; 8; 7]");
Julien Lopez's avatar
Julien Lopez committed
187
188
    );

189
    "union" >:: ( fun test_ctxt ->
190
      assert_equal ~msg:"Test CDuce.runtime.union.identity_precise failed"
191
192
	~printer:(fun x -> x) "Abstraction((Int, Int) ,([ Char* ], [ Char* ]),{})"
	(run_test_eval "fun ((Int -> Int) & (String -> String)) | x : (Int | String) -> x");
193
      assert_equal ~msg:"Test CDuce.runtime.union.identity failed"
194
	~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
195
	(run_test_eval "fun f x : (Int | String) : (Int | String) -> x");
196
      assert_equal ~msg:"Test CDuce.runtime.union.match failed"
197
	~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
198
	(run_test_eval "fun f x : (Int | String) : (Int | String) ->
199
200
201
202
203
                     match x : (Int | String) with
                       | x : Int -> 2
                       | x : String -> \"Piece of cake\"");
      assert_equal ~msg:"Test CDuce.runtime.union.match_applied failed"
	~printer:(fun x -> x) "2"
204
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
205
206
207
208
209
                      match x : (Int | String) with
                        | x : Int -> 2
                        | x : String -> \"Piece of cake\").5");
      assert_equal ~msg:"Test CDuce.runtime.union.match_applied2 failed"
	~printer:(fun x -> x) "\"Piece of cake\""
210
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
211
212
213
214
215
                      match x : (Int | String) with
                        | x : Int -> 2
                        | x : String -> \"Piece of cake\").\"test\"");
    );

216
    "poly" >:: ( fun test_ctxt ->
Julien Lopez's avatar
Julien Lopez committed
217
218
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_pure failed"
	~printer:(fun x -> x)
219
220
	"Abstraction((`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow, 
`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow),{})"
221
	(run_test_eval "fun f x : 'A{} : 'A{} -> x");
222
      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
223
	~printer:(fun x -> x)
224
	"Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
225
	(run_test_eval "(fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
226
                   {A/Int;A/String}");
227
228
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
229
	(run_test_eval "((fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
230
                   {A/Int;A/String}).2");
231
232
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
233
	(run_test_eval "((fun f x : 'A{A/String} : 'A{A/String} -> x){A/String}).2");
Julien Lopez's avatar
Julien Lopez committed
234
235
      assert_equal ~msg:"Test CDuce.runtime.poly.tail failed"
	~printer:(fun x -> x)
236
237
238
	"Abstraction(([ (`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any |
               Arrow)* ], [ (`$A & Int | Char | Atom | (Any,Any) |
                            <(Any) (Any)>Any | Arrow)* ]),{})"
239
	(run_test_eval "fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
240
241
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.poly.tail_applied failed"
242
	~printer:(fun x -> x) "(7, (8, 5, {}), {})"
243
	(run_test_eval "(fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
244
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest).[3; 7; 8; 5]");
245

246
247
    );

248
249
  ]

Pietro Abate's avatar
Pietro Abate committed
250
251
252
253
let _ =
  run_test_tt_main (
    test_list
      [ tests_compile;
254
        tests_eval
Pietro Abate's avatar
Pietro Abate committed
255
256
257
258
      ]
  )
;;