comparison 2.00/problem.py @ 22:f07b7a431ea6

Further 2.00 work Testconfs in all supported kinds of archives should now work. Test runs are now cancelled by pressing Escape rather than Ctrl+C. Improved time control. Greatly improved temporary and helper file cleanup. The pause configuration variable can now be a callable and is now processed using subprocess rather than system().
author Oleg Oshmyan <chortos@inbox.lv>
date Wed, 22 Sep 2010 22:01:56 +0000
parents ec6f1a132109
children c1f52b5d80d6
comparison
equal deleted inserted replaced
21:ec6f1a132109 22:f07b7a431ea6
8 import config, testcases 8 import config, testcases
9 except ImportError: 9 except ImportError:
10 import __main__ 10 import __main__
11 __main__.import_error(sys.exc_info()[1]) 11 __main__.import_error(sys.exc_info()[1])
12 else: 12 else:
13 from __main__ import clock 13 from __main__ import clock, options
14 14
15 import sys, re 15 import os, re, sys
16 16
17 try: 17 try:
18 import signal 18 import signal
19 except ImportError: 19 except ImportError:
20 signalnames = () 20 signalnames = ()
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')) 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 = {} 25 signalnames = {}
26 for name in dir(signal): 26 for name in dir(signal):
27 if re.match('SIG[A-Z]+$', name): 27 if re.match('SIG[A-Z]+$', name):
28 value = signal.__dict__[name] 28 value = signal.__dict__[name]
29 if isinstance(value, int) and (value not in signalnames or signalnames[value][3:] not in unixnames): 29 if isinstance(value, int) and (value not in signalnames or name[3:] in unixnames):
30 signalnames[value] = name 30 signalnames[value] = name
31 del unixnames 31 del unixnames
32 32
33 __all__ = 'Problem', 33 __all__ = 'Problem',
34 34
56 # TODO 56 # TODO
57 def build(prob): 57 def build(prob):
58 raise NotImplementedError 58 raise NotImplementedError
59 59
60 def test(prob): 60 def test(prob):
61 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0 61 try:
62 for case in prob.testcases: 62 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0
63 ntotal += 1 63 for case in prob.testcases:
64 max += case.points 64 ntotal += 1
65 if case.points: nvalued += 1 65 max += case.points
66 granted = 0 66 if case.points: nvalued += 1
67 id = str(case.id) 67 granted = 0
68 if case.isdummy: 68 id = str(case.id)
69 id = 'sample ' + id 69 if case.isdummy:
70 say('%*s: ' % (prob.cache.padoutput, id), end='') 70 id = 'sample ' + id
71 sys.stdout.flush() 71 say('%*s: ' % (prob.cache.padoutput, id), end='')
72 try: 72 sys.stdout.flush()
73 granted = case() 73 try:
74 except KeyboardInterrupt: 74 granted = case(lambda: (say('%7.3f%s s, ' % (case.time_stopped - case.time_started, case.time_limit_string), end=''), sys.stdout.flush()))
75 if not hasattr(case, 'time_stopped'): 75 except testcases.CanceledByUser:
76 # Too quick! The testing has not even started! 76 verdict = 'canceled by the user'
77 raise 77 except testcases.TimeLimitExceeded:
78 verdict = 'canceled by the user' 78 verdict = 'time limit exceeded'
79 except testcases.TimeLimitExceeded: 79 except testcases.WrongAnswer:
80 verdict = 'time limit exceeded' 80 e = sys.exc_info()[1]
81 except testcases.WrongAnswer: 81 if e.comment:
82 e = sys.exc_info()[1] 82 verdict = 'wrong answer (%s)' % e.comment
83 if e.comment: 83 else:
84 verdict = 'wrong answer (%s)' % e.comment 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
94 else:
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
85 else: 132 else:
86 verdict = 'wrong answer' 133 if hasattr(granted, '__iter__'):
87 except testcases.NonZeroExitCode: 134 granted, comment = granted
88 e = sys.exc_info()[1] 135 if comment:
89 if e.exitcode < 0: 136 comment = ' (%s)' % comment
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: 137 else:
95 verdict = 'terminated by signal %d' % -e.exitcode 138 comment = ''
96 else: 139 if granted == case.points:
97 verdict = 'non-zero return code %d' % e.exitcode 140 ncorrect += 1
98 except testcases.CannotStartTestee: 141 if granted: ncorrectvalued += 1
99 e = sys.exc_info()[1] 142 verdict = 'OK' + comment
100 if e.upstream.strerror: 143 elif not granted:
101 verdict = 'cannot launch the program to test (%s)' % e.upstream.strerror.lower() 144 verdict = 'wrong answer' + comment
102 else: 145 else:
103 verdict = 'cannot launch the program to test' 146 verdict = 'partly correct' + comment
104 except testcases.CannotStartValidator: 147 say('%g/%g, %s' % (granted, case.points, verdict))
105 e = sys.exc_info()[1] 148 real += granted
106 if e.upstream.strerror: 149 weighted = real * prob.config.taskweight / max if max else 0
107 verdict = 'cannot launch the validator (%s)' % e.upstream.strerror.lower() 150 if nvalued != ntotal:
108 else: 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))
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: 152 else:
135 if hasattr(granted, '__iter__'): 153 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight))
136 granted, comment = granted 154 return weighted, prob.config.taskweight
137 if comment: 155 finally:
138 comment = ' (%s)' % comment 156 if options.erase and (not prob.config.stdio or case.validator):
139 else: 157 for var in 'in', 'out':
140 comment = '' 158 name = getattr(prob.config, var + 'name')
141 if granted == case.points: 159 if name:
142 ncorrect += 1 160 try:
143 if granted: ncorrectvalued += 1 161 os.remove(name)
144 verdict = 'OK' + comment 162 except Exception:
145 elif not granted: 163 pass
146 verdict = 'wrong answer' + comment 164 if case.validator and not callable(case.validator):
147 else: 165 if prob.config.ansname:
148 verdict = 'partly correct' + comment 166 try:
149 say('%.3f%s s, %g/%g, %s' % (case.time_stopped - case.time_started, case.time_limit_string, granted, case.points, verdict)) 167 os.remove(prob.config.ansname)
150 real += granted 168 except Exception:
151 weighted = real * prob.config.taskweight / max if max else 0 169 pass
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