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