marmoset-test-cases/run-tests.py

37 lines
1.6 KiB
Python
Raw Normal View History

2023-09-11 20:51:15 +01:00
#!/bin/python3
import glob
import subprocess
import os
2023-09-12 13:24:27 +01:00
import sys
2023-09-11 20:51:15 +01:00
# will break if there are namespace collisions
2023-09-12 14:13:44 +01:00
if os.path.isfile("a.out"):
raise FileExistsError # TODO: generate uuid .out
2023-09-12 13:49:39 +01:00
file_list = glob.glob(f"{os.path.dirname(__file__)}/**/*.cases", recursive=True)
file_only_list = [os.path.basename(x.rstrip(".cases")) for x in file_list]
2023-09-11 20:51:15 +01:00
2023-09-12 13:24:27 +01:00
if sys.platform == "win32":
2023-09-12 13:30:10 +01:00
os.system("color") # enables ANSI escape characters, bug in Python
2023-09-12 13:24:27 +01:00
2023-09-11 20:51:15 +01:00
for file_name in glob.glob("*.c", recursive=False):
if file_name.rstrip(".c") in file_only_list:
2023-09-12 14:10:27 +01:00
should_continue = input(f"{file_name}; run (y/N)? ").lower()
2023-09-12 13:31:53 +01:00
if should_continue != "y":
2023-09-11 20:51:15 +01:00
continue
subprocess.run(["gcc", "-o", "a.out", "-std=c11", "-Wall", "-g", file_name], check=True)
2023-09-12 13:49:39 +01:00
with open(next(x for x in file_list if file_name.rstrip(".c") in x)) as f:
2023-09-11 20:51:15 +01:00
to_run = f.read().split("\n\n")
total_tests, passed_tests = 0, 0
2023-09-12 13:59:47 +01:00
for x in to_run:
2023-09-12 13:59:17 +01:00
x_split = x.split("\n")
2023-09-12 14:04:53 +01:00
return_data = subprocess.run(["./a.out"], input=x_split[0].rstrip().encode(), capture_output=True).stdout.decode().rstrip()
test_passed_int = int(return_data == x_split[1].rstrip())
total_tests += 1
passed_tests += test_passed_int
2023-09-13 15:01:43 +01:00
print(f"({x_split[0]})", x_split[1], f"\u001b[{31 + test_passed_int}m{return_data}\u001b[0m")
print(f"\u001b[32m{passed_tests} tests passed\u001b[0m, \u001b[{31 - 31*int(total_tests == passed_tests)}m{total_tests - passed_tests} tests failed\u001b[0m")
2023-09-12 14:10:27 +01:00
os.remove("a.out")