Mercurial > lcfOS
annotate test/testc3.py @ 341:4d204f6f7d4e devel
Rewrite of assembler parts
author | Windel Bouwman |
---|---|
date | Fri, 28 Feb 2014 18:07:14 +0100 |
parents | 04cf4d26a3bc |
children | 86b02c98a717 |
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 |
313 | 101 def testConstant(self): |
102 snip = """module C; | |
103 const int a = 2; | |
104 """ | |
105 i = self.expectOK(snip) | |
106 | |
107 @unittest.skip('Not checked yet') | |
108 def testConstantMutual(self): | |
109 snip = """module C; | |
110 const int a = b + 1; | |
111 const int b = a + 1; | |
112 function void f() | |
113 { | |
114 return b; | |
115 } | |
116 """ | |
117 i = self.expectOK(snip) | |
118 | |
293 | 119 def testPackageNotExists(self): |
120 p1 = """module p1; | |
121 import p23; | |
122 """ | |
306 | 123 self.expectErrors(p1, [0]) |
293 | 124 |
215 | 125 def testFunctArgs(self): |
287 | 126 snippet = """ |
284 | 127 module testargs; |
167 | 128 function void t2(int a, double b) |
129 { | |
130 t2(2, 2); | |
131 t2(2); | |
132 t2(1, 1.2); | |
133 } | |
287 | 134 """ |
135 self.expectErrors(snippet, [5, 6]) | |
148 | 136 |
303 | 137 def testReturn(self): |
138 snippet = """ | |
139 module testreturn; | |
140 function void t() | |
141 { | |
142 return; | |
143 } | |
144 """ | |
145 self.expectOK(snippet) | |
146 | |
147 def testReturn2(self): | |
148 snippet = """ | |
149 module testreturn; | |
150 function int t() | |
151 { | |
152 return 2; | |
153 } | |
154 """ | |
155 self.expectOK(snippet) | |
156 | |
215 | 157 def testExpressions(self): |
287 | 158 snippet = """ |
284 | 159 module test; |
167 | 160 function void t(int a, double b) |
161 { | |
162 var int a2; | |
163 var bool c; | |
164 | |
165 a2 = b * a; | |
166 c = a; | |
167 } | |
287 | 168 """ |
307 | 169 self.expectErrors(snippet, [8, 9]) |
205 | 170 |
230 | 171 def testExpression1(self): |
287 | 172 snippet = """ |
284 | 173 module testexpr1; |
230 | 174 function void t() |
175 { | |
176 var int a, b, c; | |
177 a = 1; | |
178 b = a * 2 + a * a; | |
179 c = b * a - 3; | |
180 } | |
287 | 181 """ |
182 self.expectOK(snippet) | |
230 | 183 |
215 | 184 def testEmpty(self): |
287 | 185 snippet = """ |
186 module A | |
187 """ | |
188 self.expectErrors(snippet, [3]) | |
205 | 189 |
215 | 190 def testEmpty2(self): |
287 | 191 snippet = "" |
192 self.expectErrors(snippet, [1]) | |
205 | 193 |
215 | 194 def testRedefine(self): |
287 | 195 snippet = """ |
196 module test; | |
197 var int a; | |
198 var int b; | |
199 var int a; | |
200 """ | |
201 self.expectErrors(snippet, [5]) | |
205 | 202 |
215 | 203 def testWhile(self): |
287 | 204 snippet = """ |
205 module tstwhile; | |
206 function void t() | |
207 { | |
306 | 208 var int i; |
209 i = 0; | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
210 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
211 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
212 i = i + 3; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
213 } |
307 | 214 } |
215 """ | |
216 self.expectOK(snippet) | |
205 | 217 |
307 | 218 def testWhile2(self): |
219 snippet = """ | |
220 module tstwhile; | |
221 function void t() | |
222 { | |
205 | 223 while(true) |
224 { | |
225 } | |
226 | |
227 while(false) | |
228 { | |
229 } | |
287 | 230 } |
231 """ | |
232 self.expectOK(snippet) | |
215 | 233 |
234 def testIf(self): | |
235 snippet = """ | |
287 | 236 module tstIFF; |
237 function void t(int b) | |
238 { | |
180 | 239 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
240 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
241 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
242 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
243 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
244 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
245 b = 2; |
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 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
248 else |
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 b = 1; |
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 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
253 return b; |
287 | 254 } |
215 | 255 """ |
256 self.expectOK(snippet) | |
225 | 257 |
308 | 258 def testAndCondition(self): |
259 snippet = """ | |
260 module tst; | |
261 function void t() { | |
262 if (4 > 3 and 1 < 10) { | |
263 } | |
264 } | |
265 """ | |
266 self.expectOK(snippet) | |
267 | |
268 def testOrCondition(self): | |
269 snippet = """ | |
270 module tst; | |
271 function void t() { | |
272 if (3 > 4 or 3 < 10) { | |
273 } | |
274 } | |
275 """ | |
276 self.expectOK(snippet) | |
277 | |
278 def testNonBoolCondition(self): | |
279 snippet = """ | |
280 module tst; | |
281 function void t() { | |
311 | 282 if (3+3) { |
308 | 283 } |
284 } | |
285 """ | |
286 self.expectErrors(snippet, [4]) | |
287 | |
225 | 288 def testTypeDef(self): |
289 snippet = """ | |
284 | 290 module testtypedef; |
225 | 291 type int my_int; |
292 function void t() | |
293 { | |
294 var my_int a; | |
295 var int b; | |
296 a = 2; | |
297 b = a + 2; | |
298 } | |
299 """ | |
300 self.expectOK(snippet) | |
301 | |
230 | 302 def testLocalVariable(self): |
303 snippet = """ | |
284 | 304 module testlocalvar; |
230 | 305 function void t() |
306 { | |
307 var int a, b; | |
308 a = 2; | |
309 b = a + 2; | |
310 } | |
311 """ | |
268 | 312 self.expectOK(snippet) |
230 | 313 |
249 | 314 def testUnknownType(self): |
284 | 315 snippet = """module testlocalvar; |
249 | 316 function void t() |
317 { | |
318 var int2 a; | |
319 } | |
320 """ | |
321 self.expectErrors(snippet, [4]) | |
322 | |
230 | 323 def testStruct1(self): |
324 snippet = """ | |
284 | 325 module teststruct1; |
230 | 326 function void t() |
327 { | |
328 var struct {int x, y;} a; | |
329 a.x = 2; | |
330 a.y = a.x + 2; | |
331 } | |
332 """ | |
268 | 333 self.expectOK(snippet) |
230 | 334 |
308 | 335 def testStruct2(self): |
336 """ Select struct member from non struct type """ | |
337 snippet = """ | |
338 module teststruct1; | |
339 function void t() { | |
340 var int a; | |
341 a.z = 2; | |
342 } | |
343 """ | |
344 self.expectErrors(snippet, [5]) | |
345 | |
293 | 346 def testStructCall(self): |
347 snippet = """ | |
348 module teststruct1; | |
349 function void t() | |
350 { | |
351 var struct {int x, y;} a; | |
307 | 352 a.x(9); |
293 | 353 } |
354 """ | |
308 | 355 self.expectErrors(snippet, [6]) |
293 | 356 |
225 | 357 def testPointerType1(self): |
358 snippet = """ | |
284 | 359 module testpointer1; |
225 | 360 var int* pa; |
361 function void t() | |
362 { | |
363 var int a; | |
364 pa = &a; | |
365 *pa = 22; | |
308 | 366 a = *pa + *pa * 8; |
225 | 367 } |
368 """ | |
369 self.expectOK(snippet) | |
370 | |
215 | 371 def testPointerType(self): |
213 | 372 snippet = """ |
284 | 373 module testpointer; |
225 | 374 var int* pa, pb; |
213 | 375 function void t(int a, double b) |
376 { | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
377 var int a2; |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
378 a2 = a; // parameters cannot be escaped for now.. |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
379 pa = &a2; |
225 | 380 pb = pa; |
213 | 381 *pa = 22; |
382 } | |
383 """ | |
215 | 384 self.expectOK(snippet) |
213 | 385 |
221 | 386 def testPointerTypeInCorrect(self): |
387 snippet = """ | |
284 | 388 module testpointerincorrect; |
221 | 389 var int* pa; |
390 function void t(int a, double b) | |
391 { | |
392 pa = 2; // type conflict | |
393 pa = &a; | |
307 | 394 pa = &2; // No valid lvalue |
221 | 395 &a = pa; // No valid lvalue |
396 **pa = 22; // Cannot deref int | |
397 } | |
398 """ | |
307 | 399 self.expectErrors(snippet, [6, 8, 9, 10]) |
221 | 400 |
230 | 401 def testPointerTypeIr(self): |
402 snippet = """ | |
284 | 403 module testptr_ir; |
230 | 404 function void t() |
405 { | |
406 var int* a; | |
407 a = cast<int*>(40); | |
408 *a = 2; | |
409 } | |
410 """ | |
268 | 411 self.expectOK(snippet) |
230 | 412 |
413 def testPointerTypeIr2(self): | |
414 snippet = """ | |
284 | 415 module testptr_ir; |
230 | 416 type struct {int x,y;}* gpio; |
417 function void t() | |
418 { | |
419 var gpio a; | |
420 a = cast<gpio>(40); | |
421 a->x = 2; | |
422 a->y = a->x - 14; | |
423 } | |
424 """ | |
268 | 425 self.expectOK(snippet) |
230 | 426 |
427 def testWrongCast(self): | |
428 snippet = """ | |
284 | 429 module testptr_ir; |
230 | 430 type struct {int x,y;}* gpio; |
431 function void t() | |
432 { | |
433 var gpio a; | |
434 *cast<gpio>(*a); | |
435 } | |
436 """ | |
308 | 437 self.expectErrors(snippet, [7]) |
230 | 438 |
215 | 439 def testComplexType(self): |
213 | 440 snippet = """ |
284 | 441 module testpointer; |
213 | 442 type int my_int; |
443 | |
444 type struct { | |
445 int x, y; | |
446 } point; | |
447 | |
448 type struct { | |
449 int mem1; | |
450 int memb2; | |
451 point P1; | |
452 } my_struct; | |
453 | |
454 type my_struct* my_sptr; | |
225 | 455 var int* pa; |
213 | 456 |
227 | 457 function void t(int a, int b, my_sptr x) |
213 | 458 { |
459 var my_struct *msp; | |
460 | |
225 | 461 var my_struct u, v; |
462 var point *pt; | |
463 | |
464 pt = &msp->P1; | |
213 | 465 msp = x; |
225 | 466 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 467 x->memb2 = *pa + a * b; |
468 | |
225 | 469 msp->P1.x = a * x->P1.y; |
213 | 470 } |
471 """ | |
215 | 472 self.expectOK(snippet) |
205 | 473 |
167 | 474 |
475 if __name__ == '__main__': | |
243 | 476 unittest.main() |