#!/usr/bin/env python # -*- coding: utf-8 -*- """ 验证程序测试脚本 用于测试自定义验证程序是否工作正常 """ import os import subprocess import tempfile def test_float_validator(): """测试浮点数验证程序""" print("测试浮点数验证程序...") # 创建测试文件 input_file = tempfile.NamedTemporaryFile(mode='w', delete=False) input_file.write("1.0") input_file.close() expected_file = tempfile.NamedTemporaryFile(mode='w', delete=False) expected_file.write("3.14159") expected_file.close() result_file = tempfile.NamedTemporaryFile(mode='w', delete=False) result_file.write("3.14159001") # 轻微误差 result_file.close() try: # 编译验证程序 cmd = ['g++', '-o', 'float_validator', 'float_validator.cpp'] compile_result = subprocess.run(cmd, capture_output=True, text=True) if compile_result.returncode != 0: print("编译失败:", compile_result.stderr) return # 运行验证程序 cmd = ['./float_validator', input_file.name, expected_file.name, result_file.name] run_result = subprocess.run(cmd, capture_output=True, text=True) print("验证结果:", run_result.stdout.strip()) # 清理 if os.path.exists('float_validator'): os.remove('float_validator') finally: # 清理临时文件 for f in [input_file, expected_file, result_file]: if os.path.exists(f.name): os.unlink(f.name) def test_multi_solution_validator(): """测试多解验证程序""" print("测试多解验证程序...") # 创建测试文件 input_file = tempfile.NamedTemporaryFile(mode='w', delete=False) input_file.write("") input_file.close() expected_file = tempfile.NamedTemporaryFile(mode='w', delete=False) expected_file.write("1 2 3 4 5") expected_file.close() result_file = tempfile.NamedTemporaryFile(mode='w', delete=False) result_file.write("5 4 3 2 1") # 相同元素,不同顺序 result_file.close() try: # 运行验证程序 cmd = ['python3', 'multi_solution_validator.py', input_file.name, expected_file.name, result_file.name] run_result = subprocess.run(cmd, capture_output=True, text=True) print("验证结果:", run_result.stdout.strip()) finally: # 清理临时文件 for f in [input_file, expected_file, result_file]: if os.path.exists(f.name): os.unlink(f.name) def main(): """主测试函数""" print("=== 自定义验证程序测试 ===") # 切换到示例目录 os.chdir('/Users/liushuming/Desktop/bitoj/examples') test_float_validator() print() test_multi_solution_validator() print("\n测试完成!") if __name__ == "__main__": main()