Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/problem.py @ 39:2b459f9743b4
Test groups are now supported
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Fri, 03 Dec 2010 02:46:06 +0000 |
| parents | 5bbb68833868 |
| children |
comparison
equal
deleted
inserted
replaced
| 38:a6d554679ce8 | 39:2b459f9743b4 |
|---|---|
| 28 value = signal.__dict__[name] | 28 value = signal.__dict__[name] |
| 29 if isinstance(value, int) and (value not in signalnames or name[3:] 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', 'TestContext', 'test_context_end', 'TestGroup' |
| 34 | 34 |
| 35 def strerror(e): | 35 def strerror(e): |
| 36 s = getattr(e, 'strerror') | 36 s = getattr(e, 'strerror') |
| 37 if not s: s = str(e) | 37 if not s: s = str(e) |
| 38 return ' (%s%s)' % (s[0].lower(), s[1:]) if s else '' | 38 return ' (%s%s)' % (s[0].lower(), s[1:]) if s else '' |
| 39 | 39 |
| 40 class Cache(object): | 40 class Cache(object): |
| 41 def __init__(self, mydict): | 41 def __init__(self, mydict): |
| 42 self.__dict__ = mydict | 42 self.__dict__ = mydict |
| 43 | |
| 44 class TestContext(object): | |
| 45 pass | |
| 46 | |
| 47 test_context_end = object() | |
| 48 | |
| 49 class TestGroup(TestContext): | |
| 50 __slots__ = 'case', 'log', 'correct', 'allcorrect', 'real', 'max', 'ntotal', 'nvalued', 'ncorrect', 'ncorrectvalued' | |
| 51 | |
| 52 def __init__(self): | |
| 53 self.real = self.max = self.ntotal = self.nvalued = self.ncorrect = self.ncorrectvalued = 0 | |
| 54 self.allcorrect = True | |
| 55 self.log = [] | |
| 56 | |
| 57 def case_start(self, case): | |
| 58 self.case = case | |
| 59 self.correct = False | |
| 60 self.ntotal += 1 | |
| 61 self.max += case.points | |
| 62 if case.points: | |
| 63 self.nvalued += 1 | |
| 64 | |
| 65 def case_correct(self): | |
| 66 self.correct = True | |
| 67 self.ncorrect += 1 | |
| 68 if self.case.points: | |
| 69 self.ncorrectvalued += 1 | |
| 70 | |
| 71 def case_end(self, granted): | |
| 72 self.log.append((self.case, self.correct, granted)) | |
| 73 self.real += granted | |
| 74 del self.case | |
| 75 if not self.correct: | |
| 76 self.allcorrect = False | |
| 77 | |
| 78 def end(self): | |
| 79 say('Group total: %d/%d tests; %d/%d points' % (self.ncorrect, self.ntotal, self.real if self.allcorrect else 0, self.max)) | |
| 80 # No real need to flush stdout, as it will anyway be flushed in a moment, | |
| 81 # when either the problem total or the next test case's ID is printed | |
| 82 if self.allcorrect: | |
| 83 return self.log | |
| 84 else: | |
| 85 return ((case, correct, 0) for case, correct, granted in self.log) | |
| 43 | 86 |
| 44 class Problem(object): | 87 class Problem(object): |
| 45 __slots__ = 'name', 'config', 'cache', 'testcases' | 88 __slots__ = 'name', 'config', 'cache', 'testcases' |
| 46 | 89 |
| 47 def __init__(prob, name): | 90 def __init__(prob, name): |
| 49 # This shouldn't happen, of course | 92 # This shouldn't happen, of course |
| 50 raise TypeError('Problem() argument 1 must be string, not ' + type(name).__name__) | 93 raise TypeError('Problem() argument 1 must be string, not ' + type(name).__name__) |
| 51 prob.name = name | 94 prob.name = name |
| 52 prob.config = config.load_problem(name) | 95 prob.config = config.load_problem(name) |
| 53 if not getattr(prob.config, 'kind', None): prob.config.kind = 'batch' | 96 if not getattr(prob.config, 'kind', None): prob.config.kind = 'batch' |
| 54 prob.cache = Cache({'padoutput': 0, 'usegroups': False}) | 97 prob.cache = Cache({'padoutput': 0}) |
| 55 prob.testcases = testcases.load_problem(prob) | 98 prob.testcases = testcases.load_problem(prob) |
| 56 | 99 |
| 57 # TODO | 100 # TODO |
| 58 def build(prob): | 101 def build(prob): |
| 59 raise NotImplementedError | 102 raise NotImplementedError |
| 60 | 103 |
| 61 def test(prob): | 104 def test(prob): |
| 62 case = None | 105 case = None |
| 63 try: | 106 try: |
| 64 real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0 | 107 contexts = [TestGroup()] |
| 65 for case in prob.testcases: | 108 for case in prob.testcases: |
| 66 ntotal += 1 | 109 if case is test_context_end: |
| 67 max += case.points | 110 for case, correct, granted in contexts.pop().end(): |
| 68 if case.points: nvalued += 1 | 111 contexts[-1].case_start(case) |
| 112 if correct: | |
| 113 contexts[-1].case_correct() | |
| 114 contexts[-1].case_end(granted) | |
| 115 continue | |
| 116 elif isinstance(case, TestContext): | |
| 117 contexts.append(case) | |
| 118 continue | |
| 119 contexts[-1].case_start(case) | |
| 69 granted = 0 | 120 granted = 0 |
| 70 id = str(case.id) | 121 id = str(case.id) |
| 71 if case.isdummy: | 122 if case.isdummy: |
| 72 id = 'sample ' + id | 123 id = 'sample ' + id |
| 73 say('%*s: ' % (prob.cache.padoutput, id), end='') | 124 say('%*s: ' % (prob.cache.padoutput, id), end='') |
| 108 except testcases.TestCaseNotPassed: | 159 except testcases.TestCaseNotPassed: |
| 109 verdict = 'unspecified reason [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) | 160 verdict = 'unspecified reason [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) |
| 110 #except Exception: | 161 #except Exception: |
| 111 # verdict = 'unknown error [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) | 162 # verdict = 'unknown error [this may be a bug in test.py]%s' % strerror(sys.exc_info()[1]) |
| 112 else: | 163 else: |
| 113 if hasattr(granted, '__iter__'): | 164 try: |
| 114 granted, comment = granted | 165 granted, comment = granted |
| 166 except TypeError: | |
| 167 comment = '' | |
| 168 else: | |
| 115 if comment: | 169 if comment: |
| 116 comment = ' (%s)' % comment | 170 comment = ' (%s)' % comment |
| 117 else: | |
| 118 comment = '' | |
| 119 if granted >= 1: | 171 if granted >= 1: |
| 120 ncorrect += 1 | 172 contexts[-1].case_correct() |
| 121 if case.points: ncorrectvalued += 1 | |
| 122 verdict = 'OK' + comment | 173 verdict = 'OK' + comment |
| 123 elif not granted: | 174 elif not granted: |
| 124 verdict = 'wrong answer' + comment | 175 verdict = 'wrong answer' + comment |
| 125 else: | 176 else: |
| 126 verdict = 'partly correct' + comment | 177 verdict = 'partly correct' + comment |
| 127 granted *= case.points | 178 granted *= case.points |
| 128 say('%g/%g, %s' % (granted, case.points, verdict)) | 179 say('%g/%g, %s' % (granted, case.points, verdict)) |
| 129 real += granted | 180 contexts[-1].case_end(granted) |
| 130 weighted = real * prob.config.taskweight / max if max else 0 | 181 weighted = contexts[0].real * prob.config.taskweight / contexts[0].max if contexts[0].max else 0 |
| 131 if nvalued != ntotal: | 182 if contexts[0].nvalued != contexts[0].ntotal: |
| 132 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)) | 183 say('Problem total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (contexts[0].ncorrect, contexts[0].ntotal, contexts[0].ncorrectvalued, contexts[0].nvalued, contexts[0].real, contexts[0].max, weighted, prob.config.taskweight)) |
| 133 else: | 184 else: |
| 134 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight)) | 185 say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (contexts[0].ncorrect, contexts[0].ntotal, contexts[0].real, contexts[0].max, weighted, prob.config.taskweight)) |
| 186 sys.stdout.flush() | |
| 135 return weighted, prob.config.taskweight | 187 return weighted, prob.config.taskweight |
| 136 finally: | 188 finally: |
| 137 if options.erase and (not prob.config.stdio or case and case.validator): | 189 if options.erase and (not prob.config.stdio or case and case.validator): |
| 138 for var in 'in', 'out': | 190 for var in 'in', 'out': |
| 139 name = getattr(prob.config, var + 'name') | 191 name = getattr(prob.config, var + 'name') |
