Beispiel #1
0
 def test_bad_archive(self):
     self.problem_data = {'init.yml': 'archive: %s' % (os.devnull, )}
     with mock.patch('dmoj.problem.get_problem_root') as gpr:
         gpr.return_value = '/'
         with self.assertRaisesRegex(InvalidInitException, 'bad archive:'):
             Problem('test', 2, 16384, {})
Beispiel #2
0
    def _begin_grading(self,
                       problem_id,
                       language,
                       source,
                       time_limit,
                       memory_limit,
                       short_circuit,
                       pretests_only,
                       report=print,
                       retsaver=None):
        submission_id = self.current_submission
        report(
            ansi_style(
                'Start grading #ansi[%s](yellow)/#ansi[%s](green|bold) in %s...'
                % (problem_id, submission_id, language)))

        try:
            problem = Problem(problem_id,
                              time_limit,
                              memory_limit,
                              load_pretests_only=pretests_only)
        except Exception:
            return self.internal_error()

        if 'signature_grader' in problem.config:
            grader_class = graders.SignatureGrader
        elif 'custom_judge' in problem.config:
            grader_class = graders.CustomGrader
        elif 'notest_judge' in problem.config:
            grader_class = graders.NotestGrader
        else:
            grader_class = graders.StandardGrader

        grader = self.get_grader_from_source(grader_class,
                                             problem,
                                             language,
                                             source,
                                             report=report)
        binary = grader.binary if grader else None

        # the compiler may have failed, or an error could have happened while initializing a custom judge
        # either way, we can't continue
        if binary:
            self.packet_manager.begin_grading_packet(problem.is_pretested)

            batch_counter = 1
            in_batch = False

            # cases are indexed at 1
            case_number = 1
            try:
                for result in self.grade_cases(grader,
                                               problem.cases,
                                               short_circuit=short_circuit):
                    if isinstance(result, BatchBegin):
                        self.packet_manager.batch_begin_packet()
                        report(
                            ansi_style("#ansi[Batch #%d](yellow|bold)" %
                                       batch_counter))
                        in_batch = True
                    elif isinstance(result, BatchEnd):
                        self.packet_manager.batch_end_packet()
                        batch_counter += 1
                        in_batch = False
                    else:
                        codes = result.readable_codes()

                        # here be cancer
                        is_sc = (result.result_flag & Result.SC)
                        colored_codes = list(
                            map(
                                lambda x: '#ansi[%s](%s|bold)' %
                                ('--'
                                 if x == 'SC' else x, Result.COLORS_BYID[x]),
                                codes))
                        colored_aux_codes = '{%s}' % ', '.join(
                            colored_codes[1:]) if len(codes) > 1 else ''
                        colored_feedback = '(#ansi[%s](|underline)) ' % result.feedback if result.feedback else ''
                        case_info = '[%.3fs (%.3fs) | %dkb] %s%s' % (
                            result.execution_time, result.r_execution_time,
                            result.max_memory, colored_feedback,
                            colored_aux_codes) if not is_sc else ''
                        case_padding = '  ' * in_batch
                        report(
                            ansi_style('%sTest case %2d %-3s %s' %
                                       (case_padding, case_number,
                                        colored_codes[0], case_info)))

                        self.packet_manager.test_case_status_packet(
                            case_number, result)
                        if retsaver:
                            retsaver.saveinfo(
                                result.proc_output, result.proc_stderr,
                                result.get_main_code(),
                                int(result.execution_time * 1000),
                                result.max_memory)

                        case_number += 1
            except TerminateGrading:
                self.packet_manager.submission_terminated_packet()
                report(
                    ansi_style(
                        '#ansi[Forcefully terminating grading. '
                        'Temporary files may not be deleted.](red|bold)'))
                pass
            except:
                self.internal_error()
            else:
                self.packet_manager.grading_end_packet()

        report(
            ansi_style(
                'Done grading #ansi[%s](yellow)/#ansi[%s](green|bold).\n' %
                (problem_id, submission_id)))

        self._terminate_grading = False
        self.current_submission_thread = None
        self.current_submission = None
        self.current_grader = None
Beispiel #3
0
 def test_blank_init(self):
     self.problem_data = {'init.yml': 'archive: does_not_exist.txt'}
     with mock.patch('dmoj.problem.get_problem_root') as gpr:
         gpr.return_value = '/proc'
         with self.assertRaisesRegex(InvalidInitException, 'archive file'):
             Problem('test', 2, 16384, {})
Beispiel #4
0
 def test_no_init(self):
     self.problem_data = {}
     with mock.patch('dmoj.problem.get_problem_root') as gpr:
         gpr.return_value = '/proc'
         with self.assertRaises(InvalidInitException):
             Problem('test', 2, 16384, {})
Beispiel #5
0
 def test_bad_init(self):
     self.problem_data = {'init.yml': '"'}
     with self.assertRaisesRegex(InvalidInitException,
                                 'while scanning a quoted scalar'):
         Problem('test', 2, 16384)
Beispiel #6
0
 def test_empty_init(self):
     self.problem_data = {'init.yml': ''}
     with self.assertRaisesRegex(InvalidInitException, 'lack of content'):
         Problem('test', 2, 16384)
Beispiel #7
0
 def test_no_init(self):
     self.problem_data = {}
     with self.assertRaises(InvalidInitException):
         Problem('test', 2, 16384)