main.ml 11.1 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
15
16
17
18
19
20
21
  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) ->
        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
      | 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
35
36
37
38
39

  "Test CDuce.runtime.poly.tail failed",
  "Abstraction(([ (`$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
40
41
]

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

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(* 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
      Printf.eprintf "File %s, line %d, characters %d-%d:\n" (Loc.file_name loc) l
        cbegin cend; raise exn
    | e -> Printf.eprintf "Runtime error.\n"; raise e
61

62
let tests_eval = "CDuce runtime tests (Typed -> Lambda -> Value)" >:::
63
64
65
  [
    "abstr" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.abstr.simple failed"
Pietro Abate's avatar
Pietro Abate committed
66
       ~printer:(fun x -> x) "Abstraction((Int, Int),{})"
67
	(run_test_eval "fun f x : Int : Int -> 2");
68
      assert_equal ~msg:"Test CDuce.runtime.abstr.medium failed"
69
70
       ~printer:(fun x -> x)
	"Abstraction((Int, [ Char* ] -> [ Int Char* ]),{})"
71
	(run_test_eval "fun f x : Int y : String : (Int*String) -> x,y");
72
73
74
75
76
    );

    "apply" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.apply.simple failed"
	 ~printer:(fun x -> x) "2"
77
	(run_test_eval "(fun f x : Int : Int -> x).2");
78
      assert_equal ~msg:"Test CDuce.runtime.apply.simple_pair failed"
Pietro Abate's avatar
Pietro Abate committed
79
       ~printer:(fun x -> x) "(3, 2, {})"
80
	(run_test_eval "(fun f x : (Int*Int) : (Int*Int) -> x).(3,2)");
81
      assert_equal ~msg:"Test CDuce.runtime.apply.medium failed"
Pietro Abate's avatar
Pietro Abate committed
82
       ~printer:(fun x -> x) "(2, 3, {})"
83
	(run_test_eval "((fun f x : Int y : Int : (Int*Int) -> x,y).2).3");
84
85
86
    );

    "misc" >:: ( fun test_ctxt ->
87
88
89
90
91
92
93
94
95
96
97
98
99
100
      assert_equal ~msg:"Test CDuce.runtime.misc.is_int failed"
       ~printer:(fun x -> x)
	"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"
       ~printer:(fun x -> x)
	"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");
101
      assert_equal ~msg:"Test CDuce.runtime.misc.firsts failed"
102
103
       ~printer:(fun x -> x)
	"Abstraction(((Int,Int), X1 -> X1 where X1 = (Int,Int)),{})"
104
	(run_test_eval "fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
105
106
107
                     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"
Pietro Abate's avatar
Pietro Abate committed
108
       ~printer:(fun x -> x) "(5, 1, {})"
109
	(run_test_eval "((fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
110
111
112
                       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
113
      assert_equal ~msg:"Test CDuce.runtime.misc.applier failed"
Pietro Abate's avatar
Pietro Abate committed
114
       ~printer:(fun x -> x) "Abstraction((Int, Int -> Int -> Int),{})"
115
	(run_test_eval "fun applier x : Int f : (Int->Int) : Int -> f.x");
Julien Lopez's avatar
Julien Lopez committed
116
117
      assert_equal ~msg:"Test CDuce.runtime.misc.applier_applied failed"
	 ~printer:(fun x -> x) "2"
118
	(run_test_eval "((fun applier x : Int f : (Int->Int) : Int ->
Julien Lopez's avatar
Julien Lopez committed
119
                       f.x).2).(fun g x : Int : Int -> x)");
120
121
122
123
124
    );

    "match" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.match.simple failed"
	 ~printer:(fun x -> x) "1"
125
	(run_test_eval "match 1 : Int with | 1 -> 1 | \"true\" -> \"true\"");
126
127
      assert_equal ~msg:"Test CDuce.runtime.match.unused_branches failed"
	 ~printer:(fun x -> x) "1"
128
	(run_test_eval "match 1 : Int with
129
130
131
                     | s : String -> s | b : Bool -> b | i : Int -> i");
      assert_equal ~msg:"Test CDuce.runtime.match.simple_var failed"
	 ~printer:(fun x -> x) "2"
132
	(run_test_eval "(fun f x : Int : Int ->
133
134
135
                      match x : Int with | y : Int -> x).2");
      assert_equal ~msg:"Test CDuce.runtime.match.medium failed"
	 ~printer:(fun x -> x) "2"
136
	(run_test_eval "(fun f x : Int : Int ->
137
                      match x : Int with | 1 -> 3 | x : Int -> x).2");
138
139
      assert_equal ~msg:"Test CDuce.runtime.match.rec failed"
	 ~printer:(fun x -> x) "3"
140
	(run_test_eval "(fun f x : Int : Int ->
141
                      match x : Int with | 1 -> 3 | x : Int -> f.1).2");
142
143
144
145
146
    );

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

