diff options
author | Lauro Neto <lauro.neto@openbossa.org> | 2010-02-18 19:17:25 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2010-02-19 16:18:52 -0300 |
commit | 5f80d538253837cf474fe10601374be40a2b8b98 (patch) | |
tree | 7ea48f3bba3557c9c906a46e457b37f2b41d474e | |
parent | 4b0fa3fd45ed42cf881a25897ed66b5c0cf3a3fa (diff) |
Adding test for sequenceToIntArray
Reviewed by Marcelo Lira <marcelo.lira@openbossa.org>
-rw-r--r-- | tests/libsample/injectcode.cpp | 12 | ||||
-rw-r--r-- | tests/libsample/injectcode.h | 1 | ||||
-rwxr-xr-x | tests/samplebinding/injectcode_test.py | 22 | ||||
-rw-r--r-- | tests/samplebinding/typesystem_sample.xml | 30 |
4 files changed, 65 insertions, 0 deletions
diff --git a/tests/libsample/injectcode.cpp b/tests/libsample/injectcode.cpp index 9a8091a5d..3621acb2d 100644 --- a/tests/libsample/injectcode.cpp +++ b/tests/libsample/injectcode.cpp @@ -100,3 +100,15 @@ int InjectCode::arrayMethod(int count, int *values) const ret += values[i]; return ret; } + +int InjectCode::sumArrayAndLength(int* values) const +{ + int sum = 0; + + while(*values) { + sum = sum + *values + 1; + values++; + } + + return sum; +} diff --git a/tests/libsample/injectcode.h b/tests/libsample/injectcode.h index 42cf01c7e..f22f3d5fa 100644 --- a/tests/libsample/injectcode.h +++ b/tests/libsample/injectcode.h @@ -56,6 +56,7 @@ public: virtual int arrayMethod(int count, int* values) const; int callArrayMethod(int count, int *values) const { return arrayMethod(count, values); } virtual const char* virtualMethod(int arg); + int sumArrayAndLength(int* values) const; private: // This attr is just to retain the memory pointed by all return values, // So, the memory returned by all methods will be valid until someone call diff --git a/tests/samplebinding/injectcode_test.py b/tests/samplebinding/injectcode_test.py index 3888f07b6..2941b9233 100755 --- a/tests/samplebinding/injectcode_test.py +++ b/tests/samplebinding/injectcode_test.py @@ -90,5 +90,27 @@ class InjectCodeTest(unittest.TestCase): result = ic.callArrayMethod(values) self.assertEqual(result, ic.multiplier * sum(values)) + +class IntArrayTest(unittest.TestCase): + '''Test case for converting python sequence to int array''' + + def testBasic(self): + '''Shiboken::sequenceToIntArray - basic case''' + args = [1, 2, 3, 4] + ic = InjectCode() + self.assertEqual(sum(args) + len(args), ic.sumArrayAndLength(args)) + + def testEmpty(self): + '''Shiboken::sequenceToIntArray - empty sequence''' + args = [] + ic = InjectCode() + self.assertEqual(sum(args) + len(args), ic.sumArrayAndLength(args)) + + def testWithZero(self): + '''Shiboken::sequenceToIntArray - count only up to zero''' + args = [1, 2, 0, 3] + ic = InjectCode() + self.assertEqual(sum([1, 2]) + len([1, 2]), ic.sumArrayAndLength(args)) + if __name__ == '__main__': unittest.main() diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml index 1f22805bd..8fa684a83 100644 --- a/tests/samplebinding/typesystem_sample.xml +++ b/tests/samplebinding/typesystem_sample.xml @@ -601,6 +601,36 @@ Note: Some uses of inject code here are used just for testing purposes, consider using the add-function tag. --> + <modify-function signature="sumArrayAndLength(int *) const"> + <modify-argument index="1"> + <replace-type modified-type="PyObject"/> + </modify-argument> + <inject-code class="target" position="beginning"> + int* array = NULL; + bool errorOccurred = false; + + if (PySequence_Check(%PYARG_1)) { + if((array = Shiboken::sequenceToIntArray(%PYARG_1, true)) == NULL && PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, "Should be a sequence of ints"); + errorOccurred = true; + } + } else { + PyErr_SetString(PyExc_TypeError, "Should be a sequence of ints"); + errorOccurred = true; + } + + if (!errorOccurred) { + %RETURN_TYPE retval = %CPPSELF.%FUNCTION_NAME(array); + + if (array) + delete array; + + %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](retval); + } + </inject-code> + + </modify-function> + <modify-function signature="arrayMethod(int, int*) const"> <modify-argument index="1"> <remove-argument/> |