Skip to content

Instantly share code, notes, and snippets.

@techtide
Created April 21, 2023 19:36
Show Gist options
  • Save techtide/b9e6c59184edd7ad008bb7d6570b677d to your computer and use it in GitHub Desktop.
Save techtide/b9e6c59184edd7ad008bb7d6570b677d to your computer and use it in GitHub Desktop.
""" expected directory structure
├── input
│   ├── input01.txt
│   ├── ...
├── output
│   ├── output01.txt
│   ├── ...
├── tester.py
└── two_sat.py
"""
import os
import sys
# test files
INPUTS = "input/"
OUTPUTS = "output/"
SCRIPT_NAME = "two_sat.py"
input_files = sorted([os.path.join(INPUTS, entry) for entry in os.listdir(INPUTS) if os.path.isfile(os.path.join(INPUTS, entry))])
output_files = sorted([os.path.join(OUTPUTS, entry) for entry in os.listdir(INPUTS) if os.path.isfile(os.path.join(INPUTS, entry))])
# build array of commands to test
test_commands = ["cat {0} | python3 {1}".format(infile, SCRIPT_NAME) for infile in input_files]
def runtest(testnum, quiet=False):
cmd = test_commands[testnum]
filenum = int(cmd.split("input/")[1].split("input")[1].split(".txt")[0])
lines = []
with os.popen(cmd) as pse:
lines = [p.strip() for p in list(pse)]
output_file = "output/output{:02d}.txt".format(filenum)
out_lines = []
with open(output_file) as f:
for line in f:
out_lines.append(line.strip())
str2bool = lambda s: True if s=="True" else False
outline_bools = [str2bool(o.split(" ")[1]) for o in out_lines]
line_bools = [str2bool(o.split(" ")[1]) for o in lines]
num_conflicts = 0
for i in range(min(len(lines), len(out_lines))):
inline = line_bools[i]
outline = outline_bools[i]
if inline != outline:
if not quiet:
print("====================")
print("Conflict @ Line", i)
print("Program Gave:", lines[i])
print("But Expected:", out_lines[i])
print("====================")
num_conflicts += 1
return num_conflicts > 0
passed_param = sys.argv[1]
if passed_param == "all":
num_files_w_conflicts = 0
conflict_flies = []
for i in range(len(input_files)):
print("TESTING:", input_files[i])
conflicts = runtest(i, quiet=True)
if conflicts:
num_files_w_conflicts += 1
conflict_flies.append(input_files[i])
print("================")
print(num_files_w_conflicts, "tests failed")
print("failed:", conflict_flies)
elif not passed_param.isdigit():
print("argument must either be 'all' for all tests, or i for the i-th test")
else:
print("TESTING:", input_files[int(passed_param)])
runtest(int(passed_param))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment