|
1 |
| -import getopt, sys |
2 |
| -import glob |
3 |
| -import time |
4 |
| -import string |
5 |
| -import subprocess |
6 |
| - |
7 |
| -def usage(): |
8 |
| - s = "Options: \n" |
9 |
| - s += "-d : compile in Debug mode" |
10 |
| - s += "\n-p : test python" |
11 |
| - print(s) |
12 |
| - |
13 |
| -def colorCode(q): |
14 |
| - return [ "\033[41m%s\033[0m", "\033[31m%s\033[0m", "%s", "\033[32m%s\033[0m" ][q+2] |
15 |
| - |
16 |
| -def colorTestResult(r): |
17 |
| - q = {'+': 1, '?':0, False: -1, True: 1} |
18 |
| - return colorCode( q[r] if r in q else -1 ) % r |
19 |
| - |
20 |
| -def prettyStr(x): |
21 |
| - if type(x) == str: |
22 |
| - return '"%s"' % x |
23 |
| - elif type(x) == tuple: |
24 |
| - return '(%s)' % string.join( (prettyStr(y) for y in x), ',' ) |
25 |
| - else: |
26 |
| - return str(x) |
27 |
| - |
28 |
| -def tc_equal(expected, received): |
29 |
| - try: |
30 |
| - _t = type(expected) |
31 |
| - # print(_t, type(received)) |
32 |
| - received = _t(received) |
33 |
| - # print(_t) |
34 |
| - if _t == list or _t == tuple: |
35 |
| - print("checking list or tuple") |
36 |
| - if len(expected) != len(received): return False |
37 |
| - return all(tc_equal(e, r) for (e, r) in zip(expected, received)) |
38 |
| - elif _t == float: |
39 |
| - eps = 1e-9 |
40 |
| - if abs(expected - received) < eps: return True |
41 |
| - if abs(received) > abs(expected) * (1.0 - eps) and abs(received) < abs(expected) * (1.0 + eps): return True |
42 |
| - elif isinstance(expected, basestring): |
43 |
| - exp_lines = expected.splitlines() |
44 |
| - rec_lines = received.splitlines() |
45 |
| - if len(exp_lines) == 1: |
46 |
| - return expected == received |
47 |
| - else: |
48 |
| - return tc_equal(exp_lines, rec_lines) |
49 |
| - else: |
50 |
| - print("check equality") |
51 |
| - return expected == received |
52 |
| - except: |
53 |
| - return False |
54 |
| - |
55 |
| -def do_test(exc_str): |
56 |
| - in_files = sorted(glob.glob("input*")) |
57 |
| - out_files = sorted(glob.glob("output*")) |
58 |
| - print(in_files, out_files) |
59 |
| - |
60 |
| - retlist = [] |
61 |
| - for inf, outf in zip(in_files, out_files): |
62 |
| - print("") |
63 |
| - print("="*10) |
64 |
| - print("="*10) |
65 |
| - print(inf, outf) |
66 |
| - with open(inf) as f: |
67 |
| - input_content = f.read() |
68 |
| - f.seek(0) |
69 |
| - p = subprocess.Popen(exc_str, stdout=subprocess.PIPE, stdin=f, shell=True) |
70 |
| - recv = p.stdout.read() |
71 |
| - |
72 |
| - with open(outf) as of: |
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 |
| - |
89 |
| - |
90 |
| - |
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 |
| - |
98 |
| -def main(): |
99 |
| - DBG = ""; |
100 |
| - use_py = False |
101 |
| - try: |
102 |
| - opts, args = getopt.getopt(sys.argv[1:], "dp" ) |
103 |
| - except getopt.GetoptError as err: |
104 |
| - # print help information and exit: |
105 |
| - print str(err) # will print something like "option -a not recognized" |
106 |
| - usage() |
107 |
| - sys.exit(2) |
108 |
| - |
109 |
| - for o, a in opts: |
110 |
| - if o == '-d': |
111 |
| - DBG = "-DDEBUG" |
112 |
| - if o == '-p': |
113 |
| - use_py = True |
114 |
| - |
115 |
| - 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" |
121 |
| - |
122 |
| - do_test(exc_str) |
123 |
| - |
124 |
| - |
125 |
| -if __name__ == '__main__': |
126 |
| - main() |
127 |
| - # do_test() |
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import getopt, sys |
| 4 | +import glob |
| 5 | +import os |
| 6 | +import time |
| 7 | +import string |
| 8 | +import subprocess |
| 9 | + |
| 10 | +def usage(): |
| 11 | + s = "Options: \n" |
| 12 | + s += "-d : compile in Debug mode" |
| 13 | + s += "\n-p : test python" |
| 14 | + print(s) |
| 15 | + |
| 16 | +def colorCode(q): |
| 17 | + return [ "\033[41m%s\033[0m", "\033[31m%s\033[0m", "%s", "\033[32m%s\033[0m" ][q+2] |
| 18 | + |
| 19 | +def colorTestResult(r): |
| 20 | + q = {'+': 1, '?':0, False: -1, True: 1} |
| 21 | + return colorCode( q[r] if r in q else -1 ) % r |
| 22 | + |
| 23 | +def prettyStr(x): |
| 24 | + if type(x) == str: |
| 25 | + return '"%s"' % x |
| 26 | + elif type(x) == tuple: |
| 27 | + return '(%s)' % string.join( (prettyStr(y) for y in x), ',' ) |
| 28 | + else: |
| 29 | + return str(x) |
| 30 | + |
| 31 | +def tc_equal(expected, received): |
| 32 | + try: |
| 33 | + _t = type(expected) |
| 34 | + # print(_t, type(received)) |
| 35 | + received = _t(received) |
| 36 | + # print(_t) |
| 37 | + if _t == list or _t == tuple: |
| 38 | + print("checking list or tuple") |
| 39 | + if len(expected) != len(received): return False |
| 40 | + return all(tc_equal(e, r) for (e, r) in zip(expected, received)) |
| 41 | + elif _t == float: |
| 42 | + eps = 1e-9 |
| 43 | + if abs(expected - received) < eps: return True |
| 44 | + if abs(received) > abs(expected) * (1.0 - eps) and abs(received) < abs(expected) * (1.0 + eps): return True |
| 45 | + elif isinstance(expected, basestring): |
| 46 | + exp_lines = expected.splitlines() |
| 47 | + rec_lines = received.splitlines() |
| 48 | + if len(exp_lines) == 1: |
| 49 | + return expected == received |
| 50 | + else: |
| 51 | + return tc_equal(exp_lines, rec_lines) |
| 52 | + else: |
| 53 | + print("check equality") |
| 54 | + return expected == received |
| 55 | + except: |
| 56 | + return False |
| 57 | + |
| 58 | +def do_test(exc_str): |
| 59 | + in_files = sorted(glob.glob("input*")) |
| 60 | + out_files = sorted(glob.glob("output*")) |
| 61 | + print(in_files, out_files) |
| 62 | + |
| 63 | + retlist = [] |
| 64 | + for inf, outf in zip(in_files, out_files): |
| 65 | + print("") |
| 66 | + print("="*10) |
| 67 | + print("="*10) |
| 68 | + print(inf, outf) |
| 69 | + with open(inf) as f: |
| 70 | + input_content = f.read() |
| 71 | + f.seek(0) |
| 72 | + p = subprocess.Popen(exc_str, stdout=subprocess.PIPE, stdin=f, shell=True) |
| 73 | + recv = p.stdout.read() |
| 74 | + |
| 75 | + with open(outf) as of: |
| 76 | + correct_answer = of.read() |
| 77 | + print("Input") |
| 78 | + print(prettyStr(input_content.rstrip())) |
| 79 | + print("Expected:") |
| 80 | + print(prettyStr(correct_answer.rstrip())) |
| 81 | + print("-"*10) |
| 82 | + print("Received:") |
| 83 | + print(prettyStr(recv.rstrip())) |
| 84 | + ret_comp = tc_equal(correct_answer, recv) |
| 85 | + |
| 86 | + print("Success: " + colorTestResult(ret_comp)) |
| 87 | + retlist.append(colorTestResult( "+" if ret_comp else "-")) |
| 88 | + |
| 89 | + print("\n\n" + "="*20 + "\nSummary: ") |
| 90 | + print(" ".join(retlist)) |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + # print("Successful? " + colorTestResult(tc_equal(correct_answer, recv))) |
| 95 | + |
| 96 | +def compile_cpp(DBG): |
| 97 | + exc_str = 'g++ -std=c++11 ' + DBG + ' -Wall A.cc -o A.exe' |
| 98 | + print(exc_str) |
| 99 | + subprocess.call(exc_str, shell=True) |
| 100 | + |
| 101 | +def main(): |
| 102 | + DBG = ""; |
| 103 | + use_py = False |
| 104 | + try: |
| 105 | + opts, args = getopt.getopt(sys.argv[1:], "dp" ) |
| 106 | + except getopt.GetoptError as err: |
| 107 | + # print help information and exit: |
| 108 | + print str(err) # will print something like "option -a not recognized" |
| 109 | + usage() |
| 110 | + sys.exit(2) |
| 111 | + |
| 112 | + for o, a in opts: |
| 113 | + if o == '-d': |
| 114 | + DBG = "-DDEBUG" |
| 115 | + if o == '-p': |
| 116 | + use_py = True |
| 117 | + |
| 118 | + print(DBG, use_py) |
| 119 | + if(not use_py): |
| 120 | + compile_cpp(DBG) |
| 121 | + exc_str = './A.exe' |
| 122 | + else: |
| 123 | + exc_str = "python A.py" |
| 124 | + |
| 125 | + do_test(exc_str) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + print(os.getcwd()) |
| 130 | + main() |
| 131 | + # do_test() |
0 commit comments