4-bit virtual CPU
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

59 lines
1.7 KiB

#!/usr/bin/env python3
import os
import subprocess
from tempfile import NamedTemporaryFile
import cv2
class AssemblerAcceptanceTest:
def __init__(self, source, binary):
self.source_code = os.path.join(os.getcwd(), "acceptance-tests", source)
self.expected_binary = os.path.join(os.getcwd(), "acceptance-tests", binary)
def __str__(self):
return f'acceptance_test(asm("{self.source_code}") → {self.expected_binary})'
def run(self, toyasm):
tmp_file = NamedTemporaryFile(delete=False)
expected_binary = cv2.imread(self.expected_binary)
result = subprocess.run([toyasm, self.source_code, tmp_file.name])
assert result.returncode == 0
compiled_binary = tmp_file.read()
for i in range(len(expected_binary[0])):
assert compiled_binary[i] == expected_binary[0][i][0]
for i in range(len(expected_binary[0]), 256):
assert compiled_binary[i] == 0
os.unlink(tmp_file.name)
print(f'OK: asm("{self.source_code}") == "{self.expected_binary}"')
class AcceptanceTestsRunner:
def __init__(self):
self.toyasm = self.prepare_tool("toyasm")
self.toyvm = self.prepare_tool("toyvm")
self.tests = [
AssemblerAcceptanceTest("add_four_ints.asm", "add_four_ints.pgm")
]
def prepare_tool(self, name):
tool_path = os.path.join(os.getcwd(), "target", "release", name)
if not os.path.exists(tool_path):
raise FileNotFoundError(tool_path)
return tool_path
def run(self):
for test in self.tests:
test.run(self.toyasm)
def main():
runner = AcceptanceTestsRunner()
runner.run()
if __name__ == '__main__':
main()