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:
|
|
13 from __main__ import clock
|
|
14
|
|
15 import sys, re
|
|
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]
|
|
29 if isinstance(value, int) and (value not in signalnames or signalnames[value][3:] not in unixnames):
|
|
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):
|
21
|
61 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0
|
16
|
62 for case in prob.testcases:
|
21
|
63 ntotal += 1
|
|
64 max += case.points
|
|
65 if case.points: nvalued += 1
|
|
66 granted = 0
|
|
67 id = str(case.id)
|
|
68 if case.isdummy:
|
|
69 id = 'sample ' + id
|
|
70 say('%*s: ' % (prob.cache.padoutput, id), end='')
|
|
71 sys.stdout.flush()
|
|
72 try:
|
|
73 granted = case()
|
|
74 except KeyboardInterrupt:
|
|
75 if not hasattr(case, 'time_stopped'):
|
|
76 # Too quick! The testing has not even started!
|
|
77 raise
|
|
78 verdict = 'canceled by the user'
|
|
79 except testcases.TimeLimitExceeded:
|
|
80 verdict = 'time limit exceeded'
|
|
81 except testcases.WrongAnswer:
|
|
82 e = sys.exc_info()[1]
|
|
83 if e.comment:
|
|
84 verdict = 'wrong answer (%s)' % e.comment
|
|
85 else:
|
|
86 verdict = 'wrong answer'
|
|
87 except testcases.NonZeroExitCode:
|
|
88 e = sys.exc_info()[1]
|
|
89 if e.exitcode < 0:
|
|
90 if sys.platform == 'win32':
|
|
91 verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000)
|
|
92 elif -e.exitcode in signalnames:
|
|
93 verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode])
|
|
94 else:
|
|
95 verdict = 'terminated by signal %d' % -e.exitcode
|
|
96 else:
|
|
97 verdict = 'non-zero return code %d' % e.exitcode
|
|
98 except testcases.CannotStartTestee:
|
|
99 e = sys.exc_info()[1]
|
|
100 if e.upstream.strerror:
|
|
101 verdict = 'cannot launch the program to test (%s)' % e.upstream.strerror.lower()
|
|
102 else:
|
|
103 verdict = 'cannot launch the program to test'
|
|
104 except testcases.CannotStartValidator:
|
|
105 e = sys.exc_info()[1]
|
|
106 if e.upstream.strerror:
|
|
107 verdict = 'cannot launch the validator (%s)' % e.upstream.strerror.lower()
|
|
108 else:
|
|
109 verdict = 'cannot launch the validator'
|
|
110 except testcases.CannotReadOutputFile:
|
|
111 e = sys.exc_info()[1]
|
|
112 if e.upstream.strerror:
|
|
113 verdict = 'cannot read the output file (%s)' % e.upstream.strerror.lower()
|
|
114 else:
|
|
115 verdict = 'cannot read the output file'
|
|
116 except testcases.CannotReadInputFile:
|
|
117 e = sys.exc_info()[1]
|
|
118 if e.upstream.strerror:
|
|
119 verdict = 'cannot read the input file (%s)' % e.upstream.strerror.lower()
|
|
120 else:
|
|
121 verdict = 'cannot read the input file'
|
|
122 except testcases.CannotReadAnswerFile:
|
|
123 e = sys.exc_info()[1]
|
|
124 if e.upstream.strerror:
|
|
125 verdict = 'cannot read the reference output file (%s)' % e.upstream.strerror.lower()
|
|
126 else:
|
|
127 verdict = 'cannot read the reference output file'
|
|
128 except testcases.TestCaseNotPassed:
|
|
129 e = sys.exc_info()[1]
|
|
130 verdict = 'unspecified reason [this may be a bug in test.py] (%s)' % e
|
|
131 #except Exception:
|
|
132 # e = sys.exc_info()[1]
|
|
133 # verdict = 'unknown error [this may be a bug in test.py] (%s)' % e
|
|
134 else:
|
|
135 if hasattr(granted, '__iter__'):
|
|
136 granted, comment = granted
|
|
137 if comment:
|
|
138 comment = ' (%s)' % comment
|
|
139 else:
|
|
140 comment = ''
|
|
141 if granted == case.points:
|
|
142 ncorrect += 1
|
|
143 if granted: ncorrectvalued += 1
|
|
144 verdict = 'OK' + comment
|
|
145 elif not granted:
|
|
146 verdict = 'wrong answer' + comment
|
|
147 else:
|
|
148 verdict = 'partly correct' + comment
|
|
149 say('%.3f%s s, %g/%g, %s' % (case.time_stopped - case.time_started, case.time_limit_string, granted, case.points, verdict))
|
|
150 real += granted
|
|
151 weighted = real * prob.config.taskweight / max if max else 0
|
|
152 if nvalued != ntotal:
|
|
153 say('Grand total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, ncorrectvalued, nvalued, real, max, weighted, prob.config.taskweight))
|
|
154 else:
|
|
155 say('Grand total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight))
|
|
156 return weighted, prob.config.taskweight
|