comparison python/testc3.py @ 215:c1ccb1cb4cef

Major changes in c3 frontend
author Windel Bouwman
date Fri, 05 Jul 2013 13:00:03 +0200
parents 003c8a976fff
children 8b2e5f3cd579
comparison
equal deleted inserted replaced
214:6875360e8390 215:c1ccb1cb4cef
2 import time, ppci, x86, ir 2 import time, ppci, x86, ir
3 import unittest 3 import unittest
4 4
5 testsrc = """package test; 5 testsrc = """package test;
6 6
7 var u32 c, d; 7 /*
8 demo of the source that is correct :)
9 */
10 var int c, d;
8 var double e; 11 var double e;
9 var int f; 12 var int f;
10 13
11 const int A = 1337; 14 const int A = 1337;
12 15
13 function void test1() 16 function void test1()
14 { 17 {
15 var u32 bdd; 18 var int bdd;
16 var int a = 10; 19 var int a = 10;
17 bd = 20; 20 bdd = 20;
18 var int buf; 21 var int buf;
19 var int i; 22 var int i;
20 i = 2; 23 i = 2;
21 var int zero = i - 2; 24 var int zero = i - 2;
22 if (i > 1) 25 if (i > 1)
29 } 32 }
30 33
31 t2(2, 3); 34 t2(2, 3);
32 } 35 }
33 36
34 function int t2(u32 a, u32 b) 37 function int t2(int a, int b)
35 { 38 {
36 if (a > 0) 39 if (a > 0)
37 { 40 {
38 a = 2 + t2(a - 1, 1.0); 41 a = 2 + t2(a - 1, 10);
39 } 42 }
40 43
41 return a + b; 44 return a + b;
42 } 45 }
43 46
47 var int a, b;
48
44 function int t3(int aap, int blah) 49 function int t3(int aap, int blah)
45 { 50 {
46 if (a > blah and blah < 45 + 33 or 33 > aap or 6 and true) 51 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true)
47 { 52 {
48 a = 2 + t2(a - 1); 53 a = 2 + t2(a - 1, 0);
49 } 54 }
50 55
51 return a + b; 56 return a + b;
52 } 57 }
53 58
80 """ 85 """
81 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] 86 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
82 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) 87 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)
83 88
84 class testBuilder(unittest.TestCase): 89 class testBuilder(unittest.TestCase):
85 def setUp(self): 90 def setUp(self):
86 self.diag = ppci.DiagnosticsManager() 91 self.diag = ppci.DiagnosticsManager()
87 self.builder = c3.Builder(self.diag) 92 self.builder = c3.Builder(self.diag)
88 def testSrc(self): 93 self.diag.clear()
89 self.builder.build(testsrc) 94
90 def testFunctArgs(self): 95 def testSrc(self):
96 self.expectOK(testsrc)
97
98 def expectErrors(self, snippet, rows):
99 """ Helper to test for expected errors on rows """
100 ircode = self.builder.build(snippet)
101 actualErrors = [err.row for err in self.diag.diags]
102 if rows != actualErrors:
103 self.diag.printErrors(snippet)
104 self.assertSequenceEqual(rows, actualErrors)
105 self.assertFalse(ircode)
106
107 def expectOK(self, snippet):
108 ircode = self.builder.build(snippet)
109 if not ircode:
110 self.diag.printErrors(snippet)
111 self.assertTrue(ircode)
112
113 def testFunctArgs(self):
91 snippet = """ 114 snippet = """
92 package testargs; 115 package testargs;
93 function void t2(int a, double b) 116 function void t2(int a, double b)
94 { 117 {
95 t2(2, 2); 118 t2(2, 2);
96 t2(2); 119 t2(2);
97 t2(1, 1.2); 120 t2(1, 1.2);
98 } 121 }
99 """ 122 """
100 self.diag.clear() 123 self.expectErrors(snippet, [5, 6])
101 ir = self.builder.build(snippet) 124
102 assert len(self.diag.diags) == 2 125 def testExpressions(self):
103 self.assertEqual(5, self.diag.diags[0].loc.row)
104 self.assertEqual(6, self.diag.diags[1].loc.row)
105
106 def testExpressions(self):
107 snippet = """ 126 snippet = """
108 package test; 127 package test;
109 function void t(int a, double b) 128 function void t(int a, double b)
110 { 129 {
111 var int a2; 130 var int a2;
114 a2 = b * a; 133 a2 = b * a;
115 c = a; 134 c = a;
116 c = b > 1; 135 c = b > 1;
117 } 136 }
118 """ 137 """
119 self.diag.clear() 138 self.expectErrors(snippet, [8, 9, 10])
120 ircode = self.builder.build(snippet) 139
121 self.assertEqual(len(self.diag.diags), 3) 140 def testEmpty(self):
122 self.assertEqual(self.diag.diags[0].loc.row, 8)
123 self.assertEqual(self.diag.diags[1].loc.row, 9)
124 self.assertEqual(self.diag.diags[2].loc.row, 10)
125 self.assertFalse(ircode)
126
127 def testEmpty(self):
128 snippet = """ 141 snippet = """
129 package A 142 package A
130 """ 143 """
131 ircode = self.builder.build(snippet) 144 self.expectErrors(snippet, [3])
132 self.assertFalse(ircode) 145
133 146 def testEmpty2(self):
134 def testEmpty2(self):
135 snippet = "" 147 snippet = ""
136 self.diag.clear() 148 self.expectErrors(snippet, [1])
137 ircode = self.builder.build(snippet) 149
138 self.assertFalse(ircode) 150 def testRedefine(self):
139
140 def testRedefine(self):
141 snippet = """ 151 snippet = """
142 package test; 152 package test;
143 var int a; 153 var int a;
144 var int b; 154 var int b;
145 var int a; 155 var int a;
146 """ 156 """
147 self.diag.clear() 157 self.expectErrors(snippet, [5])
148 ircode = self.builder.build(snippet) 158
149 self.assertFalse(ircode) 159 def testWhile(self):
150 self.assertEqual(len(self.diag.diags), 1)
151 self.assertEqual(self.diag.diags[0].loc.row, 5)
152
153 def testWhile(self):
154 snippet = """ 160 snippet = """
155 package tstwhile; 161 package tstwhile;
156 var int a; 162 var int a;
157 function void t() 163 function void t()
158 { 164 {
170 while(false) 176 while(false)
171 { 177 {
172 } 178 }
173 } 179 }
174 """ 180 """
175 ircode = self.builder.build(snippet) 181 self.expectOK(snippet)
176 if not ircode: 182
177 self.diag.printErrors(snippet) 183 def testIf(self):
178 self.assertTrue(ircode) 184 snippet = """
179 def testIf(self):
180 snippet = """
181 package tstIFF; 185 package tstIFF;
182 function void t(int b) 186 function void t(int b)
183 { 187 {
184 var int a; 188 var int a;
185 a = 2; 189 a = 2;
195 b = 1; 199 b = 1;
196 } 200 }
197 201
198 return b; 202 return b;
199 } 203 }
200 """ 204 """
201 ircode = self.builder.build(snippet) 205 self.expectOK(snippet)
202 if not ircode:
203 self.diag.printErrors(snippet)
204 self.assertTrue(ircode)
205 206
206 @unittest.skip 207 @unittest.skip
207 def testPointerType(self): 208 def testPointerType(self):
208 snippet = """ 209 snippet = """
209 package testpointer; 210 package testpointer;
210 var int* pa; 211 var int* pa;
211 function void t(int a, double b) 212 function void t(int a, double b)
212 { 213 {
213 *pa = 22; 214 *pa = 22;
214 } 215 }
215 """ 216 """
216 self.diag.clear() 217 self.expectOK(snippet)
217 ircode = self.builder.build(snippet) 218
218 if not ircode: 219 @unittest.skip
219 self.diag.printErrors(snippet) 220 def testComplexType(self):
220 self.assertTrue(ircode)
221
222 @unittest.skip
223 def testComplexType(self):
224 snippet = """ 221 snippet = """
225 package testpointer; 222 package testpointer;
226 type int my_int; 223 type int my_int;
227 224
228 type struct { 225 type struct {
246 x->memb2 = *pa + a * b; 243 x->memb2 = *pa + a * b;
247 244
248 mxp->P1.x = a * x->P1.y; 245 mxp->P1.x = a * x->P1.y;
249 } 246 }
250 """ 247 """
251 self.diag.clear() 248 self.expectOK(snippet)
252 ircode = self.builder.build(snippet) 249
253 if not ircode: 250 def test2(self):
254 self.diag.printErrors(snippet)
255 self.assertTrue(ircode)
256
257 def test2(self):
258 # testsrc2 is valid code: 251 # testsrc2 is valid code:
259 testsrc2 = """ 252 snippet = """
260 package test2; 253 package test2;
261 254
262 function void tst() 255 function void tst()
263 { 256 {
264 var int a, b; 257 var int a, b;
276 a = b + a; 269 a = b + a;
277 } 270 }
278 } 271 }
279 272
280 """ 273 """
281 self.diag.clear() 274 self.expectOK(snippet)
282 ir = self.builder.build(testsrc2)
283 self.assertTrue(ir)
284 275
285 if __name__ == '__main__': 276 if __name__ == '__main__':
286 unittest.main() 277 unittest.main()
287 278
288 279