Mercurial > lcfOS
annotate test/testc3.py @ 303:be7f60545368
Final fixups
author | Windel Bouwman |
---|---|
date | Fri, 06 Dec 2013 12:37:48 +0100 |
parents | 6753763d3bec |
children | b145f8e6050b |
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 | |
48 def expectErrors(self, snippet, rows): | |
49 """ Helper to test for expected errors on rows """ | |
287 | 50 ircode = list(self.builder.build([io.StringIO(snippet)])) |
215 | 51 actualErrors = [err.row for err in self.diag.diags] |
52 if rows != actualErrors: | |
53 self.diag.printErrors(snippet) | |
54 self.assertSequenceEqual(rows, actualErrors) | |
287 | 55 # self.assertFalse(all(ircode)) |
215 | 56 |
287 | 57 def expectOK(self, snippet): |
58 if type(snippet) is list: | |
59 ircode = self.builder.build(snippet) | |
60 else: | |
61 ircode = self.builder.build([io.StringIO(snippet)]) | |
215 | 62 if not ircode: |
63 self.diag.printErrors(snippet) | |
287 | 64 self.assertTrue(all(ircode)) |
217 | 65 return ircode |
215 | 66 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
67 def testPackage(self): |
284 | 68 p1 = """module p1; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
69 type int A; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
70 """ |
284 | 71 p2 = """module p2; |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
72 import p1; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
73 var A b; |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
74 """ |
287 | 75 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
|
76 |
288 | 77 def testPackageMutual(self): |
78 p1 = """module p1; | |
79 import p2; | |
80 type int A; | |
81 var p2.B b; | |
82 """ | |
83 p2 = """module p2; | |
84 import p1; | |
85 var p1.A a; | |
86 """ | |
87 self.expectOK([io.StringIO(s) for s in (p1, p2)]) | |
88 | |
293 | 89 def testPackageNotExists(self): |
90 p1 = """module p1; | |
91 import p23; | |
92 """ | |
93 self.expectOK([io.StringIO(p1)]) | |
94 | |
215 | 95 def testFunctArgs(self): |
287 | 96 snippet = """ |
284 | 97 module testargs; |
167 | 98 function void t2(int a, double b) |
99 { | |
100 t2(2, 2); | |
101 t2(2); | |
102 t2(1, 1.2); | |
103 } | |
287 | 104 """ |
105 self.expectErrors(snippet, [5, 6]) | |
148 | 106 |
303 | 107 def testReturn(self): |
108 snippet = """ | |
109 module testreturn; | |
110 function void t() | |
111 { | |
112 return; | |
113 } | |
114 """ | |
115 self.expectOK(snippet) | |
116 | |
117 def testReturn2(self): | |
118 snippet = """ | |
119 module testreturn; | |
120 function int t() | |
121 { | |
122 return 2; | |
123 } | |
124 """ | |
125 self.expectOK(snippet) | |
126 | |
215 | 127 def testExpressions(self): |
287 | 128 snippet = """ |
284 | 129 module test; |
167 | 130 function void t(int a, double b) |
131 { | |
132 var int a2; | |
133 var bool c; | |
134 | |
135 a2 = b * a; | |
136 c = a; | |
137 c = b > 1; | |
138 } | |
287 | 139 """ |
140 self.expectErrors(snippet, [8, 9, 10]) | |
205 | 141 |
230 | 142 def testExpression1(self): |
287 | 143 snippet = """ |
284 | 144 module testexpr1; |
230 | 145 function void t() |
146 { | |
147 var int a, b, c; | |
148 a = 1; | |
149 b = a * 2 + a * a; | |
150 c = b * a - 3; | |
151 } | |
287 | 152 """ |
153 self.expectOK(snippet) | |
230 | 154 |
215 | 155 def testEmpty(self): |
287 | 156 snippet = """ |
157 module A | |
158 """ | |
159 self.expectErrors(snippet, [3]) | |
205 | 160 |
215 | 161 def testEmpty2(self): |
287 | 162 snippet = "" |
163 self.expectErrors(snippet, [1]) | |
205 | 164 |
215 | 165 def testRedefine(self): |
287 | 166 snippet = """ |
167 module test; | |
168 var int a; | |
169 var int b; | |
170 var int a; | |
171 """ | |
172 self.expectErrors(snippet, [5]) | |
205 | 173 |
215 | 174 def testWhile(self): |
287 | 175 snippet = """ |
176 module tstwhile; | |
177 var int a; | |
178 function void t() | |
179 { | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
180 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
181 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
182 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
183 i = i + 3; |
186 | 184 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
185 } |
205 | 186 |
187 while(true) | |
188 { | |
189 } | |
190 | |
191 while(false) | |
192 { | |
193 } | |
287 | 194 } |
195 """ | |
196 self.expectOK(snippet) | |
215 | 197 |
198 def testIf(self): | |
199 snippet = """ | |
287 | 200 module tstIFF; |
201 function void t(int b) | |
202 { | |
180 | 203 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
204 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
205 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
206 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
207 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
208 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
209 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
210 } |
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 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
213 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
214 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
215 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
216 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
217 return b; |
287 | 218 } |
215 | 219 """ |
220 self.expectOK(snippet) | |
225 | 221 |
222 def testTypeDef(self): | |
223 snippet = """ | |
284 | 224 module testtypedef; |
225 | 225 type int my_int; |
226 function void t() | |
227 { | |
228 var my_int a; | |
229 var int b; | |
230 a = 2; | |
231 b = a + 2; | |
232 } | |
233 """ | |
234 self.expectOK(snippet) | |
235 | |
230 | 236 def testLocalVariable(self): |
237 snippet = """ | |
284 | 238 module testlocalvar; |
230 | 239 function void t() |
240 { | |
241 var int a, b; | |
242 a = 2; | |
243 b = a + 2; | |
244 } | |
245 """ | |
268 | 246 self.expectOK(snippet) |
230 | 247 |
249 | 248 def testUnknownType(self): |
284 | 249 snippet = """module testlocalvar; |
249 | 250 function void t() |
251 { | |
252 var int2 a; | |
253 } | |
254 """ | |
255 self.expectErrors(snippet, [4]) | |
256 | |
230 | 257 def testStruct1(self): |
258 snippet = """ | |
284 | 259 module teststruct1; |
230 | 260 function void t() |
261 { | |
262 var struct {int x, y;} a; | |
263 a.x = 2; | |
264 a.y = a.x + 2; | |
265 } | |
266 """ | |
268 | 267 self.expectOK(snippet) |
230 | 268 |
293 | 269 def testStructCall(self): |
270 snippet = """ | |
271 module teststruct1; | |
272 function void t() | |
273 { | |
274 var struct {int x, y;} a; | |
275 a.b.c(9); | |
276 } | |
277 """ | |
278 self.expectOK(snippet) | |
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 self.expectErrors(snippet, [7, 7]) | |
360 | |
215 | 361 def testComplexType(self): |
213 | 362 snippet = """ |
284 | 363 module testpointer; |
213 | 364 type int my_int; |
365 | |
366 type struct { | |
367 int x, y; | |
368 } point; | |
369 | |
370 type struct { | |
371 int mem1; | |
372 int memb2; | |
373 point P1; | |
374 } my_struct; | |
375 | |
376 type my_struct* my_sptr; | |
225 | 377 var int* pa; |
213 | 378 |
227 | 379 function void t(int a, int b, my_sptr x) |
213 | 380 { |
381 var my_struct *msp; | |
382 | |
225 | 383 var my_struct u, v; |
384 var point *pt; | |
385 | |
386 pt = &msp->P1; | |
213 | 387 msp = x; |
225 | 388 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 389 x->memb2 = *pa + a * b; |
390 | |
225 | 391 msp->P1.x = a * x->P1.y; |
213 | 392 } |
393 """ | |
215 | 394 self.expectOK(snippet) |
205 | 395 |
167 | 396 |
397 if __name__ == '__main__': | |
243 | 398 unittest.main() |