Exemple #1
0
    def _TestOneCase(self, solution, testcase, ui):
        """Test a solution with one case.

        Cache results if option is set.
        Returns TestCaseResult.
        """
        cache_file_name = os.path.join(
            solution.out_dir,
            os.path.splitext(os.path.basename(testcase.infile))[0] +
            consts.CACHE_EXT)
        solution_file_name = os.path.join(solution.src_dir,
                                          solution.code.src_name)

        cache_flag = (ui.options.cache_tests
                      and files.GetModified(solution_file_name) <
                      files.GetModified(cache_file_name)
                      and files.GetModified(testcase.infile) <
                      files.GetModified(cache_file_name))

        if cache_flag:
            case_result_cache = files.ReadFile(cache_file_name)
            if case_result_cache is not None:
                j = json.loads(case_result_cache)
                if j['time'] is not None:
                    j['time'] = float(j['time'])
                if j['verdict'] is not None:
                    j['verdict'] = j['verdict'].encode('ascii')

                case_result = test.TestCaseResult(solution, testcase, None,
                                                  None, True)
                case_result.time = j['time']
                case_result.verdict = [
                    verdict
                    for verdict in test.TestCaseResult.__dict__.values()
                    if isinstance(verdict, test.TestVerdict)
                    and verdict.msg == j['verdict']
                ][0]

            yield case_result

        case_result = yield self._TestOneCaseNoCache(solution, testcase, ui)

        # always cache in json
        files.WriteFile(
            json.dumps({
                'verdict': case_result.verdict.msg,
                'time': case_result.time
            }), cache_file_name)

        yield case_result
Exemple #2
0
    def Pack(self, ui, testset):
        testcases = testset.ListTestCases()
        try:
            files.RemoveTree(testset.aoj_pack_dir)
            files.MakeDir(testset.aoj_pack_dir)
        except Exception:
            ui.errors.Exception(testset)
            yield False
        for (i, testcase) in enumerate(testcases):
            basename = os.path.splitext(testcase.infile)[0]
            difffile = basename + consts.DIFF_EXT
            packed_infile = 'in' + str(i + 1) + '.txt'
            packed_difffile = 'out' + str(i + 1) + '.txt'
            try:
                ui.console.PrintAction(
                    'PACK',
                    testset,
                    '%s -> %s' % (os.path.basename(testcase.infile),
                                  packed_infile),
                    progress=True)
                files.CopyFile(os.path.join(testset.out_dir, testcase.infile),
                               os.path.join(testset.aoj_pack_dir,
                                            packed_infile))
                ui.console.PrintAction(
                    'PACK',
                    testset,
                    '%s -> %s' % (os.path.basename(difffile), packed_difffile),
                    progress=True)
                files.CopyFile(os.path.join(testset.out_dir, difffile),
                               os.path.join(testset.aoj_pack_dir,
                                            packed_difffile))
            except Exception:
                ui.errors.Exception(testset)
                yield False

        # case.txt
        files.WriteFile(str(len(testcases)),
                        os.path.join(testset.aoj_pack_dir, 'case.txt'))

        # build.sh
        # TODO(mizuno): reactive
        checker = testset.judges[0]
        if (len(testset.judges) == 1 and
                not isinstance(checker, basic_codes.InternalDiffCode)):
            ui.console.PrintAction(
                'PACK', testset, 'checker files', progress=True)
            files.CopyFile(os.path.join(testset.src_dir, checker.src_name),
                           os.path.join(testset.aoj_pack_dir, 'checker.cpp'))
            for f in checker.dependency:
                files.CopyFile(os.path.join(testset.project.library_dir, f),
                               os.path.join(testset.aoj_pack_dir, f))
            files.WriteFile(
                '#!/bin/bash\ng++ -o checker -std=c++11 checker.cpp',
                os.path.join(testset.aoj_pack_dir, 'build.sh'))
        elif len(testset.judges) > 1:
            ui.errors.Error(
                testset, "Multiple output checker is not supported!")
            yield False

        # AOJCONF
        aoj_conf = '''\
# -*- coding: utf-8; mode: python -*-

# Problem ID
PROBLEM_ID = '*'

# Judge type
#   'diff-validator' for a problem without special judge
#   'float-validator' for a problem with floating point validator
#   'special-validator' for a problem with special validator
#   'reactive' for a reactive problem
{0}

# Language of problem description
#   'ja', 'en' or 'ja-en'
DOCUMENT_TYPE = '*'

# Title of the problem
TITLE = '{1}'

# Time limit (integer, in seconds)
TIME_LIMIT = 1

# Memory limit (integer, in KB)
MEMORY_LIMIT = 32768

# Date when the problem description will be able to be seen
PUBLICATION_DATE = datetime.datetime(*, *, *, *, *)
'''
        if not isinstance(checker, basic_codes.InternalDiffCode):
            files.WriteFile(
                aoj_conf.format(
                    'JUDGE_TYPE = \'special-validator\'',
                    testset.problem.title),
                os.path.join(testset.aoj_pack_dir, 'AOJCONF'))
        else:
            files.WriteFile(
                aoj_conf.format(
                    'JUDGE_TYPE = \'diff-validator\'', testset.problem.title),
                os.path.join(testset.aoj_pack_dir, 'AOJCONF'))

        yield True
