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