import glob import os import subprocess SPIM_COPYRIGHT_HEADER = '''SPIM Version 7.3. of August 28, 2006 Copyright 1990-2004 by James R. Larus (larus@cs.wisc.edu). All Rights Reserved. See the file README for a full copyright notice. Loaded: /usr/lib/spim/exceptions.s ''' def bold(s): return "\033[1;37m%s\033[0m" % s OK = "\033[1;32mOK\033[0m" NOTOK = "\033[1;31m!!\033[0m" def list_tests(): tests = [] for path in ("lexing", "parsing", "typing", "assembly"): tests += glob.glob("tests/%s/*.ml" % path) return tests def run_test(test_file): f = open(test_file) lines = f.readlines() f.close() expected_return_code = int(lines[1][13]) if expected_return_code != 0: expected_error = "" i = 3 while not lines[i].startswith("*)"): expected_error += lines[i] i += 1 base_command = "./petit-caml %s" % test_file test_output = False test_assembly = False if "lexing" in test_file or "parsing" in test_file: command = base_command + " -parse-only" elif "typing" in test_file: test_output = True command = base_command + " -type-only -print-decl-types" else: test_assembly = True command = base_command process = subprocess.Popen(command, stderr = subprocess.PIPE, stdout = subprocess.PIPE, shell = True) return_code = process.wait() if return_code != 0: error = process.stderr.read() status = OK if return_code == expected_return_code else NOTOK if return_code != 0: if status == OK: if expected_error == error: status = OK else: status = "%s\n%s\n%s\n%s" % (NOTOK, error.strip(), bold("Expected :"), expected_error.strip()) else: status = "%s\n%s" % (NOTOK, error.strip()) if return_code == 0 and (test_output or test_assembly): i = 3 expected_output = "" while not lines[i].startswith("*)"): expected_output += lines[i] i += 1 if status == OK and return_code == 0 and test_output: output = process.stdout.read() if output != expected_output: status = "%s\n%s\n%s\n%s" % (NOTOK, output.strip(), bold("Expected :"), expected_output.strip()) if status == OK and return_code == 0 and test_assembly: process = subprocess.Popen("spim %s" % test_file.replace(".ml", ".s"), stderr = subprocess.PIPE, stdout = subprocess.PIPE, shell = True) return_code = process.wait() if return_code != 0: error = process.stderr.read() status = "%s\n%s" % (NOTOK, error.strip()) output = process.stdout.read() output = output.replace(SPIM_COPYRIGHT_HEADER, "") if output != expected_output: status = "%s\n%s\n%s\n%s" % (NOTOK, output.strip(), bold("Expected :"), expected_output.strip()) print "%s : %s" % (bold(test_file), status) def run_tests(): tests = list_tests() tests.sort() map(run_test, tests) if __name__ == "__main__": if not os.path.isfile("./petit-caml"): print NOTOK, \ bold("Petit-Caml unavailable - please build it first"), \ NOTOK raise SystemExit run_tests()