comparison python/testc3.py @ 230:88a1e0baef65

Added some tests for IR-code
author Windel Bouwman
date Sat, 13 Jul 2013 19:53:44 +0200
parents 7f18ed9b6b7e
children 521567d17388
comparison
equal deleted inserted replaced
229:51d5ed1bd503 230:88a1e0baef65
12 var double e; 12 var double e;
13 var int f; 13 var int f;
14 14
15 const int A = 1337; 15 const int A = 1337;
16 16
17 function void test1() 17 function void test1()
18 { 18 {
19 var int bdd; 19 var int bdd;
20 var int a = 10; 20 var int a = 10;
21 bdd = 20; 21 bdd = 20;
22 var int buf; 22 var int buf;
110 if not ircode: 110 if not ircode:
111 self.diag.printErrors(snippet) 111 self.diag.printErrors(snippet)
112 self.assertTrue(ircode) 112 self.assertTrue(ircode)
113 return ircode 113 return ircode
114 114
115 def expectIR(self, snippet, ir_out):
116 ircode = self.builder.build(snippet)
117 if not ircode:
118 self.diag.printErrors(snippet)
119 self.assertTrue(ircode)
120 actual_ins = [str(i) for i in ircode.Instructions]
121 expected_ins = [i.strip() for i in ir_out.split('\n')]
122 self.assertSequenceEqual(expected_ins, actual_ins)
123 return ircode
124
115 def testFunctArgs(self): 125 def testFunctArgs(self):
116 snippet = """ 126 snippet = """
117 package testargs; 127 package testargs;
118 function void t2(int a, double b) 128 function void t2(int a, double b)
119 { 129 {
136 c = a; 146 c = a;
137 c = b > 1; 147 c = b > 1;
138 } 148 }
139 """ 149 """
140 self.expectErrors(snippet, [8, 9, 10]) 150 self.expectErrors(snippet, [8, 9, 10])
151
152 def testExpression1(self):
153 snippet = """
154 package testexpr1;
155 function void t()
156 {
157 var int a, b, c;
158 a = 1;
159 b = a * 2 + a * a;
160 c = b * a - 3;
161 }
162 """
163 block_code = """ a0 = alloc
164 b1 = alloc
165 c2 = alloc
166 t3 = 1
167 [a0] = t3
168 t4 = [a0]
169 t5 = 2
170 mul6 = t4 * t5
171 t7 = [a0]
172 t8 = [a0]
173 mul9 = t7 * t8
174 add10 = mul6 + mul9
175 [b1] = add10
176 t11 = [b1]
177 t12 = [a0]
178 mul13 = t11 * t12
179 t14 = 3
180 sub15 = mul13 - t14
181 [c2] = sub15
182 ret """
183 self.expectIR(snippet, block_code)
141 184
142 def testEmpty(self): 185 def testEmpty(self):
143 snippet = """ 186 snippet = """
144 package A 187 package A
145 """ 188 """
218 b = a + 2; 261 b = a + 2;
219 } 262 }
220 """ 263 """
221 self.expectOK(snippet) 264 self.expectOK(snippet)
222 265
223 266 def testLocalVariable(self):
267 snippet = """
268 package testlocalvar;
269 function void t()
270 {
271 var int a, b;
272 a = 2;
273 b = a + 2;
274 }
275 """
276 block_code = """a0 = alloc
277 b1 = alloc
278 t2 = 2
279 [a0] = t2
280 t3 = [a0]
281 t4 = 2
282 add5 = t3 + t4
283 [b1] = add5
284 ret """
285 self.expectIR(snippet, block_code)
286
287 def testStruct1(self):
288 snippet = """
289 package teststruct1;
290 function void t()
291 {
292 var struct {int x, y;} a;
293 a.x = 2;
294 a.y = a.x + 2;
295 }
296 """
297 block_code = """a0 = alloc
298 t1 = 2
299 off_x2 = 0
300 adr_x3 = a0 + off_x2
301 [adr_x3] = t1
302 off_x4 = 0
303 adr_x5 = a0 + off_x4
304 t6 = [adr_x5]
305 t7 = 2
306 add8 = t6 + t7
307 off_y9 = 0
308 adr_y10 = a0 + off_y9
309 [adr_y10] = add8
310 ret """
311 self.expectIR(snippet, block_code)
312
224 def testPointerType1(self): 313 def testPointerType1(self):
225 snippet = """ 314 snippet = """
226 package testpointer1; 315 package testpointer1;
227 var int* pa; 316 var int* pa;
228 function void t() 317 function void t()
259 &a = pa; // No valid lvalue 348 &a = pa; // No valid lvalue
260 **pa = 22; // Cannot deref int 349 **pa = 22; // Cannot deref int
261 } 350 }
262 """ 351 """
263 self.expectErrors(snippet, [6, 9, 10]) 352 self.expectErrors(snippet, [6, 9, 10])
353
354 def testPointerTypeIr(self):
355 snippet = """
356 package testptr_ir;
357 function void t()
358 {
359 var int* a;
360 a = cast<int*>(40);
361 *a = 2;
362 }
363 """
364 block_code = """a0 = alloc
365 t1 = 40
366 [a0] = t1
367 t2 = 2
368 deref3 = [a0]
369 [deref3] = t2
370 ret """
371 self.expectIR(snippet, block_code)
372
373 def testPointerTypeIr2(self):
374 snippet = """
375 package testptr_ir;
376 type struct {int x,y;}* gpio;
377 function void t()
378 {
379 var gpio a;
380 a = cast<gpio>(40);
381 a->x = 2;
382 a->y = a->x - 14;
383 }
384 """
385 block_code = """a0 = alloc
386 t1 = 40
387 [a0] = t1
388 t2 = 2
389 deref3 = [a0]
390 off_x4 = 0
391 adr_x5 = deref3 + off_x4
392 [adr_x5] = t2
393 deref6 = [a0]
394 off_x7 = 0
395 adr_x8 = deref6 + off_x7
396 t9 = [adr_x8]
397 t10 = 14
398 sub11 = t9 - t10
399 deref12 = [a0]
400 off_y13 = 0
401 adr_y14 = deref12 + off_y13
402 [adr_y14] = sub11
403 ret """
404 self.expectIR(snippet, block_code)
405
406 def testWrongCast(self):
407 snippet = """
408 package testptr_ir;
409 type struct {int x,y;}* gpio;
410 function void t()
411 {
412 var gpio a;
413 *cast<gpio>(*a);
414 }
415 """
416 # TODO: remove the duplicate error:
417 self.expectErrors(snippet, [7, 7])
264 418
265 def testComplexType(self): 419 def testComplexType(self):
266 snippet = """ 420 snippet = """
267 package testpointer; 421 package testpointer;
268 type int my_int; 422 type int my_int;