#!/usr/bin/env python from pathlib import Path from subprocess import run tests_1: dict = { "2 1" : "1", "3 2" : "1", "2 4" : "2", "1769 427" : "61", "698917 -102089" : "7853", "917 872" : "1", "-417143 -455947" : "9701", "63862 -55298" : "2", "702228 373125" : "3", "-659323 727699" : "7" } def test(file_name: str, compile_cmd: str, run_cmd: list[str], tests_dict: dict): print(f"=== TESTING {file_name.upper()} ===") file_path = Path(file_name) if not file_path.is_file(): print(f"file '{file_name}' was not found.") return print(f"{file_name} found") run([compile_cmd, file_name], check=True) for t, res in tests_dict.items(): out = run( run_cmd, input=t, text=True, capture_output=True ) program_result = out.stdout.strip() if program_result != res: print("test failed:") print(f"\ttest: {t}") print(f"\tprogram result: {program_result}") print(f"\tcorrect result: {res}") break else: print("test passed:") print(f"\ttest: {t}") print(f"\tresult: {res}") else: print("all tests passed!") def main(): test("solution.c", "gcc", ["./a.out"], tests_1) test("solution.cpp", "g++", ["./a.out"], tests_1) if __name__ == "__main__": main()