Mercurial > lcfOS
annotate python/testc3.py @ 243:ef683881c64e
Remove various files
author | Windel Bouwman |
---|---|
date | Tue, 23 Jul 2013 16:50:02 +0200 |
parents | 521567d17388 |
children | e41e4109addd |
rev | line source |
---|---|
158 | 1 import c3 |
2 import time, ppci, x86, ir | |
167 | 3 import unittest |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
4 import glob |
148 | 5 |
162 | 6 testsrc = """package test; |
148 | 7 |
215 | 8 /* |
9 demo of the source that is correct :) | |
10 */ | |
11 var int c, d; | |
163 | 12 var double e; |
13 var int f; | |
14 | |
167 | 15 const int A = 1337; |
149 | 16 |
230 | 17 function void test1() |
148 | 18 { |
215 | 19 var int bdd; |
148 | 20 var int a = 10; |
215 | 21 bdd = 20; |
155 | 22 var int buf; |
148 | 23 var int i; |
24 i = 2; | |
155 | 25 var int zero = i - 2; |
148 | 26 if (i > 1) |
27 { | |
157 | 28 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1; |
148 | 29 } |
155 | 30 else |
31 { | |
32 ;;; | |
157 | 33 } |
155 | 34 |
35 t2(2, 3); | |
148 | 36 } |
37 | |
215 | 38 function int t2(int a, int b) |
148 | 39 { |
157 | 40 if (a > 0) |
41 { | |
215 | 42 a = 2 + t2(a - 1, 10); |
157 | 43 } |
44 | |
148 | 45 return a + b; |
46 } | |
47 | |
215 | 48 var int a, b; |
49 | |
166 | 50 function int t3(int aap, int blah) |
51 { | |
215 | 52 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true) |
166 | 53 { |
215 | 54 a = 2 + t2(a - 1, 0); |
166 | 55 } |
56 | |
57 return a + b; | |
58 } | |
59 | |
155 | 60 var int hahaa = 23 * 2; |
150 | 61 |
149 | 62 |
148 | 63 """ |
64 | |
204 | 65 class testLexer(unittest.TestCase): |
205 | 66 def testUnexpectedCharacter(self): |
67 snippet = """ var s \u6c34 """ | |
68 with self.assertRaises(ppci.CompilerError): | |
69 list(c3.lexer.tokenize(snippet)) | |
70 | |
204 | 71 def testBlockComment(self): |
72 snippet = """ | |
73 /* Demo */ | |
74 var int x = 0; | |
75 """ | |
76 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
77 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
205 | 78 |
204 | 79 def testBlockCommentMultiLine(self): |
80 snippet = """ | |
81 /* Demo | |
82 bla1 | |
83 bla2 | |
84 */ | |
85 var int x = 0; | |
86 """ | |
87 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
88 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
89 | |
90 class testBuilder(unittest.TestCase): | |
215 | 91 def setUp(self): |
92 self.diag = ppci.DiagnosticsManager() | |
93 self.builder = c3.Builder(self.diag) | |
94 self.diag.clear() | |
95 | |
96 def testSrc(self): | |
97 self.expectOK(testsrc) | |
98 | |
99 def expectErrors(self, snippet, rows): | |
100 """ Helper to test for expected errors on rows """ | |
101 ircode = self.builder.build(snippet) | |
102 actualErrors = [err.row for err in self.diag.diags] | |
103 if rows != actualErrors: | |
104 self.diag.printErrors(snippet) | |
105 self.assertSequenceEqual(rows, actualErrors) | |
106 self.assertFalse(ircode) | |
107 | |
108 def expectOK(self, snippet): | |
109 ircode = self.builder.build(snippet) | |
110 if not ircode: | |
111 self.diag.printErrors(snippet) | |
112 self.assertTrue(ircode) | |
217 | 113 return ircode |
215 | 114 |
230 | 115 def expectIR(self, snippet, ir_out): |
116 ircode = self.builder.build(snippet) | |
117 if not ircode: | |
118 self.diag.printErrors(snippet) | |
119 self.assertTrue(ircode) | |
120 actual_ins = [str(i) for i in ircode.Instructions] | |
121 expected_ins = [i.strip() for i in ir_out.split('\n')] | |
122 self.assertSequenceEqual(expected_ins, actual_ins) | |
123 return ircode | |
124 | |
215 | 125 def testFunctArgs(self): |
167 | 126 snippet = """ |
127 package testargs; | |
128 function void t2(int a, double b) | |
129 { | |
130 t2(2, 2); | |
131 t2(2); | |
132 t2(1, 1.2); | |
133 } | |
134 """ | |
215 | 135 self.expectErrors(snippet, [5, 6]) |
148 | 136 |
215 | 137 def testExpressions(self): |
167 | 138 snippet = """ |
139 package test; | |
140 function void t(int a, double b) | |
141 { | |
142 var int a2; | |
143 var bool c; | |
144 | |
145 a2 = b * a; | |
146 c = a; | |
147 c = b > 1; | |
148 } | |
149 """ | |
215 | 150 self.expectErrors(snippet, [8, 9, 10]) |
205 | 151 |
230 | 152 def testExpression1(self): |
153 snippet = """ | |
154 package testexpr1; | |
155 function void t() | |
156 { | |
157 var int a, b, c; | |
158 a = 1; | |
159 b = a * 2 + a * a; | |
160 c = b * a - 3; | |
161 } | |
162 """ | |
163 block_code = """ a0 = alloc | |
164 b1 = alloc | |
165 c2 = alloc | |
166 t3 = 1 | |
167 [a0] = t3 | |
168 t4 = [a0] | |
169 t5 = 2 | |
170 mul6 = t4 * t5 | |
171 t7 = [a0] | |
172 t8 = [a0] | |
173 mul9 = t7 * t8 | |
174 add10 = mul6 + mul9 | |
175 [b1] = add10 | |
176 t11 = [b1] | |
177 t12 = [a0] | |
178 mul13 = t11 * t12 | |
179 t14 = 3 | |
180 sub15 = mul13 - t14 | |
181 [c2] = sub15 | |
182 ret """ | |
183 self.expectIR(snippet, block_code) | |
184 | |
215 | 185 def testEmpty(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 """ |
215 | 189 self.expectErrors(snippet, [3]) |
205 | 190 |
215 | 191 def testEmpty2(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 snippet = "" |
215 | 193 self.expectErrors(snippet, [1]) |
205 | 194 |
215 | 195 def testRedefine(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
197 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
198 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
199 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
200 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
201 """ |
215 | 202 self.expectErrors(snippet, [5]) |
205 | 203 |
215 | 204 def testWhile(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
205 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
206 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
207 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
208 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
209 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
210 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
211 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
212 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
213 i = i + 3; |
186 | 214 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
215 } |
205 | 216 |
217 while(true) | |
218 { | |
219 } | |
220 | |
221 while(false) | |
222 { | |
223 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
224 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
225 """ |
215 | 226 self.expectOK(snippet) |
227 | |
228 def testIf(self): | |
229 snippet = """ | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
230 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
231 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
232 { |
180 | 233 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
234 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
235 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
236 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
237 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
238 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
239 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
240 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
241 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
242 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
243 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
244 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
245 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
246 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
247 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
248 } |
215 | 249 """ |
250 self.expectOK(snippet) | |
225 | 251 |
252 def testTypeDef(self): | |
253 snippet = """ | |
254 package testtypedef; | |
255 type int my_int; | |
256 function void t() | |
257 { | |
258 var my_int a; | |
259 var int b; | |
260 a = 2; | |
261 b = a + 2; | |
262 } | |
263 """ | |
264 self.expectOK(snippet) | |
265 | |
230 | 266 def testLocalVariable(self): |
267 snippet = """ | |
268 package testlocalvar; | |
269 function void t() | |
270 { | |
271 var int a, b; | |
272 a = 2; | |
273 b = a + 2; | |
274 } | |
275 """ | |
276 block_code = """a0 = alloc | |
277 b1 = alloc | |
278 t2 = 2 | |
279 [a0] = t2 | |
280 t3 = [a0] | |
281 t4 = 2 | |
282 add5 = t3 + t4 | |
283 [b1] = add5 | |
284 ret """ | |
285 self.expectIR(snippet, block_code) | |
286 | |
287 def testStruct1(self): | |
288 snippet = """ | |
289 package teststruct1; | |
290 function void t() | |
291 { | |
292 var struct {int x, y;} a; | |
293 a.x = 2; | |
294 a.y = a.x + 2; | |
295 } | |
296 """ | |
297 block_code = """a0 = alloc | |
298 t1 = 2 | |
299 off_x2 = 0 | |
300 adr_x3 = a0 + off_x2 | |
301 [adr_x3] = t1 | |
302 off_x4 = 0 | |
303 adr_x5 = a0 + off_x4 | |
304 t6 = [adr_x5] | |
305 t7 = 2 | |
306 add8 = t6 + t7 | |
231 | 307 off_y9 = 4 |
230 | 308 adr_y10 = a0 + off_y9 |
309 [adr_y10] = add8 | |
310 ret """ | |
311 self.expectIR(snippet, block_code) | |
312 | |
225 | 313 def testPointerType1(self): |
314 snippet = """ | |
315 package testpointer1; | |
316 var int* pa; | |
317 function void t() | |
318 { | |
319 var int a; | |
320 pa = &a; | |
321 *pa = 22; | |
322 } | |
323 """ | |
324 self.expectOK(snippet) | |
325 | |
215 | 326 def testPointerType(self): |
213 | 327 snippet = """ |
328 package testpointer; | |
225 | 329 var int* pa, pb; |
213 | 330 function void t(int a, double b) |
331 { | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
332 pa = &a; |
225 | 333 pb = pa; |
213 | 334 *pa = 22; |
335 } | |
336 """ | |
215 | 337 self.expectOK(snippet) |
213 | 338 |
221 | 339 def testPointerTypeInCorrect(self): |
340 snippet = """ | |
341 package testpointerincorrect; | |
342 var int* pa; | |
343 function void t(int a, double b) | |
344 { | |
345 pa = 2; // type conflict | |
346 pa = &a; | |
347 pa = &2; | |
348 &a = pa; // No valid lvalue | |
349 **pa = 22; // Cannot deref int | |
350 } | |
351 """ | |
228 | 352 self.expectErrors(snippet, [6, 9, 10]) |
221 | 353 |
230 | 354 def testPointerTypeIr(self): |
355 snippet = """ | |
356 package testptr_ir; | |
357 function void t() | |
358 { | |
359 var int* a; | |
360 a = cast<int*>(40); | |
361 *a = 2; | |
362 } | |
363 """ | |
364 block_code = """a0 = alloc | |
365 t1 = 40 | |
366 [a0] = t1 | |
367 t2 = 2 | |
368 deref3 = [a0] | |
369 [deref3] = t2 | |
370 ret """ | |
371 self.expectIR(snippet, block_code) | |
372 | |
373 def testPointerTypeIr2(self): | |
374 snippet = """ | |
375 package testptr_ir; | |
376 type struct {int x,y;}* gpio; | |
377 function void t() | |
378 { | |
379 var gpio a; | |
380 a = cast<gpio>(40); | |
381 a->x = 2; | |
382 a->y = a->x - 14; | |
383 } | |
384 """ | |
385 block_code = """a0 = alloc | |
386 t1 = 40 | |
387 [a0] = t1 | |
388 t2 = 2 | |
389 deref3 = [a0] | |
390 off_x4 = 0 | |
391 adr_x5 = deref3 + off_x4 | |
392 [adr_x5] = t2 | |
393 deref6 = [a0] | |
394 off_x7 = 0 | |
395 adr_x8 = deref6 + off_x7 | |
396 t9 = [adr_x8] | |
397 t10 = 14 | |
398 sub11 = t9 - t10 | |
399 deref12 = [a0] | |
231 | 400 off_y13 = 4 |
230 | 401 adr_y14 = deref12 + off_y13 |
402 [adr_y14] = sub11 | |
403 ret """ | |
404 self.expectIR(snippet, block_code) | |
405 | |
406 def testWrongCast(self): | |
407 snippet = """ | |
408 package testptr_ir; | |
409 type struct {int x,y;}* gpio; | |
410 function void t() | |
411 { | |
412 var gpio a; | |
413 *cast<gpio>(*a); | |
414 } | |
415 """ | |
416 # TODO: remove the duplicate error: | |
417 self.expectErrors(snippet, [7, 7]) | |
418 | |
215 | 419 def testComplexType(self): |
213 | 420 snippet = """ |
421 package testpointer; | |
422 type int my_int; | |
423 | |
424 type struct { | |
425 int x, y; | |
426 } point; | |
427 | |
428 type struct { | |
429 int mem1; | |
430 int memb2; | |
431 point P1; | |
432 } my_struct; | |
433 | |
434 type my_struct* my_sptr; | |
225 | 435 var int* pa; |
213 | 436 |
227 | 437 function void t(int a, int b, my_sptr x) |
213 | 438 { |
439 var my_struct *msp; | |
440 | |
225 | 441 var my_struct u, v; |
442 var point *pt; | |
443 | |
444 pt = &msp->P1; | |
213 | 445 msp = x; |
225 | 446 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 447 x->memb2 = *pa + a * b; |
448 | |
225 | 449 msp->P1.x = a * x->P1.y; |
213 | 450 } |
451 """ | |
215 | 452 self.expectOK(snippet) |
205 | 453 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
454 def testExamples(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
455 """ Test all examples in the c3/examples directory """ |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
456 example_filenames = glob.glob('./c3/examples/*.c3') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
457 for filename in example_filenames: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
458 with open(filename, 'r') as f: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
459 src = f.read() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
460 self.expectOK(src) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
461 |
215 | 462 def test2(self): |
205 | 463 # testsrc2 is valid code: |
215 | 464 snippet = """ |
205 | 465 package test2; |
466 | |
467 function void tst() | |
468 { | |
469 var int a, b; | |
470 a = 2 * 33 - 12; | |
471 b = a * 2 + 13; | |
472 a = b + a; | |
473 if (a > b and b == 3) | |
474 { | |
475 var int x = a; | |
476 x = b * 2 - a; | |
477 a = x*x; | |
478 } | |
479 else | |
480 { | |
481 a = b + a; | |
482 } | |
483 } | |
484 | |
485 """ | |
217 | 486 ircode = self.expectOK(snippet) |
487 self.assertEqual(1, len(ircode.Functions)) | |
167 | 488 |
489 if __name__ == '__main__': | |
243 | 490 unittest.main() |
167 | 491 |
492 |