From 39f4e2eb94a191991b03a3c081c61bf0ff950a38 Mon Sep 17 00:00:00 2001 From: nanocoh Date: Sat, 12 Oct 2024 17:52:12 +0200 Subject: [PATCH 1/6] PySNLOccurrence --- src/snl/python/snl_wrapping/PySNL.cpp | 7 ++- .../python/snl_wrapping/PySNLOccurrence.cpp | 60 +++++++++++++++++++ src/snl/python/snl_wrapping/PySNLOccurrence.h | 32 ++++++++++ src/snl/snl/kernel/SNLOccurrence.cpp | 25 ++++++++ src/snl/snl/kernel/SNLOccurrence.h | 4 ++ .../python/snl_wrapping/test_snloccurrence.py | 54 +++++++++++++++++ 6 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 src/snl/python/snl_wrapping/PySNLOccurrence.cpp create mode 100644 src/snl/python/snl_wrapping/PySNLOccurrence.h create mode 100644 test/snl/python/snl_wrapping/test_snloccurrence.py diff --git a/src/snl/python/snl_wrapping/PySNL.cpp b/src/snl/python/snl_wrapping/PySNL.cpp index ccae038e..c95e6370 100644 --- a/src/snl/python/snl_wrapping/PySNL.cpp +++ b/src/snl/python/snl_wrapping/PySNL.cpp @@ -34,6 +34,7 @@ #include "PySNLInstances.h" #include "PySNLInstTerms.h" #include "PySNLPath.h" +#include "PySNLOccurrence.h" namespace PYSNL { @@ -73,6 +74,7 @@ PyMODINIT_FUNC PyInit_snl(void) { PySNLInstParameter_LinkPyType(); PySNLInstTerm_LinkPyType(); PySNLPath_LinkPyType(); + PySNLOccurrence_LinkPyType(); PySNLLibraries_LinkPyType(); PySNLDesigns_LinkPyType(); @@ -112,8 +114,9 @@ PyMODINIT_FUNC PyInit_snl(void) { PYTYPE_READY_SUB(SNLBusTermBit, SNLBitTerm); PYTYPE_READY_SUB(SNLInstance, SNLDesignObject); PYTYPE_READY_SUB(SNLInstTerm, SNLNetComponent); - PYTYPE_READY(SNLPath); + PYTYPE_READY(SNLPath); + PYTYPE_READY(SNLOccurrence) PYTYPE_READY(SNLLibraries); PYTYPE_READY(SNLLibrariesIterator); PYTYPE_READY(SNLDesigns); @@ -166,6 +169,7 @@ PyMODINIT_FUNC PyInit_snl(void) { Py_INCREF(&PyTypeSNLBusTermBit); Py_INCREF(&PyTypeSNLInstance); Py_INCREF(&PyTypeSNLPath); + Py_INCREF(&PyTypeSNLOccurrence); Py_INCREF(&PyTypeSNLInstTerm); Py_INCREF(&PyTypeSNLLibraries); Py_INCREF(&PyTypeSNLDesigns); @@ -214,6 +218,7 @@ PyMODINIT_FUNC PyInit_snl(void) { PyModule_AddObject(mod, "SNLInstParameter", (PyObject*)&PyTypeSNLInstParameter); PyModule_AddObject(mod, "SNLInstTerm", (PyObject*)&PyTypeSNLInstTerm); PyModule_AddObject(mod, "SNLPath", (PyObject*)&PyTypeSNLPath); + PyModule_AddObject(mod, "SNLOccurrence", (PyObject*)&PyTypeSNLOccurrence); PySNLTerm_postModuleInit(); PySNLNet_postModuleInit(); diff --git a/src/snl/python/snl_wrapping/PySNLOccurrence.cpp b/src/snl/python/snl_wrapping/PySNLOccurrence.cpp new file mode 100644 index 00000000..0fa54c2d --- /dev/null +++ b/src/snl/python/snl_wrapping/PySNLOccurrence.cpp @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: 2023 The Naja authors +// +// +// SPDX-License-Identifier: Apache-2.0 + +#include "PySNLOccurrence.h" +#include "PyInterface.h" +#include "SNLDesignObject.h" +#include "PySNLDesignObject.h" +#include "PySNLPath.h" +#include "SNLDesignObject.h" +#include "SNLPath.h" +#include "SNLOccurrence.h" + +namespace PYSNL { + +using namespace naja::SNL; + +#define METHOD_HEAD(function) GENERIC_METHOD_HEAD(SNLOccurrence, function) + +static int PySNLOccurrence_Init(PySNLOccurrence* self, PyObject* args, PyObject* kwargs) { + SNLOccurrence* snlPath = nullptr; + PyObject* arg0 = nullptr; + PyObject* arg1 = nullptr; + + //SNLOccurrence has three types of constructors: + if (not PyArg_ParseTuple(args, "|OO:SNLOccurrence", &arg0, &arg1)) { + setError("malformed SNLOccurrence create method"); + return -1; + } + if (arg0 == nullptr) { + snlPath = new SNLOccurrence(); + } else if (arg1 == nullptr) { + if (IsPySNLDesignObject(arg0)) { + snlPath = new SNLOccurrence(PYSNLDesignObject_O(arg0)); + } else { + setError("SNLOccurrence create accepts SNLDesignObject as only argument"); + return -1; + } + } else if (IsPySNLPath(arg0) and IsPySNLDesignObject(arg1)) { + snlPath = new SNLOccurrence(*PYSNLPath_O(arg0), PYSNLDesignObject_O(arg1)); + } else { + setError("invalid number of parameters for Path constructor."); + return -1; + } + self->object_ = snlPath; + return 0; +} + +ManagedTypeLinkCreateMethod(SNLOccurrence) +ManagedTypeDeallocMethod(SNLOccurrence) + +PyMethodDef PySNLOccurrence_Methods[] = { + {NULL, NULL, 0, NULL} /* sentinel */ +}; + +PyTypeManagedSNLObjectWithoutSNLIDLinkPyType(SNLOccurrence) +PyTypeObjectDefinitions(SNLOccurrence) + +} // namespace PYSNL \ No newline at end of file diff --git a/src/snl/python/snl_wrapping/PySNLOccurrence.h b/src/snl/python/snl_wrapping/PySNLOccurrence.h new file mode 100644 index 00000000..d4a18b60 --- /dev/null +++ b/src/snl/python/snl_wrapping/PySNLOccurrence.h @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2024 The Naja authors +// +// SPDX-License-Identifier: Apache-2.0 + +#ifndef __PY_SNL_OCCURRENCE_H_ +#define __PY_SNL_OCCURRENCE_H_ + +#include + +namespace naja::SNL { + class SNLOccurrence; +} + +namespace PYSNL { + +typedef struct { + PyObject_HEAD + naja::SNL::SNLOccurrence* object_; +} PySNLOccurrence; + +extern PyTypeObject PyTypeSNLOccurrence; + +extern PyObject* PySNLOccurrence_Link(naja::SNL::SNLOccurrence* occurrence); +extern void PySNLOccurrence_LinkPyType(); + +#define IsPySNLOccurrence(v) (PyObject_TypeCheck(v, &PyTypeSNLOccurrence)) +#define PYSNLOccurrence(v) ((PySNLOccurrence*)(v)) +#define PYSNLOccurrence_O(v) (PYSNLOccurrence(v)->object_) + +} // PYSNL namespace + +#endif // __PY_SNL_INSTANCE_H_ \ No newline at end of file diff --git a/src/snl/snl/kernel/SNLOccurrence.cpp b/src/snl/snl/kernel/SNLOccurrence.cpp index 878c6338..51ebac28 100644 --- a/src/snl/snl/kernel/SNLOccurrence.cpp +++ b/src/snl/snl/kernel/SNLOccurrence.cpp @@ -57,6 +57,18 @@ bool SNLOccurrence::operator<(const SNLOccurrence& rhs) const { ((*getObject() == *rhs.getObject()) and (getPath() < rhs.getPath())); } +bool SNLOccurrence::operator<=(const SNLOccurrence& rhs) const { + return (*this < rhs) or (*this == rhs); +} + +bool SNLOccurrence::operator>(const SNLOccurrence& rhs) const { + return not (*this <= rhs); +} + +bool SNLOccurrence::operator>=(const SNLOccurrence& rhs) const { + return not (*this < rhs); +} + SNLPath SNLOccurrence::getPath() const { if (path_) { return SNLPath(path_); @@ -64,4 +76,17 @@ SNLPath SNLOccurrence::getPath() const { return SNLPath(); } +std::string SNLOccurrence::getString() const { + std::ostringstream oss; + oss << "Occurrence: "; + if (object_) { + oss << object_->getDescription(); + } else { + oss << "null"; + } + oss << " at path: "; + oss << getPath().getString(); + return oss.str(); +} + }} // namespace SNL // namespace naja diff --git a/src/snl/snl/kernel/SNLOccurrence.h b/src/snl/snl/kernel/SNLOccurrence.h index 5f1bc13a..ddc2ae5a 100644 --- a/src/snl/snl/kernel/SNLOccurrence.h +++ b/src/snl/snl/kernel/SNLOccurrence.h @@ -45,6 +45,10 @@ class SNLOccurrence { bool operator==(const SNLOccurrence& occurrence) const; bool operator<(const SNLOccurrence& occurrence) const; + bool operator<=(const SNLOccurrence& occurrence) const; + bool operator>(const SNLOccurrence& occurrence) const; + bool operator>=(const SNLOccurrence& occurrence) const; + std::string getString() const; #if 0 // Following methods can be removed if SNLOccurrence inherits diff --git a/test/snl/python/snl_wrapping/test_snloccurrence.py b/test/snl/python/snl_wrapping/test_snloccurrence.py new file mode 100644 index 00000000..1abb9744 --- /dev/null +++ b/test/snl/python/snl_wrapping/test_snloccurrence.py @@ -0,0 +1,54 @@ +# SPDX-FileCopyrightText: 2024 The Naja authors +# +# SPDX-License-Identifier: Apache-2.0 + +import unittest +import snl + +class SNLOccurrenceTest(unittest.TestCase): + def setUp(self): + universe = snl.SNLUniverse.create() + db = snl.SNLDB.create(universe) + lib = snl.SNLLibrary.create(db) + self.top = snl.SNLDesign.create(lib) + self.model = snl.SNLDesign.create(lib) + self.submodel = snl.SNLDesign.create(lib, "submodel") + + def tearDown(self): + if snl.SNLUniverse.get(): + snl.SNLUniverse.get().destroy() + + def testFunctions(self): + ins2 = snl.SNLInstance.create(self.model, self.submodel, "ins2") + ins1 = snl.SNLInstance.create(self.top, self.model, "ins1") + + path0 = snl.SNLPath() + print(path0) + self.assertIsNotNone(path0) + self.assertTrue(path0.empty()) + self.assertEqual(0, path0.size()) + self.assertEqual(snl.SNLPath(), path0.getHeadPath()) + + path1 = snl.SNLPath(ins1) + self.assertIsNotNone(path1) + self.assertFalse(path1.empty()) + self.assertEqual(1, path1.size()) + self.assertEqual(snl.SNLPath(), path1.getHeadPath()) + + path2 = snl.SNLPath(path1, ins2) + self.assertIsNotNone(path2) + self.assertFalse(path2.empty()) + self.assertEqual(2, path2.size()) + self.assertEqual(path1, path2.getHeadPath()) + + path3 = snl.SNLPath(ins2) + path4 = snl.SNLPath(ins1, path3) + + occurrence = snl.SNLOccurrence(path1, ins2) + + with self.assertRaises(RuntimeError) as context: snl.SNLPath(path1) + with self.assertRaises(RuntimeError) as context: snl.SNLPath(path1, path2) + with self.assertRaises(RuntimeError) as context: snl.SNLPath(1, 1, 1) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 562fb07dba753e4e35d9c9c6728dd4758756a60e Mon Sep 17 00:00:00 2001 From: nanocoh Date: Sat, 12 Oct 2024 18:03:02 +0200 Subject: [PATCH 2/6] coverage --- test/snl/python/snl_wrapping/test_snloccurrence.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/snl/python/snl_wrapping/test_snloccurrence.py b/test/snl/python/snl_wrapping/test_snloccurrence.py index 1abb9744..f56a88a7 100644 --- a/test/snl/python/snl_wrapping/test_snloccurrence.py +++ b/test/snl/python/snl_wrapping/test_snloccurrence.py @@ -45,10 +45,12 @@ def testFunctions(self): path4 = snl.SNLPath(ins1, path3) occurrence = snl.SNLOccurrence(path1, ins2) + occurrence2 = snl.SNLOccurrence(ins1) + occurrence3 = snl.SNLOccurrence() - with self.assertRaises(RuntimeError) as context: snl.SNLPath(path1) - with self.assertRaises(RuntimeError) as context: snl.SNLPath(path1, path2) - with self.assertRaises(RuntimeError) as context: snl.SNLPath(1, 1, 1) + with self.assertRaises(RuntimeError) as context: snl.SNLOccurrence(path1) + with self.assertRaises(RuntimeError) as context: snl.SNLOccurrence(-1, -1, -1) + with self.assertRaises(RuntimeError) as context: snl.SNLOccurrence(path1, path1) if __name__ == '__main__': unittest.main() \ No newline at end of file From 12ce5ba787b2d9d0cd39e275c7760a8be26ca11f Mon Sep 17 00:00:00 2001 From: nanocoh Date: Sat, 12 Oct 2024 18:55:18 +0200 Subject: [PATCH 3/6] Cmake update --- src/snl/python/snl_wrapping/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/snl/python/snl_wrapping/CMakeLists.txt b/src/snl/python/snl_wrapping/CMakeLists.txt index d4ad10c9..bd902f88 100644 --- a/src/snl/python/snl_wrapping/CMakeLists.txt +++ b/src/snl/python/snl_wrapping/CMakeLists.txt @@ -19,7 +19,7 @@ set(SOURCES PySNLNetComponents.cpp PySNLTerms.cpp PySNLBitTerms.cpp PySNLScalarTerms.cpp PySNLBusTerms.cpp PySNLNets.cpp PySNLBitNets.cpp PySNLScalarNets.cpp PySNLBusNets.cpp - PySNLInstances.cpp PySNLInstTerms.cpp PySNLPath.cpp + PySNLInstances.cpp PySNLInstTerms.cpp PySNLPath.cpp PySNLOccurrence.cpp ) Python3_add_library(naja_snl_python SHARED ${SOURCES}) From c67f6f7497df8c6fb0c3c28a401f84d6e62d9b7b Mon Sep 17 00:00:00 2001 From: nanocoh Date: Sat, 12 Oct 2024 19:17:59 +0200 Subject: [PATCH 4/6] coverage --- test/snl/snl/kernel/SNLOccurrenceTest.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/snl/snl/kernel/SNLOccurrenceTest.cpp b/test/snl/snl/kernel/SNLOccurrenceTest.cpp index 8613742e..a95be33c 100644 --- a/test/snl/snl/kernel/SNLOccurrenceTest.cpp +++ b/test/snl/snl/kernel/SNLOccurrenceTest.cpp @@ -100,6 +100,10 @@ TEST_F(SNLOccurrenceTest, testhEmptyOccurrences1) { EXPECT_EQ(emptyNetOccurrence, SNLInstTermOccurrence()); EXPECT_FALSE(emptyNetOccurrence < emptyInstTermOccurrence); EXPECT_FALSE(emptyInstTermOccurrence < emptyNetOccurrence); + EXPECT_TRUE(emptyInstTermOccurrence <= emptyNetOccurrence); + EXPECT_TRUE(emptyInstTermOccurrence >= emptyNetOccurrence); + EXPECT_FALSE(emptyInstTermOccurrence > emptyNetOccurrence); + std::string emptyPathString = emptyTermOccurrence.getString(); } TEST_F(SNLOccurrenceTest, testh0Level) { From 018e037ec512bb255dc6ff3191e54046f7ba780b Mon Sep 17 00:00:00 2001 From: nanocoh Date: Sat, 12 Oct 2024 19:32:20 +0200 Subject: [PATCH 5/6] coverage --- test/snl/snl/kernel/SNLOccurrenceTest.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/snl/snl/kernel/SNLOccurrenceTest.cpp b/test/snl/snl/kernel/SNLOccurrenceTest.cpp index a95be33c..7892a8b1 100644 --- a/test/snl/snl/kernel/SNLOccurrenceTest.cpp +++ b/test/snl/snl/kernel/SNLOccurrenceTest.cpp @@ -130,6 +130,7 @@ TEST_F(SNLOccurrenceTest, testh0Level) { auto h0IInstTerm = h0Instance_->getInstTerm(iTerm); ASSERT_NE(h0IInstTerm, nullptr); auto h0IInstTermOccurrence = SNLInstTermOccurrence(h0IInstTerm); + std::string h0IInstTermString = h0IInstTermOccurrence.getString(); EXPECT_EQ(h0IInstTermOccurrence, SNLInstTermOccurrence(SNLPath(), h0IInstTerm)); EXPECT_TRUE(h0IInstTermOccurrence.getPath().empty()); EXPECT_EQ(h0IInstTermOccurrence.getInstTerm(), h0IInstTerm); From bb3c8533a0ef2401fd4cf6747858772ddc9a4487 Mon Sep 17 00:00:00 2001 From: nanocoh Date: Sat, 12 Oct 2024 22:52:06 +0200 Subject: [PATCH 6/6] coverage --- src/snl/python/snl_wrapping/PySNLOccurrence.cpp | 14 ++++++++------ src/snl/snl/kernel/SNLOccurrence.cpp | 2 ++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/snl/python/snl_wrapping/PySNLOccurrence.cpp b/src/snl/python/snl_wrapping/PySNLOccurrence.cpp index 0fa54c2d..23ebe536 100644 --- a/src/snl/python/snl_wrapping/PySNLOccurrence.cpp +++ b/src/snl/python/snl_wrapping/PySNLOccurrence.cpp @@ -19,7 +19,7 @@ using namespace naja::SNL; #define METHOD_HEAD(function) GENERIC_METHOD_HEAD(SNLOccurrence, function) static int PySNLOccurrence_Init(PySNLOccurrence* self, PyObject* args, PyObject* kwargs) { - SNLOccurrence* snlPath = nullptr; + SNLOccurrence* snlOccurrence = nullptr; PyObject* arg0 = nullptr; PyObject* arg1 = nullptr; @@ -29,25 +29,27 @@ static int PySNLOccurrence_Init(PySNLOccurrence* self, PyObject* args, PyObject* return -1; } if (arg0 == nullptr) { - snlPath = new SNLOccurrence(); + snlOccurrence = new SNLOccurrence(); } else if (arg1 == nullptr) { if (IsPySNLDesignObject(arg0)) { - snlPath = new SNLOccurrence(PYSNLDesignObject_O(arg0)); + snlOccurrence = new SNLOccurrence(PYSNLDesignObject_O(arg0)); } else { setError("SNLOccurrence create accepts SNLDesignObject as only argument"); return -1; } } else if (IsPySNLPath(arg0) and IsPySNLDesignObject(arg1)) { - snlPath = new SNLOccurrence(*PYSNLPath_O(arg0), PYSNLDesignObject_O(arg1)); + snlOccurrence = new SNLOccurrence(*PYSNLPath_O(arg0), PYSNLDesignObject_O(arg1)); } else { - setError("invalid number of parameters for Path constructor."); + setError("invalid number of parameters for Occurrence constructor."); return -1; } - self->object_ = snlPath; + self->object_ = snlOccurrence; return 0; } +//LCOV_EXCL_START ManagedTypeLinkCreateMethod(SNLOccurrence) +//LCOV_EXCL_STOP ManagedTypeDeallocMethod(SNLOccurrence) PyMethodDef PySNLOccurrence_Methods[] = { diff --git a/src/snl/snl/kernel/SNLOccurrence.cpp b/src/snl/snl/kernel/SNLOccurrence.cpp index 51ebac28..4b3b6cce 100644 --- a/src/snl/snl/kernel/SNLOccurrence.cpp +++ b/src/snl/snl/kernel/SNLOccurrence.cpp @@ -80,7 +80,9 @@ std::string SNLOccurrence::getString() const { std::ostringstream oss; oss << "Occurrence: "; if (object_) { + //LCOV_EXCL_START oss << object_->getDescription(); + //LCOV_EXCL_STOP } else { oss << "null"; }