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