From 174bb14124d909bed93a17119be8c0a60f2717a0 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Tue, 27 Nov 2018 16:41:46 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ opbtest/__init__.py | 6 ++++++ opbtest/opbtestcase.py | 48 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 11 ++++++++++ test/basicasm.py | 13 ++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 opbtest/__init__.py create mode 100644 opbtest/opbtestcase.py create mode 100644 setup.py create mode 100644 test/basicasm.py diff --git a/.gitignore b/.gitignore index 894a44c..f4e6946 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.idea + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/opbtest/__init__.py b/opbtest/__init__.py new file mode 100644 index 0000000..7389d7b --- /dev/null +++ b/opbtest/__init__.py @@ -0,0 +1,6 @@ + +# use "import opbtest" and extend from OpbTestCase: +# from opbtest import OpbTestCase +# class TestMyStuff(OpbTestCase): + +from .opbtestcase import OpbTestCase diff --git a/opbtest/opbtestcase.py b/opbtest/opbtestcase.py new file mode 100644 index 0000000..4a1d0b4 --- /dev/null +++ b/opbtest/opbtestcase.py @@ -0,0 +1,48 @@ + +from unittest import TestCase +import subprocess, os, time, json, glob + +class OpbTestAssertions(): + def __init__(self, json, case): + self.jsondata = json + self.case = case + self.register = None + + def reg(self, register): + self.register = register + return self + + def contains(self, expected): + if self.register is None: + self.case.fail("First call reg() to assert which register to check!") + + self.case.assertReg(self.jsondata, "a", int(self.register[1], 16), expected) + +class OpbTestCase(TestCase): + + def tearDown(self): + for file in glob.glob('tmp_*'): + pass + os.remove(file) + + def execute_psm(self, psm): + with open("tmp_" + str(time.time()) + ".psm4", "w") as file: + file.write(psm) + + r = subprocess.call("opbasm -6 -c " + file.name, shell=True) + self.assertTrue(r == 0, "Opbasm compilation failed of source: " + psm) + + try: + json_out = subprocess.check_output("opbsim -v -j -m:" + file.name.replace(".psm4", ".mem"), shell=True) + except subprocess.CalledProcessError as e: + self.fail("Opbsim simulation failed with: " + '\n>>> '.join(('\n' + str(e.output)).splitlines()).lstrip()) + + json_out = json.loads(json_out) + self.assertTrue(json_out['termination'] == 'termNormal', 'Simulation failed with ' + json_out['termination']) + return json_out + + def assertPsm(self, jsondata): + return OpbTestAssertions(jsondata, self) + + def assertReg(self, jsondata, bank, nr, expected): + self.assertEqual(expected, jsondata["regs_" + bank][nr]) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a3d4b77 --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +from setuptools import setup + +setup(name='opbtest', + version='0.1', + description='Open PicoBlaze Assembler Test package', + url='https://github.com/wgroeneveld/opbtest', + author='Wouter Groeneveld', + author_email='wouter.groeneveld@kuleuven.be', + license='MIT', + packages=['opbtest'], + zip_safe=False) \ No newline at end of file diff --git a/test/basicasm.py b/test/basicasm.py new file mode 100644 index 0000000..c4e96b5 --- /dev/null +++ b/test/basicasm.py @@ -0,0 +1,13 @@ + + +from opbtest import OpbTestCase + +class TestBasicAsm(OpbTestCase): + + def test_basic_register_loading(self): + psm = """load s0, 1 +output sD, FF""" + + result = self.execute_psm(psm) + + self.assertPsm(result).reg("s0").contains(1)