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