Files
PeakRDL-python-regmap/tests/base.py
Byron Lathi b43de9206b
All checks were successful
build / test (3.10) (push) Successful in 6s
build / test (3.11) (push) Successful in 6s
build / test (3.12) (push) Successful in 6s
build / test (3.13) (push) Successful in 6s
build / test (3.9) (push) Successful in 6s
build / lint (push) Successful in 7s
build / mypy (push) Successful in 8s
build / test (3.10) (release) Successful in 6s
build / test (3.11) (release) Successful in 6s
build / test (3.12) (release) Successful in 6s
build / test (3.13) (release) Successful in 6s
build / test (3.9) (release) Successful in 6s
build / lint (release) Successful in 8s
build / mypy (release) Successful in 8s
build / Build distributions (push) Successful in 7s
build / Build distributions (release) Successful in 8s
build / deploy (push) Has been skipped
build / deploy (release) Successful in 6s
Create project
2025-11-23 17:05:27 -08:00

84 lines
2.3 KiB
Python

from unittest import TestCase
import os
import subprocess
from itertools import product
from systemrdl import RDLCompiler
from peakrdl_python_regmap.exporter import PythonRegmapExporter
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 = PythonRegmapExporter()
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()