Mercurial > lcfOS
annotate test/testc3.py @ 284:05184b95fa16
Moved tests to seperate folder
author | Windel Bouwman |
---|---|
date | Fri, 15 Nov 2013 13:43:22 +0100 |
parents | python/testc3.py@02385f62f250 |
children | 1c7c1e619be8 |
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 |
284 | 6 testsrc = """module 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 | |
272 | 108 def expectOK(self, snippet, pack_dir=None): |
109 ircode = self.builder.build(snippet, pack_dir=pack_dir) | |
215 | 110 if not ircode: |
111 self.diag.printErrors(snippet) | |
112 self.assertTrue(ircode) | |
217 | 113 return ircode |
215 | 114 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
115 def testPackage(self): |
284 | 116 p1 = """module p1; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
117 type int A; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
118 """ |
268 | 119 self.assertTrue(self.builder.build(p1)) |
284 | 120 p2 = """module p2; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
121 import p1; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
122 var A b; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
123 """ |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
124 self.expectOK(p2) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
125 |
215 | 126 def testFunctArgs(self): |
167 | 127 snippet = """ |
284 | 128 module testargs; |
167 | 129 function void t2(int a, double b) |
130 { | |
131 t2(2, 2); | |
132 t2(2); | |
133 t2(1, 1.2); | |
134 } | |
135 """ | |
215 | 136 self.expectErrors(snippet, [5, 6]) |
148 | 137 |
215 | 138 def testExpressions(self): |
167 | 139 snippet = """ |
284 | 140 module test; |
167 | 141 function void t(int a, double b) |
142 { | |
143 var int a2; | |
144 var bool c; | |
145 | |
146 a2 = b * a; | |
147 c = a; | |
148 c = b > 1; | |
149 } | |
150 """ | |
215 | 151 self.expectErrors(snippet, [8, 9, 10]) |
205 | 152 |
230 | 153 def testExpression1(self): |
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 } | |
163 """ | |
268 | 164 self.expectOK(snippet) |
230 | 165 |
215 | 166 def testEmpty(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
167 snippet = """ |
284 | 168 module A |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
169 """ |
215 | 170 self.expectErrors(snippet, [3]) |
205 | 171 |
215 | 172 def testEmpty2(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
173 snippet = "" |
215 | 174 self.expectErrors(snippet, [1]) |
205 | 175 |
215 | 176 def testRedefine(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
177 snippet = """ |
284 | 178 module test; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
179 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
180 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
181 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
182 """ |
215 | 183 self.expectErrors(snippet, [5]) |
205 | 184 |
215 | 185 def testWhile(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 snippet = """ |
284 | 187 module tstwhile; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
190 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
191 var int i = 0; |
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; |
186 | 195 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 } |
205 | 197 |
198 while(true) | |
199 { | |
200 } | |
201 | |
202 while(false) | |
203 { | |
204 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
205 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
206 """ |
215 | 207 self.expectOK(snippet) |
208 | |
209 def testIf(self): | |
210 snippet = """ | |
284 | 211 module tstIFF; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
212 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
213 { |
180 | 214 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
215 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
216 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
217 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
218 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
219 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
220 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
221 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
222 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
223 else |
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 b = 1; |
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 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
228 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
229 } |
215 | 230 """ |
231 self.expectOK(snippet) | |
225 | 232 |
233 def testTypeDef(self): | |
234 snippet = """ | |
284 | 235 module testtypedef; |
225 | 236 type int my_int; |
237 function void t() | |
238 { | |
239 var my_int a; | |
240 var int b; | |
241 a = 2; | |
242 b = a + 2; | |
243 } | |
244 """ | |
245 self.expectOK(snippet) | |
246 | |
230 | 247 def testLocalVariable(self): |
248 snippet = """ | |
284 | 249 module testlocalvar; |
230 | 250 function void t() |
251 { | |
252 var int a, b; | |
253 a = 2; | |
254 b = a + 2; | |
255 } | |
256 """ | |
268 | 257 self.expectOK(snippet) |
230 | 258 |
249 | 259 def testUnknownType(self): |
284 | 260 snippet = """module testlocalvar; |
249 | 261 function void t() |
262 { | |
263 var int2 a; | |
264 } | |
265 """ | |
266 self.expectErrors(snippet, [4]) | |
267 | |
230 | 268 def testStruct1(self): |
269 snippet = """ | |
284 | 270 module teststruct1; |
230 | 271 function void t() |
272 { | |
273 var struct {int x, y;} a; | |
274 a.x = 2; | |
275 a.y = a.x + 2; | |
276 } | |
277 """ | |
268 | 278 self.expectOK(snippet) |
230 | 279 |
225 | 280 def testPointerType1(self): |
281 snippet = """ | |
284 | 282 module testpointer1; |
225 | 283 var int* pa; |
284 function void t() | |
285 { | |
286 var int a; | |
287 pa = &a; | |
288 *pa = 22; | |
289 } | |
290 """ | |
291 self.expectOK(snippet) | |
292 | |
215 | 293 def testPointerType(self): |
213 | 294 snippet = """ |
284 | 295 module testpointer; |
225 | 296 var int* pa, pb; |
213 | 297 function void t(int a, double b) |
298 { | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
299 var int a2; |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
300 a2 = a; // parameters cannot be escaped for now.. |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
301 pa = &a2; |
225 | 302 pb = pa; |
213 | 303 *pa = 22; |
304 } | |
305 """ | |
215 | 306 self.expectOK(snippet) |
213 | 307 |
221 | 308 def testPointerTypeInCorrect(self): |
309 snippet = """ | |
284 | 310 module testpointerincorrect; |
221 | 311 var int* pa; |
312 function void t(int a, double b) | |
313 { | |
314 pa = 2; // type conflict | |
315 pa = &a; | |
316 pa = &2; | |
317 &a = pa; // No valid lvalue | |
318 **pa = 22; // Cannot deref int | |
319 } | |
320 """ | |
228 | 321 self.expectErrors(snippet, [6, 9, 10]) |
221 | 322 |
230 | 323 def testPointerTypeIr(self): |
324 snippet = """ | |
284 | 325 module testptr_ir; |
230 | 326 function void t() |
327 { | |
328 var int* a; | |
329 a = cast<int*>(40); | |
330 *a = 2; | |
331 } | |
332 """ | |
268 | 333 self.expectOK(snippet) |
230 | 334 |
335 def testPointerTypeIr2(self): | |
336 snippet = """ | |
284 | 337 module testptr_ir; |
230 | 338 type struct {int x,y;}* gpio; |
339 function void t() | |
340 { | |
341 var gpio a; | |
342 a = cast<gpio>(40); | |
343 a->x = 2; | |
344 a->y = a->x - 14; | |
345 } | |
346 """ | |
268 | 347 self.expectOK(snippet) |
230 | 348 |
349 def testWrongCast(self): | |
350 snippet = """ | |
284 | 351 module testptr_ir; |
230 | 352 type struct {int x,y;}* gpio; |
353 function void t() | |
354 { | |
355 var gpio a; | |
356 *cast<gpio>(*a); | |
357 } | |
358 """ | |
359 # TODO: remove the duplicate error: | |
360 self.expectErrors(snippet, [7, 7]) | |
361 | |
215 | 362 def testComplexType(self): |
213 | 363 snippet = """ |
284 | 364 module testpointer; |
213 | 365 type int my_int; |
366 | |
367 type struct { | |
368 int x, y; | |
369 } point; | |
370 | |
371 type struct { | |
372 int mem1; | |
373 int memb2; | |
374 point P1; | |
375 } my_struct; | |
376 | |
377 type my_struct* my_sptr; | |
225 | 378 var int* pa; |
213 | 379 |
227 | 380 function void t(int a, int b, my_sptr x) |
213 | 381 { |
382 var my_struct *msp; | |
383 | |
225 | 384 var my_struct u, v; |
385 var point *pt; | |
386 | |
387 pt = &msp->P1; | |
213 | 388 msp = x; |
225 | 389 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 390 x->memb2 = *pa + a * b; |
391 | |
225 | 392 msp->P1.x = a * x->P1.y; |
213 | 393 } |
394 """ | |
215 | 395 self.expectOK(snippet) |
205 | 396 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
397 def testExamples(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
398 """ 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
|
399 example_filenames = glob.glob('./c3/examples/*.c3') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
400 for filename in example_filenames: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
401 with open(filename, 'r') as f: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
402 src = f.read() |
272 | 403 self.expectOK(src, pack_dir='./c3/examples') |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
404 |
215 | 405 def test2(self): |
205 | 406 # testsrc2 is valid code: |
215 | 407 snippet = """ |
284 | 408 module test2; |
205 | 409 |
410 function void tst() | |
411 { | |
412 var int a, b; | |
413 a = 2 * 33 - 12; | |
414 b = a * 2 + 13; | |
415 a = b + a; | |
416 if (a > b and b == 3) | |
417 { | |
418 var int x = a; | |
419 x = b * 2 - a; | |
420 a = x*x; | |
421 } | |
422 else | |
423 { | |
424 a = b + a; | |
425 } | |
426 } | |
427 | |
428 """ | |
217 | 429 ircode = self.expectOK(snippet) |
430 self.assertEqual(1, len(ircode.Functions)) | |
167 | 431 |
432 if __name__ == '__main__': | |
243 | 433 unittest.main() |
167 | 434 |
435 |