diff options
author | John Cummings <jcummings2@users.sf.net> | 2012-06-07 11:25:55 -0500 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2012-06-14 22:12:24 +0200 |
commit | 22b6e8243784aa57c820d01b73ed16e604d7bca8 (patch) | |
tree | 1e9f269e31f551a3b366d07860f524e84f2fefcc /tests | |
parent | f465efbff6830866c0a82b30193389bb97ab2210 (diff) |
Add typedef examples to minimal
Add an example for a typedef of a template
Add an example for a typedef of a typedef
Add a python unit test for these examples
Change-Id: Id2ea1a5f1e4b3d865c081625f2d5b7ed4c38bbdb
Reviewed-by: Marcelo Lira <marcelo.lira@openbossa.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/libminimal/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/libminimal/typedef.cpp | 47 | ||||
-rw-r--r-- | tests/libminimal/typedef.h | 26 | ||||
-rw-r--r-- | tests/minimalbinding/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/minimalbinding/global.h | 1 | ||||
-rw-r--r-- | tests/minimalbinding/typedef_test.py | 104 | ||||
-rw-r--r-- | tests/minimalbinding/typesystem_minimal.xml | 42 |
7 files changed, 222 insertions, 0 deletions
diff --git a/tests/libminimal/CMakeLists.txt b/tests/libminimal/CMakeLists.txt index 9b47bff5e..a25892571 100644 --- a/tests/libminimal/CMakeLists.txt +++ b/tests/libminimal/CMakeLists.txt @@ -3,6 +3,7 @@ project(libminimal) set(libminimal_SRC obj.cpp listuser.cpp +typedef.cpp ) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/tests/libminimal/typedef.cpp b/tests/libminimal/typedef.cpp new file mode 100644 index 000000000..700d50b5f --- /dev/null +++ b/tests/libminimal/typedef.cpp @@ -0,0 +1,47 @@ +#include "typedef.h" + +// +// Test wrapping of a typedef +// +bool arrayFuncInt(std::vector<int> a) +{ + return a.empty(); +} + +bool arrayFuncIntTypedef(MyArray a) +{ + return arrayFuncInt(a); +} + +std::vector<int> arrayFuncIntReturn(int size) +{ + return std::vector<int>(size); +} + +MyArray arrayFuncIntReturnTypedef(int size) +{ + return arrayFuncIntReturn(size); +} + +// +// Test wrapping of a typedef of a typedef +// +bool arrayFunc(std::vector<int> a) +{ + return a.empty(); +} + +bool arrayFuncTypedef(MyArray a) +{ + return arrayFunc(a); +} + +std::vector<int> arrayFuncReturn(int size) +{ + return std::vector<int>(size); +} + +MyArray arrayFuncReturnTypedef(int size) +{ + return arrayFuncReturn(size); +} diff --git a/tests/libminimal/typedef.h b/tests/libminimal/typedef.h new file mode 100644 index 000000000..4b7b5d633 --- /dev/null +++ b/tests/libminimal/typedef.h @@ -0,0 +1,26 @@ +#ifndef TYPEDEF_H +#define TYPEDEF_H + +#include "libminimalmacros.h" + +#include <vector> + +// Test wrapping of a typedef +typedef std::vector<int> MyArrayInt; + +LIBMINIMAL_API bool arrayFuncInt(std::vector<int> a); +LIBMINIMAL_API bool arrayFuncIntTypedef(MyArrayInt a); + +LIBMINIMAL_API std::vector<int> arrayFuncIntReturn(int size); +LIBMINIMAL_API MyArrayInt arrayFuncIntReturnTypedef(int size); + +// Test wrapping of a typedef of a typedef +typedef MyArrayInt MyArray; + +LIBMINIMAL_API bool arrayFunc(std::vector<int> a); +LIBMINIMAL_API bool arrayFuncTypedef(MyArray a); + +LIBMINIMAL_API std::vector<int> arrayFuncReturn(int size); +LIBMINIMAL_API MyArray arrayFuncReturnTypedef(int size); + +#endif diff --git a/tests/minimalbinding/CMakeLists.txt b/tests/minimalbinding/CMakeLists.txt index 5522e6ad6..15eaafc77 100644 --- a/tests/minimalbinding/CMakeLists.txt +++ b/tests/minimalbinding/CMakeLists.txt @@ -19,6 +19,7 @@ add_custom_command(OUTPUT ${minimal_SRC} COMMAND ${shibokengenerator_BINARY_DIR}/shiboken --project-file=${CMAKE_CURRENT_BINARY_DIR}/minimal-binding.txt ${GENERATOR_EXTRA_FLAGS} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Running generator for 'minimal' test binding..." +DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/typesystem_minimal.xml libminimal ) include_directories(${CMAKE_CURRENT_SOURCE_DIR} diff --git a/tests/minimalbinding/global.h b/tests/minimalbinding/global.h index da283d11e..3bc13d484 100644 --- a/tests/minimalbinding/global.h +++ b/tests/minimalbinding/global.h @@ -2,3 +2,4 @@ #include "val.h" #include "minbool.h" #include "listuser.h" +#include "typedef.h" diff --git a/tests/minimalbinding/typedef_test.py b/tests/minimalbinding/typedef_test.py new file mode 100644 index 000000000..4c1c217fa --- /dev/null +++ b/tests/minimalbinding/typedef_test.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of the Shiboken Python Bindings Generator project. +# +# Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +# +# Contact: PySide team <contact@pyside.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License +# version 2.1 as published by the Free Software Foundation. Please +# review the following information to ensure the GNU Lesser General +# Public License version 2.1 requirements will be met: +# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +# # +# 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser 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 unittest +from minimal import * +from py3kcompat import IS_PY3K +import numpy as np + +if IS_PY3K: + import functools + reduce = functools.reduce + + +class TypedefTest(unittest.TestCase): + + def setUp(self): + self.the_size = 8 + + def test_arrayFuncInt(self): + none = () + full = range(self.the_size) + self.assertTrue(arrayFuncInt(none), "None is empty, arrayFuncInt should return true") + self.assertFalse(arrayFuncInt(full), "Full is NOT empty, arrayFuncInt should return false") + + self.assertTrue(arrayFuncInt(np.array(none)), "None is empty, arrayFuncInt should return true") + self.assertFalse(arrayFuncInt(np.array(full)), "Full is NOT empty, arrayFuncInt should return false") + + def test_arrayFuncIntTypedef(self): + none = () + full = (1, 2, 3) + self.assertTrue(arrayFuncIntTypedef(none), "None is empty, arrayFuncIntTypedef should return true") + self.assertFalse(arrayFuncIntTypedef(full), "Full is NOT empty, arrayFuncIntTypedef should return false") + + self.assertTrue(arrayFuncIntTypedef(np.array(none)), "None is empty, arrayFuncIntTypedef should return true") + self.assertFalse(arrayFuncIntTypedef(np.array(full)), "Full is NOT empty, arrayFuncIntTypedef should return false") + + def test_arrayFuncIntReturn(self): + none = arrayFuncIntReturn(0) + full = arrayFuncIntReturn(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + def test_arrayFuncIntReturnTypedef(self): + none = arrayFuncIntReturnTypedef(0) + full = arrayFuncIntReturnTypedef(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + def test_arrayFunc(self): + none = () + full = range(self.the_size) + self.assertTrue(arrayFunc(none), "None is empty, arrayFunc should return true") + self.assertFalse(arrayFunc(full), "Full is NOT empty, arrayFunc should return false") + + self.assertTrue(arrayFunc(np.array(none)), "None is empty, arrayFunc should return true") + self.assertFalse(arrayFunc(np.array(full)), "Full is NOT empty, arrayFunc should return false") + + def test_arrayFuncTypedef(self): + none = () + full = (1, 2, 3) + self.assertTrue(arrayFuncTypedef(none), "None is empty, arrayFuncTypedef should return true") + self.assertFalse(arrayFuncTypedef(full), "Full is NOT empty, arrayFuncTypedef should return false") + + self.assertTrue(arrayFuncTypedef(np.array(none)), "None is empty, arrayFuncTypedef should return true") + self.assertFalse(arrayFuncTypedef(np.array(full)), "Full is NOT empty, arrayFuncTypedef should return false") + + def test_arrayFuncReturn(self): + none = arrayFuncReturn(0) + full = arrayFuncReturn(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + def test_arrayFuncReturnTypedef(self): + none = arrayFuncReturnTypedef(0) + full = arrayFuncReturnTypedef(self.the_size) + self.assertTrue((len(none) == 0), "none should be empty") + self.assertTrue((len(full) == self.the_size), "full should have " + str(self.the_size) + " elements") + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/minimalbinding/typesystem_minimal.xml b/tests/minimalbinding/typesystem_minimal.xml index be52d191c..86af78d11 100644 --- a/tests/minimalbinding/typesystem_minimal.xml +++ b/tests/minimalbinding/typesystem_minimal.xml @@ -48,4 +48,46 @@ </value-type> <value-type name="ListUser"/> <value-type name="MinBoolUser"/> + + <container-type name="std::vector" type="vector"> + <include file-name="vector" location="global"/> + <conversion-rule> + <native-to-target> + PyObject* %out = PyList_New((int) %in.size()); + %INTYPE::const_iterator it = %in.begin(); + for (int idx = 0; it != %in.end(); ++it, ++idx) { + %INTYPE_0 cppItem(*it); + PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem)); + } + return %out; + </native-to-target> + <target-to-native> + <add-conversion type="PySequence"> + Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0)); + int the_size = PySequence_Fast_GET_SIZE(seq.object()); + %out.reserve(the_size); + for (int i = 0; i < the_size; i++) { + PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), i); + %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem); + %out.push_back(cppItem); + } + </add-conversion> + </target-to-native> + </conversion-rule> + </container-type> + <!-- Test wrapping of a typedef --> + <function signature="arrayFuncInt(std::vector<int>)" /> + <!-- Note manual expansion of the typedef --> + <function signature="arrayFuncIntTypedef(std::vector<int>)" /> + + <function signature="arrayFuncIntReturn(int)" /> + <function signature="arrayFuncIntReturnTypedef(int)" /> + + <!-- Test wrapping of a typedef of a typedef --> + <function signature="arrayFunc(std::vector<int>)" /> + <!-- Note manual expansion of the typedef --> + <function signature="arrayFuncTypedef(std::vector<int>)" /> + + <function signature="arrayFuncReturn(int)" /> + <function signature="arrayFuncReturnTypedef(int)" /> </typesystem> |