Mercurial > ~astiob > upreckon > hgweb
annotate testcases.py @ 56:693a938bdeee
Accurate run-time reporting on POSIX
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Tue, 21 Dec 2010 02:24:18 +0200 |
| parents | c726235e49ee |
| children | 855bdfeb32a6 |
| rev | line source |
|---|---|
| 21 | 1 #! /usr/bin/env python |
| 16 | 2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> |
| 3 | |
| 43 | 4 # TODO: copy the ansfile if not options.erase even if no validator is used |
| 5 | |
| 21 | 6 from __future__ import division, with_statement |
| 7 | |
| 8 try: | |
| 9 from compat import * | |
| 10 import files, problem, config | |
| 11 except ImportError: | |
| 12 import __main__ | |
| 13 __main__.import_error(sys.exc_info()[1]) | |
| 14 else: | |
| 15 from __main__ import clock, options | |
| 16 | |
| 17 import glob, re, sys, tempfile, time | |
| 18 from subprocess import Popen, PIPE, STDOUT | |
| 19 | |
| 20 import os | |
| 21 devnull = open(os.path.devnull, 'w+') | |
| 22 | |
| 23 try: | |
| 24 from signal import SIGTERM, SIGKILL | |
| 25 except ImportError: | |
| 26 SIGTERM = 15 | |
| 27 SIGKILL = 9 | |
| 28 | |
| 16 | 29 try: |
| 21 | 30 from _subprocess import TerminateProcess |
| 31 except ImportError: | |
| 32 # CPython 2.5 does define _subprocess.TerminateProcess even though it is | |
| 33 # not used in the subprocess module, but maybe something else does not | |
| 34 try: | |
| 35 import ctypes | |
| 36 TerminateProcess = ctypes.windll.kernel32.TerminateProcess | |
| 37 except (ImportError, AttributeError): | |
| 38 TerminateProcess = None | |
| 39 | |
| 22 | 40 |
| 41 # Do the hacky-wacky dark magic needed to catch presses of the Escape button. | |
| 42 # If only Python supported forcible termination of threads... | |
| 43 if not sys.stdin.isatty(): | |
| 44 canceled = init_canceled = lambda: False | |
| 45 pause = None | |
| 46 else: | |
| 47 try: | |
| 48 # Windows has select() too, but it is not the select() we want | |
| 49 import msvcrt | |
| 50 except ImportError: | |
| 51 try: | |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
52 from select import select |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
53 import termios, tty, atexit |
| 22 | 54 except ImportError: |
| 55 # It cannot be helped! | |
| 56 # Silently disable support for killing the program being tested | |
| 57 canceled = init_canceled = lambda: False | |
| 58 pause = None | |
| 59 else: | |
| 60 def cleanup(old=termios.tcgetattr(sys.stdin.fileno())): | |
| 61 termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, old) | |
| 62 atexit.register(cleanup) | |
| 63 del cleanup | |
| 64 tty.setcbreak(sys.stdin.fileno()) | |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
65 def canceled(select=select, stdin=sys.stdin, read=sys.stdin.read): |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
66 while select((stdin,), (), (), 0)[0]: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
67 if read(1) == '\33': |
| 22 | 68 return True |
| 69 return False | |
| 70 def init_canceled(): | |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
71 while select((sys.stdin,), (), (), 0)[0]: |
| 22 | 72 sys.stdin.read(1) |
| 73 def pause(): | |
| 74 sys.stdin.read(1) | |
| 75 else: | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
76 def canceled(kbhit=msvcrt.kbhit, getch=msvcrt.getch): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
77 while kbhit(): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
78 c = getch() |
| 22 | 79 if c == '\33': |
| 80 return True | |
| 81 elif c == '\0': | |
| 82 # Let's hope no-one is fiddling with this | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
83 getch() |
| 22 | 84 return False |
| 85 def init_canceled(): | |
| 86 while msvcrt.kbhit(): | |
| 87 msvcrt.getch() | |
| 88 def pause(): | |
| 89 msvcrt.getch() | |
| 90 | |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
91 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
92 from signal import SIGCHLD, signal, SIG_DFL |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
93 from select import select, error as select_error |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
94 from errno import EINTR |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
95 except ImportError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
96 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
97 import ctypes |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
98 WaitForMultipleObjects = ctypes.windll.kernel32.WaitForMultipleObjects |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
99 except (ImportError, AttributeError): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
100 pass |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
101 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
102 # TODO: implement Win32 call() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
103 pass |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
104 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
105 # Make SIGCHLD interrupt sleep() and select() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
106 def bury_child(signum, frame): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
107 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
108 bury_child.case.time_stopped = clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
109 except Exception: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
110 pass |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
111 signal(SIGCHLD, bury_child) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
112 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
113 # If you want this to work, don't set any stdio argument to PIPE |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
114 def call_real(*args, **kwargs): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
115 bury_child.case = case = kwargs.pop('case') |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
116 # FIXME: kill the process no matter what |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
117 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
118 case.process = Popen(*args, **kwargs) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
119 except OSError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
120 raise CannotStartTestee(sys.exc_info()[1]) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
121 case.time_started = clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
122 if pause is None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
123 if case.maxtime: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
124 time.sleep(case.maxtime) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
125 if case.process.poll() is None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
126 raise TimeLimitExceeded |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
127 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
128 case.process.wait() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
129 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
130 if not case.maxtime: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
131 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
132 while case.process.poll() is None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
133 if select((sys.stdin,), (), ())[0]: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
134 if sys.stdin.read(1) == '\33': |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
135 raise CanceledByUser |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
136 except select_error: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
137 if sys.exc_info()[1].args[0] != EINTR: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
138 raise |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
139 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
140 time_end = clock() + case.maxtime |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
141 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
142 while case.process.poll() is None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
143 remaining = time_end - clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
144 if remaining > 0: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
145 if select((sys.stdin,), (), (), remaining)[0]: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
146 if sys.stdin.read(1) == '\33': |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
147 raise CanceledByUser |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
148 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
149 raise TimeLimitExceeded |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
150 except select_error: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
151 if sys.exc_info()[1].args[0] != EINTR: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
152 raise |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
153 del bury_child.case |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
154 def call(*args, **kwargs): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
155 if 'preexec_fn' in kwargs: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
156 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
157 return call_real(*args, **kwargs) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
158 except MemoryError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
159 # If there is not enough memory for the forked test.py, |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
160 # opt for silent dropping of the limit |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
161 # TODO: show a warning somewhere |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
162 del kwargs['preexec_fn'] |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
163 return call_real(*args, **kwargs) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
164 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
165 return call_real(*args, **kwargs) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
166 |
| 22 | 167 |
| 21 | 168 __all__ = ('TestCase', 'load_problem', 'TestCaseNotPassed', |
| 22 | 169 'TimeLimitExceeded', 'CanceledByUser', 'WrongAnswer', |
| 170 'NonZeroExitCode', 'CannotStartTestee', | |
| 171 'CannotStartValidator', 'CannotReadOutputFile', | |
| 172 'CannotReadInputFile', 'CannotReadAnswerFile') | |
| 21 | 173 |
| 174 | |
| 175 | |
| 176 # Exceptions | |
| 177 | |
| 178 class TestCaseNotPassed(Exception): __slots__ = () | |
| 179 class TimeLimitExceeded(TestCaseNotPassed): __slots__ = () | |
| 22 | 180 class CanceledByUser(TestCaseNotPassed): __slots__ = () |
| 21 | 181 |
| 182 class WrongAnswer(TestCaseNotPassed): | |
| 183 __slots__ = 'comment' | |
| 184 def __init__(self, comment=''): | |
| 185 self.comment = comment | |
| 186 | |
| 187 class NonZeroExitCode(TestCaseNotPassed): | |
| 188 __slots__ = 'exitcode' | |
| 189 def __init__(self, exitcode): | |
| 190 self.exitcode = exitcode | |
| 191 | |
| 192 class ExceptionWrapper(TestCaseNotPassed): | |
| 193 __slots__ = 'upstream' | |
| 194 def __init__(self, upstream): | |
| 195 self.upstream = upstream | |
| 196 | |
| 197 class CannotStartTestee(ExceptionWrapper): __slots__ = () | |
| 198 class CannotStartValidator(ExceptionWrapper): __slots__ = () | |
| 199 class CannotReadOutputFile(ExceptionWrapper): __slots__ = () | |
| 200 class CannotReadInputFile(ExceptionWrapper): __slots__ = () | |
| 201 class CannotReadAnswerFile(ExceptionWrapper): __slots__ = () | |
| 202 | |
| 203 | |
| 204 | |
| 22 | 205 # Helper context managers |
| 206 | |
| 207 class CopyDeleting(object): | |
| 208 __slots__ = 'case', 'file', 'name' | |
| 209 | |
| 210 def __init__(self, case, file, name): | |
| 211 self.case = case | |
| 212 self.file = file | |
| 213 self.name = name | |
| 214 | |
| 215 def __enter__(self): | |
| 216 if self.name: | |
| 217 try: | |
| 218 self.file.copy(self.name) | |
| 219 except: | |
| 220 try: | |
| 221 self.__exit__(None, None, None) | |
| 222 except: | |
| 223 pass | |
| 224 raise | |
| 225 | |
| 226 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 227 if self.name: | |
| 228 self.case.files_to_delete.append(self.name) | |
| 229 | |
| 230 | |
| 231 class Copying(object): | |
| 232 __slots__ = 'file', 'name' | |
| 233 | |
| 234 def __init__(self, file, name): | |
| 235 self.file = file | |
| 236 self.name = name | |
| 237 | |
| 238 def __enter__(self): | |
| 239 if self.name: | |
| 240 self.file.copy(self.name) | |
| 241 | |
| 242 def __exit__(self, exc_type, exc_val, exc_tb): | |
| 243 pass | |
| 244 | |
| 245 | |
| 246 | |
| 21 | 247 # Test case types |
| 16 | 248 |
| 249 class TestCase(object): | |
| 21 | 250 __slots__ = ('problem', 'id', 'isdummy', 'infile', 'outfile', 'points', |
| 251 'process', 'time_started', 'time_stopped', 'time_limit_string', | |
| 22 | 252 'realinname', 'realoutname', 'maxtime', 'maxmemory', |
| 253 'has_called_back', 'files_to_delete') | |
| 21 | 254 |
| 255 if ABCMeta: | |
| 256 __metaclass__ = ABCMeta | |
| 16 | 257 |
| 21 | 258 def __init__(case, prob, id, isdummy, points): |
| 16 | 259 case.problem = prob |
| 21 | 260 case.id = id |
| 261 case.isdummy = isdummy | |
| 262 case.points = points | |
| 263 case.maxtime = case.problem.config.maxtime | |
| 264 case.maxmemory = case.problem.config.maxmemory | |
| 265 if case.maxtime: | |
| 266 case.time_limit_string = '/%.3f' % case.maxtime | |
| 267 else: | |
| 268 case.time_limit_string = '' | |
| 269 if not isdummy: | |
| 270 case.realinname = case.problem.config.testcaseinname | |
| 271 case.realoutname = case.problem.config.testcaseoutname | |
| 272 else: | |
| 273 case.realinname = case.problem.config.dummyinname | |
| 274 case.realoutname = case.problem.config.dummyoutname | |
| 275 | |
| 276 @abstractmethod | |
| 277 def test(case): raise NotImplementedError | |
| 16 | 278 |
| 22 | 279 def __call__(case, callback): |
| 280 case.has_called_back = False | |
| 281 case.files_to_delete = [] | |
| 21 | 282 try: |
| 22 | 283 return case.test(callback) |
| 21 | 284 finally: |
| 22 | 285 now = clock() |
| 286 if not getattr(case, 'time_started', None): | |
| 287 case.time_started = case.time_stopped = now | |
| 288 elif not getattr(case, 'time_stopped', None): | |
| 289 case.time_stopped = now | |
| 290 if not case.has_called_back: | |
| 291 callback() | |
| 21 | 292 case.cleanup() |
| 293 | |
| 294 def cleanup(case): | |
| 295 #if getattr(case, 'infile', None): | |
| 296 # case.infile.close() | |
| 297 #if getattr(case, 'outfile', None): | |
| 298 # case.outfile.close() | |
| 299 if getattr(case, 'process', None): | |
| 300 # Try killing after three unsuccessful TERM attempts in a row | |
| 301 # (except on Windows, where TERMing is killing) | |
| 302 for i in range(3): | |
| 303 try: | |
| 304 try: | |
| 305 case.process.terminate() | |
| 306 except AttributeError: | |
| 307 # Python 2.5 | |
| 308 if TerminateProcess and hasattr(proc, '_handle'): | |
| 309 # Windows API | |
| 310 TerminateProcess(proc._handle, 1) | |
| 311 else: | |
| 312 # POSIX | |
| 313 os.kill(proc.pid, SIGTERM) | |
| 314 except Exception: | |
| 315 time.sleep(0) | |
| 316 case.process.poll() | |
| 317 else: | |
| 22 | 318 case.process.wait() |
| 21 | 319 break |
| 320 else: | |
| 321 # If killing the process is unsuccessful three times in a row, | |
| 322 # just silently stop trying | |
| 323 for i in range(3): | |
| 324 try: | |
| 325 try: | |
| 326 case.process.kill() | |
| 327 except AttributeError: | |
| 328 # Python 2.5 | |
| 329 if TerminateProcess and hasattr(proc, '_handle'): | |
| 330 # Windows API | |
| 331 TerminateProcess(proc._handle, 1) | |
| 332 else: | |
| 333 # POSIX | |
| 334 os.kill(proc.pid, SIGKILL) | |
| 335 except Exception: | |
| 336 time.sleep(0) | |
| 337 case.process.poll() | |
| 338 else: | |
| 22 | 339 case.process.wait() |
| 21 | 340 break |
| 22 | 341 if case.files_to_delete: |
| 342 for name in case.files_to_delete: | |
| 343 try: | |
| 344 os.remove(name) | |
| 345 except Exception: | |
| 346 # It can't be helped | |
| 347 pass | |
| 21 | 348 |
| 349 def open_infile(case): | |
| 350 try: | |
| 351 case.infile = files.File('/'.join((case.problem.name, case.realinname.replace('$', case.id)))) | |
| 352 except IOError: | |
| 353 e = sys.exc_info()[1] | |
| 354 raise CannotReadInputFile(e) | |
| 355 | |
| 356 def open_outfile(case): | |
| 357 try: | |
| 358 case.outfile = files.File('/'.join((case.problem.name, case.realoutname.replace('$', case.id)))) | |
| 359 except IOError: | |
| 360 e = sys.exc_info()[1] | |
| 361 raise CannotReadAnswerFile(e) | |
| 362 | |
| 16 | 363 |
| 21 | 364 class ValidatedTestCase(TestCase): |
| 365 __slots__ = 'validator' | |
| 366 | |
| 367 def __init__(case, *args): | |
| 368 TestCase.__init__(case, *args) | |
| 369 if not case.problem.config.tester: | |
| 370 case.validator = None | |
| 371 else: | |
| 372 case.validator = case.problem.config.tester | |
| 373 | |
| 374 def validate(case, output): | |
| 375 if not case.validator: | |
| 376 # Compare the output with the reference output | |
| 377 case.open_outfile() | |
| 378 with case.outfile.open() as refoutput: | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
379 for line, refline in zip_longest(output, refoutput): |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
380 if refline is not None and not isinstance(refline, basestring): |
| 21 | 381 line = bytes(line, sys.getdefaultencoding()) |
| 382 if line != refline: | |
| 22 | 383 raise WrongAnswer |
|
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
384 return 1 |
| 21 | 385 elif callable(case.validator): |
| 386 return case.validator(output) | |
| 387 else: | |
| 388 # Call the validator program | |
| 389 output.close() | |
| 23 | 390 if case.problem.config.ansname: |
| 391 case.open_outfile() | |
| 392 case.outfile.copy(case.problem.config.ansname) | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
393 try: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
394 case.process = Popen(case.validator, stdin=devnull, stdout=PIPE, stderr=STDOUT, universal_newlines=True, bufsize=-1) |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
395 except OSError: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
396 raise CannotStartValidator(sys.exc_info()[1]) |
| 21 | 397 comment = case.process.communicate()[0].strip() |
| 26 | 398 match = re.match(r'(?i)(ok|(?:correct|wrong)(?:(?:\s|_)*answer)?)(?:$|\s+|[.,!:]+\s*)', comment) |
| 21 | 399 if match: |
| 400 comment = comment[match.end():] | |
| 401 if not case.problem.config.maxexitcode: | |
| 402 if case.process.returncode: | |
| 403 raise WrongAnswer(comment) | |
| 404 else: | |
|
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
405 return 1, comment |
| 21 | 406 else: |
|
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
407 return case.process.returncode / case.problem.config.maxexitcode, comment |
| 21 | 408 |
| 409 | |
| 410 class BatchTestCase(ValidatedTestCase): | |
| 411 __slots__ = () | |
| 412 | |
| 22 | 413 def test(case, callback): |
| 414 init_canceled() | |
| 21 | 415 if sys.platform == 'win32' or not case.maxmemory: |
| 416 preexec_fn = None | |
| 417 else: | |
| 418 def preexec_fn(): | |
| 419 try: | |
| 420 import resource | |
| 421 maxmemory = int(case.maxmemory * 1048576) | |
| 422 resource.setrlimit(resource.RLIMIT_AS, (maxmemory, maxmemory)) | |
| 423 # I would also set a CPU time limit but I do not want the time | |
| 424 # that passes between the calls to fork and exec to be counted in | |
| 425 except MemoryError: | |
| 426 # We do not have enough memory for ourselves; | |
| 427 # let the parent know about this | |
| 428 raise | |
| 429 except Exception: | |
| 430 # Well, at least we tried | |
| 431 pass | |
| 432 case.open_infile() | |
| 433 case.time_started = None | |
| 434 if case.problem.config.stdio: | |
| 54 | 435 if options.erase and not case.validator or not case.problem.config.inname: |
| 22 | 436 # TODO: re-use the same file name if possible |
| 21 | 437 # FIXME: 2.5 lacks the delete parameter |
| 438 with tempfile.NamedTemporaryFile(delete=False) as f: | |
| 22 | 439 inputdatafname = f.name |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
440 contextmgr = CopyDeleting(case, case.infile, inputdatafname) |
| 21 | 441 else: |
| 442 inputdatafname = case.problem.config.inname | |
|
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
443 contextmgr = Copying(case.infile, inputdatafname) |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
444 with contextmgr: |
|
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
445 # FIXME: this U doesn't do anything good for the child process, does it? |
| 22 | 446 with open(inputdatafname, 'rU') as infile: |
| 447 with tempfile.TemporaryFile('w+') if options.erase and not case.validator else open(case.problem.config.outname, 'w+') as outfile: | |
| 21 | 448 try: |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
449 call(case.problem.config.path, case=case, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1, preexec_fn=preexec_fn) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
450 except NameError: |
| 22 | 451 try: |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
452 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
453 case.process = Popen(case.problem.config.path, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1, preexec_fn=preexec_fn) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
454 except MemoryError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
455 # If there is not enough memory for the forked test.py, |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
456 # opt for silent dropping of the limit |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
457 # TODO: show a warning somewhere |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
458 case.process = Popen(case.problem.config.path, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
459 except OSError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
460 raise CannotStartTestee(sys.exc_info()[1]) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
461 case.time_started = clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
462 time_next_check = case.time_started + .15 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
463 if not case.maxtime: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
464 while True: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
465 exitcode, now = case.process.poll(), clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
466 if exitcode is not None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
467 case.time_stopped = now |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
468 break |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
469 # For some reason (probably Microsoft's fault), |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
470 # msvcrt.kbhit() is slow as hell |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
471 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
472 if now >= time_next_check: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
473 if canceled(): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
474 raise CanceledByUser |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
475 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
476 time_next_check = now + .15 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
477 time.sleep(.001) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
478 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
479 time_end = case.time_started + case.maxtime |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
480 while True: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
481 exitcode, now = case.process.poll(), clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
482 if exitcode is not None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
483 case.time_stopped = now |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
484 break |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
485 elif now >= time_end: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
486 raise TimeLimitExceeded |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
487 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
488 if now >= time_next_check: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
489 if canceled(): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
490 raise CanceledByUser |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
491 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
492 time_next_check = now + .15 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
493 time.sleep(.001) |
| 22 | 494 if config.globalconf.force_zero_exitcode and case.process.returncode: |
| 495 raise NonZeroExitCode(case.process.returncode) | |
| 496 callback() | |
| 497 case.has_called_back = True | |
| 498 outfile.seek(0) | |
| 499 return case.validate(outfile) | |
| 21 | 500 else: |
| 22 | 501 case.infile.copy(case.problem.config.inname) |
| 21 | 502 try: |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
503 call(case.problem.config.path, case=case, stdin=devnull, stdout=devnull, stderr=STDOUT, preexec_fn=preexec_fn) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
504 except NameError: |
| 21 | 505 try: |
|
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
506 try: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
507 case.process = Popen(case.problem.config.path, stdin=devnull, stdout=devnull, stderr=STDOUT, preexec_fn=preexec_fn) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
508 except MemoryError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
509 # If there is not enough memory for the forked test.py, |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
510 # opt for silent dropping of the limit |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
511 # TODO: show a warning somewhere |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
512 case.process = Popen(case.problem.config.path, stdin=devnull, stdout=devnull, stderr=STDOUT) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
513 except OSError: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
514 raise CannotStartTestee(sys.exc_info()[1]) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
515 case.time_started = clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
516 time_next_check = case.time_started + .15 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
517 if not case.maxtime: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
518 while True: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
519 exitcode, now = case.process.poll(), clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
520 if exitcode is not None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
521 case.time_stopped = now |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
522 break |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
523 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
524 if now >= time_next_check: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
525 if canceled(): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
526 raise CanceledByUser |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
527 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
528 time_next_check = now + .15 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
529 time.sleep(.001) |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
530 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
531 time_end = case.time_started + case.maxtime |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
532 while True: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
533 exitcode, now = case.process.poll(), clock() |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
534 if exitcode is not None: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
535 case.time_stopped = now |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
536 break |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
537 elif now >= time_end: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
538 raise TimeLimitExceeded |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
539 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
540 if now >= time_next_check: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
541 if canceled(): |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
542 raise CanceledByUser |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
543 else: |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
544 time_next_check = now + .15 |
|
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
545 time.sleep(.001) |
| 21 | 546 if config.globalconf.force_zero_exitcode and case.process.returncode: |
| 547 raise NonZeroExitCode(case.process.returncode) | |
| 22 | 548 callback() |
| 549 case.has_called_back = True | |
| 21 | 550 with open(case.problem.config.outname, 'rU') as output: |
| 551 return case.validate(output) | |
| 552 | |
| 553 | |
| 554 # This is the only test case type not executing any programs to be tested | |
| 555 class OutputOnlyTestCase(ValidatedTestCase): | |
| 556 __slots__ = () | |
| 557 def cleanup(case): pass | |
| 558 | |
| 559 class BestOutputTestCase(ValidatedTestCase): | |
| 560 __slots__ = () | |
| 561 | |
| 562 # This is the only test case type executing two programs simultaneously | |
| 563 class ReactiveTestCase(TestCase): | |
| 564 __slots__ = () | |
| 565 # The basic idea is to launch the program to be tested and the grader | |
| 566 # and to pipe their standard I/O from and to each other, | |
| 567 # and then to capture the grader's exit code and use it | |
| 26 | 568 # like the exit code of an output validator is used. |
| 21 | 569 |
| 570 | |
| 571 def load_problem(prob, _types={'batch' : BatchTestCase, | |
| 572 'outonly' : OutputOnlyTestCase, | |
| 573 'bestout' : BestOutputTestCase, | |
| 574 'reactive': ReactiveTestCase}): | |
|
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
575 # We will need to iterate over these configuration variables twice |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
576 try: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
577 len(prob.config.dummies) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
578 except Exception: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
579 prob.config.dummies = tuple(prob.config.dummies) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
580 try: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
581 len(prob.config.tests) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
582 except Exception: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
583 prob.config.tests = tuple(prob.config.tests) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
584 |
| 23 | 585 if options.legacy: |
| 586 prob.config.usegroups = False | |
| 587 prob.config.tests = list(prob.config.tests) | |
| 588 for i, name in enumerate(prob.config.tests): | |
|
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
589 # Same here; we'll need to iterate over them twice |
| 23 | 590 try: |
|
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
591 l = len(name) |
| 23 | 592 except Exception: |
| 593 try: | |
|
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
594 name = tuple(name) |
| 23 | 595 except TypeError: |
|
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
596 name = (name,) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
597 l = len(name) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
598 if len(name) > 1: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
599 prob.config.usegroups = True |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
600 break |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
601 elif not len(name): |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
602 prob.config.tests[i] = (name,) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
603 |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
604 # First get prob.cache.padoutput right, |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
605 # then yield the actual test cases |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
606 for i in prob.config.dummies: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
607 s = 'sample ' + str(i).zfill(prob.config.paddummies) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
608 prob.cache.padoutput = max(prob.cache.padoutput, len(s)) |
| 16 | 609 if prob.config.usegroups: |
|
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
610 for group in prob.config.tests: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
611 for i in group: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
612 s = str(i).zfill(prob.config.padtests) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
613 prob.cache.padoutput = max(prob.cache.padoutput, len(s)) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
614 for i in prob.config.dummies: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
615 s = str(i).zfill(prob.config.paddummies) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
616 yield _types[prob.config.kind](prob, s, True, 0) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
617 for group in prob.config.tests: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
618 yield problem.TestGroup() |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
619 for i in group: |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
620 s = str(i).zfill(prob.config.padtests) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
621 yield _types[prob.config.kind](prob, s, False, prob.config.pointmap.get(i, prob.config.pointmap.get(None, prob.config.maxexitcode if prob.config.maxexitcode else 1))) |
|
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
622 yield problem.test_context_end |
| 16 | 623 else: |
| 624 for i in prob.config.tests: | |
| 21 | 625 s = str(i).zfill(prob.config.padtests) |
| 626 prob.cache.padoutput = max(prob.cache.padoutput, len(s)) | |
| 627 for i in prob.config.dummies: | |
| 628 s = str(i).zfill(prob.config.paddummies) | |
| 629 yield _types[prob.config.kind](prob, s, True, 0) | |
| 630 for i in prob.config.tests: | |
| 631 s = str(i).zfill(prob.config.padtests) | |
| 632 yield _types[prob.config.kind](prob, s, False, prob.config.pointmap.get(i, prob.config.pointmap.get(None, prob.config.maxexitcode if prob.config.maxexitcode else 1))) |
