Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/problem.py @ 21:ec6f1a132109
A pretty usable version
Test groups and testconfs in non-ZIP archives or ZIP archives with comments are not yet supported.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Fri, 06 Aug 2010 15:39:29 +0000 |
parents | f2279b7602d3 |
children | f07b7a431ea6 |
comparison
equal
deleted
inserted
replaced
20:5bfa23cd638d | 21:ec6f1a132109 |
---|---|
1 #!/usr/bin/python | 1 #! /usr/bin/env python |
2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> | 2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> |
3 | 3 |
4 from __future__ import division, with_statement | |
5 | |
4 try: | 6 try: |
5 import config as _config, testcases as _testcases | 7 from compat import * |
6 except ImportError as e: | 8 import config, testcases |
9 except ImportError: | |
7 import __main__ | 10 import __main__ |
8 __main__.import_error(e) | 11 __main__.import_error(sys.exc_info()[1]) |
12 else: | |
13 from __main__ import clock | |
9 | 14 |
15 import sys, re | |
16 | |
17 try: | |
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 | |
32 | |
33 __all__ = 'Problem', | |
34 | |
35 # This should no more be needed; pass all work on to the TestCase inheritance tree | |
10 # LIBRARY and STDIO refer to interactive aka reactive problems | 36 # LIBRARY and STDIO refer to interactive aka reactive problems |
11 BATCH, OUTONLY, LIBRARY, STDIO, BESTOUT = xrange(5) | 37 #BATCH, OUTONLY, LIBRARY, STDIO, BESTOUT = xrange(5) |
38 | |
39 class Cache(object): | |
40 def __init__(self, mydict): | |
41 self.__dict__ = mydict | |
12 | 42 |
13 class Problem(object): | 43 class Problem(object): |
14 __slots__ = 'name', 'config', 'cache', 'testcases' | 44 __slots__ = 'name', 'config', 'cache', 'testcases' |
15 | 45 |
16 def __init__(prob, name): | 46 def __init__(prob, name): |
17 if not isinstance(name, basestring): | 47 if not isinstance(name, basestring): |
18 # This shouldn't happen, of course | 48 # This shouldn't happen, of course |
19 raise TypeError, "Problem() argument 1 must be string, not " + str(type(name)).split('\'')[1] | 49 raise TypeError('Problem() argument 1 must be string, not ' + type(name).__name__) |
20 prob.name = name | 50 prob.name = name |
21 prob.config = _config.load_problem(name) | 51 prob.config = config.load_problem(name) |
22 prob.cache = type('Cache', (object,), {'padoutputtolength': 0})() | 52 if not getattr(prob.config, 'kind', None): prob.config.kind = 'batch' |
23 prob.testcases = _testcases.load_problem(prob) | 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 | |
24 | 59 |
25 def test(prob): | 60 def test(prob): |
26 real = max = 0 | 61 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0 |
27 for case in prob.testcases: | 62 for case in prob.testcases: |
28 r, m = case() | 63 ntotal += 1 |
29 real += r | 64 max += case.points |
30 max += m | 65 if case.points: nvalued += 1 |
31 return real, max | 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 |