From 2132a3eb82830d0719d19eaadb32e39222948bca Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Tue, 25 Aug 2009 02:56:00 -0300 Subject: added mapping example to test lib as well as unit tests --- tests/libsample/CMakeLists.txt | 1 + tests/libsample/mapuser.cpp | 41 +++++++++++++++++++++++++++ tests/libsample/mapuser.h | 28 +++++++++++++++++++ tests/samplebinding/CMakeLists.txt | 1 + tests/samplebinding/global.h | 1 + tests/samplebinding/map_conversions.h | 43 +++++++++++++++++++++++++++++ tests/samplebinding/map_test.py | 46 +++++++++++++++++++++++++++++++ tests/samplebinding/typesystem_sample.xml | 5 ++++ 8 files changed, 166 insertions(+) create mode 100644 tests/libsample/mapuser.cpp create mode 100644 tests/libsample/mapuser.h create mode 100644 tests/samplebinding/map_conversions.h create mode 100755 tests/samplebinding/map_test.py diff --git a/tests/libsample/CMakeLists.txt b/tests/libsample/CMakeLists.txt index a630b3cb4..83c480776 100644 --- a/tests/libsample/CMakeLists.txt +++ b/tests/libsample/CMakeLists.txt @@ -9,6 +9,7 @@ implicitconv.cpp kindergarten.cpp listuser.cpp modifications.cpp +mapuser.cpp pairuser.cpp point.cpp reference.cpp diff --git a/tests/libsample/mapuser.cpp b/tests/libsample/mapuser.cpp new file mode 100644 index 000000000..d028b7cb9 --- /dev/null +++ b/tests/libsample/mapuser.cpp @@ -0,0 +1,41 @@ +#include +#include "mapuser.h" + +using namespace std; + +std::map > +MapUser::callCreateMap() +{ + return createMap(); +} + + +std::map > +MapUser::createMap() +{ + std::map > retval; + + std::pair > + item0("zero", std::pair(Complex(1.2, 3.4), 2)); + retval.insert(item0); + + std::pair > + item1("one", std::pair(Complex(5.6, 7.8), 3)); + retval.insert(item1); + + std::pair > + item2("two", std::pair(Complex(9.1, 2.3), 5)); + retval.insert(item2); + + return retval; +} + +void +MapUser::showMap(std::map mapping) +{ + std::map::iterator it; + cout << __FUNCTION__ << endl; + for (it = mapping.begin() ; it != mapping.end(); it++) + cout << (*it).first << " => " << (*it).second << endl; +} + diff --git a/tests/libsample/mapuser.h b/tests/libsample/mapuser.h new file mode 100644 index 000000000..a5ddfe056 --- /dev/null +++ b/tests/libsample/mapuser.h @@ -0,0 +1,28 @@ +#ifndef MAPUSER_H +#define MAPUSER_H + +#include +#include +#include +#include "complex.h" + +class MapUser +{ +public: + MapUser() {} + ~MapUser() {} + + virtual std::map > createMap(); + std::map > callCreateMap(); + + void showMap(std::map mapping); + + void setMap(std::map > map) { m_map = map; } + std::map > getMap() { return m_map; } + +private: + std::map > m_map; +}; + +#endif // MAPUSER_H + diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt index e89d7d765..e371a4a5b 100644 --- a/tests/samplebinding/CMakeLists.txt +++ b/tests/samplebinding/CMakeLists.txt @@ -11,6 +11,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/derived_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/implicitconv_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/listuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/modifications_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/sample/mapuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/pairuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/point_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/sample/reference_wrapper.cpp diff --git a/tests/samplebinding/global.h b/tests/samplebinding/global.h index b5ae6a8a0..57c13c27e 100644 --- a/tests/samplebinding/global.h +++ b/tests/samplebinding/global.h @@ -7,6 +7,7 @@ #include "kindergarten.h" #include "pairuser.h" #include "listuser.h" +#include "mapuser.h" #include "samplenamespace.h" #include "modifications.h" #include "implicitconv.h" diff --git a/tests/samplebinding/map_conversions.h b/tests/samplebinding/map_conversions.h new file mode 100644 index 000000000..b1591b57c --- /dev/null +++ b/tests/samplebinding/map_conversions.h @@ -0,0 +1,43 @@ +template +struct Converter_std_map +{ + static PyObject* toPython(ValueHolder holder) + { + PyObject* result = PyDict_New(); + typedef typename StdMap::iterator IT; + IT it; + + for (it = holder.value.begin(); it != holder.value.end(); it++) { + ValueHolder h_key((*it).first); + ValueHolder h_val((*it).second); + PyDict_SetItem(result, + Converter::toPython(h_key), + Converter::toPython(h_val)); + } + + return result; + } + static StdMap toCpp(PyObject* pyobj) + { + StdMap result; + + PyObject* key; + PyObject* value; + Py_ssize_t pos = 0; + + Py_INCREF(pyobj); + + while (PyDict_Next(pyobj, &pos, &key, &value)) { + result.insert(typename StdMap::value_type( + Converter::toCpp(key), + Converter::toCpp(value))); + } + + Py_DECREF(pyobj); + + return result; + } +}; + +template +struct Converter > : Converter_std_map > {}; diff --git a/tests/samplebinding/map_test.py b/tests/samplebinding/map_test.py new file mode 100755 index 000000000..d2ac4eaad --- /dev/null +++ b/tests/samplebinding/map_test.py @@ -0,0 +1,46 @@ +#!/usr/bin/python + +'''Test cases for std::map container conversions''' + +import sys +import unittest + +from sample import MapUser + +class ExtendedMapUser(MapUser): + def __init__(self): + MapUser.__init__(self) + self.create_map_called = False + + def createMap(self): + self.create_map_called = True + return {'two' : (complex(2.2, 2.2), 2), + 'three' : (complex(3.3, 3.3), 3), + 'five' : (complex(5.5, 5.5), 5), + 'seven' : (complex(7.7, 7.7), 7)} + +class MapConversionTest(unittest.TestCase): + '''Test case for std::map container conversions''' + + def testReimplementedVirtualMethodCall(self): + '''Test if a Python override of a virtual method is correctly called from C++.''' + mu = ExtendedMapUser() + map_ = mu.callCreateMap() + self.assert_(mu.create_map_called) + self.assertEqual(type(map_), dict) + for key, value in map_.items(): + self.assertEqual(type(key), str) + self.assertEqual(type(value[0]), complex) + self.assertEqual(type(value[1]), int) + + def testConversionInBothDirections(self): + '''Test converting a map from Python to C++ and back again.''' + mu = MapUser() + map_ = {'odds' : [2, 4, 6], 'evens' : [3, 5, 7], 'primes' : [3, 4, 6]} + mu.setMap(map_) + result = mu.getMap() + self.assertEqual(result, map_) + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index c049ea9eb..b091373cc 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -17,6 +17,10 @@ + + + + @@ -200,6 +204,7 @@ + -- cgit v1.2.3