| import sys | |
| sys.path.append("/home/i-luoxianzhen/data/TestCase-Gen/methods/utils") | |
| from dataset_all import get_datasets_by_name | |
| import os | |
| import uuid | |
| import subprocess | |
| import tempfile | |
| import json | |
| import re | |
| import subprocess | |
| import tempfile | |
| import os | |
| import resource | |
| import uuid | |
| import json | |
| import random | |
| from decimal import Decimal | |
| import re | |
| def is_decimal(s): | |
| try: | |
| a = float(s) | |
| except: | |
| return False | |
| return bool(re.match(r"^-?\d+\.\d+$", s)) | |
| def run_cpp_code_linux(code, input_string, std, memory_limit, time_limit): | |
| res = {} | |
| with tempfile.TemporaryDirectory() as tmpdirname: | |
| unique_id = uuid.uuid4() | |
| cpp_file = os.path.join(tmpdirname, f"{unique_id}.cpp") | |
| exe_file = os.path.join(tmpdirname, f"{unique_id}.out") | |
| # Write C++ code to file | |
| with open(cpp_file, "w") as f: | |
| f.write(code) | |
| # Compile the C++ code | |
| compile_result = subprocess.run( | |
| ["g++", cpp_file, "-o", exe_file, f"-std={std}"], | |
| capture_output=True, | |
| text=True | |
| ) | |
| if compile_result.returncode != 0: | |
| res["status"] = "CE" | |
| res["details"] = compile_result.stderr | |
| return res | |
| memory_kb = int(memory_limit) * 1024 | |
| time_limit_int = int(time_limit) // 1000 + 1 | |
| cmd = f"ulimit -t {time_limit_int} && ulimit -v {memory_kb} && {exe_file}" | |
| error = "" | |
| try: | |
| result = subprocess.run( | |
| cmd, | |
| input=input_string, | |
| text=True, | |
| capture_output=True, | |
| shell=True, | |
| timeout=time_limit_int | |
| ) | |
| # 检查返回码 | |
| if result.returncode != 0: | |
| if result.returncode == 137: # SIGKILL - 通常是内存超限 | |
| error = "MLE" | |
| elif result.returncode == 124: # timeout命令的超时返回码 | |
| error = "TLE" | |
| else: | |
| error = "RE" | |
| if not error and result.stderr: | |
| error = "RE" | |
| actual_lines = [line.strip() for line in result.stdout.splitlines()] | |
| actual_lines = [line for line in actual_lines if line] | |
| actual_lines = (" ".join(actual_lines)).strip() | |
| ### 小数保留 6 位 | |
| if is_decimal(actual_lines): | |
| actual_lines = float(actual_lines) | |
| res['stdout'] = actual_lines | |
| res['status'] = "AC" | |
| except subprocess.TimeoutExpired: | |
| error = "TLE" | |
| except Exception as e: | |
| error = "RE" | |
| if error: | |
| res['status'] = error | |
| res['stdout'] = "" | |
| return res | |
| import json | |
| new_wrong_code = json.load(open("/home/i-luoxianzhen/data/TestCase-Gen/data/Ours/balance_v1.0.dev_with_code.json", "r", encoding="utf-8")) | |
| compileAndRunOptions = { | |
| "compiler": "g++", | |
| "std": "c++11", | |
| "O": "2", | |
| "m": "x32" | |
| } | |
| save_dict = {} | |
| i = 0 | |
| for k, v in new_wrong_code.items(): | |
| print(f"idx: {i} Start") | |
| i+=1 | |
| execute_status = [] | |
| wrong_codes = v["cowrong_codes"] | |
| if len(v['sample']) >= 1: | |
| input_string = v['sample'][0]['inputData'] | |
| output_string = v['sample'][0]['outputData'] | |
| else: | |
| continue | |
| pass_code_list = [] | |
| for item in wrong_codes: | |
| result = run_cpp_code_linux(item['code'], input_string, item['compileAndRunOptions']['std'], v['memoryLimit'], v['timeLimit']) | |
| pass_code_list.append(result['status']) | |
| save_dict[k] = pass_code_list | |
| break | |
| json.dump(save_dict, open(f"/home/i-luoxianzhen/data/TestCase-Gen/data/Ours/wrong_code_check.json", "w", encoding="utf-8"), indent=4, ensure_ascii=False) | |
Xet Storage Details
- Size:
- 3.85 kB
- Xet hash:
- c41c5ad25b79b4751815ae72a6a985ab824c2449a669fee80177705af26e5456
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.