Julien Lopez's avatar
Julien Lopez committed
150
151
152
    "list" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.list.simple failed"
	~printer:(fun x -> x) "1"
153
	(run_test_eval "match [1; 2] : [Int] with
Julien Lopez's avatar
Julien Lopez committed
154
155
156
157
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.simple2 failed"
	~printer:(fun x -> x) "3"
158
	(run_test_eval "match 2 : Int with
Julien Lopez's avatar
Julien Lopez committed
159
160
161
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.tail failed"
Pietro Abate's avatar
Pietro Abate committed
162
      ~printer:(fun x -> x) "Abstraction(([ Int* ], [ Int* ]),{})"
163
	(run_test_eval "fun tail x : [Int] : [Int] -> match x : [Int] with
164
165
                     | (el : Int) :: (rest : [Int]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.list.tail.eval failed"
Pietro Abate's avatar
Pietro Abate committed
166
      ~printer:(fun x -> x) "(2, 5, {})"
167
	(run_test_eval "(fun tail x : [Int] : [Int] -> match x : [Int] with
Julien Lopez's avatar
Julien Lopez committed
168
                     | (el : Int) :: (rest : [Int]) -> rest).[1; 2; 5]");
169
      (* TODO: Fix this test, we need to define [] aka `nil *)
170
      assert_equal ~msg:"Test CDuce.runtime.list.last failed"
171
	~printer:(fun x -> x) "7"
172
	(run_test_eval "(fun f x : [Int] : [Int] -> match x : [Int] with
173
174
                     | (el : Int) :: (rest : [Int]) -> f.rest
                     | el : [Int] -> el).[1; 2; 5; 4; 8; 7]");
Julien Lopez's avatar
Julien Lopez committed
175
176
    );

177
    "union" >:: ( fun test_ctxt ->
178
179
180
181
182
      assert_equal ~msg:"Test CDuce.runtime.union.identity_precise failed"
      ~printer:(fun x -> x) "Abstraction((Int -> Int & X1 -> X1 where X1 = [ Char* ], Int -> Int &
                                                         X1 -> X1 where
                                                         X1 = [ Char* ]),{})"
	(run_test_eval "fun _f f : ((Int -> Int) & (String -> String)) : ((Int -> Int) & (String -> String)) -> f");
183
      assert_equal ~msg:"Test CDuce.runtime.union.identity failed"
Pietro Abate's avatar
Pietro Abate committed
184
      ~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
185
	(run_test_eval "fun f x : (Int | String) : (Int | String) -> x");
186
      assert_equal ~msg:"Test CDuce.runtime.union.match failed"
Pietro Abate's avatar
Pietro Abate committed
187
      ~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
188
	(run_test_eval "fun f x : (Int | String) : (Int | String) ->
189
190
191
192
193
                     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"
194
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
195
196
197
198
199
                      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\""
200
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
201
202
203
204
205
                      match x : (Int | String) with
                        | x : Int -> 2
                        | x : String -> \"Piece of cake\").\"test\"");
    );

206
    "poly" >:: ( fun test_ctxt ->
Julien Lopez's avatar
Julien Lopez committed
207
208
209
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_pure failed"
	~printer:(fun x -> x)
	"Abstraction((`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | \
Pietro Abate's avatar
Pietro Abate committed
210
 Arrow, `$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow),{})"
211
	(run_test_eval "fun f x : 'A{} : 'A{} -> x");
212
      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
213
	~printer:(fun x -> x)
Pietro Abate's avatar
Pietro Abate committed
214
      "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
215
	(run_test_eval "(fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
216
                   {A/Int;A/String}");
217
218
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
219
	(run_test_eval "((fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
220
                   {A/Int;A/String}).2");
221
222
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
223
	(run_test_eval "((fun f x : 'A{A/String} : 'A{A/String} -> x){A/String}).2");
Julien Lopez's avatar
Julien Lopez committed
224
225
226
227
      assert_equal ~msg:"Test CDuce.runtime.poly.tail failed"
	~printer:(fun x -> x)
	"Abstraction(([ (`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any \
 | Arrow)* ], [ (`$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | \
Pietro Abate's avatar
Pietro Abate committed
228
 Arrow)* ]),{})"
229
	(run_test_eval "fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
230
231
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.poly.tail_applied failed"
Pietro Abate's avatar
Pietro Abate committed
232
      ~printer:(fun x -> x) "(7, (8, 5, {}), {})"
233
	(run_test_eval "(fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
234
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest).[3; 7; 8; 5]");
235

236
237
    );

238
239
  ]

Pietro Abate's avatar
Pietro Abate committed
240
241
242
243
let _ =
  run_test_tt_main (
    test_list
      [ tests_compile;
244
        tests_eval
Pietro Abate's avatar
Pietro Abate committed
245
246
247
248
      ]
  )
;;