Add some micro test cases
[tiger.ml.git] / tiger / src / exe / tiger_tests.ml
CommitLineData
543d3420
SK
1open Printf
2
78c9eca5
SK
3module List = ListLabels
4
68a223c2 5let test_case_from_book_01 =
19284c5d 6 let name = "an array type and an array variable" in
78c9eca5 7 let code =
8543fc37 8 " \
19284c5d 9 /* "^name^" */ \
8543fc37
SK
10 let \
11 type arrtype = array of int \
12 var arr1:arrtype := \
13 arrtype [10] of 0 \
14 in \
15 arr1 \
16 end \
78c9eca5
SK
17 "
18 in
19 let tokens =
543d3420 20 let open Tiger.Parser in
78c9eca5
SK
21 [ LET;
22 TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "int";
23 VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN;
24 ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0;
25 IN;
26 ID "arr1";
27 END
28 ]
29 in
19284c5d 30 (name, code, tokens)
8543fc37 31
68a223c2 32let test_case_from_book_02 =
19284c5d 33 let name = "arr1 is valid since expression 0 is int = myint" in
8543fc37
SK
34 let code =
35 " \
19284c5d 36 /* "^name^" */ \
8543fc37
SK
37 let \
38 type myint = int \
39 type arrtype = array of myint \
b828a6e7 40 var arr1:arrtype := \
8543fc37
SK
41 arrtype [10] of 0 \
42 in \
43 arr1 \
44 end \
45 "
46 in
47 let tokens =
543d3420 48 let open Tiger.Parser in
8543fc37
SK
49 [ LET;
50 TYPE; ID "myint"; EQ; ID "int";
51 TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "myint";
52 VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN;
53 ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0;
54 IN;
55 ID "arr1";
56 END
57 ]
58 in
19284c5d 59 (name, code, tokens)
8543fc37 60
68a223c2 61let test_case_from_book_03 =
19284c5d 62 let name = "a record type and a record variable" in
8543fc37
SK
63 let code =
64 " \
19284c5d 65 /* "^name^" */ \
8543fc37
SK
66 let \
67 type rectype = \
68 { name : string \
69 , age : int \
70 } \
71 var rec1 : rectype := \
72 rectype \
73 { name = \"Nobody\" \
74 , age = 1000 \
75 } \
76 in \
77 rec1.name := \"Somebody\"; \
78 rec1 \
79 end \
80 "
81 in
82 let tokens =
543d3420 83 let open Tiger.Parser in
8543fc37
SK
84 [ LET;
85 TYPE; ID "rectype"; EQ;
86 LBRACE; ID "name"; COLON; ID "string";
87 COMMA; ID "age"; COLON; ID "int";
88 RBRACE;
89 VAR; ID "rec1"; COLON; ID "rectype"; ASSIGN;
90 ID "rectype";
91 LBRACE; ID "name"; EQ; STRING "Nobody";
92 COMMA; ID "age"; EQ; INT 1000;
93 RBRACE;
94 IN;
95 ID "rec1"; DOT; ID "name"; ASSIGN; STRING "Somebody"; SEMICOLON;
96 ID "rec1";
97 END
98 ]
99 in
19284c5d 100 (name, code, tokens)
78c9eca5 101
68a223c2 102let test_case_from_book_04 =
1946b457
SK
103 let name = "define a recursive function" in
104 let code =
105 " \
106 /* "^name^" */ \
107 let \
108 \
109 /* calculate n! */ \
110 function nfactor(n: int): int = \
111 if n = 0 \
112 then 1 \
113 else n * nfactor(n-1) \
114 \
115 in \
116 nfactor(10) \
117 end \
118 "
119 in
120 let tokens =
543d3420 121 let open Tiger.Parser in
1946b457
SK
122 [ LET;
123 FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ;
124 IF; ID "n"; EQ; INT 0;
125 THEN; INT 1;
126 ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN;
127 IN;
128 ID "nfactor"; LPAREN; INT 10; RPAREN;
129 END
130 ]
131 in
132 (name, code, tokens)
133
68a223c2 134let test_case_from_book_09 =
cb00a20d
SK
135 let name = "error : types of then - else differ" in
136 let code =
543d3420
SK
137 " \
138 /* "^name^" */ \
139 if (5>4) then 13 else \" \" \
cb00a20d
SK
140 "
141 in
142 let tokens =
543d3420 143 let open Tiger.Parser in
cb00a20d
SK
144 [ IF; LPAREN; INT 5; GT; INT 4; RPAREN; THEN; INT 13; ELSE; STRING " "
145 ]
146 in
147 (* TODO: Type error test case *)
148 (name, code, tokens)
149
68a223c2 150let test_case_from_book_queens =
82e73e6c 151 let code =
68a223c2 152 "\
82e73e6c
SK
153 /* A program to solve the 8-queens problem */ \n\
154 \n\
155 let \n\
156 var N := 8 \n\
157 \n\
158 type intArray = array of int \n\
159 \n\
160 var row := intArray [ N ] of 0 \n\
161 var col := intArray [ N ] of 0 \n\
162 var diag1 := intArray [N+N-1] of 0 \n\
163 var diag2 := intArray [N+N-1] of 0 \n\
164 \n\
165 function printboard() = ( \n\
166 for i := 0 to N-1 do ( \n\
167 for j := 0 to N-1 do print(if col[i]=j then \" O\" else \" .\"); \n\
168 print(\"\n\") \n\
169 ); \n\
170 print(\"\n\") \n\
171 ) \n\
172 \n\
173 function try(c:int) = ( \n\
174 /* for i:= 0 to c do print(\".\"); print(\"\n\"); flush();*/ \n\
175 if c=N \n\
176 then printboard() \n\
177 else \n\
178 for r := 0 to N-1 \n\
179 do \n\
180 if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 \n\
181 then ( \n\
182 row[r] := 1; \n\
183 diag1[r+c] := 1; \n\
184 diag2[r+7-c] := 1; \n\
185 col[c] := r; \n\
186 try(c+1); \n\
187 row[r] := 0; \n\
188 diag1[r+c] := 0; \n\
189 diag2[r+7-c] := 0 \n\
190 ) \n\
191 ) \n\
192 in \n\
193 try(0) \n\
194 end \n\
195 "
196 in
197 (code, code, [])
198
68a223c2
SK
199let test_cases_from_book =
200 [ test_case_from_book_01
201 ; test_case_from_book_02
202 ; test_case_from_book_03
203 ; test_case_from_book_04
204 ; test_case_from_book_09
205 ; test_case_from_book_queens
206 ]
207
208let tests_micro_cases =
209 let open Tiger.Parser in
210 [ (
211 let code =
212 "nil"
213 in
214 let tokens =
215 [NIL]
216 in
217 (code, code, tokens)
218 )
219 ; (
220 let code =
221 "5"
222 in
223 let tokens =
224 [INT 5]
225 in
226 (code, code, tokens)
227 )
228 ; (
229 let code =
230 "-5"
231 in
232 let tokens =
233 [MINUS; INT 5]
234 in
235 (code, code, tokens)
236 )
237 ; (
238 let code =
239 "f()"
240 in
241 let tokens =
242 [ID "f"; LPAREN; RPAREN]
243 in
244 (code, code, tokens)
245 )
246 ; (
247 let code =
248 "f(\"a\", 3, foo)"
249 in
250 let tokens =
251 [ID "f"; LPAREN; STRING "a"; COMMA; INT 3; COMMA; ID "foo"; RPAREN]
252 in
253 (code, code, tokens)
254 )
255 ; (
256 let code =
257 "abc.i"
258 in
259 let tokens =
260 [ID "abc"; DOT; ID "i"]
261 in
262 (code, code, tokens)
263 )
264 ; (
265 let code =
266 "abc [5] of nil"
267 in
268 let tokens =
269 [ID "abc"; LBRACK; INT 5; RBRACK; OF; NIL]
270 in
271 (code, code, tokens)
272 )
273 ; (
274 let code =
275 "abc[0]"
276 in
277 let tokens =
278 [ID "abc"; LBRACK; INT 0; RBRACK]
279 in
280 (code, code, tokens)
281 )
282 ; (
283 let code =
284 "abc[0] := foo()"
285 in
286 let tokens =
287 [ID "abc"; LBRACK; INT 0; RBRACK; ASSIGN; ID "foo"; LPAREN; RPAREN]
288 in
289 (code, code, tokens)
290 )
78c9eca5
SK
291 ]
292
68a223c2
SK
293let tests =
294 test_cases_from_book @ tests_micro_cases
295
78c9eca5 296let () =
f03690fc
SK
297 let tokens_of_code code =
298 let lexbuf = Lexing.from_string code in
299 let rec tokens () =
300 let token = Tiger.Lexer.token lexbuf in
301 (* Avoiding fragile pattern-matching *)
302 if token = Tiger.Parser.EOF then [] else token :: tokens ()
303 in
304 tokens ()
305 in
306 let parsetree_of_code code =
307 let lb = Lexing.from_string code in
308 (match Tiger.Parser.program Tiger.Lexer.token lb with
309 | exception Parsing.Parse_error ->
310 let module L = Lexing in
311 let L.({lex_curr_p = {pos_lnum=l; pos_bol=b; pos_cnum=c; _}; _}) = lb in
312 let msg = sprintf "Syntax error around line: %d, column: %d" l (c - b) in
313 Error msg
314 | parsetree ->
315 Ok parsetree
316 )
317 in
8543fc37
SK
318 let bar_sep = String.make 80 '-' in
319 let bar_end = String.make 80 '=' in
543d3420
SK
320 let indent n = String.make (2 * n) ' ' in
321 let color_on_green = "\027[0;32m" in
322 let color_on_red = "\027[1;31m" in
323 let color_off = "\027[0m" in
858f923a
SK
324 List.iteri tests ~f:(fun i (name, code, tokens_expected) ->
325 let i = i + 1 in (* Because iteri starts with 0 *)
543d3420
SK
326 printf "%s\n%sTest %d : %S\n" bar_sep (indent 0) i name;
327
328 printf "%sLexing : " (indent 1);
8543fc37
SK
329 let tokens_emitted = tokens_of_code code in
330 (try
331 assert (tokens_emitted = tokens_expected);
543d3420 332 printf "%sOK%s\n" color_on_green color_off;
8543fc37
SK
333 with Assert_failure _ ->
334 let tokens_to_string tokens =
543d3420 335 String.concat "; " (List.map ~f:Tiger.Parser_token.to_string tokens)
8543fc37
SK
336 in
337 printf
543d3420
SK
338 "%sERROR%s\n%sExpected: %s\n%sEmitted : %s\n\n"
339 color_on_red
340 color_off
341 (indent 2)
8543fc37 342 (tokens_to_string tokens_expected)
543d3420 343 (indent 2)
8543fc37
SK
344 (tokens_to_string tokens_emitted)
345 );
543d3420
SK
346
347 printf "%sParsing: " (indent 1);
348 (match parsetree_of_code code with
349 | Error errmsg -> printf "%sERROR:%s %s\n" color_on_red color_off errmsg
350 | Ok parsetree -> printf "%sOK:%s %s\n" color_on_green color_off parsetree
351 );
352
8543fc37
SK
353 );
354 print_endline bar_end;
This page took 0.061945 seconds and 4 git commands to generate.