Esempio n. 1
0
def do_predict_constants(result: QualityResult):
    if result.problem_content is None or result.problem_content.original_html is None:
        result.modulo_error = Skipped()
        result.yes_str_error = Skipped()
        result.no_str_error = Skipped()
        result.constant_set = ProblemConstantSet()
        return
    try:
        result.modulo = predict_modulo(result.problem_content.original_html)
    except Exception as e:
        result.modulo_error = e

    result.yes_str, result.no_str = predict_yes_no(
        result.problem_content.original_html)

    judge_method = None
    try:
        judge_method = predict_judge_method(
            result.problem_content.original_html)
        if judge_method is not None:
            result.judge_method = judge_method.to_dict()

    except Exception as e:
        result.judge_method_error = e

    result.constant_set = ProblemConstantSet(mod=result.modulo,
                                             yes_str=result.yes_str,
                                             no_str=result.no_str,
                                             judge_method=judge_method)
def predict_constants(html: str) -> ProblemConstantSet:
    try:
        yes_str, no_str = predict_yes_no(html)
    except YesNoPredictionFailedError:
        yes_str = no_str = None

    try:
        mod = predict_modulo(html)
    except MultipleModCandidatesError as e:
        logger.warning(
            "Modulo prediction failed -- "
            "two or more candidates {} are detected as modulo values".format(
                e.cands))
        mod = None

    try:
        judge = predict_judge_method(html)
    except MultipleModCandidatesError as e:
        logger.warning(
            "decimal prediction failed -- "
            "two or more candidates {} are detected as decimal values".format(
                e.cands))
        judge = NormalJudge()

    return ProblemConstantSet(mod=mod,
                              yes_str=yes_str,
                              no_str=no_str,
                              judge_method=judge)
Esempio n. 3
0
    def _compile_and_run(self, lang, format, template_file,
                         expected_generated_code_file, input_file):
        code_file = os.path.join(self.temp_dir, lang.source_code_name("main"))
        exec_file, exec_args = self._exec_file_and_args(lang)
        compile_cmd = self._compile_command(lang, code_file)
        args = CodeGenArgs(template=load_text_file(template_file),
                           format_=format,
                           constants=ProblemConstantSet(123, "yes", "NO"),
                           config=CodeStyleConfig(lang=lang.name))
        code = lang.default_code_generator(args)
        # to remove version strings from test resources
        code = re.sub(r'Generated by \d+.\d+.\d+', 'Generated by x.y.z', code)
        self.compare_two_texts_ignoring_trailing_spaces(
            load_text_file(expected_generated_code_file), code)
        create_code(code, code_file)
        try:
            print("Executing:", compile_cmd)
            print(run_command(compile_cmd, self.temp_dir))

            print("Run program:", [exec_file] + exec_args)
            exec_result = run_program(exec_file, input_file, 2, exec_args,
                                      self.temp_dir)
        finally:
            self._clean_up(lang)

        print("== stdout ==")
        print(exec_result.output)
        print("== stderr ==")
        print(exec_result.stderr)

        self.assertEqual(exec_result.status.NORMAL, exec_result.status)
        return exec_result
Esempio n. 4
0
    def _compile_and_run(self, lang, format, template_file,
                         expected_generated_code_file, input_file):
        code_file = os.path.join(self.temp_dir, lang.source_code_name("main"))
        exec_file, exec_args = self._exec_file_and_args(lang)
        compile_cmd = self._compile_command(lang, code_file)

        args = CodeGenArgs(template=load_text_file(template_file),
                           format_=format,
                           constants=ProblemConstantSet(123, "yes", "NO"),
                           config=CodeStyleConfig())

        code = lang.default_code_generator(args)
        self.compare_two_texts_ignoring_trailing_spaces(
            load_text_file(expected_generated_code_file), code)
        create_code(code, code_file)
        print(run_command(compile_cmd, self.temp_dir))
        exec_result = run_program(exec_file, input_file, 2, exec_args,
                                  self.temp_dir)
        print("== stdout ==")
        print(exec_result.output)
        print("== stderr ==")
        print(exec_result.stderr)

        self.assertEqual(exec_result.status.NORMAL, exec_result.status)
        return exec_result
Esempio n. 5
0
def do_predict_constants(result: QualityResult):
    if result.problem_content is None or result.problem_content.original_html is None:
        result.modulo_error = Skipped()
        result.yes_str_error = Skipped()
        result.no_str_error = Skipped()
        result.constant_set = ProblemConstantSet()
        return
    try:
        result.modulo = predict_modulo(result.problem_content.original_html)
    except Exception as e:
        result.modulo_error = e

    result.yes_str, result.no_str = predict_yes_no(
        result.problem_content.original_html)

    result.constant_set = ProblemConstantSet(mod=result.modulo,
                                             yes_str=result.yes_str,
                                             no_str=result.no_str)
Esempio n. 6
0
 def verify(self,
            response: Response,
            py_test_name: str,
            lang: Language,
            template_type: str = "old",
            constants: ProblemConstantSet = ProblemConstantSet()):
     self.assertEqual(load_intermediate_format(py_test_name),
                      str(response.simple_format))
     self.assertEqual(load_intermediate_types(py_test_name),
                      str(response.types))
     self.assertEqual(
         load_generated_code(py_test_name, lang),
         self.lang_to_code_generator_func[lang](CodeGenArgs(
             self.get_template(lang, template_type),
             response.original_result.format, constants,
             CodeStyleConfig(lang=lang.name))))
Esempio n. 7
0
 def test_yes_no_case(self):
     response = self.runner.run('agc021-C')
     for l in ALL_LANGUAGES:
         self.verify(response,
                     sys._getframe().f_code.co_name, l, "jinja",
                     ProblemConstantSet(yes_str="YES", no_str="NO"))
Esempio n. 8
0
 def test_mod_case(self):
     response = self.runner.run('agc019-E')
     for l in ALL_LANGUAGES:
         self.verify(response,
                     sys._getframe().f_code.co_name, l, "jinja",
                     ProblemConstantSet(mod=998244353))