Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
283:c9781c73e7e2 | 284:05184b95fa16 |
---|---|
1 import c3 | |
2 import time, ppci, x86, ir | |
3 import unittest | |
4 import glob | |
5 | |
6 testsrc = """module test; | |
7 | |
8 /* | |
9 demo of the source that is correct :) | |
10 */ | |
11 var int c, d; | |
12 var double e; | |
13 var int f; | |
14 | |
15 const int A = 1337; | |
16 | |
17 function void test1() | |
18 { | |
19 var int bdd; | |
20 var int a = 10; | |
21 bdd = 20; | |
22 var int buf; | |
23 var int i; | |
24 i = 2; | |
25 var int zero = i - 2; | |
26 if (i > 1) | |
27 { | |
28 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1; | |
29 } | |
30 else | |
31 { | |
32 ;;; | |
33 } | |
34 | |
35 t2(2, 3); | |
36 } | |
37 | |
38 function int t2(int a, int b) | |
39 { | |
40 if (a > 0) | |
41 { | |
42 a = 2 + t2(a - 1, 10); | |
43 } | |
44 | |
45 return a + b; | |
46 } | |
47 | |
48 var int a, b; | |
49 | |
50 function int t3(int aap, int blah) | |
51 { | |
52 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true) | |
53 { | |
54 a = 2 + t2(a - 1, 0); | |
55 } | |
56 | |
57 return a + b; | |
58 } | |
59 | |
60 var int hahaa = 23 * 2; | |
61 | |
62 | |
63 """ | |
64 | |
65 class testLexer(unittest.TestCase): | |
66 def testUnexpectedCharacter(self): | |
67 snippet = """ var s \u6c34 """ | |
68 with self.assertRaises(ppci.CompilerError): | |
69 list(c3.lexer.tokenize(snippet)) | |
70 | |
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) | |
78 | |
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): | |
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 | |
108 def expectOK(self, snippet, pack_dir=None): | |
109 ircode = self.builder.build(snippet, pack_dir=pack_dir) | |
110 if not ircode: | |
111 self.diag.printErrors(snippet) | |
112 self.assertTrue(ircode) | |
113 return ircode | |
114 | |
115 def testPackage(self): | |
116 p1 = """module p1; | |
117 type int A; | |
118 """ | |
119 self.assertTrue(self.builder.build(p1)) | |
120 p2 = """module p2; | |
121 import p1; | |
122 var A b; | |
123 """ | |
124 self.expectOK(p2) | |
125 | |
126 def testFunctArgs(self): | |
127 snippet = """ | |
128 module testargs; | |
129 function void t2(int a, double b) | |
130 { | |
131 t2(2, 2); | |
132 t2(2); | |
133 t2(1, 1.2); | |
134 } | |
135 """ | |
136 self.expectErrors(snippet, [5, 6]) | |
137 | |
138 def testExpressions(self): | |
139 snippet = """ | |
140 module test; | |
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 """ | |
151 self.expectErrors(snippet, [8, 9, 10]) | |
152 | |
153 def testExpression1(self): | |
154 snippet = """ | |
155 module testexpr1; | |
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 """ | |
164 self.expectOK(snippet) | |
165 | |
166 def testEmpty(self): | |
167 snippet = """ | |
168 module A | |
169 """ | |
170 self.expectErrors(snippet, [3]) | |
171 | |
172 def testEmpty2(self): | |
173 snippet = "" | |
174 self.expectErrors(snippet, [1]) | |
175 | |
176 def testRedefine(self): | |
177 snippet = """ | |
178 module test; | |
179 var int a; | |
180 var int b; | |
181 var int a; | |
182 """ | |
183 self.expectErrors(snippet, [5]) | |
184 | |
185 def testWhile(self): | |
186 snippet = """ | |
187 module tstwhile; | |
188 var int a; | |
189 function void t() | |
190 { | |
191 var int i = 0; | |
192 while (i < 1054) | |
193 { | |
194 i = i + 3; | |
195 a = a + i; | |
196 } | |
197 | |
198 while(true) | |
199 { | |
200 } | |
201 | |
202 while(false) | |
203 { | |
204 } | |
205 } | |
206 """ | |
207 self.expectOK(snippet) | |
208 | |
209 def testIf(self): | |
210 snippet = """ | |
211 module tstIFF; | |
212 function void t(int b) | |
213 { | |
214 var int a; | |
215 a = 2; | |
216 if (a > b) | |
217 { | |
218 if (a > 1337) | |
219 { | |
220 b = 2; | |
221 } | |
222 } | |
223 else | |
224 { | |
225 b = 1; | |
226 } | |
227 | |
228 return b; | |
229 } | |
230 """ | |
231 self.expectOK(snippet) | |
232 | |
233 def testTypeDef(self): | |
234 snippet = """ | |
235 module testtypedef; | |
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 | |
247 def testLocalVariable(self): | |
248 snippet = """ | |
249 module testlocalvar; | |
250 function void t() | |
251 { | |
252 var int a, b; | |
253 a = 2; | |
254 b = a + 2; | |
255 } | |
256 """ | |
257 self.expectOK(snippet) | |
258 | |
259 def testUnknownType(self): | |
260 snippet = """module testlocalvar; | |
261 function void t() | |
262 { | |
263 var int2 a; | |
264 } | |
265 """ | |
266 self.expectErrors(snippet, [4]) | |
267 | |
268 def testStruct1(self): | |
269 snippet = """ | |
270 module teststruct1; | |
271 function void t() | |
272 { | |
273 var struct {int x, y;} a; | |
274 a.x = 2; | |
275 a.y = a.x + 2; | |
276 } | |
277 """ | |
278 self.expectOK(snippet) | |
279 | |
280 def testPointerType1(self): | |
281 snippet = """ | |
282 module testpointer1; | |
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 | |
293 def testPointerType(self): | |
294 snippet = """ | |
295 module testpointer; | |
296 var int* pa, pb; | |
297 function void t(int a, double b) | |
298 { | |
299 var int a2; | |
300 a2 = a; // parameters cannot be escaped for now.. | |
301 pa = &a2; | |
302 pb = pa; | |
303 *pa = 22; | |
304 } | |
305 """ | |
306 self.expectOK(snippet) | |
307 | |
308 def testPointerTypeInCorrect(self): | |
309 snippet = """ | |
310 module testpointerincorrect; | |
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 """ | |
321 self.expectErrors(snippet, [6, 9, 10]) | |
322 | |
323 def testPointerTypeIr(self): | |
324 snippet = """ | |
325 module testptr_ir; | |
326 function void t() | |
327 { | |
328 var int* a; | |
329 a = cast<int*>(40); | |
330 *a = 2; | |
331 } | |
332 """ | |
333 self.expectOK(snippet) | |
334 | |
335 def testPointerTypeIr2(self): | |
336 snippet = """ | |
337 module testptr_ir; | |
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 """ | |
347 self.expectOK(snippet) | |
348 | |
349 def testWrongCast(self): | |
350 snippet = """ | |
351 module testptr_ir; | |
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 | |
362 def testComplexType(self): | |
363 snippet = """ | |
364 module testpointer; | |
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; | |
378 var int* pa; | |
379 | |
380 function void t(int a, int b, my_sptr x) | |
381 { | |
382 var my_struct *msp; | |
383 | |
384 var my_struct u, v; | |
385 var point *pt; | |
386 | |
387 pt = &msp->P1; | |
388 msp = x; | |
389 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; | |
390 x->memb2 = *pa + a * b; | |
391 | |
392 msp->P1.x = a * x->P1.y; | |
393 } | |
394 """ | |
395 self.expectOK(snippet) | |
396 | |
397 def testExamples(self): | |
398 """ Test all examples in the c3/examples directory """ | |
399 example_filenames = glob.glob('./c3/examples/*.c3') | |
400 for filename in example_filenames: | |
401 with open(filename, 'r') as f: | |
402 src = f.read() | |
403 self.expectOK(src, pack_dir='./c3/examples') | |
404 | |
405 def test2(self): | |
406 # testsrc2 is valid code: | |
407 snippet = """ | |
408 module test2; | |
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 """ | |
429 ircode = self.expectOK(snippet) | |
430 self.assertEqual(1, len(ircode.Functions)) | |
431 | |
432 if __name__ == '__main__': | |
433 unittest.main() | |
434 | |
435 |