-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.py
122 lines (106 loc) · 4.09 KB
/
tester.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
import os
import platform
import json
# print(type(meta_data))
class tester:
def __init__(self, path):
new_path = ""
if platform.system() == 'Linux':
for word in path.split('/'):
if len(word.split()) == 2:
new_path += ("'"+word+"'/")
else:
new_path += (word+"/")
else:
new_path = path
self.path = new_path
def build_report(self, problem_id):
for files in os.walk(os.getcwd()):
# print(files[2])
if(files[2].count('report.html') != 0):
os.remove('report.html')
break
with open('report.html', 'w') as report:
report.write("<h3>"+"testing "+problem_id+".....</h3>\n")
report.write("<div style=""display:flex;"" >")
report.write(
"<div style=""padding:10px;font-size:15px; >\n<p>Your otput: </p> \n<pre"" > \n"+open("your_output_temp.txt", 'r').read()+" </pre> \n</div>")
report.write(
"<div style=""padding:10px;font-size:15px; >\n<p>Expected otput: </p> \n<pre"" > \n"+open("outputs/" + problem_id + ".txt", 'r').read()+" </pre> \n</div>")
report.write("</div>")
def tester(self, problem_id):
print("testing "+problem_id+".....\n")
meta_data = open('meta_data.json', 'r')
meta_data = json.load(meta_data)
n_tests = meta_data[problem_id]
k = 1
found = True
for files in os.walk(self.path):
# print(files[2])
if(files[2].count(problem_id+'.cpp') == 0):
found = False
break
# print(" found? " + str(found))
if found:
os.system("g++ \""+self.path+"\\"+problem_id +
".cpp\" -o "+problem_id+".exe")
while k <= n_tests:
print("test case #" + str(k) + ": ")
os.system("echo test case #" + str(k) +
": >> your_output_temp.txt")
in_file = problem_id+"_in_"+str(k)+".txt | "
(os.system("cat inputs/"+in_file +
problem_id+".exe >> your_output_temp.txt"))
# print(test_result)
k = k+1
print()
self.build_report(problem_id)
else:
print(" *.cpp not found")
def tester_linux(self, problem_id):
print("testing "+problem_id+".....\n")
meta_data = open('meta_data.json', 'r')
meta_data = json.load(meta_data)
n_tests = meta_data[problem_id]
k = 1
found = True
for files in os.walk(self.path):
print(files[2])
if(files[2].count(problem_id+'.cpp') == 0):
found = False
break
# print(" found? " + str(found))
if found:
os.system("g++ "+self.path+"/"+problem_id +
".cpp -o "+problem_id+".out")
while k <= n_tests:
print("test case #" + str(k) + ": ")
# print("echo test case #" + str(k) +
# ": >> your_output_temp.txt")
os.system("echo 'test case #" + str(k) +
":' >> your_output_temp.txt")
in_file = problem_id+"_in_"+str(k)+".txt | "
(os.system("cat inputs/"+in_file + " ./" +
problem_id+".out >> your_output_temp.txt"))
# print(test_result)
k = k+1
print()
self.build_report(problem_id)
else:
print(" *.cpp not found")
if __name__ == "__main__":
with open('source_location.txt', 'r') as src_:
path = src_.read()
user_input = ''
tester_ = tester(path)
os.chdir("runtime")
while True:
user_input = input("input : ")
if user_input == 'exit':
break
try:
if(os.listdir().count('your_output_temp.txt')) != 0:
os.remove('your_output_temp.txt')
tester_.tester_linux(user_input)
finally:
continue