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