Mercurial > lcfOS
annotate test/testc3.py @ 292:534b94b40aa8
Fixup reorganize
author | Windel Bouwman |
---|---|
date | Wed, 27 Nov 2013 08:06:42 +0100 |
parents | a747a45dcd78 |
children | 6aa721e7b10b |
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): |
205 | 69 def testUnexpectedCharacter(self): |
287 | 70 snippet = io.StringIO(""" var s \u6c34 """) |
205 | 71 with self.assertRaises(ppci.CompilerError): |
72 list(c3.lexer.tokenize(snippet)) | |
73 | |
204 | 74 def testBlockComment(self): |
287 | 75 snippet = io.StringIO(""" |
204 | 76 /* Demo */ |
77 var int x = 0; | |
287 | 78 """) |
204 | 79 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] |
80 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
205 | 81 |
204 | 82 def testBlockCommentMultiLine(self): |
287 | 83 snippet = io.StringIO(""" |
204 | 84 /* Demo |
85 bla1 | |
86 bla2 | |
87 */ | |
88 var int x = 0; | |
287 | 89 """) |
204 | 90 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] |
91 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
92 | |
287 | 93 |
204 | 94 class testBuilder(unittest.TestCase): |
215 | 95 def setUp(self): |
96 self.diag = ppci.DiagnosticsManager() | |
97 self.builder = c3.Builder(self.diag) | |
98 self.diag.clear() | |
99 | |
100 def testSrc(self): | |
101 self.expectOK(testsrc) | |
102 | |
103 def expectErrors(self, snippet, rows): | |
104 """ Helper to test for expected errors on rows """ | |
287 | 105 ircode = list(self.builder.build([io.StringIO(snippet)])) |
215 | 106 actualErrors = [err.row for err in self.diag.diags] |
107 if rows != actualErrors: | |
108 self.diag.printErrors(snippet) | |
109 self.assertSequenceEqual(rows, actualErrors) | |
287 | 110 # self.assertFalse(all(ircode)) |
215 | 111 |
287 | 112 def expectOK(self, snippet): |
113 if type(snippet) is list: | |
114 ircode = self.builder.build(snippet) | |
115 else: | |
116 ircode = self.builder.build([io.StringIO(snippet)]) | |
215 | 117 if not ircode: |
118 self.diag.printErrors(snippet) | |
287 | 119 self.assertTrue(all(ircode)) |
217 | 120 return ircode |
215 | 121 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
122 def testPackage(self): |
284 | 123 p1 = """module p1; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
124 type int A; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
125 """ |
284 | 126 p2 = """module p2; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
127 import p1; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
128 var A b; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
129 """ |
287 | 130 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
|
131 |
288 | 132 def testPackageMutual(self): |
133 p1 = """module p1; | |
134 import p2; | |
135 type int A; | |
136 var p2.B b; | |
137 """ | |
138 p2 = """module p2; | |
139 import p1; | |
140 var p1.A a; | |
141 """ | |
142 self.expectOK([io.StringIO(s) for s in (p1, p2)]) | |
143 | |
215 | 144 def testFunctArgs(self): |
287 | 145 snippet = """ |
284 | 146 module testargs; |
167 | 147 function void t2(int a, double b) |
148 { | |
149 t2(2, 2); | |
150 t2(2); | |
151 t2(1, 1.2); | |
152 } | |
287 | 153 """ |
154 self.expectErrors(snippet, [5, 6]) | |
148 | 155 |
215 | 156 def testExpressions(self): |
287 | 157 snippet = """ |
284 | 158 module test; |
167 | 159 function void t(int a, double b) |
160 { | |
161 var int a2; | |
162 var bool c; | |
163 | |
164 a2 = b * a; | |
165 c = a; | |
166 c = b > 1; | |
167 } | |
287 | 168 """ |
169 self.expectErrors(snippet, [8, 9, 10]) | |
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 var int a; | |
207 function void t() | |
208 { | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
209 var int i = 0; |
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; |
186 | 213 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
214 } |
205 | 215 |
216 while(true) | |
217 { | |
218 } | |
219 | |
220 while(false) | |
221 { | |
222 } | |
287 | 223 } |
224 """ | |
225 self.expectOK(snippet) | |
215 | 226 |
227 def testIf(self): | |
228 snippet = """ | |
287 | 229 module tstIFF; |
230 function void t(int b) | |
231 { | |
180 | 232 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
233 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
234 if (a > b) |
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 if (a > 1337) |
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 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
239 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
240 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
241 else |
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 b = 1; |
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 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
246 return b; |
287 | 247 } |
215 | 248 """ |
249 self.expectOK(snippet) | |
225 | 250 |
251 def testTypeDef(self): | |
252 snippet = """ | |
284 | 253 module testtypedef; |
225 | 254 type int my_int; |
255 function void t() | |
256 { | |
257 var my_int a; | |
258 var int b; | |
259 a = 2; | |
260 b = a + 2; | |
261 } | |
262 """ | |
263 self.expectOK(snippet) | |
264 | |
230 | 265 def testLocalVariable(self): |
266 snippet = """ | |
284 | 267 module testlocalvar; |
230 | 268 function void t() |
269 { | |
270 var int a, b; | |
271 a = 2; | |
272 b = a + 2; | |
273 } | |
274 """ | |
268 | 275 self.expectOK(snippet) |
230 | 276 |
249 | 277 def testUnknownType(self): |
284 | 278 snippet = """module testlocalvar; |
249 | 279 function void t() |
280 { | |
281 var int2 a; | |
282 } | |
283 """ | |
284 self.expectErrors(snippet, [4]) | |
285 | |
230 | 286 def testStruct1(self): |
287 snippet = """ | |
284 | 288 module teststruct1; |
230 | 289 function void t() |
290 { | |
291 var struct {int x, y;} a; | |
292 a.x = 2; | |
293 a.y = a.x + 2; | |
294 } | |
295 """ | |
268 | 296 self.expectOK(snippet) |
230 | 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; | |
334 pa = &2; | |
335 &a = pa; // No valid lvalue | |
336 **pa = 22; // Cannot deref int | |
337 } | |
338 """ | |
228 | 339 self.expectErrors(snippet, [6, 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 |
288 | 414 @unittest.skip('To be rewritten') |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
415 def testExamples(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
416 """ Test all examples in the c3/examples directory """ |
288 | 417 example_filenames = glob.glob('./c3examples/*.c3') |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
418 for filename in example_filenames: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
419 with open(filename, 'r') as f: |
288 | 420 self.expectOK(f) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
421 |
215 | 422 def test2(self): |
205 | 423 # testsrc2 is valid code: |
215 | 424 snippet = """ |
284 | 425 module test2; |
205 | 426 |
427 function void tst() | |
428 { | |
429 var int a, b; | |
430 a = 2 * 33 - 12; | |
431 b = a * 2 + 13; | |
432 a = b + a; | |
433 if (a > b and b == 3) | |
434 { | |
435 var int x = a; | |
436 x = b * 2 - a; | |
437 a = x*x; | |
438 } | |
439 else | |
440 { | |
441 a = b + a; | |
442 } | |
443 } | |
444 | |
445 """ | |
287 | 446 self.expectOK(snippet) |
167 | 447 |
448 if __name__ == '__main__': | |
243 | 449 unittest.main() |
167 | 450 |
451 |