|
| 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 += "-p : test python" |
| 11 | + print(s) |
| 12 | + |
| 13 | +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] |
| 15 | + |
| 16 | +def colorTestResult(r): |
| 17 | + q = {'+': 1, '?':0 } |
| 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 | + |
| 56 | +def do_test(): |
| 57 | + in_files = glob.glob("input*") |
| 58 | + out_files = glob.glob("output*") |
| 59 | + print(in_files, out_files) |
| 60 | + |
| 61 | + for inf, outf in zip(in_files, out_files): |
| 62 | + print(inf, outf) |
| 63 | + with open(inf) as f: |
| 64 | + p = subprocess.Popen("python myprog.py", stdout=subprocess.PIPE, stdin=f) |
| 65 | + recv = p.stdout.read() |
| 66 | + |
| 67 | + 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 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + DBG = ""; |
| 77 | + use_py = False |
| 78 | + try: |
| 79 | + opts, args = getopt.getopt(sys.argv[1:], "dp" ) |
| 80 | + except getopt.GetoptError as err: |
| 81 | + # print help information and exit: |
| 82 | + print str(err) # will print something like "option -a not recognized" |
| 83 | + usage() |
| 84 | + sys.exit(2) |
| 85 | + |
| 86 | + for o, a in opts: |
| 87 | + if o == '-d': |
| 88 | + DBG = "-DDEBUG" |
| 89 | + if o == '-p': |
| 90 | + use_py = True |
| 91 | + |
| 92 | + print(DBG, use_py) |
| 93 | + |
| 94 | + test_python() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + # main() |
| 99 | + do_test() |
0 commit comments