fx92-interpreter/tests.py

75 lines
1.7 KiB
Python
Executable File

#! /usr/bin/python3
import sys
import glob
import os.path
import subprocess
from PIL import Image
from PIL import ImageChops
def imgequal(ref, out):
ref = Image.open(ref)
out = Image.open(out)
diff = ImageChops.difference(ref, out)
return diff.getbbox() is None
def runtest(program, refout=None, refimg=None):
st = subprocess.run("./fx92.py --quiet --save=/tmp/fx92-test.bmp".split()
+ [program], stdout=subprocess.PIPE)
if st.returncode != 0:
print(" -> Execution FAILED!")
return 0
print(" Execution completed.")
success = True
if refout is not None:
with open(refout, "rb") as fp:
refout = fp.read()
if st.stdout == refout:
print(" Output is correct.")
else:
print(" -> Output is incorrect!")
print(" ref:", repr(refout))
print(" out:", repr(st.stdout))
success = False
if refimg is not None:
if imgequal(refimg, "/tmp/fx92-test.bmp"):
print(" Image is correct.")
else:
print(" -> Image is incorrect!")
success = False
return success
def main():
paths = glob.glob("tests/*.txt")
print("Found {} tests.".format(len(paths)))
total = 0
passed = 0
for t in paths:
print("\n{}:".format(t))
refout = t[:-4] + ".out"
refout = refout if os.path.isfile(refout) else None
refimg = t[:-4] + ".png"
refimg = refimg if os.path.isfile(refimg) else None
passed += runtest(t, refout=refout, refimg=refimg)
total += 1
if passed < total:
print("\nWarning, some tests failed!")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())