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