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):
|
23
|
61 case = None
|
22
|
62 try:
|
|
63 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0
|
|
64 for case in prob.testcases:
|
|
65 ntotal += 1
|
|
66 max += case.points
|
|
67 if case.points: nvalued += 1
|
|
68 granted = 0
|
|
69 id = str(case.id)
|
|
70 if case.isdummy:
|
|
71 id = 'sample ' + id
|
|
72 say('%*s: ' % (prob.cache.padoutput, id), end='')
|
|
73 sys.stdout.flush()
|
|
74 try:
|
|
75 granted = case(lambda: (say('%7.3f%s s, ' % (case.time_stopped - case.time_started, case.time_limit_string), end=''), sys.stdout.flush()))
|
|
76 except testcases.CanceledByUser:
|
|
77 verdict = 'canceled by the user'
|
|
78 except testcases.TimeLimitExceeded:
|
|
79 verdict = 'time limit exceeded'
|
|
80 except testcases.WrongAnswer:
|
|
81 e = sys.exc_info()[1]
|
|
82 if e.comment:
|
|
83 verdict = 'wrong answer (%s)' % e.comment
|
|
84 else:
|
|
85 verdict = 'wrong answer'
|
|
86 except testcases.NonZeroExitCode:
|
|
87 e = sys.exc_info()[1]
|
|
88 if e.exitcode < 0:
|
|
89 if sys.platform == 'win32':
|
|
90 verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000)
|
|
91 elif -e.exitcode in signalnames:
|
|
92 verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode])
|
|
93 else:
|
|
94 verdict = 'terminated by signal %d' % -e.exitcode
|
21
|
95 else:
|
22
|
96 verdict = 'non-zero return code %d' % e.exitcode
|
|
97 except testcases.CannotStartTestee:
|
|
98 e = sys.exc_info()[1]
|
|
99 if e.upstream.strerror:
|
|
100 verdict = 'cannot launch the program to test (%s)' % e.upstream.strerror.lower()
|
|
101 else:
|
|
102 verdict = 'cannot launch the program to test'
|
|
103 except testcases.CannotStartValidator:
|
|
104 e = sys.exc_info()[1]
|
|
105 if e.upstream.strerror:
|
|
106 verdict = 'cannot launch the validator (%s)' % e.upstream.strerror.lower()
|
|
107 else:
|
|
108 verdict = 'cannot launch the validator'
|
|
109 except testcases.CannotReadOutputFile:
|
|
110 e = sys.exc_info()[1]
|
|
111 if e.upstream.strerror:
|
|
112 verdict = 'cannot read the output file (%s)' % e.upstream.strerror.lower()
|
|
113 else:
|
|
114 verdict = 'cannot read the output file'
|
|
115 except testcases.CannotReadInputFile:
|
|
116 e = sys.exc_info()[1]
|
|
117 if e.upstream.strerror:
|
|
118 verdict = 'cannot read the input file (%s)' % e.upstream.strerror.lower()
|
|
119 else:
|
|
120 verdict = 'cannot read the input file'
|
|
121 except testcases.CannotReadAnswerFile:
|
|
122 e = sys.exc_info()[1]
|
|
123 if e.upstream.strerror:
|
|
124 verdict = 'cannot read the reference output file (%s)' % e.upstream.strerror.lower()
|
|
125 else:
|
|
126 verdict = 'cannot read the reference output file'
|
|
127 except testcases.TestCaseNotPassed:
|
|
128 e = sys.exc_info()[1]
|
|
129 verdict = 'unspecified reason [this may be a bug in test.py] (%s)' % e
|
|
130 #except Exception:
|
|
131 # e = sys.exc_info()[1]
|
|
132 # verdict = 'unknown error [this may be a bug in test.py] (%s)' % e
|
21
|
133 else:
|
22
|
134 if hasattr(granted, '__iter__'):
|
|
135 granted, comment = granted
|
|
136 if comment:
|
|
137 comment = ' (%s)' % comment
|
|
138 else:
|
|
139 comment = ''
|
|
140 if granted == case.points:
|
|
141 ncorrect += 1
|
|
142 if granted: ncorrectvalued += 1
|
|
143 verdict = 'OK' + comment
|
|
144 elif not granted:
|
|
145 verdict = 'wrong answer' + comment
|
|
146 else:
|
|
147 verdict = 'partly correct' + comment
|
|
148 say('%g/%g, %s' % (granted, case.points, verdict))
|
|
149 real += granted
|
|
150 weighted = real * prob.config.taskweight / max if max else 0
|
|
151 if nvalued != ntotal:
|
|
152 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
|
153 else:
|
22
|
154 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight))
|
|
155 return weighted, prob.config.taskweight
|
|
156 finally:
|
23
|
157 if options.erase and (not prob.config.stdio or case and case.validator):
|
22
|
158 for var in 'in', 'out':
|
|
159 name = getattr(prob.config, var + 'name')
|
|
160 if name:
|
|
161 try:
|
|
162 os.remove(name)
|
|
163 except Exception:
|
|
164 pass
|
|
165 if case.validator and not callable(case.validator):
|
|
166 if prob.config.ansname:
|
|
167 try:
|
|
168 os.remove(prob.config.ansname)
|
|
169 except Exception:
|
|
170 pass |