diff options
Diffstat (limited to 'testsuite/pyunit')
-rw-r--r-- | testsuite/pyunit/SimpleEntity.vhdl | 27 | ||||
-rw-r--r-- | testsuite/pyunit/SimplePackage.vhdl | 21 | ||||
-rw-r--r-- | testsuite/pyunit/__init__.py | 14 | ||||
-rw-r--r-- | testsuite/pyunit/dom/SimpleEntity.py | 46 | ||||
-rw-r--r-- | testsuite/pyunit/dom/__init__.py | 13 | ||||
-rw-r--r-- | testsuite/pyunit/libghdl/Initialize.py | 62 | ||||
-rw-r--r-- | testsuite/pyunit/libghdl/__init__.py | 13 |
7 files changed, 196 insertions, 0 deletions
diff --git a/testsuite/pyunit/SimpleEntity.vhdl b/testsuite/pyunit/SimpleEntity.vhdl new file mode 100644 index 000000000..a26a6357c --- /dev/null +++ b/testsuite/pyunit/SimpleEntity.vhdl @@ -0,0 +1,27 @@ +library ieee; +use ieee.numeric_std.all; + +entity e1 is + generic ( + BITS : positive := 8 + ); + port ( + Clock: in std_logic; + Reset: in std_logic; + Q: out std_logic_vector(BITS - 1 downto 0) + ); +end entity e1; + +architecture behav of e1 is +begin + process(Clock) + begin + if rising_edge(Clock) then + if Reset = '1' then + Q <= (others => '0'); + else + Q <= std_logic_vector(unsigned(Q) + 1); + end if; + end if; + end process; +end architecture behav; diff --git a/testsuite/pyunit/SimplePackage.vhdl b/testsuite/pyunit/SimplePackage.vhdl new file mode 100644 index 000000000..f06cc32fa --- /dev/null +++ b/testsuite/pyunit/SimplePackage.vhdl @@ -0,0 +1,21 @@ +library ieee; +use ieee.numeric_std.all + +package pack_1 is + constant const_1 : boolean; + + type matrix is array(natural range <>, natural range <>) of std_logic; + + subtype matrix8x8 is matrix(7 downto 0, 7 downto 0); + + function func1(value : unsigned) return natural; +end package; + +package body pack_1 is + constant const_1 : boolean := true; + + function func1(value : unsigned) return natural is + begin + return to_integer(value); + end function; +end package body; diff --git a/testsuite/pyunit/__init__.py b/testsuite/pyunit/__init__.py new file mode 100644 index 000000000..eff53eb2f --- /dev/null +++ b/testsuite/pyunit/__init__.py @@ -0,0 +1,14 @@ +from unittest import TestSuite + +try: + from testsuite.pyunit import libghdl, dom +except ModuleNotFoundError: + from pyunit import libghdl, dom + +def load_tests(loader, testCases, pattern): + suite = TestSuite() + + suite.addTests(loader.loadTestsFromModule(libghdl)) + suite.addTests(loader.loadTestsFromModule(dom)) + + return suite diff --git a/testsuite/pyunit/dom/SimpleEntity.py b/testsuite/pyunit/dom/SimpleEntity.py new file mode 100644 index 000000000..2f65c9813 --- /dev/null +++ b/testsuite/pyunit/dom/SimpleEntity.py @@ -0,0 +1,46 @@ +from pathlib import Path +from unittest import TestCase + +from pyGHDL.dom.Misc import Design, Library, Document + + +if __name__ == "__main__": + print("ERROR: you called a testcase declaration file as an executable module.") + print("Use: 'python -m unitest <testcase module>'") + exit(1) + + +class SimpleEntity(TestCase): + _root = Path(__file__).resolve().parent.parent + _filename : Path = _root / "SimpleEntity.vhdl" + + def test_Design(self): + design = Design() + + self.assertIsNotNone(design) + + # def test_Library(self): + # library = Library() + + def test_Document(self): + design = Design() + document = Document(self._filename) + design.Documents.append(document) + + self.assertTrue(len(design.Documents) == 1) + + def test_Entity(self): + design = Design() + document = Document(self._filename) + design.Documents.append(document) + + self.assertEqual(len(design.Documents[0].Entities), 1) + self.assertTrue(design.Documents[0].Entities[0].Name == "e1") + + def test_Architecture(self): + design = Design() + document = Document(self._filename) + design.Documents.append(document) + + self.assertEqual(len(design.Documents[0].Architectures), 1) + self.assertTrue(design.Documents[0].Architectures[0].Name == "behav") diff --git a/testsuite/pyunit/dom/__init__.py b/testsuite/pyunit/dom/__init__.py new file mode 100644 index 000000000..9c103eb6a --- /dev/null +++ b/testsuite/pyunit/dom/__init__.py @@ -0,0 +1,13 @@ +from unittest import TestSuite + +try: + from testsuite.pyunit.dom import SimpleEntity +except ModuleNotFoundError: + from pyunit.dom import SimpleEntity + +def load_tests(loader, testCases, pattern): + suite = TestSuite() + + suite.addTests(loader.loadTestsFromModule(SimpleEntity)) + + return suite diff --git a/testsuite/pyunit/libghdl/Initialize.py b/testsuite/pyunit/libghdl/Initialize.py new file mode 100644 index 000000000..0a46fe8b4 --- /dev/null +++ b/testsuite/pyunit/libghdl/Initialize.py @@ -0,0 +1,62 @@ +from pathlib import Path +from unittest import TestCase + +import pyGHDL.libghdl as libghdl +from pyGHDL.libghdl import name_table, files_map, errorout_console +from pyGHDL.libghdl.vhdl import nodes, sem_lib + + +if __name__ == "__main__": + print("ERROR: you called a testcase declaration file as an executable module.") + print("Use: 'python -m unitest <testcase module>'") + exit(1) + + +class Instantiate(TestCase): + _root = Path(__file__).resolve().parent.parent + _filename : Path = _root / "SimpleEntity.vhdl" + + @staticmethod + def getIdentifier(node): + """Return the Python string from node :param:`node` identifier""" + return name_table.Get_Name_Ptr(nodes.Get_Identifier(node)).decode("utf-8") + + def test_InitializeGHDL(self) -> None: + """Initialization: set options and then load libaries""" + + # Print error messages on the console. + errorout_console.Install_Handler() + + # Set options. This must be done before analyze_init() + libghdl.set_option(b"--std=08") + + # Finish initialization. This will load the standard package. + if libghdl.analyze_init_status() != 0: + self.fail("libghdl initialization error") + + # Load the file + file_id = name_table.Get_Identifier(str(self._filename).encode("utf_8")) + sfe = files_map.Read_Source_File(name_table.Null_Identifier, file_id) + if sfe == files_map.No_Source_File_Entry: + self.fail("Cannot read file '{!s}'".format(self._filename)) + + # Parse + file = sem_lib.Load_File(sfe) + + # Display all design units + designUnit = nodes.Get_First_Design_Unit(file) + while designUnit != nodes.Null_Iir: + libraryUnit = nodes.Get_Library_Unit(designUnit) + + if nodes.Get_Kind(libraryUnit) == nodes.Iir_Kind.Entity_Declaration: + entityName = self.getIdentifier(libraryUnit) + self.assertEqual(entityName, "e1", "expected entity name 'e1', got '{}'".format(entityName)) + + elif nodes.Get_Kind(libraryUnit) == nodes.Iir_Kind.Architecture_Body: + architectureName = self.getIdentifier(libraryUnit) + self.assertEqual(architectureName, "behav", "expected architecture name 'behav', got '{}'".format(architectureName)) + + else: + self.fail("Unknown unit.") + + designUnit = nodes.Get_Chain(designUnit) diff --git a/testsuite/pyunit/libghdl/__init__.py b/testsuite/pyunit/libghdl/__init__.py new file mode 100644 index 000000000..4aeab3ec3 --- /dev/null +++ b/testsuite/pyunit/libghdl/__init__.py @@ -0,0 +1,13 @@ +from unittest import TestSuite + +try: + from testsuite.pyunit.libghdl import Initialize +except ModuleNotFoundError: + from pyunit.libghdl import Initialize + +def load_tests(loader, testCases, pattern): + suite = TestSuite() + + suite.addTests(loader.loadTestsFromModule(Initialize)) + + return suite |