Skip to content

Commit 33f1001

Browse files
author
matthias
committed
test.py improved; now actually uses cpp and python depending on switches
1 parent 6684cca commit 33f1001

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

test.py

+46-18
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
def usage():
88
s = "Options: \n"
99
s += "-d : compile in Debug mode"
10-
s += "-p : test python"
10+
s += "\n-p : test python"
1111
print(s)
1212

1313
def colorCode(q):
14-
return [ "\\033[1;41m%s\\033[0m", "\\033[1;31m%s\\033[0m", "%s", "\\033[1;32m%s\\033[0m" ][q+2]
14+
return [ "\033[41m%s\033[0m", "\033[31m%s\033[0m", "%s", "\033[32m%s\033[0m" ][q+2]
1515

1616
def colorTestResult(r):
17-
q = {'+': 1, '?':0 }
17+
q = {'+': 1, '?':0, False: -1, True: 1}
1818
return colorCode( q[r] if r in q else -1 ) % r
1919

2020
def prettyStr(x):
@@ -28,9 +28,9 @@ def prettyStr(x):
2828
def tc_equal(expected, received):
2929
try:
3030
_t = type(expected)
31-
print(_t, type(received))
31+
# print(_t, type(received))
3232
received = _t(received)
33-
print(_t)
33+
# print(_t)
3434
if _t == list or _t == tuple:
3535
print("checking list or tuple")
3636
if len(expected) != len(received): return False
@@ -52,26 +52,49 @@ def tc_equal(expected, received):
5252
except:
5353
return False
5454

55-
56-
def do_test():
57-
in_files = glob.glob("input*")
58-
out_files = glob.glob("output*")
55+
def do_test(exc_str):
56+
in_files = sorted(glob.glob("input*"))
57+
out_files = sorted(glob.glob("output*"))
5958
print(in_files, out_files)
6059

60+
retlist = []
6161
for inf, outf in zip(in_files, out_files):
62+
print("")
63+
print("="*10)
64+
print("="*10)
6265
print(inf, outf)
6366
with open(inf) as f:
64-
p = subprocess.Popen("python myprog.py", stdout=subprocess.PIPE, stdin=f)
67+
input_content = f.read()
68+
f.seek(0)
69+
p = subprocess.Popen(exc_str, stdout=subprocess.PIPE, stdin=f, shell=True)
6570
recv = p.stdout.read()
6671

6772
with open(outf) as of:
68-
s = of.read()
69-
print(s)
70-
print(recv)
71-
print(s == recv)
72-
print tc_equal(s, recv)
73+
correct_answer = of.read()
74+
print("Input")
75+
print(prettyStr(input_content.rstrip()))
76+
print("Expected:")
77+
print(prettyStr(correct_answer.rstrip()))
78+
print("-"*10)
79+
print("Received:")
80+
print(prettyStr(recv.rstrip()))
81+
ret_comp = tc_equal(correct_answer, recv)
82+
83+
print("Success: " + colorTestResult(ret_comp))
84+
retlist.append(colorTestResult( "+" if ret_comp else "-"))
85+
86+
print("\n\n" + "="*20 + "\nSummary: ")
87+
print(" ".join(retlist))
88+
7389

7490

91+
# print("Successful? " + colorTestResult(tc_equal(correct_answer, recv)))
92+
93+
def compile_cpp(DBG):
94+
exc_str = 'g++ -std=c++11 ' + DBG + ' -Wall A.cc -o A.exe'
95+
print(exc_str)
96+
subprocess.call(exc_str, shell=True)
97+
7598
def main():
7699
DBG = "";
77100
use_py = False
@@ -90,10 +113,15 @@ def main():
90113
use_py = True
91114

92115
print(DBG, use_py)
116+
if(not use_py):
117+
compile_cpp(DBG)
118+
exc_str = './A.exe'
119+
else:
120+
exc_str = "python A.py"
93121

94-
test_python()
122+
do_test(exc_str)
95123

96124

97125
if __name__ == '__main__':
98-
# main()
99-
do_test()
126+
main()
127+
# do_test()

0 commit comments

Comments
 (0)