Exemple #3
0
def EditFile(filename, initial):
    EDITOR = os.environ.get('EDITOR', 'vi')
    files.WriteFile(initial, filename)
    call([EDITOR, filename])
Exemple #4
0
    def Pack(self, ui, testset):
        testcases = testset.ListTestCases()
        try:
            files.RemoveTree(testset.atcoder_pack_dir)
            files.MakeDir(testset.atcoder_pack_dir)
            files.MakeDir(os.path.join(testset.atcoder_pack_dir, 'in'))
            files.MakeDir(os.path.join(testset.atcoder_pack_dir, 'out'))
            files.MakeDir(os.path.join(testset.atcoder_pack_dir, 'etc'))
        except Exception:
            ui.errors.Exception(testset)
            yield False
        for (i, testcase) in enumerate(testcases):
            basename = os.path.splitext(testcase.infile)[0]
            difffile = basename + consts.DIFF_EXT
            packed_infile = os.path.join('in', os.path.basename(basename))
            packed_difffile = os.path.join('out', os.path.basename(basename))
            try:
                ui.console.PrintAction(
                    'PACK',
                    testset,
                    '%s -> %s' % (os.path.basename(testcase.infile),
                                  packed_infile),
                    progress=True)
                files.CopyFile(os.path.join(testset.out_dir, testcase.infile),
                               os.path.join(testset.atcoder_pack_dir,
                                            packed_infile))
                ui.console.PrintAction(
                    'PACK',
                    testset,
                    '%s -> %s' % (os.path.basename(difffile), packed_difffile),
                    progress=True)
                files.CopyFile(os.path.join(testset.out_dir, difffile),
                               os.path.join(testset.atcoder_pack_dir,
                                            packed_difffile))
            except Exception:
                ui.errors.Exception(testset)
                yield False

        # checker
        checker = testset.judges[0]
        if (len(testset.judges) == 1 and
                not isinstance(checker, basic_codes.InternalDiffCode)):
            ui.console.PrintAction(
                'PACK', testset, 'output checker files', progress=True)
            files.CopyFile(
                os.path.join(testset.src_dir, checker.src_name),
                os.path.join(testset.atcoder_pack_dir, 'etc',
                             'output_checker.cpp'))
            for f in checker.dependency:
                files.CopyFile(os.path.join(testset.project.library_dir, f),
                               os.path.join(testset.atcoder_pack_dir, 'etc',
                                            f))
        elif len(testset.judges) > 1:
            ui.errors.Error(
                testset, "Multiple output checker is not supported!")
            yield False

        # reactive
        if len(testset.reactives) == 1:
            reactive = testset.reactives[0]
            ui.console.PrintAction(
                'PACK', testset, 'reactive checker files', progress=True)
            files.CopyFile(
                os.path.join(testset.src_dir, reactive.src_name),
                os.path.join(testset.atcoder_pack_dir, 'etc', 'reactive.cpp'))
            for f in reactive.dependency:
                files.CopyFile(os.path.join(testset.project.library_dir, f),
                               os.path.join(testset.atcoder_pack_dir, 'etc',
                                            f))
            # outは使わない
            files.RemoveTree(os.path.join(testset.atcoder_pack_dir, 'out'))
        elif len(testset.judges) > 1:
            ui.errors.Error(
                testset, "Multiple reactive checker is not supported!")
            yield False

        # score.txt
        subtasks = testset.subtask_testcases
        if len(subtasks) > 0:
            score = '\n'.join([
                s.name + '(' + str(s.score) + ')' + ': ' +
                ','.join(s.input_patterns)
                for s in subtasks])
        else:
            score = 'All(100): *'
        files.WriteFile(
            score, os.path.join(testset.atcoder_pack_dir, 'etc', 'score.txt'))

        yield True