blob: bb9ef014a860691cae0ad64b7b58f3a0b8253f46 [file] [log] [blame]
Jack Palevich609c9942009-06-25 11:49:43 -07001#
2# Test the acc compiler
3
4import unittest
5import subprocess
6import os
Jack Palevich59178c02009-07-13 14:15:18 -07007import sets
8
9gArmInitialized = False
Jack Palevich609c9942009-06-25 11:49:43 -070010
11def compile(args):
12 proc = subprocess.Popen(["acc"] + args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
13 result = proc.communicate()
14 return result
15
16def runCmd(args):
17 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18 result = proc.communicate()
19 return result[0].strip()
20
21def which(item):
22 return runCmd(["which", item])
23
24def fileType(item):
25 return runCmd(["file", item])
26
27def outputCanRun():
28 ft = fileType(which("acc"))
29 return ft.find("ELF 32-bit LSB executable, Intel 80386") >= 0
30
31def adb(args):
32 return runCmd(["adb"] + args)
33
Jack Palevich609c9942009-06-25 11:49:43 -070034def setupArm():
Jack Palevich59178c02009-07-13 14:15:18 -070035 global gArmInitialized
Jack Palevich609c9942009-06-25 11:49:43 -070036 if gArmInitialized:
37 return
38 print "Setting up arm"
39 adb(["remount"])
40 adb(["shell", "rm", "/system/bin/acc"])
41 adb(["shell", "mkdir", "/system/bin/accdata"])
42 adb(["shell", "mkdir", "/system/bin/accdata/data"])
43 # Clear out old data TODO: handle recursion
44 adb(["shell", "rm", "/system/bin/accdata/data/*"])
45 # Copy over data
46 for root, dirs, files in os.walk("data"):
47 for d in dirs:
48 adb(["shell", "mkdir", os.path.join(root, d)])
49 for f in files:
50 adb(["push", os.path.join(root, f), os.path.join("/system/bin/accdata", root, f)])
51 # Copy over compiler
52 adb(["sync"])
Jack Palevich59178c02009-07-13 14:15:18 -070053 gArmInitialized = True
Jack Palevich609c9942009-06-25 11:49:43 -070054
55def compileArm(args):
56 setupArm()
57 proc = subprocess.Popen(["adb", "shell", "/system/bin/acc"] + args, stdout=subprocess.PIPE)
58 result = proc.communicate()
59 return result[0].replace("\r","")
60
61def compare(a, b):
62 if a != b:
Jack Palevich40600de2009-07-01 15:32:35 -070063 firstDiff = firstDifference(a, b)
Jack Palevichbab80642009-07-09 13:54:54 -070064 print "Strings differ at character %d. Common: %s. Difference '%s' != '%s'" % (
65 firstDiff, a[0:firstDiff], safeAccess(a, firstDiff), safeAccess(b, firstDiff))
Jack Palevich40600de2009-07-01 15:32:35 -070066
67def safeAccess(s, i):
68 if 0 <= i < len(s):
69 return s[i]
70 else:
71 return '?'
Jack Palevich609c9942009-06-25 11:49:43 -070072
73def firstDifference(a, b):
74 commonLen = min(len(a), len(b))
75 for i in xrange(0, commonLen):
76 if a[i] != b[i]:
77 return i
78 return commonLen
79
Jack Palevich45431bc2009-07-13 15:57:26 -070080# a1 and a2 are the expected stdout and stderr.
81# b1 and b2 are the actual stdout and stderr.
82# Compare the two, sets. Allow any individual line
83# to appear in either stdout or stderr. This is because
84# the way we obtain output on the ARM combines both
85# streams into one sequence.
86
87def compareOuput(a1,a2,b1,b2):
Jack Palevich59178c02009-07-13 14:15:18 -070088 while True:
89 totalLen = len(a1) + len(a2) + len(b1) + len(b2)
90 a1, b1 = matchCommon(a1, b1)
91 a1, b2 = matchCommon(a1, b2)
92 a2, b1 = matchCommon(a2, b1)
93 a2, b2 = matchCommon(a2, b2)
94 newTotalLen = len(a1) + len(a2) + len(b1) + len(b2)
95 if newTotalLen == 0:
96 return True
97 if newTotalLen == totalLen:
98 print "Failed at %d %d %d %d" % (len(a1), len(a2), len(b1), len(b2))
99 print "a1", a1
100 print "a2", a2
101 print "b1", b1
102 print "b2", b2
103 return False
104
105def matchCommon(a, b):
Jack Palevich45431bc2009-07-13 15:57:26 -0700106 """Remove common items from the beginning of a and b,
107 return just the tails that are different."""
Jack Palevich59178c02009-07-13 14:15:18 -0700108 while len(a) > 0 and len(b) > 0 and a[0] == b[0]:
109 a = a[1:]
110 b = b[1:]
111 return a, b
112
113def rewritePaths(args):
114 return [rewritePath(x) for x in args]
115
116def rewritePath(p):
Jack Palevich45431bc2009-07-13 15:57:26 -0700117 """Take a path that's correct on the x86 and convert to a path
118 that's correct on ARM."""
Jack Palevich59178c02009-07-13 14:15:18 -0700119 if p.startswith("data/"):
120 p = "/system/bin/accdata/" + p
121 return p
122
Jack Palevich609c9942009-06-25 11:49:43 -0700123class TestACC(unittest.TestCase):
Jack Palevich609c9942009-06-25 11:49:43 -0700124
Jack Palevich59178c02009-07-13 14:15:18 -0700125 def checkResult(self, out, err, stdErrResult, stdOutResult=""):
126 a1 = out.splitlines()
127 a2 = err.splitlines()
128 b2 = stdErrResult.splitlines()
129 b1 = stdOutResult.splitlines()
Jack Palevich45431bc2009-07-13 15:57:26 -0700130 self.assertEqual(True, compareOuput(a1,a2,b1,b2))
Jack Palevich59178c02009-07-13 14:15:18 -0700131
132 def compileCheck(self, args, stdErrResult, stdOutResult="",
133 targets=['arm', 'x86']):
134 targetSet = sets.ImmutableSet(targets)
135 if 'x86' in targetSet:
136 out, err = compile(args)
137 self.checkResult(out, err, stdErrResult, stdOutResult)
138 if 'arm' in targetSet:
139 out = compileArm(rewritePaths(args))
140 self.checkResult(out, "", stdErrResult, stdOutResult)
141
Jack Palevich609c9942009-06-25 11:49:43 -0700142 def compileCheckArm(self, args, result):
143 self.assertEqual(compileArm(args), result)
144
145 def testCompileReturnVal(self):
146 self.compileCheck(["data/returnval-ansi.c"], "")
147
Jack Palevich59178c02009-07-13 14:15:18 -0700148 def testCompileOTCCANSII(self):
149 self.compileCheck(["data/otcc-ansi.c"], "", "", ['x86'])
Jack Palevich609c9942009-06-25 11:49:43 -0700150
151 def testRunReturnVal(self):
152 self.compileCheck(["-R", "data/returnval-ansi.c"],
153 "Executing compiled code:\nresult: 42\n")
Jack Palevich40600de2009-07-01 15:32:35 -0700154
Jack Palevichbab80642009-07-09 13:54:54 -0700155 def testStringLiteralConcatenation(self):
Jack Palevich40600de2009-07-01 15:32:35 -0700156 self.compileCheck(["-R", "data/testStringConcat.c"],
157 "Executing compiled code:\nresult: 13\n", "Hello, world\n")
158
Jack Palevich609c9942009-06-25 11:49:43 -0700159 def testRunOTCCANSI(self):
160 self.compileCheck(["-R", "data/otcc-ansi.c", "data/returnval.c"],
Jack Palevich59178c02009-07-13 14:15:18 -0700161 "Executing compiled code:\notcc-ansi.c: About to execute compiled code:\natcc-ansi.c: result: 42\nresult: 42\n", "",
162 ['x86'])
Jack Palevich609c9942009-06-25 11:49:43 -0700163
164 def testRunOTCCANSI2(self):
165 self.compileCheck(["-R", "data/otcc-ansi.c", "data/otcc.c", "data/returnval.c"],
Jack Palevich59178c02009-07-13 14:15:18 -0700166 "Executing compiled code:\notcc-ansi.c: About to execute compiled code:\notcc.c: about to execute compiled code.\natcc-ansi.c: result: 42\nresult: 42\n", "",['x86'])
Jack Palevich609c9942009-06-25 11:49:43 -0700167
168 def testRunConstants(self):
169 self.compileCheck(["-R", "data/constants.c"],
170 "Executing compiled code:\nresult: 12\n",
171 "0 = 0\n010 = 8\n0x10 = 16\n'\\a' = 7\n'\\b' = 8\n'\\f' = 12\n'\\n' = 10\n'\\r' = 13\n'\\t' = 9\n'\\v' = 11\n'\\\\' = 92\n'\\'' = 39\n" +
172 "'\\\"' = 34\n'\\?' = 63\n'\\0' = 0\n'\\1' = 1\n'\\12' = 10\n'\\123' = 83\n'\\x0' = 0\n'\\x1' = 1\n'\\x12' = 18\n'\\x123' = 291\n'\\x1f' = 31\n'\\x1F' = 31\n")
173
Jack Palevichbab80642009-07-09 13:54:54 -0700174 def testRunFloat(self):
175 self.compileCheck(["-R", "data/float.c"],
176 "Executing compiled code:\nresult: 0\n",
Jack Palevichb7718b92009-07-09 22:00:24 -0700177 "int: 1 float: 2.2 double: 3.3\n ftoi(1.4f)=1\n dtoi(2.4)=2\n itof(3)=3\n itod(4)=4\nglobals: 1 2 3 4\nargs: 1 2 3 4\nlocals: 1 2 3 4\ncast rval: 2 4\ncast lval: 1.1 2 3.3 4\n")
Jack Palevichbab80642009-07-09 13:54:54 -0700178
179 def testRunFlops(self):
180 self.compileCheck(["-R", "data/flops.c"],
Jack Palevich45431bc2009-07-13 15:57:26 -0700181 """Executing compiled code:
182result: 0""",
183"""-1.1 = -1.1
184!1.2 = 0
185!0 = 1
186double op double:
1871 + 2 = 3
1881 - 2 = -1
1891 * 2 = 2
1901 / 2 = 0.5
191float op float:
1921 + 2 = 3
1931 - 2 = -1
1941 * 2 = 2
1951 / 2 = 0.5
196double op float:
1971 + 2 = 3
1981 - 2 = -1
1991 * 2 = 2
2001 / 2 = 0.5
201double op int:
2021 + 2 = 3
2031 - 2 = -1
2041 * 2 = 2
2051 / 2 = 0.5
206int op double:
2071 + 2 = 3
2081 - 2 = -1
2091 * 2 = 2
2101 / 2 = 0.5
211double op double:
2121 op 2: < 1 <= 1 == 0 >= 0 > 0 != 1
2131 op 1: < 0 <= 1 == 1 >= 1 > 0 != 0
2142 op 1: < 0 <= 0 == 0 >= 1 > 1 != 1
215double op float:
2161 op 2: < 1 <= 1 == 0 >= 0 > 0 != 1
2171 op 1: < 0 <= 1 == 1 >= 1 > 0 != 0
2182 op 1: < 0 <= 0 == 0 >= 1 > 1 != 1
219float op float:
2201 op 2: < 1 <= 1 == 0 >= 0 > 0 != 1
2211 op 1: < 0 <= 1 == 1 >= 1 > 0 != 0
2222 op 1: < 0 <= 0 == 0 >= 1 > 1 != 1
223int op double:
2241 op 2: < 1 <= 1 == 0 >= 0 > 0 != 1
2251 op 1: < 0 <= 1 == 1 >= 1 > 0 != 0
2262 op 1: < 0 <= 0 == 0 >= 1 > 1 != 1
227double op int:
2281 op 2: < 1 <= 1 == 0 >= 0 > 0 != 1
2291 op 1: < 0 <= 1 == 1 >= 1 > 0 != 0
2302 op 1: < 0 <= 0 == 0 >= 1 > 1 != 1
231branching: 1 0 1
Jack Palevichfd3db482009-07-14 19:39:36 -0700232testpassi: 1 2 3 4 5 6 7 8 9 10 11 12
233testpassf: 1 2 3 4 5 6 7 8 9 10 11 12
234testpassd: 1 2 3 4 5 6 7 8 9 10 11 12
235testpassi: 1 2 3 4 5 6 7 8 9 10 11 12
236testpassf: 1 2 3 4 5 6 7 8 9 10 11 12
237testpassd: 1 2 3 4 5 6 7 8 9 10 11 12
238testpassi: 1 2 3 4 5 6 7 8 9 10 11 12
239testpassf: 1 2 3 4 5 6 7 8 9 10 11 12
240testpassd: 1 2 3 4 5 6 7 8 9 10 11 12
Jack Palevich45431bc2009-07-13 15:57:26 -0700241testpassidf: 1 2 3
242""")
243 def testCasts(self):
244 self.compileCheck(["-R", "data/casts.c"],
245 """Executing compiled code:
246result: 0""", """Reading from a pointer: 3 3
247Writing to a pointer: 4
248Testing casts: 3 3 4.5 4
249Testing reading (int*): 4
250Testing writing (int*): 8 9
251Testing reading (char*): 0x78 0x56 0x34 0x12
252Testing writing (char*): 0x87654321
253f(10)
254Function pointer result: 70
255Testing read/write (float*): 8.8 9.9
256Testing read/write (double*): 8.8 9.9
257""")
Jack Palevich609c9942009-06-25 11:49:43 -0700258
Jack Palevich25c0cca2009-07-13 16:56:28 -0700259 def testChar(self):
260 self.compileCheck(["-R", "data/char.c"], """Executing compiled code:
261result: 0""", """a = 99, b = 41
262ga = 100, gb = 44""")
263
Jack Palevicha8f427f2009-07-13 18:40:08 -0700264 def testPointerArithmetic(self):
265 self.compileCheck(["-R", "data/pointers.c"], """Executing compiled code:
266result: 0""", """Pointer difference: 1 4
267Pointer addition: 2
268Pointer comparison to zero: 0 0 1
269Pointer comparison: 1 0 0 0 1
270""")
Jack Palevich37c54bd2009-07-14 18:35:36 -0700271 def testRollo3(self):
272 self.compileCheck(["-R", "data/rollo3.c"], """Executing compiled code:
273result: 10""", """""")
274
Jack Palevicha8f427f2009-07-13 18:40:08 -0700275
Jack Palevich25c0cca2009-07-13 16:56:28 -0700276
Jack Palevich609c9942009-06-25 11:49:43 -0700277if __name__ == '__main__':
278 if not outputCanRun():
279 print "Many tests are expected to fail, because acc is not a 32-bit x86 Linux executable."
280 unittest.main()
281