Mercurial > lcfOS
annotate test/testc3.py @ 307:e609d5296ee9
Massive rewrite of codegenerator
author | Windel Bouwman |
---|---|
date | Thu, 12 Dec 2013 20:42:56 +0100 |
parents | b145f8e6050b |
children | 2e7f55319858 |
rev | line source |
---|---|
300 | 1 from ppci.c3 import Builder, Lexer |
301 | 2 from target import SimpleTarget |
287 | 3 import ppci |
167 | 4 import unittest |
287 | 5 import io |
148 | 6 |
7 | |
204 | 8 class testLexer(unittest.TestCase): |
293 | 9 def setUp(self): |
10 diag = ppci.DiagnosticsManager() | |
300 | 11 self.l = Lexer(diag) |
293 | 12 |
205 | 13 def testUnexpectedCharacter(self): |
287 | 14 snippet = io.StringIO(""" var s \u6c34 """) |
205 | 15 with self.assertRaises(ppci.CompilerError): |
293 | 16 list(self.l.tokenize(snippet)) |
17 | |
18 def check(self, snippet, toks): | |
19 toks2 = list(tok.typ for tok in self.l.tokenize(io.StringIO(snippet))) | |
20 self.assertSequenceEqual(toks, toks2) | |
205 | 21 |
204 | 22 def testBlockComment(self): |
293 | 23 snippet = """ |
204 | 24 /* Demo */ |
25 var int x = 0; | |
293 | 26 """ |
204 | 27 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] |
293 | 28 self.check(snippet, toks) |
205 | 29 |
204 | 30 def testBlockCommentMultiLine(self): |
293 | 31 snippet = """ |
204 | 32 /* Demo |
33 bla1 | |
34 bla2 | |
35 */ | |
36 var int x = 0; | |
293 | 37 """ |
204 | 38 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] |
293 | 39 self.check(snippet, toks) |
204 | 40 |
287 | 41 |
204 | 42 class testBuilder(unittest.TestCase): |
215 | 43 def setUp(self): |
44 self.diag = ppci.DiagnosticsManager() | |
301 | 45 self.builder = Builder(self.diag, SimpleTarget()) |
215 | 46 self.diag.clear() |
47 | |
306 | 48 def makeFileList(self, snippet): |
49 """ Try to make a list with opened files """ | |
50 if type(snippet) is list: | |
51 l2 = [] | |
52 for s in snippet: | |
53 if type(s) is str: | |
54 l2.append(io.StringIO(s)) | |
55 else: | |
56 l2.append(s) | |
57 return l2 | |
58 else: | |
59 return [io.StringIO(snippet)] | |
60 | |
215 | 61 def expectErrors(self, snippet, rows): |
62 """ Helper to test for expected errors on rows """ | |
287 | 63 ircode = list(self.builder.build([io.StringIO(snippet)])) |
215 | 64 actualErrors = [err.row for err in self.diag.diags] |
65 if rows != actualErrors: | |
306 | 66 self.diag.printErrors() |
215 | 67 self.assertSequenceEqual(rows, actualErrors) |
287 | 68 # self.assertFalse(all(ircode)) |
215 | 69 |
287 | 70 def expectOK(self, snippet): |
306 | 71 """ Expect a snippet to be OK """ |
72 ircode = list(self.builder.build(self.makeFileList(snippet))) | |
73 if len(self.diag.diags) > 0: | |
74 self.diag.printErrors() | |
287 | 75 self.assertTrue(all(ircode)) |
306 | 76 self.assertEqual(0, len(self.diag.diags)) |
217 | 77 return ircode |
215 | 78 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
79 def testPackage(self): |
284 | 80 p1 = """module p1; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
81 type int A; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
82 """ |
284 | 83 p2 = """module p2; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
84 import p1; |
306 | 85 var p1.A b; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
86 """ |
306 | 87 self.expectOK([p1, p2]) |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
88 |
288 | 89 def testPackageMutual(self): |
90 p1 = """module p1; | |
91 import p2; | |
92 type int A; | |
93 var p2.B b; | |
94 """ | |
95 p2 = """module p2; | |
96 import p1; | |
97 var p1.A a; | |
98 """ | |
306 | 99 self.expectOK([p1, p2]) |
288 | 100 |
293 | 101 def testPackageNotExists(self): |
102 p1 = """module p1; | |
103 import p23; | |
104 """ | |
306 | 105 self.expectErrors(p1, [0]) |
293 | 106 |
215 | 107 def testFunctArgs(self): |
287 | 108 snippet = """ |
284 | 109 module testargs; |
167 | 110 function void t2(int a, double b) |
111 { | |
112 t2(2, 2); | |
113 t2(2); | |
114 t2(1, 1.2); | |
115 } | |
287 | 116 """ |
117 self.expectErrors(snippet, [5, 6]) | |
148 | 118 |
303 | 119 def testReturn(self): |
120 snippet = """ | |
121 module testreturn; | |
122 function void t() | |
123 { | |
124 return; | |
125 } | |
126 """ | |
127 self.expectOK(snippet) | |
128 | |
129 def testReturn2(self): | |
130 snippet = """ | |
131 module testreturn; | |
132 function int t() | |
133 { | |
134 return 2; | |
135 } | |
136 """ | |
137 self.expectOK(snippet) | |
138 | |
215 | 139 def testExpressions(self): |
287 | 140 snippet = """ |
284 | 141 module test; |
167 | 142 function void t(int a, double b) |
143 { | |
144 var int a2; | |
145 var bool c; | |
146 | |
147 a2 = b * a; | |
148 c = a; | |
149 } | |
287 | 150 """ |
307 | 151 self.expectErrors(snippet, [8, 9]) |
205 | 152 |
230 | 153 def testExpression1(self): |
287 | 154 snippet = """ |
284 | 155 module testexpr1; |
230 | 156 function void t() |
157 { | |
158 var int a, b, c; | |
159 a = 1; | |
160 b = a * 2 + a * a; | |
161 c = b * a - 3; | |
162 } | |
287 | 163 """ |
164 self.expectOK(snippet) | |
230 | 165 |
215 | 166 def testEmpty(self): |
287 | 167 snippet = """ |
168 module A | |
169 """ | |
170 self.expectErrors(snippet, [3]) | |
205 | 171 |
215 | 172 def testEmpty2(self): |
287 | 173 snippet = "" |
174 self.expectErrors(snippet, [1]) | |
205 | 175 |
215 | 176 def testRedefine(self): |
287 | 177 snippet = """ |
178 module test; | |
179 var int a; | |
180 var int b; | |
181 var int a; | |
182 """ | |
183 self.expectErrors(snippet, [5]) | |
205 | 184 |
215 | 185 def testWhile(self): |
287 | 186 snippet = """ |
187 module tstwhile; | |
188 function void t() | |
189 { | |
306 | 190 var int i; |
191 i = 0; | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
193 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
194 i = i + 3; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
195 } |
307 | 196 } |
197 """ | |
198 self.expectOK(snippet) | |
205 | 199 |
307 | 200 def testWhile2(self): |
201 snippet = """ | |
202 module tstwhile; | |
203 function void t() | |
204 { | |
205 | 205 while(true) |
206 { | |
207 } | |
208 | |
209 while(false) | |
210 { | |
211 } | |
287 | 212 } |
213 """ | |
214 self.expectOK(snippet) | |
215 | 215 |
216 def testIf(self): | |
217 snippet = """ | |
287 | 218 module tstIFF; |
219 function void t(int b) | |
220 { | |
180 | 221 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
222 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
223 if (a > b) |
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 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
226 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
227 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
228 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
229 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
230 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
231 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
232 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
233 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
234 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
235 return b; |
287 | 236 } |
215 | 237 """ |
238 self.expectOK(snippet) | |
225 | 239 |
240 def testTypeDef(self): | |
241 snippet = """ | |
284 | 242 module testtypedef; |
225 | 243 type int my_int; |
244 function void t() | |
245 { | |
246 var my_int a; | |
247 var int b; | |
248 a = 2; | |
249 b = a + 2; | |
250 } | |
251 """ | |
252 self.expectOK(snippet) | |
253 | |
230 | 254 def testLocalVariable(self): |
255 snippet = """ | |
284 | 256 module testlocalvar; |
230 | 257 function void t() |
258 { | |
259 var int a, b; | |
260 a = 2; | |
261 b = a + 2; | |
262 } | |
263 """ | |
268 | 264 self.expectOK(snippet) |
230 | 265 |
249 | 266 def testUnknownType(self): |
284 | 267 snippet = """module testlocalvar; |
249 | 268 function void t() |
269 { | |
270 var int2 a; | |
271 } | |
272 """ | |
273 self.expectErrors(snippet, [4]) | |
274 | |
230 | 275 def testStruct1(self): |
276 snippet = """ | |
284 | 277 module teststruct1; |
230 | 278 function void t() |
279 { | |
280 var struct {int x, y;} a; | |
281 a.x = 2; | |
282 a.y = a.x + 2; | |
283 } | |
284 """ | |
268 | 285 self.expectOK(snippet) |
230 | 286 |
293 | 287 def testStructCall(self): |
288 snippet = """ | |
289 module teststruct1; | |
290 function void t() | |
291 { | |
292 var struct {int x, y;} a; | |
307 | 293 a.x(9); |
293 | 294 } |
295 """ | |
296 self.expectOK(snippet) | |
297 | |
225 | 298 def testPointerType1(self): |
299 snippet = """ | |
284 | 300 module testpointer1; |
225 | 301 var int* pa; |
302 function void t() | |
303 { | |
304 var int a; | |
305 pa = &a; | |
306 *pa = 22; | |
307 } | |
308 """ | |
309 self.expectOK(snippet) | |
310 | |
215 | 311 def testPointerType(self): |
213 | 312 snippet = """ |
284 | 313 module testpointer; |
225 | 314 var int* pa, pb; |
213 | 315 function void t(int a, double b) |
316 { | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
317 var int a2; |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
318 a2 = a; // parameters cannot be escaped for now.. |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
319 pa = &a2; |
225 | 320 pb = pa; |
213 | 321 *pa = 22; |
322 } | |
323 """ | |
215 | 324 self.expectOK(snippet) |
213 | 325 |
221 | 326 def testPointerTypeInCorrect(self): |
327 snippet = """ | |
284 | 328 module testpointerincorrect; |
221 | 329 var int* pa; |
330 function void t(int a, double b) | |
331 { | |
332 pa = 2; // type conflict | |
333 pa = &a; | |
307 | 334 pa = &2; // No valid lvalue |
221 | 335 &a = pa; // No valid lvalue |
336 **pa = 22; // Cannot deref int | |
337 } | |
338 """ | |
307 | 339 self.expectErrors(snippet, [6, 8, 9, 10]) |
221 | 340 |
230 | 341 def testPointerTypeIr(self): |
342 snippet = """ | |
284 | 343 module testptr_ir; |
230 | 344 function void t() |
345 { | |
346 var int* a; | |
347 a = cast<int*>(40); | |
348 *a = 2; | |
349 } | |
350 """ | |
268 | 351 self.expectOK(snippet) |
230 | 352 |
353 def testPointerTypeIr2(self): | |
354 snippet = """ | |
284 | 355 module testptr_ir; |
230 | 356 type struct {int x,y;}* gpio; |
357 function void t() | |
358 { | |
359 var gpio a; | |
360 a = cast<gpio>(40); | |
361 a->x = 2; | |
362 a->y = a->x - 14; | |
363 } | |
364 """ | |
268 | 365 self.expectOK(snippet) |
230 | 366 |
367 def testWrongCast(self): | |
368 snippet = """ | |
284 | 369 module testptr_ir; |
230 | 370 type struct {int x,y;}* gpio; |
371 function void t() | |
372 { | |
373 var gpio a; | |
374 *cast<gpio>(*a); | |
375 } | |
376 """ | |
377 self.expectErrors(snippet, [7, 7]) | |
378 | |
215 | 379 def testComplexType(self): |
213 | 380 snippet = """ |
284 | 381 module testpointer; |
213 | 382 type int my_int; |
383 | |
384 type struct { | |
385 int x, y; | |
386 } point; | |
387 | |
388 type struct { | |
389 int mem1; | |
390 int memb2; | |
391 point P1; | |
392 } my_struct; | |
393 | |
394 type my_struct* my_sptr; | |
225 | 395 var int* pa; |
213 | 396 |
227 | 397 function void t(int a, int b, my_sptr x) |
213 | 398 { |
399 var my_struct *msp; | |
400 | |
225 | 401 var my_struct u, v; |
402 var point *pt; | |
403 | |
404 pt = &msp->P1; | |
213 | 405 msp = x; |
225 | 406 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 407 x->memb2 = *pa + a * b; |
408 | |
225 | 409 msp->P1.x = a * x->P1.y; |
213 | 410 } |
411 """ | |
215 | 412 self.expectOK(snippet) |
205 | 413 |
167 | 414 |
415 if __name__ == '__main__': | |
243 | 416 unittest.main() |