comparison test/testc3.py @ 300:158068af716c

yafm
author Windel Bouwman
date Tue, 03 Dec 2013 18:00:22 +0100
parents 917eab04b8b7
children 6753763d3bec
comparison
equal deleted inserted replaced
299:674789d9ff37 300:158068af716c
1 import c3 1 from ppci.c3 import Builder, Lexer
2 import time
3 import ppci 2 import ppci
4 import ir
5 import unittest 3 import unittest
6 import glob
7 import io 4 import io
8 5
9 testsrc = """module test;
10
11 /*
12 demo of the source that is correct :)
13 */
14 var int c, d;
15 var double e;
16 var int f;
17
18 const int A = 1337;
19
20 function void test1()
21 {
22 var int bdd;
23 var int a = 10;
24 bdd = 20;
25 var int buf;
26 var int i;
27 i = 2;
28 var int zero = i - 2;
29 if (i > 1)
30 {
31 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
32 }
33 else
34 {
35 ;;;
36 }
37
38 t2(2, 3);
39 }
40
41 function int t2(int a, int b)
42 {
43 if (a > 0)
44 {
45 a = 2 + t2(a - 1, 10);
46 }
47
48 return a + b;
49 }
50
51 var int a, b;
52
53 function int t3(int aap, int blah)
54 {
55 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true)
56 {
57 a = 2 + t2(a - 1, 0);
58 }
59
60 return a + b;
61 }
62
63 var int hahaa = 23 * 2;
64
65
66 """
67 6
68 class testLexer(unittest.TestCase): 7 class testLexer(unittest.TestCase):
69 def setUp(self): 8 def setUp(self):
70 diag = ppci.DiagnosticsManager() 9 diag = ppci.DiagnosticsManager()
71 self.l = c3.Lexer(diag) 10 self.l = Lexer(diag)
72 11
73 def testUnexpectedCharacter(self): 12 def testUnexpectedCharacter(self):
74 snippet = io.StringIO(""" var s \u6c34 """) 13 snippet = io.StringIO(""" var s \u6c34 """)
75 with self.assertRaises(ppci.CompilerError): 14 with self.assertRaises(ppci.CompilerError):
76 list(self.l.tokenize(snippet)) 15 list(self.l.tokenize(snippet))
100 39
101 40
102 class testBuilder(unittest.TestCase): 41 class testBuilder(unittest.TestCase):
103 def setUp(self): 42 def setUp(self):
104 self.diag = ppci.DiagnosticsManager() 43 self.diag = ppci.DiagnosticsManager()
105 self.builder = c3.Builder(self.diag) 44 self.builder = Builder(self.diag)
106 self.diag.clear() 45 self.diag.clear()
107
108 def testSrc(self):
109 self.expectOK(testsrc)
110 46
111 def expectErrors(self, snippet, rows): 47 def expectErrors(self, snippet, rows):
112 """ Helper to test for expected errors on rows """ 48 """ Helper to test for expected errors on rows """
113 ircode = list(self.builder.build([io.StringIO(snippet)])) 49 ircode = list(self.builder.build([io.StringIO(snippet)]))
114 actualErrors = [err.row for err in self.diag.diags] 50 actualErrors = [err.row for err in self.diag.diags]
307 a.y = a.x + 2; 243 a.y = a.x + 2;
308 } 244 }
309 """ 245 """
310 self.expectOK(snippet) 246 self.expectOK(snippet)
311 247
312 @unittest.skip('Too strange to handle')
313 def testStructCall(self): 248 def testStructCall(self):
314 snippet = """ 249 snippet = """
315 module teststruct1; 250 module teststruct1;
316 function void t() 251 function void t()
317 { 252 {
435 msp->P1.x = a * x->P1.y; 370 msp->P1.x = a * x->P1.y;
436 } 371 }
437 """ 372 """
438 self.expectOK(snippet) 373 self.expectOK(snippet)
439 374
440 @unittest.skip('To be rewritten')
441 def testExamples(self):
442 """ Test all examples in the c3/examples directory """
443 example_filenames = glob.glob('./c3examples/*.c3')
444 for filename in example_filenames:
445 with open(filename, 'r') as f:
446 self.expectOK(f)
447
448 def test2(self):
449 # testsrc2 is valid code:
450 snippet = """
451 module test2;
452
453 function void tst()
454 {
455 var int a, b;
456 a = 2 * 33 - 12;
457 b = a * 2 + 13;
458 a = b + a;
459 if (a > b and b == 3)
460 {
461 var int x = a;
462 x = b * 2 - a;
463 a = x*x;
464 }
465 else
466 {
467 a = b + a;
468 }
469 }
470
471 """
472 self.expectOK(snippet)
473 375
474 if __name__ == '__main__': 376 if __name__ == '__main__':
475 unittest.main() 377 unittest.main()
476
477