Fixed timing of tcs
This commit is contained in:
parent
afc31abe30
commit
dfb26e14c9
47
ucompile
47
ucompile
@ -4,6 +4,7 @@ import os
|
|||||||
from glob import glob
|
from glob import glob
|
||||||
import subprocess
|
import subprocess
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
def check_dependencies() -> None:
|
def check_dependencies() -> None:
|
||||||
@ -15,11 +16,21 @@ def check_dependencies() -> None:
|
|||||||
print(f"{Fore.RED}Error: {command} not found{Style.RESET_ALL}")
|
print(f"{Fore.RED}Error: {command} not found{Style.RESET_ALL}")
|
||||||
return
|
return
|
||||||
|
|
||||||
def run_input_cases(program, verbose, output=False, tests_dir=".", time=False):
|
|
||||||
|
def run_input_cases(program, verbose, output=False, tests_dir=".", time_flag=False):
|
||||||
input_files = glob(os.path.join(tests_dir, "*.in"))
|
input_files = glob(os.path.join(tests_dir, "*.in"))
|
||||||
|
|
||||||
if len(input_files) < 1:
|
if len(input_files) < 1:
|
||||||
return os.system(program)
|
if time_flag:
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
result = subprocess.run(program, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
elapsed_time = end_time - start_time
|
||||||
|
print(result.stdout.strip())
|
||||||
|
print(f"Time taken: {elapsed_time:.4f} seconds")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return os.system(program)
|
||||||
|
|
||||||
for input_file in input_files:
|
for input_file in input_files:
|
||||||
print(f"Running {input_file}")
|
print(f"Running {input_file}")
|
||||||
@ -34,9 +45,15 @@ def run_input_cases(program, verbose, output=False, tests_dir=".", time=False):
|
|||||||
with open(out_file, 'r') as f:
|
with open(out_file, 'r') as f:
|
||||||
expected_output = f.read()
|
expected_output = f.read()
|
||||||
|
|
||||||
if time:
|
# Measure time if time_flag is True
|
||||||
end_command = f"time {end_command}"
|
if time_flag:
|
||||||
result = subprocess.run(end_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
start_time = time.perf_counter()
|
||||||
|
result = subprocess.run(end_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
elapsed_time = end_time - start_time
|
||||||
|
else:
|
||||||
|
result = subprocess.run(end_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||||
|
|
||||||
program_output = result.stdout
|
program_output = result.stdout
|
||||||
|
|
||||||
if verbose and not output:
|
if verbose and not output:
|
||||||
@ -51,7 +68,6 @@ def run_input_cases(program, verbose, output=False, tests_dir=".", time=False):
|
|||||||
print(program_output.strip())
|
print(program_output.strip())
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
|
||||||
if output:
|
if output:
|
||||||
if expected_output is not None:
|
if expected_output is not None:
|
||||||
# Compare the outputs
|
# Compare the outputs
|
||||||
@ -73,34 +89,42 @@ def run_input_cases(program, verbose, output=False, tests_dir=".", time=False):
|
|||||||
if not verbose:
|
if not verbose:
|
||||||
print(program_output.strip())
|
print(program_output.strip())
|
||||||
|
|
||||||
|
if time_flag:
|
||||||
|
print(f"Time taken: {elapsed_time:.4f} seconds")
|
||||||
|
|
||||||
print("-----------")
|
print("-----------")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def python_compile(file_name, verbose=False, tests_dir=".", time=False):
|
def python_compile(file_name, verbose=False, tests_dir=".", time=False):
|
||||||
run_input_cases("python3 " + file_name, verbose, tests_dir=tests_dir, time=time)
|
run_input_cases("python3 " + file_name, verbose, tests_dir=tests_dir, time_flag=time)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def java_compile(file_name, maven=False, verbose=False, tests_dir=".", time=False):
|
def java_compile(file_name, maven=False, verbose=False, tests_dir=".", time=False):
|
||||||
if maven:
|
if maven:
|
||||||
os.system("mvn clean verify")
|
os.system("mvn clean verify")
|
||||||
os.system("java -jar target/*.jar")
|
run_input_cases("java -jar target/*.jar", verbose, tests_dir=tests_dir, time_flag=time)
|
||||||
return
|
return
|
||||||
os.system("javac " + file_name)
|
os.system("javac " + file_name)
|
||||||
run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir, time=time)
|
run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir, time_flag=time)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def haskell_compile(file_name, verbose=False, tests_dir=".", time=False):
|
def haskell_compile(file_name, verbose=False, tests_dir=".", time=False):
|
||||||
os.system("ghc -O2 " + file_name + " -o program")
|
os.system("ghc -O2 " + file_name + " -o program")
|
||||||
run_input_cases("./program", verbose, tests_dir=tests_dir, time=time)
|
run_input_cases("./program", verbose, tests_dir=tests_dir, time_flag=time)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def c_compile(file_name, verbose=False, output=False, tests_dir=".", time=False):
|
def c_compile(file_name, verbose=False, output=False, tests_dir=".", time=False):
|
||||||
# Style the code in Meijster's way
|
# Style the code in Meijster's way
|
||||||
os.system("astyle -A2s2cxgk3W3xbj " + file_name)
|
os.system("astyle -A2s2cxgk3W3xbj " + file_name)
|
||||||
os.system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name)
|
os.system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name)
|
||||||
run_input_cases("./program", verbose, output, tests_dir=tests_dir, time=time)
|
run_input_cases("./program", verbose, output, tests_dir=tests_dir, time_flag=time)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
SUPPORTED_LANGS = [".c", ".py", ".java", ".hs"]
|
SUPPORTED_LANGS = [".c", ".py", ".java", ".hs"]
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@ -134,6 +158,7 @@ def main():
|
|||||||
haskell_compile(file_name, args.verbose, tests_dir=tests_dir, time=args.time)
|
haskell_compile(file_name, args.verbose, tests_dir=tests_dir, time=args.time)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
check_dependencies()
|
check_dependencies()
|
||||||
main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user