1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| import subprocess import sys import os
def compare_files(file1, file2): with open(file1, 'r') as f1, open(file2, 'r') as f2: lines1 = [line.rstrip() for line in f1] lines2 = [line.rstrip() for line in f2] while lines1 and lines1[-1] == '': lines1.pop() while lines2 and lines2[-1] == '': lines2.pop() return lines1 == lines2
try: script_dir = os.path.dirname(os.path.abspath(__file__)) for t in range(1, 10001): subprocess.run( [os.path.join(script_dir, 'D-datamaker')], check=True, cwd=script_dir, stdout=open(os.path.join(script_dir, 'test.in'), 'w') ) subprocess.run( [os.path.join(script_dir, 'sol1')], check=True, cwd=script_dir, stdin=open(os.path.join(script_dir, 'test.in'), 'r'), stdout=open(os.path.join(script_dir, 'sol1.out'), 'w') ) subprocess.run( [os.path.join(script_dir, 'sol2')], check=True, cwd=script_dir, stdin=open(os.path.join(script_dir, 'test.in'), 'r'), stdout=open(os.path.join(script_dir, 'sol2.out'), 'w') ) if not compare_files( os.path.join(script_dir, 'sol1.out'), os.path.join(script_dir, 'sol2.out') ): print("Error: Outputs differ!") sys.exit(1) else: print(f"Test case {t} completed.") pass
except subprocess.CalledProcessError as e: print(f"Process error: {e}") sys.exit(1) except KeyboardInterrupt: print("\nStopped by user") sys.exit(0)
|