Added unit test for briding sample lib.

This commit is contained in:
Matthias Koefferlein 2018-06-09 02:11:32 +02:00
parent 9418185771
commit e1858973e2
4 changed files with 95 additions and 5 deletions

View File

@ -1,4 +1,43 @@
/*
KLayout Layout Viewer
Copyright (C) 2006-2018 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
This Python library demonstrates the use of the GSI/Python binding
API for developing "bridge" applications - i.e. libraries that access
the KLayout objects through their C++ API.
This sample library provides two conversion functions:
bridge.p2a(poly) Converts pya.DSimplePolygon objects to Python
arrays with the structure [ (x, y), ... ].
bridge.a2p(array) Does the inverse transformation
Use cases for such libraries are fast C++ based conversion of KLayout
objects into other objects and vice versa.
*/
#include <Python.h>
#include "pyaConvert.h"
@ -108,16 +147,16 @@ static PyMethodDef BridgeMethods[] = {
};
PyMODINIT_FUNC
initbridge ()
initbridge_sample ()
{
PyObject *m;
m = Py_InitModule ("bridge", BridgeMethods);
m = Py_InitModule ("bridge_sample", BridgeMethods);
if (m == NULL) {
return;
}
BridgeError = PyErr_NewException ((char *) "bridge.error", NULL, NULL);
BridgeError = PyErr_NewException ((char *) "bridge_sample.error", NULL, NULL);
Py_INCREF (BridgeError);
PyModule_AddObject (m, "error", BridgeError);
}

View File

@ -35,10 +35,11 @@ DESTDIR = $$OUT_PWD/../..
TEMPLATE = lib
# That's how we name our library
TARGET = bridge
TARGET = bridge_sample
# The only source
SOURCES = bridge.cc
SOURCES = \
bridge_sample.cc
# Include QtCore required for some includes
QT = core

View File

@ -53,6 +53,8 @@ int run_pymodtest (tl::TestBase * /*_this*/, const std::string &fn)
#define PYMODTEST(n, file) \
TEST(n) { EXPECT_EQ (run_pymodtest(_this, file), 0); }
PYMODTEST (bridge, "bridge.py")
PYMODTEST (import_tl, "import_tl.py")
PYMODTEST (import_db, "import_db.py")
PYMODTEST (import_lay, "import_lay.py")

48
testdata/pymod/bridge.py vendored Normal file
View File

@ -0,0 +1,48 @@
# KLayout Layout Viewer
# Copyright (C) 2006-2018 Matthias Koefferlein
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import pykl.db as db
import bridge_sample as bridge
import unittest
import sys
# Tests the basic abilities of the module
class BridgeTest(unittest.TestCase):
def test_1(self):
p = db.DSimplePolygon(db.DBox(1,2,3,4))
a = bridge.p2a(p)
self.assertEqual(repr(bridge.p2a(p)), "[(1.0, 2.0), (1.0, 4.0), (3.0, 4.0), (3.0, 2.0)]")
def test_2(self):
p = db.DSimplePolygon(db.DBox(1,2,3,4))
a = bridge.p2a(p)
pp = bridge.a2p(a)
self.assertEqual(repr(pp), "(1,2;1,4;3,4;3,2)")
self.assertEqual(type(pp).__name__, "DSimplePolygon")
# run unit tests
if __name__ == '__main__':
suite = unittest.TestSuite()
suite = unittest.TestLoader().loadTestsFromTestCase(BridgeTest)
if not unittest.TextTestRunner(verbosity = 1).run(suite).wasSuccessful():
sys.exit(1)