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