Mercurial > lcfOS
annotate test/testc3.py @ 398:c0d9837acde8
x86 target refactor
author | Windel Bouwman |
---|---|
date | Thu, 29 May 2014 12:13:37 +0200 |
parents | fb3c1f029b30 |
children |
rev | line source |
---|---|
167 | 1 import unittest |
355 | 2 import logging |
287 | 3 import io |
342 | 4 from ppci.c3 import Builder, Lexer |
5 from ppci.target import SimpleTarget | |
6 import ppci | |
148 | 7 |
8 | |
204 | 9 class testLexer(unittest.TestCase): |
293 | 10 def setUp(self): |
11 diag = ppci.DiagnosticsManager() | |
300 | 12 self.l = Lexer(diag) |
293 | 13 |
205 | 14 def testUnexpectedCharacter(self): |
287 | 15 snippet = io.StringIO(""" var s \u6c34 """) |
205 | 16 with self.assertRaises(ppci.CompilerError): |
396 | 17 list(self.l.lex(snippet)) |
293 | 18 |
19 def check(self, snippet, toks): | |
396 | 20 toks2 = list(tok.typ for tok in self.l.lex(io.StringIO(snippet))) |
293 | 21 self.assertSequenceEqual(toks, toks2) |
205 | 22 |
204 | 23 def testBlockComment(self): |
293 | 24 snippet = """ |
204 | 25 /* Demo */ |
26 var int x = 0; | |
293 | 27 """ |
396 | 28 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'EOF'] |
293 | 29 self.check(snippet, toks) |
205 | 30 |
204 | 31 def testBlockCommentMultiLine(self): |
293 | 32 snippet = """ |
204 | 33 /* Demo |
34 bla1 | |
35 bla2 | |
36 */ | |
37 var int x = 0; | |
293 | 38 """ |
396 | 39 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'EOF'] |
293 | 40 self.check(snippet, toks) |
204 | 41 |
287 | 42 |
204 | 43 class testBuilder(unittest.TestCase): |
215 | 44 def setUp(self): |
45 self.diag = ppci.DiagnosticsManager() | |
301 | 46 self.builder = Builder(self.diag, SimpleTarget()) |
215 | 47 self.diag.clear() |
355 | 48 # Add a null logging handler to disable warning log messages: |
49 nh = logging.NullHandler() | |
50 logging.getLogger().addHandler(nh) | |
215 | 51 |
306 | 52 def makeFileList(self, snippet): |
53 """ Try to make a list with opened files """ | |
54 if type(snippet) is list: | |
55 l2 = [] | |
56 for s in snippet: | |
57 if type(s) is str: | |
58 l2.append(io.StringIO(s)) | |
59 else: | |
60 l2.append(s) | |
61 return l2 | |
62 else: | |
63 return [io.StringIO(snippet)] | |
64 | |
215 | 65 def expectErrors(self, snippet, rows): |
66 """ Helper to test for expected errors on rows """ | |
389 | 67 list(self.builder.build([io.StringIO(snippet)])) |
215 | 68 actualErrors = [err.row for err in self.diag.diags] |
69 if rows != actualErrors: | |
306 | 70 self.diag.printErrors() |
215 | 71 self.assertSequenceEqual(rows, actualErrors) |
72 | |
287 | 73 def expectOK(self, snippet): |
306 | 74 """ Expect a snippet to be OK """ |
75 ircode = list(self.builder.build(self.makeFileList(snippet))) | |
76 if len(self.diag.diags) > 0: | |
77 self.diag.printErrors() | |
287 | 78 self.assertTrue(all(ircode)) |
306 | 79 self.assertEqual(0, len(self.diag.diags)) |
217 | 80 return ircode |
215 | 81 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
82 def testPackage(self): |
284 | 83 p1 = """module p1; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
84 type int A; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
85 """ |
284 | 86 p2 = """module p2; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
87 import p1; |
306 | 88 var p1.A b; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
89 """ |
306 | 90 self.expectOK([p1, p2]) |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
91 |
288 | 92 def testPackageMutual(self): |
93 p1 = """module p1; | |
94 import p2; | |
95 type int A; | |
96 var p2.B b; | |
97 """ | |
98 p2 = """module p2; | |
99 import p1; | |
100 var p1.A a; | |
101 """ | |
306 | 102 self.expectOK([p1, p2]) |
288 | 103 |
313 | 104 def testConstant(self): |
105 snip = """module C; | |
106 const int a = 2; | |
107 """ | |
389 | 108 self.expectOK(snip) |
313 | 109 |
110 @unittest.skip('Not checked yet') | |
111 def testConstantMutual(self): | |
112 snip = """module C; | |
113 const int a = b + 1; | |
114 const int b = a + 1; | |
115 function void f() | |
116 { | |
117 return b; | |
118 } | |
119 """ | |
389 | 120 self.expectOK(snip) |
313 | 121 |
293 | 122 def testPackageNotExists(self): |
123 p1 = """module p1; | |
124 import p23; | |
125 """ | |
306 | 126 self.expectErrors(p1, [0]) |
293 | 127 |
215 | 128 def testFunctArgs(self): |
287 | 129 snippet = """ |
284 | 130 module testargs; |
167 | 131 function void t2(int a, double b) |
132 { | |
133 t2(2, 2); | |
134 t2(2); | |
135 t2(1, 1.2); | |
136 } | |
287 | 137 """ |
138 self.expectErrors(snippet, [5, 6]) | |
148 | 139 |
303 | 140 def testReturn(self): |
141 snippet = """ | |
142 module testreturn; | |
143 function void t() | |
144 { | |
145 return; | |
146 } | |
147 """ | |
148 self.expectOK(snippet) | |
149 | |
150 def testReturn2(self): | |
151 snippet = """ | |
152 module testreturn; | |
153 function int t() | |
154 { | |
155 return 2; | |
156 } | |
157 """ | |
158 self.expectOK(snippet) | |
159 | |
215 | 160 def testExpressions(self): |
287 | 161 snippet = """ |
284 | 162 module test; |
167 | 163 function void t(int a, double b) |
164 { | |
165 var int a2; | |
166 var bool c; | |
167 | |
168 a2 = b * a; | |
169 c = a; | |
170 } | |
287 | 171 """ |
307 | 172 self.expectErrors(snippet, [8, 9]) |
205 | 173 |
230 | 174 def testExpression1(self): |
287 | 175 snippet = """ |
284 | 176 module testexpr1; |
230 | 177 function void t() |
178 { | |
179 var int a, b, c; | |
180 a = 1; | |
181 b = a * 2 + a * a; | |
182 c = b * a - 3; | |
183 } | |
287 | 184 """ |
185 self.expectOK(snippet) | |
230 | 186 |
215 | 187 def testEmpty(self): |
287 | 188 snippet = """ |
189 module A | |
190 """ | |
191 self.expectErrors(snippet, [3]) | |
205 | 192 |
215 | 193 def testEmpty2(self): |
287 | 194 snippet = "" |
195 self.expectErrors(snippet, [1]) | |
205 | 196 |
215 | 197 def testRedefine(self): |
287 | 198 snippet = """ |
199 module test; | |
200 var int a; | |
201 var int b; | |
202 var int a; | |
203 """ | |
204 self.expectErrors(snippet, [5]) | |
205 | 205 |
215 | 206 def testWhile(self): |
287 | 207 snippet = """ |
208 module tstwhile; | |
209 function void t() | |
210 { | |
306 | 211 var int i; |
212 i = 0; | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
213 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
214 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
215 i = i + 3; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
216 } |
307 | 217 } |
218 """ | |
219 self.expectOK(snippet) | |
205 | 220 |
307 | 221 def testWhile2(self): |
222 snippet = """ | |
223 module tstwhile; | |
224 function void t() | |
225 { | |
205 | 226 while(true) |
227 { | |
228 } | |
229 | |
230 while(false) | |
231 { | |
232 } | |
287 | 233 } |
234 """ | |
235 self.expectOK(snippet) | |
215 | 236 |
237 def testIf(self): | |
238 snippet = """ | |
287 | 239 module tstIFF; |
240 function void t(int b) | |
241 { | |
180 | 242 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
243 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
244 if (a > b) |
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 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
247 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
248 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
249 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
250 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
251 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
252 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
253 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
254 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
255 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
256 return b; |
287 | 257 } |
215 | 258 """ |
259 self.expectOK(snippet) | |
225 | 260 |
308 | 261 def testAndCondition(self): |
262 snippet = """ | |
263 module tst; | |
264 function void t() { | |
265 if (4 > 3 and 1 < 10) { | |
266 } | |
267 } | |
268 """ | |
269 self.expectOK(snippet) | |
270 | |
271 def testOrCondition(self): | |
272 snippet = """ | |
273 module tst; | |
274 function void t() { | |
275 if (3 > 4 or 3 < 10) { | |
276 } | |
277 } | |
278 """ | |
279 self.expectOK(snippet) | |
280 | |
281 def testNonBoolCondition(self): | |
282 snippet = """ | |
283 module tst; | |
284 function void t() { | |
311 | 285 if (3+3) { |
308 | 286 } |
287 } | |
288 """ | |
289 self.expectErrors(snippet, [4]) | |
290 | |
225 | 291 def testTypeDef(self): |
292 snippet = """ | |
284 | 293 module testtypedef; |
225 | 294 type int my_int; |
295 function void t() | |
296 { | |
297 var my_int a; | |
298 var int b; | |
299 a = 2; | |
300 b = a + 2; | |
301 } | |
302 """ | |
303 self.expectOK(snippet) | |
304 | |
230 | 305 def testLocalVariable(self): |
306 snippet = """ | |
284 | 307 module testlocalvar; |
230 | 308 function void t() |
309 { | |
310 var int a, b; | |
311 a = 2; | |
312 b = a + 2; | |
313 } | |
314 """ | |
268 | 315 self.expectOK(snippet) |
230 | 316 |
249 | 317 def testUnknownType(self): |
284 | 318 snippet = """module testlocalvar; |
249 | 319 function void t() |
320 { | |
321 var int2 a; | |
322 } | |
323 """ | |
324 self.expectErrors(snippet, [4]) | |
325 | |
230 | 326 def testStruct1(self): |
327 snippet = """ | |
284 | 328 module teststruct1; |
230 | 329 function void t() |
330 { | |
331 var struct {int x, y;} a; | |
332 a.x = 2; | |
333 a.y = a.x + 2; | |
334 } | |
335 """ | |
268 | 336 self.expectOK(snippet) |
230 | 337 |
308 | 338 def testStruct2(self): |
339 """ Select struct member from non struct type """ | |
340 snippet = """ | |
341 module teststruct1; | |
342 function void t() { | |
343 var int a; | |
344 a.z = 2; | |
345 } | |
346 """ | |
347 self.expectErrors(snippet, [5]) | |
348 | |
354 | 349 def testArray(self): |
350 snippet = """ | |
351 module testarray; | |
352 function void t() | |
353 { | |
354 var int[100] x; | |
355 var int a, b; | |
356 a = 2; | |
357 b = x[a*2+9 - a] * x[22+12]; | |
358 x[1] = x[2]; | |
359 } | |
360 """ | |
361 self.expectOK(snippet) | |
362 | |
363 def testArrayFail(self): | |
364 snippet = """ | |
365 module testarray; | |
366 function void t() | |
367 { | |
368 var bool c; | |
369 c = false; | |
370 var int[100] x; | |
371 x[1] = x[c]; | |
372 } | |
373 """ | |
374 self.expectErrors(snippet, [8]) | |
375 | |
376 def testArrayFail2(self): | |
377 snippet = """ | |
378 module testarray; | |
379 function void t() | |
380 { | |
381 var int c; | |
382 var int x; | |
383 c = x[2]; | |
384 } | |
385 """ | |
386 self.expectErrors(snippet, [7]) | |
387 | |
355 | 388 @unittest.skip('TODO') |
389 def testArrayFail3(self): | |
390 snippet = """ | |
391 module testarray; | |
392 function void t() | |
393 { | |
393 | 394 var int c[20]; |
355 | 395 } |
396 """ | |
397 self.expectErrors(snippet, [7]) | |
398 | |
293 | 399 def testStructCall(self): |
400 snippet = """ | |
401 module teststruct1; | |
402 function void t() | |
403 { | |
404 var struct {int x, y;} a; | |
307 | 405 a.x(9); |
293 | 406 } |
407 """ | |
308 | 408 self.expectErrors(snippet, [6]) |
293 | 409 |
353 | 410 def testString(self): |
411 snippet = """ | |
412 module teststring; | |
413 function void t() | |
414 { | |
415 var string a; | |
416 a = "Hello world"; | |
417 print(a); | |
418 print("Moi"); | |
419 } | |
393 | 420 |
353 | 421 function void print(string a) |
422 { | |
423 } | |
424 """ | |
425 self.expectOK(snippet) | |
426 | |
393 | 427 def testSizeof1(self): |
428 snippet = """ | |
429 module testsizeof; | |
430 | |
431 function void t() | |
432 { | |
433 var int a; | |
434 a = sizeof(int*); | |
435 } | |
436 """ | |
437 self.expectOK(snippet) | |
438 | |
439 def testSizeof2(self): | |
440 snippet = """ | |
441 module testsizeof2; | |
442 | |
443 function void t() | |
444 { | |
445 sizeof(int*) = 2; | |
446 } | |
447 """ | |
448 self.expectErrors(snippet, [6]) | |
449 | |
450 @unittest.skip('TODO: Too hard') | |
451 def testWrongVarUse(self): | |
452 snippet = """ | |
453 module testsizeof; | |
454 | |
455 function void t() | |
456 { | |
457 int a = 1; | |
458 } | |
459 """ | |
460 self.expectOK(snippet) | |
461 | |
225 | 462 def testPointerType1(self): |
463 snippet = """ | |
284 | 464 module testpointer1; |
225 | 465 var int* pa; |
466 function void t() | |
467 { | |
468 var int a; | |
469 pa = &a; | |
470 *pa = 22; | |
308 | 471 a = *pa + *pa * 8; |
225 | 472 } |
473 """ | |
474 self.expectOK(snippet) | |
475 | |
215 | 476 def testPointerType(self): |
213 | 477 snippet = """ |
284 | 478 module testpointer; |
225 | 479 var int* pa, pb; |
213 | 480 function void t(int a, double b) |
481 { | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
482 var int a2; |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
483 a2 = a; // parameters cannot be escaped for now.. |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
484 pa = &a2; |
225 | 485 pb = pa; |
213 | 486 *pa = 22; |
487 } | |
488 """ | |
215 | 489 self.expectOK(snippet) |
213 | 490 |
221 | 491 def testPointerTypeInCorrect(self): |
492 snippet = """ | |
284 | 493 module testpointerincorrect; |
221 | 494 var int* pa; |
495 function void t(int a, double b) | |
496 { | |
497 pa = 2; // type conflict | |
498 pa = &a; | |
307 | 499 pa = &2; // No valid lvalue |
221 | 500 &a = pa; // No valid lvalue |
501 **pa = 22; // Cannot deref int | |
502 } | |
503 """ | |
307 | 504 self.expectErrors(snippet, [6, 8, 9, 10]) |
221 | 505 |
230 | 506 def testPointerTypeIr(self): |
507 snippet = """ | |
284 | 508 module testptr_ir; |
230 | 509 function void t() |
510 { | |
511 var int* a; | |
512 a = cast<int*>(40); | |
513 *a = 2; | |
514 } | |
515 """ | |
268 | 516 self.expectOK(snippet) |
230 | 517 |
518 def testPointerTypeIr2(self): | |
519 snippet = """ | |
284 | 520 module testptr_ir; |
230 | 521 type struct {int x,y;}* gpio; |
522 function void t() | |
523 { | |
524 var gpio a; | |
525 a = cast<gpio>(40); | |
526 a->x = 2; | |
527 a->y = a->x - 14; | |
528 } | |
529 """ | |
268 | 530 self.expectOK(snippet) |
230 | 531 |
393 | 532 def testPointerArithmatic(self): |
533 snippet = """ | |
534 module testpointerarithmatic; | |
535 function void t() | |
536 { | |
537 var int* pa; | |
538 *(pa+2) = 2; | |
539 } | |
540 """ | |
541 self.expectOK(snippet) | |
542 | |
230 | 543 def testWrongCast(self): |
544 snippet = """ | |
284 | 545 module testptr_ir; |
230 | 546 type struct {int x,y;}* gpio; |
547 function void t() | |
548 { | |
549 var gpio a; | |
550 *cast<gpio>(*a); | |
551 } | |
552 """ | |
308 | 553 self.expectErrors(snippet, [7]) |
230 | 554 |
389 | 555 def testLinkedList(self): |
556 """ | |
557 Test if a struct can contain a field with a pointer to itself | |
558 """ | |
559 snippet = """ | |
560 module testlinkedlist; | |
561 | |
562 type struct { | |
563 int x; | |
564 list_t* next; | |
565 } list_t; | |
566 | |
567 function void t() | |
568 { | |
569 var list_t* a; | |
570 a = a->next; | |
571 } | |
572 """ | |
573 self.expectOK(snippet) | |
574 | |
575 def testInfiniteStruct(self): | |
576 """ | |
577 Test if a struct can contain a field with itself as type? | |
578 This should not be possible! | |
579 """ | |
580 snippet = """ | |
581 module testnestedstruct; | |
582 | |
583 type struct { | |
584 int x; | |
585 list_t inner; | |
586 } list_t; | |
587 | |
588 """ | |
589 self.expectErrors(snippet, [0]) | |
590 | |
591 def testMutualStructs(self): | |
592 """ | |
593 Test if two structs can contain each other! | |
594 This should not be possible! | |
595 """ | |
596 snippet = """ | |
597 module testnestedstruct; | |
598 | |
599 type struct { | |
600 int x; | |
601 B other; | |
602 } A; | |
603 | |
604 type struct { | |
605 int x; | |
606 A other; | |
607 } B; | |
608 | |
609 """ | |
610 self.expectErrors(snippet, [0]) | |
611 | |
215 | 612 def testComplexType(self): |
213 | 613 snippet = """ |
284 | 614 module testpointer; |
213 | 615 type int my_int; |
616 | |
617 type struct { | |
618 int x, y; | |
619 } point; | |
620 | |
621 type struct { | |
622 int mem1; | |
623 int memb2; | |
624 point P1; | |
625 } my_struct; | |
626 | |
627 type my_struct* my_sptr; | |
225 | 628 var int* pa; |
213 | 629 |
227 | 630 function void t(int a, int b, my_sptr x) |
213 | 631 { |
632 var my_struct *msp; | |
633 | |
225 | 634 var my_struct u, v; |
635 var point *pt; | |
636 | |
637 pt = &msp->P1; | |
213 | 638 msp = x; |
225 | 639 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 640 x->memb2 = *pa + a * b; |
641 | |
225 | 642 msp->P1.x = a * x->P1.y; |
213 | 643 } |
644 """ | |
215 | 645 self.expectOK(snippet) |
205 | 646 |
167 | 647 |
648 if __name__ == '__main__': | |
243 | 649 unittest.main() |