Tsukihjy/testcase / testcase-data /Ours /get_no_ce_correct_code.py
Tsukihjy's picture
download
raw
4.61 kB
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
def is_decimal(s):
try:
a = float(s)
except:
return False
return bool(re.match(r"^-?\d+\.\d+$", s))
def get_testcases(testcase_path, rank):
data = []
if not os.path.exists(testcase_path):
return []
with open(testcase_path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip():
data.append(json.loads(line))
return data
def execute_cpp_code(code, input_string, compile_options, time_limit_int):
# Create a temporary directory for storing the cpp file and executable
with tempfile.TemporaryDirectory() as tmpdirname:
# Generate a unique identifier for file names
unique_id = uuid.uuid4()
# Define the paths for the C++ file and executable
cpp_file = os.path.join(tmpdirname, f"{unique_id}.cpp")
exe_file = os.path.join(tmpdirname, f"{unique_id}.out")
# Write the provided C++ code to the cpp 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={compile_options['std']}"],
capture_output=True,
text=True
)
# Check if the compilation was successful
if compile_result.returncode != 0:
return f"Compilation failed: {compile_result.stderr}"
# Run the compiled executable with the provided input
cmd = [exe_file]
try:
result = subprocess.run(
cmd,
input=input_string,
text=True,
capture_output=True,
shell=True,
timeout=time_limit_int + 1
)
except Exception as e:
print(e)
return ""
# Return the output or error from the execution
if result.returncode != 0:
return f"Execution failed: {result.stderr}"
return result.stdout
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():
if "选课" not in k:
continue
print(f"idx: {i} Start")
i+=1
correct_codes = v["correct_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 correct_codes:
if item['lang'] != "cpp":
continue
result = execute_cpp_code(item['code'], input_string, compileAndRunOptions, 0)
actual_lines = [line.strip() for line in result.splitlines()]
actual_lines = [line for line in actual_lines if line]
actual_lines = (" ".join(actual_lines)).strip()
expected_lines = [line.strip() for line in output_string.splitlines()]
expected_lines = [line for line in expected_lines if line]
expected_lines = (" ".join(expected_lines)).strip()
### 小数保留 6 位
if is_decimal(actual_lines) and is_decimal(expected_lines):
if abs(float(actual_lines) - float(expected_lines)) < 1e-6:
pass_code_list.append(item['code'])
else:
print(1)
continue
if actual_lines == expected_lines:
pass_code_list.append(item['code'])
else:
print(1)
save_dict[k] = pass_code_list
# break
# json.dump(save_dict, open(f"/home/i-luoxianzhen/data/TestCase-Gen/data/Ours/correct_code.json", "w", encoding="utf-8"), indent=4, ensure_ascii=False)
# 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"))
# total_c = 0
# save_c = 0
# for k, v in new_wrong_code.items():
# total_c += len([item for item in v['correct_codes'] if item['lang'] == "cpp"])
# save_c_code = json.load(open("/home/i-luoxianzhen/data/TestCase-Gen/data/Ours/correct_code.json", "r", encoding="utf-8"))
# for k, v in save_c_code.items():
# save_c += len(v)
# print(f"saved {save_c}")
# print(f"saved {total_c}")

Xet Storage Details

Size:
4.61 kB
·
Xet hash:
8da0c76beae3b9db5a51d5994e0e4cea6fcd7c8ef68b428c743d2abe5c0207f9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.