blob: ef5963b9c9e947756f307d09e60c341197bec6c8 [file] [log] [blame]
Jack Palevich609c9942009-06-25 11:49:43 -07001#
2# Test the acc compiler
3
4import unittest
5import subprocess
6import os
7
8def compile(args):
9 proc = subprocess.Popen(["acc"] + args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
10 result = proc.communicate()
11 return result
12
13def runCmd(args):
14 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
15 result = proc.communicate()
16 return result[0].strip()
17
18def which(item):
19 return runCmd(["which", item])
20
21def fileType(item):
22 return runCmd(["file", item])
23
24def outputCanRun():
25 ft = fileType(which("acc"))
26 return ft.find("ELF 32-bit LSB executable, Intel 80386") >= 0
27
28def adb(args):
29 return runCmd(["adb"] + args)
30
31gArmInitialized = False
32
33def setupArm():
34 if gArmInitialized:
35 return
36 print "Setting up arm"
37 adb(["remount"])
38 adb(["shell", "rm", "/system/bin/acc"])
39 adb(["shell", "mkdir", "/system/bin/accdata"])
40 adb(["shell", "mkdir", "/system/bin/accdata/data"])
41 # Clear out old data TODO: handle recursion
42 adb(["shell", "rm", "/system/bin/accdata/data/*"])
43 # Copy over data
44 for root, dirs, files in os.walk("data"):
45 for d in dirs:
46 adb(["shell", "mkdir", os.path.join(root, d)])
47 for f in files:
48 adb(["push", os.path.join(root, f), os.path.join("/system/bin/accdata", root, f)])
49 # Copy over compiler
50 adb(["sync"])
51 gArmInitialied = True
52
53def compileArm(args):
54 setupArm()
55 proc = subprocess.Popen(["adb", "shell", "/system/bin/acc"] + args, stdout=subprocess.PIPE)
56 result = proc.communicate()
57 return result[0].replace("\r","")
58
59def compare(a, b):
60 if a != b:
61 firstDiff = firstDifference(a,b)
62 print "Strings differ at character", firstDiff, a[firstDiff], b[firstDiff]
63
64def firstDifference(a, b):
65 commonLen = min(len(a), len(b))
66 for i in xrange(0, commonLen):
67 if a[i] != b[i]:
68 return i
69 return commonLen
70
71class TestACC(unittest.TestCase):
72
73 def compileCheck(self, args, stdErrResult, stdOutResult=""):
74 out, err = compile(args)
75 compare(out, stdOutResult)
76 compare(err, stdErrResult)
77 self.assertEqual(out, stdOutResult)
78 self.assertEqual(err, stdErrResult)
79
80 def compileCheckArm(self, args, result):
81 self.assertEqual(compileArm(args), result)
82
83 def testCompileReturnVal(self):
84 self.compileCheck(["data/returnval-ansi.c"], "")
85
86 def testCompileReturnVal(self):
87 self.compileCheck(["data/otcc-ansi.c"], "")
88
89 def testRunReturnVal(self):
90 self.compileCheck(["-R", "data/returnval-ansi.c"],
91 "Executing compiled code:\nresult: 42\n")
92 def testRunOTCCANSI(self):
93 self.compileCheck(["-R", "data/otcc-ansi.c", "data/returnval.c"],
94 "Executing compiled code:\notcc-ansi.c: About to execute compiled code:\natcc-ansi.c: result: 42\nresult: 42\n")
95
96 def testRunOTCCANSI2(self):
97 self.compileCheck(["-R", "data/otcc-ansi.c", "data/otcc.c", "data/returnval.c"],
98 "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")
99
100 def testRunConstants(self):
101 self.compileCheck(["-R", "data/constants.c"],
102 "Executing compiled code:\nresult: 12\n",
103 "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" +
104 "'\\\"' = 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")
105
106 def testArmRunReturnVal(self):
107 self.compileCheckArm(["-R", "/system/bin/accdata/data/returnval-ansi.c"],
108 "Executing compiled code:\nresult: 42\n")
109
110if __name__ == '__main__':
111 if not outputCanRun():
112 print "Many tests are expected to fail, because acc is not a 32-bit x86 Linux executable."
113 unittest.main()
114