21
|
1 #! /usr/bin/env python
|
16
|
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv>
|
|
3
|
21
|
4 from __future__ import division, with_statement
|
|
5
|
|
6 try:
|
|
7 from compat import *
|
|
8 import config, testcases
|
|
9 except ImportError:
|
|
10 import __main__
|
|
11 __main__.import_error(sys.exc_info()[1])
|
|
12 else:
|
22
|
13 from __main__ import clock, options
|
21
|
14
|
22
|
15 import os, re, sys
|
21
|
16
|
16
|
17 try:
|
21
|
18 import signal
|
|
19 except ImportError:
|
|
20 signalnames = ()
|
|
21 else:
|
|
22 # Construct a cache of all signal names available on the current
|
|
23 # platform. Prefer names from the UNIX standards over other versions.
|
|
24 unixnames = frozenset(('HUP', 'INT', 'QUIT', 'ILL', 'ABRT', 'FPE', 'KILL', 'SEGV', 'PIPE', 'ALRM', 'TERM', 'USR1', 'USR2', 'CHLD', 'CONT', 'STOP', 'TSTP', 'TTIN', 'TTOU', 'BUS', 'POLL', 'PROF', 'SYS', 'TRAP', 'URG', 'VTALRM', 'XCPU', 'XFSZ'))
|
|
25 signalnames = {}
|
|
26 for name in dir(signal):
|
|
27 if re.match('SIG[A-Z]+$', name):
|
|
28 value = signal.__dict__[name]
|
22
|
29 if isinstance(value, int) and (value not in signalnames or name[3:] in unixnames):
|
21
|
30 signalnames[value] = name
|
|
31 del unixnames
|
16
|
32
|
21
|
33 __all__ = 'Problem',
|
|
34
|
|
35 # This should no more be needed; pass all work on to the TestCase inheritance tree
|
16
|
36 # LIBRARY and STDIO refer to interactive aka reactive problems
|
21
|
37 #BATCH, OUTONLY, LIBRARY, STDIO, BESTOUT = xrange(5)
|
|
38
|
|
39 class Cache(object):
|
|
40 def __init__(self, mydict):
|
|
41 self.__dict__ = mydict
|
16
|
42
|
|
43 class Problem(object):
|
|
44 __slots__ = 'name', 'config', 'cache', 'testcases'
|
|
45
|
|
46 def __init__(prob, name):
|
|
47 if not isinstance(name, basestring):
|
|
48 # This shouldn't happen, of course
|
21
|
49 raise TypeError('Problem() argument 1 must be string, not ' + type(name).__name__)
|
16
|
50 prob.name = name
|
21
|
51 prob.config = config.load_problem(name)
|
|
52 if not getattr(prob.config, 'kind', None): prob.config.kind = 'batch'
|
|
53 prob.cache = Cache({'padoutput': 0, 'usegroups': False})
|
|
54 prob.testcases = testcases.load_problem(prob)
|
|
55
|
|
56 # TODO
|
|
57 def build(prob):
|
|
58 raise NotImplementedError
|
16
|
59
|
|
60 def test(prob):
|
22
|
61 try:
|
|
62 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0
|
|
63 for case in prob.testcases:
|
|
64 ntotal += 1
|
|
65 max += case.points
|
|
66 if case.points: nvalued += 1
|
|
67 granted = 0
|
|
68 id = str(case.id)
|
|
69 if case.isdummy:
|
|
70 id = 'sample ' + id
|
|
71 say('%*s: ' % (prob.cache.padoutput, id), end='')
|
|
72 sys.stdout.flush()
|
|
73 try:
|
|
74 granted = case(lambda: (say('%7.3f%s s, ' % (case.time_stopped - case.time_started, case.time_limit_string), end=''), sys.stdout.flush()))
|
|
75 except testcases.CanceledByUser:
|
|
76 verdict = 'canceled by the user'
|
|
77 except testcases.TimeLimitExceeded:
|
|
78 verdict = 'time limit exceeded'
|
|
79 except testcases.WrongAnswer:
|
|
80 e = sys.exc_info()[1]
|
|
81 if e.comment:
|
|
82 verdict = 'wrong answer (%s)' % e.comment
|
|
83 else:
|
|
84 verdict = 'wrong answer'
|
|
85 except testcases.NonZeroExitCode:
|
|
86 e = sys.exc_info()[1]
|
|
87 if e.exitcode < 0:
|
|
88 if sys.platform == 'win32':
|
|
89 verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000)
|
|
90 elif -e.exitcode in signalnames:
|
|
91 verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode])
|
|
92 else:
|
|
93 verdict = 'terminated by signal %d' % -e.exitcode
|
21
|
94 else:
|
22
|
95 verdict = 'non-zero return code %d' % e.exitcode
|
|
96 except testcases.CannotStartTestee:
|
|
97 e = sys.exc_info()[1]
|
|
98 if e.upstream.strerror:
|
|
99 verdict = 'cannot launch the program to test (%s)' % e.upstream.strerror.lower()
|
|
100 else:
|
|
101 verdict = 'cannot launch the program to test'
|
|
102 except testcases.CannotStartValidator:
|
|
103 e = sys.exc_info()[1]
|
|
104 if e.upstream.strerror:
|
|
105 verdict = 'cannot launch the validator (%s)' % e.upstream.strerror.lower()
|
|
106 else:
|
|
107 verdict = 'cannot launch the validator'
|
|
108 except testcases.CannotReadOutputFile:
|
|
109 e = sys.exc_info()[1]
|
|
110 if e.upstream.strerror:
|
|
111 verdict = 'cannot read the output file (%s)' % e.upstream.strerror.lower()
|
|
112 else:
|
|
113 verdict = 'cannot read the output file'
|
|
114 except testcases.CannotReadInputFile:
|
|
115 e = sys.exc_info()[1]
|
|
116 if e.upstream.strerror:
|
|
117 verdict = 'cannot read the input file (%s)' % e.upstream.strerror.lower()
|
|
118 else:
|
|
119 verdict = 'cannot read the input file'
|
|
120 except testcases.CannotReadAnswerFile:
|
|
121 e = sys.exc_info()[1]
|
|
122 if e.upstream.strerror:
|
|
123 verdict = 'cannot read the reference output file (%s)' % e.upstream.strerror.lower()
|
|
124 else:
|
|
125 verdict = 'cannot read the reference output file'
|
|
126 except testcases.TestCaseNotPassed:
|
|
127 e = sys.exc_info()[1]
|
|
128 verdict = 'unspecified reason [this may be a bug in test.py] (%s)' % e
|
|
129 #except Exception:
|
|
130 # e = sys.exc_info()[1]
|
|
131 # verdict = 'unknown error [this may be a bug in test.py] (%s)' % e
|
21
|
132 else:
|
22
|
133 if hasattr(granted, '__iter__'):
|
|
134 granted, comment = granted
|
|
135 if comment:
|
|
136 comment = ' (%s)' % comment
|
|
137 else:
|
|
138 comment = ''
|
|
139 if granted == case.points:
|
|
140 ncorrect += 1
|
|
141 if granted: ncorrectvalued += 1
|
|
142 verdict = 'OK' + comment
|
|
143 elif not granted:
|
|
144 verdict = 'wrong answer' + comment
|
|
145 else:
|
|
146 verdict = 'partly correct' + comment
|
|
147 say('%g/%g, %s' % (granted, case.points, verdict))
|
|
148 real += granted
|
|
149 weighted = real * prob.config.taskweight / max if max else 0
|
|
150 if nvalued != ntotal:
|
|
151 say('Problem total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, ncorrectvalued, nvalued, real, max, weighted, prob.config.taskweight))
|
21
|
152 else:
|
22
|
153 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight))
|
|
154 return weighted, prob.config.taskweight
|
|
155 finally:
|
|
156 if options.erase and (not prob.config.stdio or case.validator):
|
|
157 for var in 'in', 'out':
|
|
158 name = getattr(prob.config, var + 'name')
|
|
159 if name:
|
|
160 try:
|
|
161 os.remove(name)
|
|
162 except Exception:
|
|
163 pass
|
|
164 if case.validator and not callable(case.validator):
|
|
165 if prob.config.ansname:
|
|
166 try:
|
|
167 os.remove(prob.config.ansname)
|
|
168 except Exception:
|
|
169 pass |