-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTests.py
executable file
·228 lines (198 loc) · 8.4 KB
/
Tests.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/python
import sys
from subprocess import Popen,PIPE
from os import listdir, getcwd
from os.path import isfile, join,basename,dirname
from optparse import OptionParser
from os.path import exists
from tempfile import NamedTemporaryFile
from filecmp import cmp
class Test():
def __init__(self,jlFile,category):
self.file=jlFile
self.category=category
basename=jlFile[:-3]
self.inputfile = basename+'.input' if isfile(basename+'.input') else ""
self.outputfile= basename+'.output' if isfile(basename+'.output') else ""
self.log=[]
self.okCompilation=False
def __str__(self):
return self.file + " " + self.category + " " + str(self.okCompilation)
def checkCompilation(self):
if self.category == "bad":
if self.returncode != 0:
self.log+=["OK: exit code not 0"]
else:
self.log+=["ERROR (bad test accepted): exit code 0"]
return False
if 'ERROR' in self.err.decode('utf-8'):
self.log+=["OK: \"ERROR\" is in stderr"]
else:
self.log+=["ERROR (bad test accepted): \"ERROR\" is not in stderr"]
return False
if self.category == "good" or "extension" in self.category:
if self.returncode == 0:
self.log+=["OK: exit code is 0"]
else:
self.log+=["ERROR (good test rejected): exit code is not 0"]
if 'OK' in self.err.decode('utf-8'):
self.log+=["OK: \"OK\" is in stderr"]
else:
self.log+=["ERROR: \"OK\" is not in stderr"]
return False
return True
@property
def logOutput(self):
return '\n'.join(self.log)
def runGood(self):
if not exists("a.out"):
self.log+=["ERROR: the file a.out does not exist.",]
return False
outfile=NamedTemporaryFile()
if self.inputfile: p=Popen(['./a.out'],stdout=outfile,stdin=open(self.inputfile))
else: p=Popen(['./a.out'],stdout=outfile)
ret_code = p.wait()
outfile.flush()
#TODO check that the execution didnt explote
if self.outputfile:
if cmp(self.outputfile,outfile.name): self.okRun = True
else: self.log+=["ERROR: the output of a.out does not match: %s" % self.outputfile]
else: self.okRun = True
def compile(self,jlc):
d = dirname(jlc)
if not d:
d = '.'
p = Popen([join(d,basename(jlc)), self.file], stdout=PIPE, stderr=PIPE, cwd=getcwd())
self.output, self.err = p.communicate()
self.returncode = p.returncode
self.okCompilation=self.checkCompilation()
self.okRun = False
def compileX86(self,jlc):
d = dirname(jlc)
if not d:
d = '.'
p = Popen([join(d,basename(jlc)), self.file, "-x86"], stdout=PIPE, stderr=PIPE, cwd=getcwd())
self.output, self.err = p.communicate()
self.returncode = p.returncode
self.okCompilation=self.checkCompilation()
self.okRun = False
def loadTests(testsDir,submission):
badTestsDir=join(testsDir,"bad")
tests = [ Test(join(badTestsDir,f),"bad") for f in listdir(badTestsDir) if isfile(join(badTestsDir,f)) and join(badTestsDir,f)[-3:]=='.jl']
goodTestsDir=join(testsDir,"good")
tests += [ Test(join(goodTestsDir,f),"good") for f in listdir(goodTestsDir) if isfile(join(goodTestsDir,f)) and join(goodTestsDir,f)[-3:]=='.jl']
if submission == 'C':
extTestsDir=join(testsDir,"extensions")
for ext in listdir(extTestsDir):
extDir=(join(extTestsDir,ext))
tests += [ Test(join(extDir,f),"extension - "+ext) for f in listdir(extDir) if isfile(join(extDir,f)) and join(extDir,f)[-3:]=='.jl']
return tests
class Summary():
def analyzeGood(self,test):
total=[0,0]
if test.category not in self.shouldWork: self.shouldWork[test.category]=[0,0,0,[]]
good=self.shouldWork[test.category]
good[2]+=1
if test.okCompilation:
good[0]+=1
total[0]+=1
if self.submission=="B" or self.submission=="C" or self.submission=="D":
total[1]+=1
if test.okRun:
good[1]+=1
total[0]+=1
else:
good[3]+=[test]
else:
good[3]+=[test]
self.shouldWork[test.category]=good
return total
def __init__(self,tests,submission):
self.submission=submission
#compiled/run/total/[failedtests]
self.shouldWork={}
bad=[0,0,0,[]]
#passed/total
total=[0,0]
for test in tests:
total[1]+=1
if test.category=="good" or "extension" in test.category:
subtotal=self.analyzeGood(test)
total=list(map(lambda x,y : x+y,total,subtotal))
if test.category=="bad":
bad[2]+=1
if test.okCompilation:
bad[0]+=1
total[0]+=1
else:
bad[3]+=[test]
self.good=self.shouldWork.get('good', [0,0,0,[]])
self.bad=bad
self.total=total
def __str__(self):
c1=10
c2=10
ret=['']
fails=self.good[3]+self.bad[3]
good=''
if self.submission=='A':
ret+=['Compiled/Total']
good="%i/%i" % (self.good[0],self.good[2])
elif self.submission=='B' or self.submission=='C':
ret+=['LLVM Tests:']
ret+=['Run/Compiled/Total']
good="%i/%i/%i" % (self.good[1],self.good[0],self.good[2])
elif self.submission=="D":
ret+=['x86 Tests:']
ret+=['Run/Compiled/Total']
good="%i/%i/%i" % (self.good[1],self.good[0],self.good[2])
bad= "%i/%i" % (self.bad[0],self.bad[2])
mistakes = len(self.good[3])
result = '1 mistake found' if mistakes==1 else 'OK' if not mistakes else "%s mistakes found" % mistakes
ret+=["good".rjust(c1)+good.rjust(c2)+" <- %s" % (result)]
mistakes = len(self.bad[3])
result = '1 mistake found' if mistakes==1 else 'OK' if not mistakes else "%s mistakes found" % mistakes
ret+=["bad".rjust(c1)+bad.rjust(c2)+" <- %s" % (result)]
if self.submission=='C':
ret+=["",'extensions:']
for (i,j) in self.shouldWork.iteritems():
if 'extension' not in i: continue
good="%i/%i/%i" % (j[1],j[0],j[2])
mistakes = len(j[3])
result = '1 mistake found' if mistakes==1 else 'OK' if not mistakes else "%s mistakes found" % mistakes
ret+=[i.split(' - ')[1].rjust(c1)+good.rjust(c2)+" <- %s" % (result)]
total="%i/%i" % (self.total[0],self.total[1])
ret+=["--"+"-"*c1,"total".rjust(c1)+total.rjust(c2)]
if self.submission=="D":
ret+= ["",'Failed X86 tests:']+[ "\n - %s\n%s" %(f.file,f.logOutput) for f in fails] if fails else ["","All test passed for x86!"]
else:
ret+= ["",'Failed tests:']+[ "\n - %s\n%s" %(f.file,f.logOutput) for f in fails] if fails else ["","All test passed!"]
return '\n'.join(ret)
def showProgress():
sys.stderr.write('.')
sys.stderr.flush()
def main(jlc,testsDir,submission):
showProgress();
tests=loadTests(testsDir,submission)
for test in tests:
showProgress();
test.compile(jlc)
if (submission == "B" or submission == "C") and test.category is not "bad":
showProgress();
test.runGood();
sys.stdout.write("%s\n" % Summary(tests,submission))
if (submission == "C"):
for test in tests:
showProgress();
if (submission == "C") and test.category is not "bad":
showProgress();
test.compileX86(jlc);
test.runGood();
sys.stdout.write("%s\n" % Summary(tests,"D"))
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-c", "--compiler", dest="compiler", help="Your compiler (default ./jlc)", metavar="FILE",default="./jlc")
parser.add_option("-s", "--submission", dest="submission", help="testing for which submission (A, B or C?)", metavar="A|B|C")
parser.add_option("-t", "--testsuite", dest="testsuite", help="testsuite directory (default ./testsuite)", default="./testsuite", metavar="DIR")
(options, args) = parser.parse_args()
main(options.compiler,options.testsuite,options.submission)