[go: up one dir, main page]

Menu

[390b88]: / ir / _tests.py  Maximize  Restore  History

Download this file

88 lines (66 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
import unittest
import aterm.factory
import box
import transf.exception
import ir.check
import ir.pprint
class TestPrettyPrint(unittest.TestCase):
def setUp(self):
self.factory = aterm.factory.factory
def parseArgs(self, args):
return [self.factory.parse(value) for value in args]
def parseKargs(self, kargs):
res = {}
for name, value in kargs.iteritems():
res[name] = self.factory.parse(value)
return res
checkerTestCases = {
'expr': [
('Binary(Plus(Int(32,Signed)),Lit(Int(32,Unsigned),1),Sym("x"))', True),
],
'stmt': [
('Label("label")', True),
('Asm("ret",[])', True),
('Asm("mov",[Sym("ax"), Lit(Int(32,Signed),1000)])', True),
('Function(Void,"main",[],[])', True),
],
'module': [
("Module([])", True),
("XXX", False),
],
}
def testChecker(self):
checker = ir.check
for methodName, subTestCases in self.checkerTestCases.iteritems():
for inputStr, expectedOutput in subTestCases:
input = self.factory.parse(inputStr)
try:
getattr(checker, methodName)(input)
output = True
except transf.exception.Fatal, ex:
output = False
self.failUnlessEqual(
output, expectedOutput,
msg = '%s(%s) = %r (%r expected)' % (methodName, inputStr, output, expectedOutput)
)
prettyPrinterTestCases = {
'ppExpr': [
('Binary(Plus(Int(32,Signed)),Lit(Int(32,Unsigned),1),Sym("x"))', '1 + x'),
],
'ppStmt': [
('Label("label")', 'label:'),
('Asm("ret",[])', 'asm("ret");'),
('Asm("mov",[Sym("ax"), Lit(Int(32,Signed),1000)])', 'asm("mov", ax, 1000);'),
('Function(Void,"main",[],[])', 'void main()\n{\n}\n'),
],
}
def testPrettyPrinter(self):
for methodName, subTestCases in self.prettyPrinterTestCases.iteritems():
for inputStr, expectedOutput in subTestCases:
input = self.factory.parse(inputStr)
boxes = getattr(ir.pprint, methodName)(input)
output = box.stringify(boxes)
self.failUnlessEqual(output, expectedOutput)
if __name__ == '__main__':
unittest.main()