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