-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from nanocoh/main
PySNLOccurrence
- Loading branch information
Showing
8 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-FileCopyrightText: 2023 The Naja authors | ||
// <https://github.com/najaeda/naja/blob/main/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* snlOccurrence = 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) { | ||
snlOccurrence = new SNLOccurrence(); | ||
} else if (arg1 == nullptr) { | ||
if (IsPySNLDesignObject(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)) { | ||
snlOccurrence = new SNLOccurrence(*PYSNLPath_O(arg0), PYSNLDesignObject_O(arg1)); | ||
} else { | ||
setError("invalid number of parameters for Occurrence constructor."); | ||
return -1; | ||
} | ||
self->object_ = snlOccurrence; | ||
return 0; | ||
} | ||
|
||
//LCOV_EXCL_START | ||
ManagedTypeLinkCreateMethod(SNLOccurrence) | ||
//LCOV_EXCL_STOP | ||
ManagedTypeDeallocMethod(SNLOccurrence) | ||
|
||
PyMethodDef PySNLOccurrence_Methods[] = { | ||
{NULL, NULL, 0, NULL} /* sentinel */ | ||
}; | ||
|
||
PyTypeManagedSNLObjectWithoutSNLIDLinkPyType(SNLOccurrence) | ||
PyTypeObjectDefinitions(SNLOccurrence) | ||
|
||
} // namespace PYSNL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#ifndef __PY_SNL_OCCURRENCE_H_ | ||
#define __PY_SNL_OCCURRENCE_H_ | ||
|
||
#include <Python.h> | ||
|
||
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/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) | ||
occurrence2 = snl.SNLOccurrence(ins1) | ||
occurrence3 = snl.SNLOccurrence() | ||
|
||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters