#!/usr/local/bin/python import os, sys, re, shutil, cmp, time sys.path.append('/home/fac/gacs/lib/python') from Util import * # ------------------ utility subroutines --------------------- def usage(progname): usage_str = "Usage: " + progname + """ Tests the student programs. Argument 0 generally attempts to compile them. Other arguments will start various tests. These tests explain themselves and also report the result. """ die(usage_str) def diagn(message): "Just to distinguish the diagnoses from the program output." sys.stderr.write("---- " + message + "\n") def compile_prog(executable, std_or_win, compile_or_link, obj_list = []): """Awkward but similar to the makefile macros.""" CCC= os.path.join(os.sep, "usr", "local", "bin", "g++") COMPILE_cc= " ".join([CCC, "-c"]) LINK_cc= CCC CFLAGS= " ".join(["-ansi", "-Wall", "-Wno-return-type", "-g"]) CCFLAGS= CFLAGS LDLIBS="" LDLIBS_WIN= " ".join(["-lm", "-L/usr/openwin/lib", "-lX11"]) INCLUDES= "-I/cs/course/cs113/current/horstmann/cccfiles" if "std" == std_or_win: libs = LDLIBS elif "win" == std_or_win: libs = LDLIBS_WIN else: die(std_or_win + "is not a legal compilation type") if "compile" == compile_or_link: compile_list = [COMPILE_cc, CCFLAGS, INCLUDES, "-o", executable + ".o", executable + ".cpp"] errors_file = executable + ".compile-errors" elif ("link" == compile_or_link): if (obj_list != []): link_input = " ".join(obj_list) else: link_input = executable + ".cpp" compile_list = [LINK_cc, CCFLAGS, INCLUDES, "-o", executable, link_input, libs] errors_file = executable + ".link-errors" compile_line = " ".join(compile_list) + " 2>" + errors_file if os.path.exists(errors_file): os.remove(errors_file) diagn("Compiling " + executable + " (" + compile_or_link + ") ... ") if 0 == os.system(compile_line): diagn("Passed.") if (0 == file_size(errors_file)): os.remove(errors_file) else: diagn("There are warnings in file " + errors_file + ".") else: diagn("Failed.") diagn("There are error messages in file " + errors_file + ".") # ---------------------- special subroutines ------------------ def compile_progs(): compile_prog("bag6_test_lib", "std", "compile") compile_prog("test_from_file", "std", "link", ["bag6_test_lib.o", "test_from_file.cpp"]) def test_with_special(prog, input): if 0 != os.system(prog + " " + input): warn("Could not run " + prog + " " + input) return time.sleep(0.5) # Or, the file might not be changed yet (primitive). my_report = "report_"+input; diagn("Comparing test_report with "+my_report) if cmp.cmp("test_report", my_report): diagn("Passed.") else: diagn("Failed.") # ----------------------------- Main -------------------------- if 1 == len(sys.argv): usage(sys.argv[0]) test = sys.argv[1] diagn("The testprogram's own messages always start with ----.") if "0" == test: compile_progs() sys.exit(0) if "1" == test: test_with_special("test_from_file", "actions_vanilla") sys.exit(0) if "2" == test: test_with_special("test_from_file", "actions_remove") sys.exit(0) if "3" == test: test_with_special("test_from_file", "actions_all") sys.exit(0)