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