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