#!/usr/bin/python3 import os from subprocess import run, DEVNULL import sys fields = [ "count", "n", "z", "p", "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" ] def parse_dump(dump): lines = list(filter(None, dump.split('\n')))[2:] state = {} state["count"] = lines.pop(0).split()[-1] state["pc"] = lines.pop(0).split()[-1] cond = lines.pop(0).split() state["n"], state["z"], state["p"] = cond[3], cond[6], cond[9] lines.pop(0) for i in range(8): state["r" + str(i)] = lines.pop(0).split()[-1] state["mem"] = {} while lines: lines.pop(0) lines.pop(0) content = lines.pop(0).lower().split() state["mem"][content[0]] = content[-1] return state def run_test(filename): expected = [] print("Testing {}...".format(filename)) with open(filename) as f: end = False for line in f: line = line.strip().lower() if end: left, right = line.split() expected.append((left, right)) elif line == "assert": end = True outfile = filename.split('.')[0] + ".out" run(["./../../Lab1/assembler", filename, outfile], check=True) run(["./sim", outfile], input=("go\nrdump\n" + ''.join("mdump {} {}\n".format(i[0], i[0]) for i in expected if i[0] not in fields) + "quit\n").encode(), stdout=DEVNULL, check=True) with open("dumpsim") as f: results = parse_dump(f.read()) for i in expected: actual = results[i[0]] if i[0] in fields else results["mem"][i[0]] if int(i[1], 0) & 0xFFFF != int(actual, 0): print("\033[91mAssertion {} = {} failed; Got {}\033[0m".format( i[0], i[1], actual)) run("clang -std=c99 -pedantic -Wall ../lc3bsim2.c -o sim".split(), check=True) if len(sys.argv) > 1: for i in sys.argv[1:]: run_test(i) else: for filename in os.listdir(): if filename.endswith(".asm"): run_test(filename)