Mercurial > lcfOS
annotate python/testc3.py @ 227:82dfe6a32717
Fixed tests
author | Windel Bouwman |
---|---|
date | Fri, 12 Jul 2013 17:42:39 +0200 |
parents | 1c7364bd74c7 |
children | 7f18ed9b6b7e |
rev | line source |
---|---|
158 | 1 import c3 |
2 import time, ppci, x86, ir | |
167 | 3 import unittest |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
4 import glob |
148 | 5 |
162 | 6 testsrc = """package test; |
148 | 7 |
215 | 8 /* |
9 demo of the source that is correct :) | |
10 */ | |
11 var int c, d; | |
163 | 12 var double e; |
13 var int f; | |
14 | |
167 | 15 const int A = 1337; |
149 | 16 |
17 function void test1() | |
148 | 18 { |
215 | 19 var int bdd; |
148 | 20 var int a = 10; |
215 | 21 bdd = 20; |
155 | 22 var int buf; |
148 | 23 var int i; |
24 i = 2; | |
155 | 25 var int zero = i - 2; |
148 | 26 if (i > 1) |
27 { | |
157 | 28 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1; |
148 | 29 } |
155 | 30 else |
31 { | |
32 ;;; | |
157 | 33 } |
155 | 34 |
35 t2(2, 3); | |
148 | 36 } |
37 | |
215 | 38 function int t2(int a, int b) |
148 | 39 { |
157 | 40 if (a > 0) |
41 { | |
215 | 42 a = 2 + t2(a - 1, 10); |
157 | 43 } |
44 | |
148 | 45 return a + b; |
46 } | |
47 | |
215 | 48 var int a, b; |
49 | |
166 | 50 function int t3(int aap, int blah) |
51 { | |
215 | 52 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true) |
166 | 53 { |
215 | 54 a = 2 + t2(a - 1, 0); |
166 | 55 } |
56 | |
57 return a + b; | |
58 } | |
59 | |
155 | 60 var int hahaa = 23 * 2; |
150 | 61 |
149 | 62 |
148 | 63 """ |
64 | |
204 | 65 class testLexer(unittest.TestCase): |
205 | 66 def testUnexpectedCharacter(self): |
67 snippet = """ var s \u6c34 """ | |
68 with self.assertRaises(ppci.CompilerError): | |
69 list(c3.lexer.tokenize(snippet)) | |
70 | |
204 | 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) | |
205 | 78 |
204 | 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): | |
215 | 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): | |
109 ircode = self.builder.build(snippet) | |
110 if not ircode: | |
111 self.diag.printErrors(snippet) | |
112 self.assertTrue(ircode) | |
217 | 113 return ircode |
215 | 114 |
115 def testFunctArgs(self): | |
167 | 116 snippet = """ |
117 package testargs; | |
118 function void t2(int a, double b) | |
119 { | |
120 t2(2, 2); | |
121 t2(2); | |
122 t2(1, 1.2); | |
123 } | |
124 """ | |
215 | 125 self.expectErrors(snippet, [5, 6]) |
148 | 126 |
215 | 127 def testExpressions(self): |
167 | 128 snippet = """ |
129 package test; | |
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 } | |
139 """ | |
215 | 140 self.expectErrors(snippet, [8, 9, 10]) |
205 | 141 |
215 | 142 def testEmpty(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
143 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
144 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
145 """ |
215 | 146 self.expectErrors(snippet, [3]) |
205 | 147 |
215 | 148 def testEmpty2(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
149 snippet = "" |
215 | 150 self.expectErrors(snippet, [1]) |
205 | 151 |
215 | 152 def testRedefine(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
153 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
154 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
155 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
156 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
157 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
158 """ |
215 | 159 self.expectErrors(snippet, [5]) |
205 | 160 |
215 | 161 def testWhile(self): |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
162 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
164 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
165 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
166 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
167 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
168 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
169 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
170 i = i + 3; |
186 | 171 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
172 } |
205 | 173 |
174 while(true) | |
175 { | |
176 } | |
177 | |
178 while(false) | |
179 { | |
180 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
181 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
182 """ |
215 | 183 self.expectOK(snippet) |
184 | |
185 def testIf(self): | |
186 snippet = """ | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 { |
180 | 190 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
191 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 if (a > b) |
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 if (a > 1337) |
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 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
197 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
198 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
199 else |
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 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
202 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
204 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
205 } |
215 | 206 """ |
207 self.expectOK(snippet) | |
225 | 208 |
209 def testTypeDef(self): | |
210 snippet = """ | |
211 package testtypedef; | |
212 type int my_int; | |
213 function void t() | |
214 { | |
215 var my_int a; | |
216 var int b; | |
217 a = 2; | |
218 b = a + 2; | |
219 } | |
220 """ | |
221 self.expectOK(snippet) | |
222 | |
213 | 223 |
225 | 224 def testPointerType1(self): |
225 snippet = """ | |
226 package testpointer1; | |
227 var int* pa; | |
228 function void t() | |
229 { | |
230 var int a; | |
231 pa = &a; | |
232 *pa = 22; | |
233 } | |
234 """ | |
235 self.expectOK(snippet) | |
236 | |
215 | 237 def testPointerType(self): |
213 | 238 snippet = """ |
239 package testpointer; | |
225 | 240 var int* pa, pb; |
213 | 241 function void t(int a, double b) |
242 { | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
243 pa = &a; |
225 | 244 pb = pa; |
213 | 245 *pa = 22; |
246 } | |
247 """ | |
215 | 248 self.expectOK(snippet) |
213 | 249 |
221 | 250 def testPointerTypeInCorrect(self): |
251 snippet = """ | |
252 package testpointerincorrect; | |
253 var int* pa; | |
254 function void t(int a, double b) | |
255 { | |
256 pa = 2; // type conflict | |
257 pa = &a; | |
258 pa = &2; | |
259 &a = 3; // no valid lvalue and incorrect types. | |
260 &a = pa; // No valid lvalue | |
261 **pa = 22; // Cannot deref int | |
262 } | |
263 """ | |
264 self.expectErrors(snippet, [6, 9, 9, 10, 11]) | |
265 | |
215 | 266 def testComplexType(self): |
213 | 267 snippet = """ |
268 package testpointer; | |
269 type int my_int; | |
270 | |
271 type struct { | |
272 int x, y; | |
273 } point; | |
274 | |
275 type struct { | |
276 int mem1; | |
277 int memb2; | |
278 point P1; | |
279 } my_struct; | |
280 | |
281 type my_struct* my_sptr; | |
225 | 282 var int* pa; |
213 | 283 |
227 | 284 function void t(int a, int b, my_sptr x) |
213 | 285 { |
286 var my_struct *msp; | |
287 | |
225 | 288 var my_struct u, v; |
289 var point *pt; | |
290 | |
291 pt = &msp->P1; | |
213 | 292 msp = x; |
225 | 293 *pa = 22 + u.mem1 * v.memb2 - u.P1.x; |
213 | 294 x->memb2 = *pa + a * b; |
295 | |
225 | 296 msp->P1.x = a * x->P1.y; |
213 | 297 } |
298 """ | |
215 | 299 self.expectOK(snippet) |
205 | 300 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
301 def testExamples(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
302 """ 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
|
303 example_filenames = glob.glob('./c3/examples/*.c3') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
304 for filename in example_filenames: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
305 with open(filename, 'r') as f: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
306 src = f.read() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
307 self.expectOK(src) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
308 |
215 | 309 def test2(self): |
205 | 310 # testsrc2 is valid code: |
215 | 311 snippet = """ |
205 | 312 package test2; |
313 | |
314 function void tst() | |
315 { | |
316 var int a, b; | |
317 a = 2 * 33 - 12; | |
318 b = a * 2 + 13; | |
319 a = b + a; | |
320 if (a > b and b == 3) | |
321 { | |
322 var int x = a; | |
323 x = b * 2 - a; | |
324 a = x*x; | |
325 } | |
326 else | |
327 { | |
328 a = b + a; | |
329 } | |
330 } | |
331 | |
332 """ | |
217 | 333 ircode = self.expectOK(snippet) |
334 self.assertEqual(1, len(ircode.Functions)) | |
167 | 335 |
336 if __name__ == '__main__': | |
337 unittest.main() | |
338 | |
339 |