from unittest import TestCase import os import subprocess from itertools import product from systemrdl import RDLCompiler from peakrdl_python.exporter import PythonExporter def get_permutations(spec): param_list = [] for v in product(*spec.values()): param_list.append(dict(zip(spec, v))) return param_list class BaseHeaderTestcase(TestCase): rdl_file = "" @classmethod def get_run_dir(cls) -> str: this_dir = os.path.dirname(__file__) run_dir = os.path.join(this_dir, "test.out", cls.__name__) return run_dir @property def output_dir(self) -> str: return self.get_run_dir() @classmethod def _write_params(cls) -> None: """ Write out the class parameters to a file so that it is easier to debug how a testcase was parameterized """ path = os.path.join(cls.get_run_dir(), "params.txt") with open(path, 'w') as f: for k, v in cls.__dict__.items(): if k.startswith("_") or callable(v): continue f.write(f"{k}: {repr(v)}\n") def do_export(self): os.makedirs(self.output_dir, exist_ok=True) rdl_path = os.path.join(os.path.dirname(__file__), self.rdl_file) rdlc = RDLCompiler() rdlc.compile_file(rdl_path) top_node = rdlc.elaborate() x = PythonExporter() x.export( top_node, path=os.path.join(self.output_dir, "out.h"), ) self._write_params() def do_compile(self): args = [ "gcc", "--std", self.std.value, os.path.join(self.output_dir, "out.h.test.c"), "-o", os.path.join(self.output_dir, "test.exe"), ] ret = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) print(" ".join(args)) print(ret.stdout.decode('utf-8')) self.assertEqual(ret.returncode, 0) def do_run(self): args = [os.path.join(self.output_dir, "test.exe")] ret = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) print(" ".join(args)) print(ret.stdout.decode('utf-8')) self.assertEqual(ret.returncode, 0) def do_test(self): return self.do_export() self.do_compile() self.do_run()