Mercurial > lcfOS
annotate test/testc3.py @ 301:6753763d3bec
merge codegen into ppci package
author | Windel Bouwman |
---|---|
date | Thu, 05 Dec 2013 17:02:38 +0100 |
parents | 158068af716c |
children | be7f60545368 |
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 |
215 | 107 def testExpressions(self): |
287 | 108 snippet = """ |
284 | 109 module test; |
167 | 110 function void t(int a, double b) |
111 { | |
112 var int a2; | |
113 var bool c; | |
114 | |
115 a2 = b * a; | |
116 c = a; | |
117 c = b > 1; | |
118 } | |
287 | 119 """ |
120 self.expectErrors(snippet, [8, 9, 10]) | |
205 | 121 |
230 | 122 def testExpression1(self): |
287 | 123 snippet = """ |
284 | 124 module testexpr1; |
230 | 125 function void t() |
126 { | |
127 var int a, b, c; | |
128 a = 1; | |
129 b = a * 2 + a * a; | |
130 c = b * a - 3; | |
131 } | |
287 | 132 """ |
133 self.expectOK(snippet) | |
230 | 134 |
215 | 135 def testEmpty(self): |
287 | 136 snippet = """ |
137 module A | |
138 """ | |
139 self.expectErrors(snippet, [3]) | |
205 | 140 |
215 | 141 def testEmpty2(self): |
287 | 142 snippet = "" |
143 self.expectErrors(snippet, [1]) | |
205 | 144 |
215 | 145 def testRedefine(self): |
287 | 146 snippet = """ |
147 module test; | |
148 var int a; | |
149 var int b; | |
150 var int a; | |
151 """ | |
152 self.expectErrors(snippet, [5]) | |
205 | 153 |
215 | 154 def testWhile(self): |
287 | 155 snippet = """ |
156 module tstwhile; | |
157 var int a; | |
158 function void t() | |
159 { | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
160 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
161 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
162 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 i = i + 3; |
186 | 164 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
165 } |
205 | 166 |
167 while(true) | |
168 { | |
169 } | |
170 | |
171 while(false) | |
172 { | |
173 } | |
287 | 174 } |
175 """ | |
176 self.expectOK(snippet) | |
215 | 177 |
178 def testIf(self): | |
179 snippet = """ | |
287 | 180 module tstIFF; |
181 function void t(int b) | |
182 { | |
180 | 183 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
184 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
185 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 b = 2; |
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 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 else |
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 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
195 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
197 return b; |
287 | 198 } |
215 | 199 """ |
200 self.expectOK(snippet) | |
225 | 201 |
202 def testTypeDef(self): | |
203 snippet = """ | |
284 | 204 module testtypedef; |
225 | 205 type int my_int; |
206 function void t() | |
207 { | |
208 var my_int a; | |
209 var int b; | |
210 a = 2; | |
211 b = a + 2; | |
212 } | |
213 """ | |
214 self.expectOK(snippet) | |
215 | |
230 | 216 def testLocalVariable(self): |
217 snippet = """ | |
284 | 218 module testlocalvar; |
230 | 219 function void t() |
220 { | |
221 var int a, b; | |
222 a = 2; | |
223 b = a + 2; | |
224 } | |
225 """ | |
268 | 226 self.expectOK(snippet) |
230 | 227 |
249 | 228 def testUnknownType(self): |
284 | 229 snippet = """module testlocalvar; |
249 | 230 function void t() |
231 { | |
232 var int2 a; | |
233 } | |
234 """ | |
235 self.expectErrors(snippet, [4]) | |
236 | |
230 | 237 def testStruct1(self): |
238 snippet = """ | |
284 | 239 module teststruct1; |
230 | 240 function void t() |
241 { | |
242 var struct {int x, y;} a; | |
243 a.x = 2; | |
244 a.y = a.x + 2; | |
245 } | |
246 """ | |
268 | 247 self.expectOK(snippet) |
230 | 248 |
293 | 249 def testStructCall(self): |
250 snippet = """ | |
251 module teststruct1; | |
252 function void t() | |
253 { | |
254 var struct {int x, y;} a; | |
255 a.b.c(9); | |
256 } | |
257 """ | |
258 self.expectOK(snippet) | |
259 | |
225 | 260 def testPointerType1(self): |
261 snippet = """ | |
284 | 262 module testpointer1; |
225 | 263 var int* pa; |
264 function void t() | |
265 { | |
266 var int a; | |
267 pa = &a; | |
268 *pa = 22; | |
269 } | |
270 """ | |
271 self.expectOK(snippet) | |
272 | |
215 | 273 def testPointerType(self): |
213 | 274 snippet = """ |
284 | 275 module testpointer; |
225 | 276 var int* pa, pb; |
213 | 277 function void t(int a, double b) |
278 { | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
279 var int a2; |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
280 a2 = a; // parameters cannot be escaped for now.. |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
281 pa = &a2; |
225 | 282 pb = pa; |
213 | 283 *pa = 22; |
284 } | |
285 """ | |
215 | 286 self.expectOK(snippet) |
213 | 287 |
221 | 288 def testPointerTypeInCorrect(self): |
289 snippet = """ | |
284 | 290 module testpointerincorrect; |
221 | 291 var int* pa; |
292 function void t(int a, double b) | |
293 { | |
294 pa = 2; // type conflict | |
295 pa = &a; | |
296 pa = &2; | |
297 &a = pa; // No valid lvalue | |
298 **pa = 22; // Cannot deref int | |
299 } | |
300 """ | |
228 | 301 self.expectErrors(snippet, [6, 9, 10]) |
221 | 302 |
230 | 303 def testPointerTypeIr(self): |
304 snippet = """ | |
284 | 305 module testptr_ir; |
230 | 306 function void t() |
307 { | |
308 var int* a; | |
309 a = cast<int*>(40); | |
310 *a = 2; | |
311 } | |
312 """ | |
268 | 313 self.expectOK(snippet) |
230 | 314 |
315 def testPointerTypeIr2(self): | |
316 snippet = """ | |
284 | 317 module testptr_ir; |
230 | 318 type struct {int x,y;}* gpio; |
319 function void t() | |
320 { | |
321 var gpio a; | |
322 a = cast<gpio>(40); | |
323 a->x = 2; | |
324 a->y = a->x - 14; | |
325 } | |
326 """ | |
268 | 327 self.expectOK(snippet) |
230 | 328 |
329 def testWrongCast(self): | |
330 snippet = """ | |
284 | 331 module testptr_ir; |
230 | 332 type struct {int x,y;}* gpio; |
333 function void t() | |
334 { | |
335 var gpio a; | |
336 *cast<gpio>(*a); | |
337 } | |
338 """ | |
339 self.expectErrors(snippet, [7, 7]) | |
340 | |
215 | 341 def testComplexType(self): |
213 | 342 snippet = """ |
284 | 343 module testpointer; |
213 | 344 type int my_int; |
345 | |
346 type struct { | |
347 int x, y; | |
348 } point; | |
349 | |
350 type struct { | |
351 int mem1; | |
352 int memb2; | |
353 point P1; | |
354 } my_struct; | |
355 | |
356 type my_struct* my_sptr; | |
225 | 357 var int* pa; |
213 | 358 |
227 | 359 function void t(int a, int b, my_sptr x) |
213 | 360 { |
361 var my_struct *msp; | |
362 | |
225 | 363 var my_struct u, v; |
364 var point *pt; | |
365 | |
366 pt = &msp->P1; | |
213 | 367 msp = x; |
225 | 368 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 369 x->memb2 = *pa + a * b; |
370 | |
225 | 371 msp->P1.x = a * x->P1.y; |
213 | 372 } |
373 """ | |
215 | 374 self.expectOK(snippet) |
205 | 375 |
167 | 376 |
377 if __name__ == '__main__': | |
243 | 378 unittest.main() |