main.ml 10.3 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
87
    );

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

    "match" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.match.simple failed"
	 ~printer:(fun x -> x) "1"
111
	(run_test_eval "match 1 : Int with | 1 -> 1 | \"true\" -> \"true\"");
112
113
      assert_equal ~msg:"Test CDuce.runtime.match.unused_branches failed"
	 ~printer:(fun x -> x) "1"
114
	(run_test_eval "match 1 : Int with
115
116
117
                     | s : String -> s | b : Bool -> b | i : Int -> i");
      assert_equal ~msg:"Test CDuce.runtime.match.simple_var failed"
	 ~printer:(fun x -> x) "2"
118
	(run_test_eval "(fun f x : Int : Int ->
119
120
121
                      match x : Int with | y : Int -> x).2");
      assert_equal ~msg:"Test CDuce.runtime.match.medium failed"
	 ~printer:(fun x -> x) "2"
122
	(run_test_eval "(fun f x : Int : Int ->
123
                      match x : Int with | 1 -> 3 | x : Int -> x).2");
124
125
      assert_equal ~msg:"Test CDuce.runtime.match.rec failed"
	 ~printer:(fun x -> x) "3"
126
	(run_test_eval "(fun f x : Int : Int ->
127
                      match x : Int with | 1 -> 3 | x : Int -> f.1).2");
128
129
130
131
132
    );

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

Julien Lopez's avatar
Julien Lopez committed
136
137
138
    "list" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.list.simple failed"
	~printer:(fun x -> x) "1"
139
	(run_test_eval "match [1; 2] : [Int] with
Julien Lopez's avatar
Julien Lopez committed
140
141
142
143
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.simple2 failed"
	~printer:(fun x -> x) "3"
144
	(run_test_eval "match 2 : Int with
Julien Lopez's avatar
Julien Lopez committed
145
146
147
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.tail failed"
Pietro Abate's avatar
Pietro Abate committed
148
      ~printer:(fun x -> x) "Abstraction(([ Int* ], [ Int* ]),{})"
149
	(run_test_eval "fun tail x : [Int] : [Int] -> match x : [Int] with
150
151
                     | (el : Int) :: (rest : [Int]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.list.tail.eval failed"
Pietro Abate's avatar
Pietro Abate committed
152
      ~printer:(fun x -> x) "(2, 5, {})"
153
	(run_test_eval "(fun tail x : [Int] : [Int] -> match x : [Int] with
Julien Lopez's avatar
Julien Lopez committed
154
                     | (el : Int) :: (rest : [Int]) -> rest).[1; 2; 5]");
155
      (* TODO: Fix this test, we need to define [] aka `nil *)
156
      assert_equal ~msg:"Test CDuce.runtime.list.last failed"
157
	~printer:(fun x -> x) "7"
158
	(run_test_eval "(fun f x : [Int] : [Int] -> match x : [Int] with
159
160
                     | (el : Int) :: (rest : [Int]) -> f.rest
                     | el : [Int] -> el).[1; 2; 5; 4; 8; 7]");
Julien Lopez's avatar
Julien Lopez committed
161
162
    );

163
    "union" >:: ( fun test_ctxt ->
164
165
166
167
168
      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");
169
      assert_equal ~msg:"Test CDuce.runtime.union.identity failed"
Pietro Abate's avatar
Pietro Abate committed
170
      ~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
171
	(run_test_eval "fun f x : (Int | String) : (Int | String) -> x");
172
      assert_equal ~msg:"Test CDuce.runtime.union.match failed"
Pietro Abate's avatar
Pietro Abate committed
173
      ~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
174
	(run_test_eval "fun f x : (Int | String) : (Int | String) ->
175
176
177
178
179
                     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"
180
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
181
182
183
184
185
                      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\""
186
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
187
188
189
190
191
                      match x : (Int | String) with
                        | x : Int -> 2
                        | x : String -> \"Piece of cake\").\"test\"");
    );

192
    "poly" >:: ( fun test_ctxt ->
Julien Lopez's avatar
Julien Lopez committed
193
194
195
      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
196
 Arrow, `$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow),{})"
197
	(run_test_eval "fun f x : 'A{} : 'A{} -> x");
198
      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
199
	~printer:(fun x -> x)
Pietro Abate's avatar
Pietro Abate committed
200
      "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
201
	(run_test_eval "(fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
202
                   {A/Int;A/String}");
203
204
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
205
	(run_test_eval "((fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
206
                   {A/Int;A/String}).2");
207
208
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
209
	(run_test_eval "((fun f x : 'A{A/String} : 'A{A/String} -> x){A/String}).2");
Julien Lopez's avatar
Julien Lopez committed
210
211
212
213
      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
214
 Arrow)* ]),{})"
215
	(run_test_eval "fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
216
217
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.poly.tail_applied failed"
Pietro Abate's avatar
Pietro Abate committed
218
      ~printer:(fun x -> x) "(7, (8, 5, {}), {})"
219
	(run_test_eval "(fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
220
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest).[3; 7; 8; 5]");
221

222
223
    );

224
225
  ]

Pietro Abate's avatar
Pietro Abate committed
226
227
228
229
let _ =
  run_test_tt_main (
    test_list
      [ tests_compile;
230
        tests_eval
Pietro Abate's avatar
Pietro Abate committed
231
232
233
234
      ]
  )
;;