11 def configure_test(e, test):
12 e.cfgs = moe.config.MoeConfigStack(e.cfgs)
13 e.test_builtins = moe.config.MoeConfig(type="test-builtins")
14 e.test_builtins.set("TEST", test)
15 e.cfgs.push(e.test_builtins)
17 test_cf = os.path.join(e["PDIR"], test + ".config")
18 if os.path.exists(test_cf):
19 cfg = moe.config.MoeConfig(name=test_cf, type="test")
22 e.cfgs.apply_overrides("TEST_" + test + "_")
25 e.cfgs.apply_overrides("EXT_" + ext + "_")
27 log = moe.log.MoeLog()
28 log.verbosity = e.log.verbosity
29 log.open(os.path.join(e["TDIR"], test + ".log"))
30 log.say("Test case %s\n\n" % test)
34 e.log_config(2, "for the test")
36 e.test_stat = moe.status.MoeStatus()
37 e.test_stat["id"] = test
38 e.stat.get_list("tests").append(e.test_stat)
40 def collect_status(e):
41 sf = os.path.join(e["TDIR"], e["TESTCASE_STATUS"])
42 if os.path.exists(sf):
43 e.log.verbose("Reading status from %s\n" % sf)
44 stat = moe.status.MoeStatus()
46 if e.log.verbosity > 1:
47 stat.write_nested(e.log.log_file, 1)
49 e.test_stat[k] = stat[k]
52 e.log.verbose("No status file present\n")
55 return os.path.join(e["TDIR"], e["TEST"] + ".tmp")
57 def collect_tmp_line(tmp_file):
58 if os.path.exists(tmp_file):
59 f = open(tmp_file, "r")
60 v = f.readline().rstrip("\n")
67 def collect_verdict(e, verdict_file):
68 v = collect_tmp_line(verdict_file)
69 if len(v) >= 4 and v[0].isalnum and v[1].isalnum and v[2] == ":" and v[3] == " ":
70 e.test_stat["status"] = v[0:2]
71 e.log.verbose("Judge's status: %s\n" % v[0:2])
75 e.test_stat["message"] = v
76 e.log.verbose("Judge's verdict: %s\n" % v)
78 def collect_points(e):
79 p = collect_tmp_line(os.path.join(e["TDIR"], e["TESTCASE_PTS"]))
81 e.log.verbose("Judge has supplied points: %s\n" % p)
82 e.test_stat["points"] = p
87 inn = e["TESTCASE_IN"]
88 out = e["TESTCASE_OUT"]
91 if os.path.exists(os.path.join(pdir, inn)):
92 moe.util.link_or_copy(os.path.join(pdir, inn), os.path.join(tdir, inn))
93 if os.path.exists(os.path.join(pdir, out)):
94 moe.util.link_or_copy(os.path.join(pdir, out), os.path.join(tdir, ok))
97 cmd = e["OUTPUT_CHECK"]
100 verdict_file = tmpname(e)
101 cmd = "exec 2>%s ; %s" % (verdict_file,cmd)
103 e.log.progress("<check> ")
104 e.log.verbose("Checking output: %s\n" % cmd)
107 collect_verdict(e, verdict_file)
111 if os.WEXITSTATUS(rc) == 0:
113 elif os.WEXITSTATUS(rc) == 1:
114 raise moe.TestError("Wrong answer", "WA")
115 raise moe.MoeError("Judge failure")
118 if e.test_stat["points"] is None:
119 e.test_stat["points"] = e["POINTS_PER_TEST"]
122 cmd = e["OUTPUT_FILTER"]
126 os.rename(os.path.join(e["TDIR"], e["TESTCASE_OUT"]), os.path.join(e["TDIR"], e["TESTCASE_RAW"]))
127 e.log.progress("<filter> ")
128 e.log.verbose("Filtering output: %s\n" % cmd)
131 if os.WIFEXITED(rc) and os.WEXITSTATUS(rc) == 0:
132 if not os.path.exists(os.path.join(e["TDIR"], e["TESTCASE_OUT"])):
133 raise moe.MoeError("Filter has generated no output")
135 raise moe.MoeError("Filter failure")
138 cmd = e["SYNTAX_CHECK"]
141 verdict_file = tmpname(e)
142 cmd = "exec 2>%s ; %s" % (verdict_file,cmd)
144 e.log.progress("<syntax> ")
145 e.log.verbose("Checking syntax: %s\n" % cmd)
148 collect_verdict(e, verdict_file)
151 if os.WEXITSTATUS(rc) == 0:
153 elif os.WEXITSTATUS(rc) == 1:
154 raise moe.TestError("Wrong syntax", "SY")
155 raise moe.MoeError("Syntax checker failure")
157 def run_test(e, test):
158 configure_test(e, test)
160 ## FIXME: interactive tasks
161 e.test_pipe.configure(e["TESTCASE_HOOKS"])
162 if e.log.verbosity >= 2:
163 e.test_pipe.dump(e.log.log_file, prefix="\t")
166 def wrap_run_test(e, test):
169 except moe.MoeError, err:
170 raise moe.TestError(err, "XX")
173 moe.log.fatal_exception()
174 traceback.print_exc(file = e.log.log_file)
175 raise moe.TestError("Internal exception", "XX")
177 def conclude_test(e):
179 if not stat["points"]:
182 if e.log.verbosity > 1:
183 e.log.verbose("Final status:\n")
184 stat.write_nested(e.log.log_file, 1)
187 msg = "%s: %s" % (stat["status"], e.test_stat["message"])
190 msg += " (%s points" % stat["points"]
192 msg += ", %s s" % stat["time"]
194 msg += ", %d MB" % ((int(stat["mem"]) + 524288) // 1048576)
200 e.test_pipe.insert(0, "setup", setup)
201 e.test_pipe.insert(400, "filter", filter)
202 e.test_pipe.insert(500, "syntax", syntax)
203 e.test_pipe.insert(600, "judge", judge)
204 e.test_pipe.insert(700, "points", points)
206 for test in e["TESTS"].split():
207 e.log.progress("Test %s: " % test)
212 wrap_run_test(e, test)
213 except moe.TestError, err:
214 if not e.test_stat["status"]:
215 e.test_stat["status"] = err.stat_code
216 if not e.test_stat["message"]:
217 e.test_stat["message"] = err.message
223 moe.log.default = old_log