Mercurial > lcfOS
annotate python/testc3.py @ 271:cf7d5fb7d9c8
Reorganization
author | Windel Bouwman |
---|---|
date | Tue, 20 Aug 2013 18:56:02 +0200 |
parents | 5ec7580976d9 |
children | e64bae57cda8 |
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 """ |
268 | 129 self.assertTrue(self.builder.build(p1)) |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
130 p2 = """package p2; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
131 import p1; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
132 var A b; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
133 """ |
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 """ | |
268 | 174 self.expectOK(snippet) |
230 | 175 |
215 | 176 def testEmpty(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
177 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
178 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
179 """ |
215 | 180 self.expectErrors(snippet, [3]) |
205 | 181 |
215 | 182 def testEmpty2(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
183 snippet = "" |
215 | 184 self.expectErrors(snippet, [1]) |
205 | 185 |
215 | 186 def testRedefine(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
190 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
191 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 """ |
215 | 193 self.expectErrors(snippet, [5]) |
205 | 194 |
215 | 195 def testWhile(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
197 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
198 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
199 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
200 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
201 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
202 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
204 i = i + 3; |
186 | 205 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
206 } |
205 | 207 |
208 while(true) | |
209 { | |
210 } | |
211 | |
212 while(false) | |
213 { | |
214 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
215 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
216 """ |
215 | 217 self.expectOK(snippet) |
218 | |
219 def testIf(self): | |
220 snippet = """ | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
221 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
222 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
223 { |
180 | 224 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
225 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
226 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
227 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
228 if (a > 1337) |
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 b = 2; |
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 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
233 else |
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 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
236 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
237 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
238 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
239 } |
215 | 240 """ |
241 self.expectOK(snippet) | |
225 | 242 |
243 def testTypeDef(self): | |
244 snippet = """ | |
245 package testtypedef; | |
246 type int my_int; | |
247 function void t() | |
248 { | |
249 var my_int a; | |
250 var int b; | |
251 a = 2; | |
252 b = a + 2; | |
253 } | |
254 """ | |
255 self.expectOK(snippet) | |
256 | |
230 | 257 def testLocalVariable(self): |
258 snippet = """ | |
259 package testlocalvar; | |
260 function void t() | |
261 { | |
262 var int a, b; | |
263 a = 2; | |
264 b = a + 2; | |
265 } | |
266 """ | |
268 | 267 self.expectOK(snippet) |
230 | 268 |
249 | 269 def testUnknownType(self): |
270 snippet = """package testlocalvar; | |
271 function void t() | |
272 { | |
273 var int2 a; | |
274 } | |
275 """ | |
276 self.expectErrors(snippet, [4]) | |
277 | |
230 | 278 def testStruct1(self): |
279 snippet = """ | |
280 package teststruct1; | |
281 function void t() | |
282 { | |
283 var struct {int x, y;} a; | |
284 a.x = 2; | |
285 a.y = a.x + 2; | |
286 } | |
287 """ | |
268 | 288 self.expectOK(snippet) |
230 | 289 |
225 | 290 def testPointerType1(self): |
291 snippet = """ | |
292 package testpointer1; | |
293 var int* pa; | |
294 function void t() | |
295 { | |
296 var int a; | |
297 pa = &a; | |
298 *pa = 22; | |
299 } | |
300 """ | |
301 self.expectOK(snippet) | |
302 | |
215 | 303 def testPointerType(self): |
213 | 304 snippet = """ |
305 package testpointer; | |
225 | 306 var int* pa, pb; |
213 | 307 function void t(int a, double b) |
308 { | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
309 pa = &a; |
225 | 310 pb = pa; |
213 | 311 *pa = 22; |
312 } | |
313 """ | |
215 | 314 self.expectOK(snippet) |
213 | 315 |
221 | 316 def testPointerTypeInCorrect(self): |
317 snippet = """ | |
318 package testpointerincorrect; | |
319 var int* pa; | |
320 function void t(int a, double b) | |
321 { | |
322 pa = 2; // type conflict | |
323 pa = &a; | |
324 pa = &2; | |
325 &a = pa; // No valid lvalue | |
326 **pa = 22; // Cannot deref int | |
327 } | |
328 """ | |
228 | 329 self.expectErrors(snippet, [6, 9, 10]) |
221 | 330 |
230 | 331 def testPointerTypeIr(self): |
332 snippet = """ | |
333 package testptr_ir; | |
334 function void t() | |
335 { | |
336 var int* a; | |
337 a = cast<int*>(40); | |
338 *a = 2; | |
339 } | |
340 """ | |
268 | 341 self.expectOK(snippet) |
230 | 342 |
343 def testPointerTypeIr2(self): | |
344 snippet = """ | |
345 package testptr_ir; | |
346 type struct {int x,y;}* gpio; | |
347 function void t() | |
348 { | |
349 var gpio a; | |
350 a = cast<gpio>(40); | |
351 a->x = 2; | |
352 a->y = a->x - 14; | |
353 } | |
354 """ | |
268 | 355 self.expectOK(snippet) |
230 | 356 |
357 def testWrongCast(self): | |
358 snippet = """ | |
359 package testptr_ir; | |
360 type struct {int x,y;}* gpio; | |
361 function void t() | |
362 { | |
363 var gpio a; | |
364 *cast<gpio>(*a); | |
365 } | |
366 """ | |
367 # TODO: remove the duplicate error: | |
368 self.expectErrors(snippet, [7, 7]) | |
369 | |
215 | 370 def testComplexType(self): |
213 | 371 snippet = """ |
372 package testpointer; | |
373 type int my_int; | |
374 | |
375 type struct { | |
376 int x, y; | |
377 } point; | |
378 | |
379 type struct { | |
380 int mem1; | |
381 int memb2; | |
382 point P1; | |
383 } my_struct; | |
384 | |
385 type my_struct* my_sptr; | |
225 | 386 var int* pa; |
213 | 387 |
227 | 388 function void t(int a, int b, my_sptr x) |
213 | 389 { |
390 var my_struct *msp; | |
391 | |
225 | 392 var my_struct u, v; |
393 var point *pt; | |
394 | |
395 pt = &msp->P1; | |
213 | 396 msp = x; |
225 | 397 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 398 x->memb2 = *pa + a * b; |
399 | |
225 | 400 msp->P1.x = a * x->P1.y; |
213 | 401 } |
402 """ | |
215 | 403 self.expectOK(snippet) |
205 | 404 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
405 def testExamples(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
406 """ 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
|
407 example_filenames = glob.glob('./c3/examples/*.c3') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
408 for filename in example_filenames: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
409 with open(filename, 'r') as f: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
410 src = f.read() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
411 self.expectOK(src) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
412 |
215 | 413 def test2(self): |
205 | 414 # testsrc2 is valid code: |
215 | 415 snippet = """ |
205 | 416 package test2; |
417 | |
418 function void tst() | |
419 { | |
420 var int a, b; | |
421 a = 2 * 33 - 12; | |
422 b = a * 2 + 13; | |
423 a = b + a; | |
424 if (a > b and b == 3) | |
425 { | |
426 var int x = a; | |
427 x = b * 2 - a; | |
428 a = x*x; | |
429 } | |
430 else | |
431 { | |
432 a = b + a; | |
433 } | |
434 } | |
435 | |
436 """ | |
217 | 437 ircode = self.expectOK(snippet) |
438 self.assertEqual(1, len(ircode.Functions)) | |
167 | 439 |
440 if __name__ == '__main__': | |
243 | 441 unittest.main() |
167 | 442 |
443 |