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
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
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) # 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) 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
def run_for_samples(exec_file: str, sample_pair_list: List[Tuple[str, str]], timeout_sec: int, judge_method: Judge = NormalJudge(), knock_out: bool = False, skip_io_on_success: bool = False) -> TestSummary: success_count = 0 has_error_output = False for in_sample_file, out_sample_file in sample_pair_list: # Run program exec_res = run_program(exec_file, in_sample_file, timeout_sec=timeout_sec) # Output header with open(out_sample_file, 'r') as f: answer_text = f.read() is_correct = exec_res.is_correct_output(answer_text, judge_method) has_error_output = has_error_output or exec_res.has_stderr() if is_correct: if exec_res.has_stderr(): message = with_color( "CORRECT but with stderr (Please remove stderr!)", Fore.LIGHTYELLOW_EX) else: message = "{} {elapsed} ms".format(with_color( "PASSED", Fore.LIGHTGREEN_EX), elapsed=exec_res.elapsed_ms) success_count += 1 else: if exec_res.status == ExecStatus.NORMAL: message = with_color("WA", Fore.LIGHTRED_EX) else: message = with_color(exec_res.status.name, Fore.LIGHTYELLOW_EX) print("# {case_name} ... {message}".format( case_name=os.path.basename(in_sample_file), message=message, )) # Output details for incorrect results or has stderr. if not is_correct or (exec_res.has_stderr() and not skip_io_on_success): print('{}\n'.format( build_details_str(exec_res, in_sample_file, out_sample_file))) if knock_out and not is_correct: print('Stop testing ...') break return TestSummary(success_count, has_error_output)
def run_for_samples(exec_file: str, sample_pair_list: List[Tuple[str, str]], timeout_sec: int, knock_out: bool = False, skip_io_on_success: bool = False) -> TestSummary: success_count = 0 has_error_output = False for in_sample_file, out_sample_file in sample_pair_list: # Run program exec_res = run_program(exec_file, in_sample_file, timeout_sec=timeout_sec) # Output header with open(out_sample_file, 'r') as f: answer_text = f.read() is_correct = exec_res.is_correct_output(answer_text) has_error_output = has_error_output or exec_res.has_stderr() if is_correct: if exec_res.has_stderr(): message = with_color( "CORRECT but with stderr (Please remove stderr!)", Fore.LIGHTYELLOW_EX) else: message = "{} {elapsed} ms".format( with_color("PASSED", Fore.LIGHTGREEN_EX), elapsed=exec_res.elapsed_ms) success_count += 1 else: if exec_res.status == ExecStatus.NORMAL: message = with_color("WA", Fore.LIGHTRED_EX) else: message = with_color( exec_res.status.name, Fore.LIGHTYELLOW_EX) print("# {case_name} ... {message}".format( case_name=os.path.basename(in_sample_file), message=message, )) # Output details for incorrect results or has stderr. if not is_correct or (exec_res.has_stderr() and not skip_io_on_success): print('{}\n'.format(build_details_str( exec_res, in_sample_file, out_sample_file))) if knock_out and not is_correct: print('Stop testing ...') break return TestSummary(success_count, has_error_output)
def run_for_samples(exec_file: str, sample_pair_list: List[Tuple[str, str]], timeout_sec: int, judge_method: Judge = NormalJudge(), knock_out: bool = False, skip_io_on_success: bool = False, cwd: str = "./") -> TestSummary: success_count = 0 has_error_output = False for in_sample_file, out_sample_file in sample_pair_list: if judge_method.judge_type == JudgeType.Interactive: exec_res = run_interactive_program(exec_file, judge_method.judge_code_lang.get_test_command( 'judge', cwd), in_sample_file, out_sample_file, timeout_sec=timeout_sec, current_working_dir=cwd ) is_correct = exec_res.is_correct_output(judge_method=judge_method) else: # Run program exec_res = run_program(exec_file, in_sample_file, timeout_sec=timeout_sec, current_working_dir=cwd) if judge_method.judge_type == JudgeType.MultiSolution: is_correct = exec_res.is_correct_output( judge_method=judge_method, sample_input_file=in_sample_file, sample_output_file=out_sample_file, cwd=cwd) else: # Output header with open(out_sample_file, 'r') as f: expected_answer_text = f.read() is_correct = exec_res.is_correct_output( expected_answer_text, judge_method) if exec_res.output is None: exec_res.output = "" elif isinstance(exec_res.output, bytes): exec_res.output = exec_res.output.decode() if exec_res.stderr is None: exec_res.stderr = "" elif isinstance(exec_res.stderr, bytes): exec_res.stderr = exec_res.stderr.decode() has_error_output = has_error_output or exec_res.has_stderr() if is_correct: if exec_res.has_stderr(): message = with_color( "CORRECT but with stderr (Please remove stderr!)", Fore.LIGHTYELLOW_EX) else: message = "{} {elapsed} ms".format( with_color("PASSED", Fore.LIGHTGREEN_EX), elapsed=exec_res.elapsed_ms) success_count += 1 else: if exec_res.status == ExecStatus.NORMAL: message = with_color("WA", Fore.LIGHTRED_EX) else: message = with_color( exec_res.status.name, Fore.LIGHTYELLOW_EX) print("# {case_name} ... {message}".format( case_name=os.path.basename(in_sample_file), message=message, )) # Output details for incorrect results or has stderr. if not is_correct or (exec_res.has_stderr() and not skip_io_on_success): print('{}\n'.format(build_details_str( exec_res, in_sample_file, out_sample_file))) if knock_out and not is_correct: print('Stop testing ...') break return TestSummary(success_count, has_error_output)