main.ml 9.51 KB
Newer Older
1
open OUnit2
Pietro Abate's avatar
Pietro Abate committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
open Camlp4.PreCast 

(* Typed -> Lamda *)
let run_test_compile expected totest =
  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
  fun _ -> assert_equal  ~printer:(fun x -> x) expected (aux totest)

let tests_poly_abstr = [
25
26
27
  "Abstraction(Dummy,,,,Sel(,(Int -> Int),{}))","fun f x : Int : Int -> 2";
  "Abstraction(Dummy,,,,Sel(,([ Char* ] | Int -> [ Char* ] | Int),Comp({},{ { (`$A/
[ 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
28
29
30
31
]

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

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(* 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
49

50
let tests_eval = "CDuce runtime tests (Typed -> Lambda -> Value)" >:::
51
52
53
  [
    "abstr" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.abstr.simple failed"
Pietro Abate's avatar
Pietro Abate committed
54
       ~printer:(fun x -> x) "Abstraction((Int, Int),{})"
55
	(run_test_eval "fun f x : Int : Int -> 2");
56
      assert_equal ~msg:"Test CDuce.runtime.abstr.medium failed"
57
58
       ~printer:(fun x -> x)
	"Abstraction((Int, [ Char* ] -> [ Int Char* ]),{})"
59
	(run_test_eval "fun f x : Int y : String : (Int*String) -> x,y");
60
61
62
63
64
    );

    "apply" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.apply.simple failed"
	 ~printer:(fun x -> x) "2"
65
	(run_test_eval "(fun f x : Int : Int -> x).2");
66
      assert_equal ~msg:"Test CDuce.runtime.apply.simple_pair failed"
Pietro Abate's avatar
Pietro Abate committed
67
       ~printer:(fun x -> x) "(3, 2, {})"
68
	(run_test_eval "(fun f x : (Int*Int) : (Int*Int) -> x).(3,2)");
69
      assert_equal ~msg:"Test CDuce.runtime.apply.medium failed"
Pietro Abate's avatar
Pietro Abate committed
70
       ~printer:(fun x -> x) "(2, 3, {})"
71
	(run_test_eval "((fun f x : Int y : Int : (Int*Int) -> x,y).2).3");
72
73
74
75
    );

    "misc" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.misc.firsts failed"
76
77
       ~printer:(fun x -> x)
	"Abstraction(((Int,Int), X1 -> X1 where X1 = (Int,Int)),{})"
78
	(run_test_eval "fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
79
80
81
                     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
82
       ~printer:(fun x -> x) "(5, 1, {})"
83
	(run_test_eval "((fun firsts x : (Int*Int) y : (Int*Int) : (Int*Int) ->
84
85
86
                       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
87
      assert_equal ~msg:"Test CDuce.runtime.misc.applier failed"
Pietro Abate's avatar
Pietro Abate committed
88
       ~printer:(fun x -> x) "Abstraction((Int, Int -> Int -> Int),{})"
89
	(run_test_eval "fun applier x : Int f : (Int->Int) : Int -> f.x");
Julien Lopez's avatar
Julien Lopez committed
90
91
      assert_equal ~msg:"Test CDuce.runtime.misc.applier_applied failed"
	 ~printer:(fun x -> x) "2"
92
	(run_test_eval "((fun applier x : Int f : (Int->Int) : Int ->
Julien Lopez's avatar
Julien Lopez committed
93
                       f.x).2).(fun g x : Int : Int -> x)");
94
95
96
97
98
    );

    "match" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.match.simple failed"
	 ~printer:(fun x -> x) "1"
99
	(run_test_eval "match 1 : Int with | 1 -> 1 | \"true\" -> \"true\"");
100
101
      assert_equal ~msg:"Test CDuce.runtime.match.unused_branches failed"
	 ~printer:(fun x -> x) "1"
102
	(run_test_eval "match 1 : Int with
103
104
105
                     | s : String -> s | b : Bool -> b | i : Int -> i");
      assert_equal ~msg:"Test CDuce.runtime.match.simple_var failed"
	 ~printer:(fun x -> x) "2"
106
	(run_test_eval "(fun f x : Int : Int ->
107
108
109
                      match x : Int with | y : Int -> x).2");
      assert_equal ~msg:"Test CDuce.runtime.match.medium failed"
	 ~printer:(fun x -> x) "2"
110
	(run_test_eval "(fun f x : Int : Int ->
111
                      match x : Int with | 1 -> 3 | x : Int -> x).2");
112
113
      assert_equal ~msg:"Test CDuce.runtime.match.rec failed"
	 ~printer:(fun x -> x) "3"
114
	(run_test_eval "(fun f x : Int : Int ->
115
                      match x : Int with | 1 -> 3 | x : Int -> f.1).2");
116
117
118
119
120
    );

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

Julien Lopez's avatar
Julien Lopez committed
124
125
126
    "list" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.list.simple failed"
	~printer:(fun x -> x) "1"
127
	(run_test_eval "match [1; 2] : [Int] with
Julien Lopez's avatar
Julien Lopez committed
128
129
130
131
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.simple2 failed"
	~printer:(fun x -> x) "3"
132
	(run_test_eval "match 2 : Int with
Julien Lopez's avatar
Julien Lopez committed
133
134
135
                     | (el : Int) :: (rest : [Int]) -> el
                     | x : Int -> 3");
      assert_equal ~msg:"Test CDuce.runtime.list.tail failed"
Pietro Abate's avatar
Pietro Abate committed
136
      ~printer:(fun x -> x) "Abstraction(([ Int* ], [ Int* ]),{})"
137
	(run_test_eval "fun tail x : [Int] : [Int] -> match x : [Int] with
138
139
                     | (el : Int) :: (rest : [Int]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.list.tail.eval failed"
Pietro Abate's avatar
Pietro Abate committed
140
      ~printer:(fun x -> x) "(2, 5, {})"
141
	(run_test_eval "(fun tail x : [Int] : [Int] -> match x : [Int] with
Julien Lopez's avatar
Julien Lopez committed
142
                     | (el : Int) :: (rest : [Int]) -> rest).[1; 2; 5]");
143
      (* TODO: Fix this test, we need to define [] aka `nil *)
144
      assert_equal ~msg:"Test CDuce.runtime.list.last failed"
145
	~printer:(fun x -> x) "7"
146
	(run_test_eval "(fun f x : [Int] : [Int] -> match x : [Int] with
147
148
                     | (el : Int) :: (rest : [Int]) -> f.rest
                     | el : [Int] -> el).[1; 2; 5; 4; 8; 7]");
Julien Lopez's avatar
Julien Lopez committed
149
150
    );

151
152
    "union" >:: ( fun test_ctxt ->
      assert_equal ~msg:"Test CDuce.runtime.union.identity failed"
Pietro Abate's avatar
Pietro Abate committed
153
      ~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
154
	(run_test_eval "fun f x : (Int | String) : (Int | String) -> x");
155
      assert_equal ~msg:"Test CDuce.runtime.union.match failed"
Pietro Abate's avatar
Pietro Abate committed
156
      ~printer:(fun x -> x) "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
157
	(run_test_eval "fun f x : (Int | String) : (Int | String) ->
158
159
160
161
162
                     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"
163
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
164
165
166
167
168
                      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\""
169
	(run_test_eval "(fun f x : (Int | String) : (Int | String) ->
170
171
172
173
174
                      match x : (Int | String) with
                        | x : Int -> 2
                        | x : String -> \"Piece of cake\").\"test\"");
    );

175
    "poly" >:: ( fun test_ctxt ->
Julien Lopez's avatar
Julien Lopez committed
176
177
178
      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
179
 Arrow, `$A & Int | Char | Atom | (Any,Any) | <(Any) (Any)>Any | Arrow),{})"
180
	(run_test_eval "fun f x : 'A{} : 'A{} -> x");
181
(*      assert_equal ~msg:"Test CDuce.runtime.poly.identity failed"
182
	~printer:(fun x -> x)
Pietro Abate's avatar
Pietro Abate committed
183
      "Abstraction(([ Char* ] | Int, [ Char* ] | Int),{})"
184
	(run_test_eval "(fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
185
                   {A/Int;A/String}");
186
187
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied failed"
	~printer:(fun x -> x) "2"
188
	(run_test_eval "((fun f x : 'A{A/Int;A/String} : 'A{A/Int;A/String} -> x)
Julien Lopez's avatar
Julien Lopez committed
189
                   {A/Int;A/String}).2");
190
191
      assert_equal ~msg:"Test CDuce.runtime.poly.identity_applied2 failed"
	~printer:(fun x -> x) "2"
192
	(run_test_eval "((fun f x : 'A{A/String} : 'A{A/String} -> x){A/String}).2");
Julien Lopez's avatar
Julien Lopez committed
193
194
195
196
      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
197
 Arrow)* ]),{})"
198
	(run_test_eval "fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
199
200
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest");
      assert_equal ~msg:"Test CDuce.runtime.poly.tail_applied failed"
Pietro Abate's avatar
Pietro Abate committed
201
      ~printer:(fun x -> x) "(7, (8, 5, {}), {})"
202
	(run_test_eval "(fun tail x : ['A{}] : ['A{}] -> match x : ['A{}] with
Julien Lopez's avatar
Julien Lopez committed
203
                     | (el : 'A{}) :: (rest : ['A{}]) -> rest).[3; 7; 8; 5]");
204
*)
205
206
    );

207
208
  ]

Pietro Abate's avatar
Pietro Abate committed
209
210
211
212
let _ =
  run_test_tt_main (
    test_list
      [ tests_compile;
213
        tests_eval
Pietro Abate's avatar
Pietro Abate committed
214
215
216
217
      ]
  )
;;