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_tmp_line(tmp_file):
57 if os.path.exists(tmp_file):
58 f = open(tmp_file, "r")
59 v = f.readline().rstrip("\n")
66 def collect_verdict(e, verdict_file):
67 v = collect_tmp_line(verdict_file)
68 if len(v) >= 4 and v[0].isalnum and v[1].isalnum and v[2] == ":" and v[3] == " ":
69 e.test_stat["status"] = v[0:2]
70 e.log.verbose("Judge's status: %s\n" % v[0:2])
74 e.test_stat["message"] = v
75 e.log.verbose("Judge's verdict: %s\n" % v)
77 def collect_points(e):
78 p = collect_tmp_line(os.path.join(e["TDIR"], e["TESTCASE_PTS"]))
80 e.log.verbose("Judge has supplied points: %s\n" % p)
81 e.test_stat["points"] = p
86 inn = e["TESTCASE_IN"]
87 out = e["TESTCASE_OUT"]
90 if os.path.exists(os.path.join(pdir, inn)):
91 moe.util.link_or_copy(os.path.join(pdir, inn), os.path.join(tdir, inn))
92 if os.path.exists(os.path.join(pdir, out)):
93 moe.util.link_or_copy(os.path.join(pdir, out), os.path.join(tdir, ok))
96 cmd = e["OUTPUT_CHECK"]
99 verdict_file = tmpname(e)
100 cmd = "exec 2>%s ; %s" % (verdict_file,cmd)
102 e.log.progress("<check> ")
103 e.log.verbose("Checking output: %s\n" % cmd)
106 collect_verdict(e, verdict_file)
110 if os.WEXITSTATUS(rc) == 0:
112 elif os.WEXITSTATUS(rc) == 1:
113 raise moe.TestError("Wrong answer", "WA")
114 raise moe.MoeError("Judge failure")
117 if e.test_stat["points"] is None:
118 e.test_stat["points"] = e["POINTS_PER_TEST"]
121 cmd = e["OUTPUT_FILTER"]
125 os.rename(os.path.join(e["TDIR"], e["TESTCASE_OUT"]), os.path.join(e["TDIR"], e["TESTCASE_RAW"]))
126 e.log.progress("<filter> ")
127 e.log.verbose("Filtering output: %s\n" % cmd)
130 if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) == 0:
131 if not os.path.exists(os.path.join(e["TDIR"], e["TESTCASE_OUT"])):
132 raise moe.MoeError("Filter has generated no output")
134 raise moe.MoeError("Filter failure")
137 cmd = e["SYNTAX_CHECK"]
140 verdict_file = tmpname(e)
141 cmd = "exec 2>%s ; %s" % (verdict_file,cmd)
143 e.log.progress("<syntax> ")
144 e.log.verbose("Checking syntax: %s\n" % cmd)
147 collect_verdict(e, verdict_file)
150 if os.WEXITSTATUS(rc) == 0:
152 elif os.WEXITSTATUS(rc) == 1:
153 raise moe.TestError("Wrong syntax", "SY")
154 raise moe.MoeError("Syntax checker failure")
156 def run_test(e, test):
157 configure_test(e, test)
159 ## FIXME: interactive tasks
160 e.test_pipe.configure(e["TESTCASE_HOOKS"])
161 if e.log.verbosity >= 2:
162 e.test_pipe.dump(e.log.log_file, prefix="\t")
165 def wrap_run_test(e, test):
168 except moe.MoeError, err:
169 raise moe.TestError(err, "XX")
171 def conclude_test(e):
173 if not stat["points"]:
176 if e.log.verbosity > 1:
177 e.log.verbose("Final status:\n")
178 stat.write_nested(e.log.log_file, 1)
181 msg = "%s: %s" % (stat["status"], e.test_stat["message"])
184 msg += " (%s points" % stat["points"]
186 msg += ", %s s" % stat["time"]
188 msg += ", %d MB" % ((int(stat["mem"]) + 524288) // 1048576)
194 e.test_pipe.insert(0, "setup", setup)
195 e.test_pipe.insert(400, "filter", filter)
196 e.test_pipe.insert(500, "syntax", syntax)
197 e.test_pipe.insert(600, "judge", judge)
198 e.test_pipe.insert(700, "points", points)
200 for test in e["TESTS"].split():
201 e.log.progress("Test %s: " % test)
206 wrap_run_test(e, test)
207 except moe.TestError, err:
208 if not e.test_stat["status"]:
209 e.test_stat["status"] = err.stat_code
210 if not e.test_stat["message"]:
211 e.test_stat["message"] = err.message
217 moe.log.default = old_log