Mercurial > ~astiob > upreckon > hgweb
comparison problem.py @ 50:4ea7133ac25c
Converted 2.00 into the default branch
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Sun, 19 Dec 2010 23:25:13 +0200 |
parents | 2.00/problem.py@2b459f9743b4 |
children | aea4fc87698a |
comparison
equal
deleted
inserted
replaced
47:06f1683c8db9 | 50:4ea7133ac25c |
---|---|
1 #! /usr/bin/env python | |
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> | |
3 | |
4 from __future__ import division, with_statement | |
5 | |
6 try: | |
7 from compat import * | |
8 import config, testcases | |
9 except ImportError: | |
10 import __main__ | |
11 __main__.import_error(sys.exc_info()[1]) | |
12 else: | |
13 from __main__ import clock, options | |
14 | |
15 import os, re, sys | |
16 | |
17 try: | |
18 import signal | |
19 except ImportError: | |
20 signalnames = () | |
21 else: | |
22 # Construct a cache of all signal names available on the current | |
23 # platform. Prefer names from the UNIX standards over other versions. | |
24 unixnames = frozenset(('HUP', 'INT', 'QUIT', 'ILL', 'ABRT', 'FPE', 'KILL', 'SEGV', 'PIPE', 'ALRM', 'TERM', 'USR1', 'USR2', 'CHLD', 'CONT', 'STOP', 'TSTP', 'TTIN', 'TTOU', 'BUS', 'POLL', 'PROF', 'SYS', 'TRAP', 'URG', 'VTALRM', 'XCPU', 'XFSZ')) | |
25 signalnames = {} | |
26 for name in dir(signal): | |
27 if re.match('SIG[A-Z]+$', name): | |
28 value = signal.__dict__[name] | |
29 if isinstance(value, int) and (value not in signalnames or name[3:] in unixnames): | |
30 signalnames[value] = name | |
31 del unixnames | |
32 | |
33 __all__ = 'Problem', 'TestContext', 'test_context_end', 'TestGroup' | |
34 | |
35 def strerror(e): | |
36 s = getattr(e, 'strerror') | |
37 if not s: s = str(e) | |
38 return ' (%s%s)' % (s[0].lower(), s[1:]) if s else '' | |
39 | |
40 class Cache(object): | |
41 def __init__(self, mydict): | |
42 self.__dict__ = mydict | |
43 | |
44 class TestContext(object): | |
45 pass | |
46 | |
47 test_context_end = object() | |
48 | |
49 class TestGroup(TestContext): | |
50 __slots__ = 'case', 'log', 'correct', 'allcorrect', 'real', 'max', 'ntotal', 'nvalued', 'ncorrect', 'ncorrectvalued' | |
51 | |
52 def __init__(self): | |
53 self.real = self.max = self.ntotal = self.nvalued = self.ncorrect = self.ncorrectvalued = 0 | |
54 self.allcorrect = True | |
55 self.log = [] | |
56 | |
57 def case_start(self, case): | |
58 self.case = case | |
59 self.correct = False | |
60 self.ntotal += 1 | |
61 self.max += case.points | |
62 if case.points: | |
63 self.nvalued += 1 | |
64 | |
65 def case_correct(self): | |
66 self.correct = True | |
67 self.ncorrect += 1 | |
68 if self.case.points: | |
69 self.ncorrectvalued += 1 | |
70 | |
71 def case_end(self, granted): | |
72 self.log.append((self.case, self.correct, granted)) | |
73 self.real += granted | |
74 del self.case | |
75 if not self.correct: | |
76 self.allcorrect = False | |
77 | |
78 def end(self): | |
79 say('Group total: %d/%d tests; %d/%d points' % (self.ncorrect, self.ntotal, self.real if self.allcorrect else 0, self.max)) | |
80 # No real need to flush stdout, as it will anyway be flushed in a moment, | |
81 # when either the problem total or the next test case's ID is printed | |
82 if self.allcorrect: | |
83 return self.log | |
84 else: | |
85 return ((case, correct, 0) for case, correct, granted in self.log) | |
86 | |
87 class Problem(object): | |
88 __slots__ = 'name', 'config', 'cache', 'testcases' | |
89 | |
90 def __init__(prob, name): | |
91 if not isinstance(name, basestring): | |
92 # This shouldn't happen, of course | |
93 raise TypeError('Problem() argument 1 must be string, not ' + type(name).__name__) | |
94 prob.name = name | |
95 prob.config = config.load_problem(name) | |
96 if not getattr(prob.config, 'kind', None): prob.config.kind = 'batch' | |
97 prob.cache = Cache({'padoutput': 0}) | |
98 prob.testcases = testcases.load_problem(prob) | |
99 | |
100 # TODO | |
101 def build(prob): | |
102 raise NotImplementedError | |
103 | |
104 def test(prob): | |
105 case = None | |
106 try: | |
107 contexts = [TestGroup()] | |
108 for case in prob.testcases: | |
109 if case is test_context_end: | |
110 for case, correct, granted in contexts.pop().end(): | |
111 contexts[-1].case_start(case) | |
112 if correct: | |
113 contexts[-1].case_correct() | |
114 contexts[-1].case_end(granted) | |
115 continue | |
116 elif isinstance(case, TestContext): | |
117 contexts.append(case) | |
118 continue | |
119 contexts[-1].case_start(case) | |
120 granted = 0 | |
121 id = str(case.id) | |
122 if case.isdummy: | |
123 id = 'sample ' + id | |
124 say('%*s: ' % (prob.cache.padoutput, id), end='') | |
125 sys.stdout.flush() | |
126 try: | |
127 granted = case(lambda: (say('%7.3f%s s, ' % (case.time_stopped - case.time_started, case.time_limit_string), end=''), sys.stdout.flush())) | |
128 except testcases.CanceledByUser: | |
129 verdict = 'canceled by the user' | |
130 except testcases.TimeLimitExceeded: | |
131 verdict = 'time limit exceeded' | |
132 except testcases.WrongAnswer: | |
133 e = sys.exc_info()[1] | |
134 if e.comment: | |
135 verdict = 'wrong answer (%s)' % e.comment | |
136 else: | |
137 verdict = 'wrong answer' | |
138 except testcases.NonZeroExitCode: | |
139 e = sys.exc_info()[1] | |
140 if e.exitcode < 0: | |
141 if sys.platform == 'win32': | |
142 verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000) | |
143 elif -e.exitcode in signalnames: | |
144 verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode]) | |
145 else: | |
146 verdict = 'terminated by signal %d' % -e.exitcode | |
147 else: | |
148 verdict = 'non-zero return code %d' % e.exitcode | |
149 except testcases.CannotStartTestee: | |
150 verdict = 'cannot launch the program to test%s' % strerror(sys.exc_info()[1].upstream) | |
151 except testcases.CannotStartValidator: | |
152 verdict = 'cannot launch the validator%s' % strerror(sys.exc_info()[1].upstream) | |
153 except testcases.CannotReadOutputFile: | |
154 verdict = 'cannot read the output file%s' % strerror(sys.exc_info()[1].upstream) | |
155 except testcases.CannotReadInputFile: | |
156 verdict = 'cannot read the input file%s' % strerror(sys.exc_info()[1].upstream) | |
157 except testcases.CannotReadAnswerFile: | |
158 verdict = 'cannot read the reference output file%s' % strerror(sys.exc_info()[1].upstream) | |
159 except testcases.TestCaseNotPassed: | |
160 verdict = 'unspecified reason [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) | |
161 #except Exception: | |
162 # verdict = 'unknown error [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) | |
163 else: | |
164 try: | |
165 granted, comment = granted | |
166 except TypeError: | |
167 comment = '' | |
168 else: | |
169 if comment: | |
170 comment = ' (%s)' % comment | |
171 if granted >= 1: | |
172 contexts[-1].case_correct() | |
173 verdict = 'OK' + comment | |
174 elif not granted: | |
175 verdict = 'wrong answer' + comment | |
176 else: | |
177 verdict = 'partly correct' + comment | |
178 granted *= case.points | |
179 say('%g/%g, %s' % (granted, case.points, verdict)) | |
180 contexts[-1].case_end(granted) | |
181 weighted = contexts[0].real * prob.config.taskweight / contexts[0].max if contexts[0].max else 0 | |
182 if contexts[0].nvalued != contexts[0].ntotal: | |
183 say('Problem total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (contexts[0].ncorrect, contexts[0].ntotal, contexts[0].ncorrectvalued, contexts[0].nvalued, contexts[0].real, contexts[0].max, weighted, prob.config.taskweight)) | |
184 else: | |
185 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (contexts[0].ncorrect, contexts[0].ntotal, contexts[0].real, contexts[0].max, weighted, prob.config.taskweight)) | |
186 sys.stdout.flush() | |
187 return weighted, prob.config.taskweight | |
188 finally: | |
189 if options.erase and (not prob.config.stdio or case and case.validator): | |
190 for var in 'in', 'out': | |
191 name = getattr(prob.config, var + 'name') | |
192 if name: | |
193 try: | |
194 os.remove(name) | |
195 except Exception: | |
196 pass | |
197 if case.validator and not callable(case.validator): | |
198 if prob.config.ansname: | |
199 try: | |
200 os.remove(prob.config.ansname) | |
201 except Exception: | |
202 pass |