10 def configure_test(e, test):
11 e.cfgs = moe.config.MoeConfigStack(e.cfgs)
12 e.test_builtins = moe.config.MoeConfig(type="test-builtins")
13 e.test_builtins.set("TEST", test)
14 e.cfgs.push(e.test_builtins)
16 test_cf = os.path.join(e["PDIR"], test + ".config")
17 if os.path.exists(test_cf):
18 cfg = moe.config.MoeConfig(name=test_cf, type="test")
21 e.cfgs.apply_overrides("TEST_" + test + "_")
24 e.cfgs.apply_overrides("EXT_" + ext + "_")
26 log = moe.log.MoeLog()
27 log.verbosity = e.log.verbosity
28 log.open(os.path.join(e["TDIR"], test + ".log"))
29 log.say("Test case %s\n\n" % test)
33 e.log_config(2, "for the test")
35 e.test_stat = moe.status.MoeStatus()
36 e.test_stat["id"] = test
37 e.stat.get_list("tests").append(e.test_stat)
39 def collect_status(e):
40 sf = os.path.join(e["TDIR"], e["TESTCASE_STATUS"])
41 if os.path.exists(sf):
42 e.log.verbose("Reading status from %s\n" % sf)
43 stat = moe.status.MoeStatus()
45 if e.log.verbosity > 1:
46 stat.write_nested(e.log.log_file, 1)
48 e.test_stat[k] = stat[k]
51 e.log.verbose("No status file present\n")
54 return os.path.join(e["TDIR"], e["TEST"] + ".tmp")
56 def collect_verdict(e, verdict_file):
58 f = open(verdict_file, "r")
59 v = f.readline().rstrip("\n")
61 except IOError, OSError:
64 if len(v) >= 4 and v[0].isalnum and v[1].isalnum and v[2] == ":" and v[3] == " ":
65 e.test_stat["status"] = v[0:2]
66 e.log.verbose("Judge's status: %s\n" % v[0:2])
70 e.test_stat["message"] = v
71 e.log.verbose("Judge's verdict: %s\n" % v)
74 os.unlink(verdict_file)
75 except IOError, OSError:
81 inn = e["TESTCASE_IN"]
82 out = e["TESTCASE_OUT"]
85 if os.path.exists(os.path.join(pdir, inn)):
86 moe.util.link_or_copy(os.path.join(pdir, inn), os.path.join(tdir, inn))
87 if os.path.exists(os.path.join(pdir, out)):
88 moe.util.link_or_copy(os.path.join(pdir, out), os.path.join(tdir, ok))
91 cmd = e["OUTPUT_CHECK"]
94 verdict_file = tmpname(e)
95 cmd = "exec 2>%s ; %s" % (verdict_file,cmd)
97 e.log.progress("<check> ")
98 e.log.verbose("Checking output: %s\n" % cmd)
101 collect_verdict(e, verdict_file)
104 if os.WEXITSTATUS(rc) == 0:
106 elif os.WEXITSTATUS(rc) == 1:
107 raise moe.TestError("Wrong answer", "WA")
108 raise moe.MoeError("Judge failure")
111 ## FIXME: check $TEST.pts
112 if e.test_stat["points"] is None:
113 e.test_stat["points"] = e["POINTS_PER_TEST"]
115 def run_test(e, test):
116 configure_test(e, test)
118 ## FIXME: interactive tasks
119 e.test_pipe.configure(e["TESTCASE_HOOKS"])
120 if e.log.verbosity >= 2:
121 e.test_pipe.dump(e.log.log_file, prefix="\t")
124 def wrap_run_test(e, test):
127 except moe.MoeError, err:
128 raise moe.TestError(err, "XX")
130 def conclude_test(e):
132 if not stat["points"]:
135 if e.log.verbosity > 1:
136 e.log.verbose("Final status:\n")
137 stat.write_nested(e.log.log_file, 1)
140 msg = "%s: %s" % (stat["status"], e.test_stat["message"])
143 msg += " (%s points" % stat["points"]
145 msg += ", %s s" % stat["time"]
147 msg += ", %d MB" % ((int(stat["mem"]) + 524288) // 1048576)
153 ## FIXME: output filter
154 ## FIXME: syntax checks
155 e.test_pipe.insert(0, "setup", setup)
156 e.test_pipe.insert(400, "judge", judge)
157 e.test_pipe.insert(500, "points", points)
159 for test in e["TESTS"].split():
160 e.log.progress("Test %s: " % test)
165 wrap_run_test(e, test)
166 except moe.TestError, err:
167 if not e.test_stat["status"]:
168 e.test_stat["status"] = err.stat_code
169 if not e.test_stat["message"]:
170 e.test_stat["message"] = err.message
176 moe.log.default = old_log