aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorLauro Neto <lauro.neto@openbossa.org>2009-12-09 19:34:12 -0300
committerLauro Neto <lauro.neto@openbossa.org>2009-12-16 15:33:02 -0300
commitf41cf0af8ed705d342ea448acac89659eea64bfd (patch)
treef6f52ed4f28f7fa5b207d4b6a4ad868b23bca50d /tests
parent61570eb7e4ce1ed0f926425d87d26dc3fdb81729 (diff)
Tests for numerical implicit conversions and fixes
Added a test for simple implicit numerical conversions involving doubles, signed and unsigned ints and longs. Some fixes to the converters were also made, mostly related to adding manual checks for boundaries with doubles for negative values Also put the overflow check in a single template Reviewer: Marcelo Lira <marcelo.lira@openbossa.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/libsample/functions.cpp29
-rw-r--r--tests/libsample/functions.h6
-rwxr-xr-xtests/samplebinding/implicitconv_numerical_test.py134
-rw-r--r--tests/samplebinding/typesystem_sample.xml1
4 files changed, 170 insertions, 0 deletions
diff --git a/tests/libsample/functions.cpp b/tests/libsample/functions.cpp
index 8a7416bc6..0df75ba90 100644
--- a/tests/libsample/functions.cpp
+++ b/tests/libsample/functions.cpp
@@ -141,3 +141,32 @@ doubleUnsignedInt(unsigned int value)
return value * 2;
}
+int
+acceptInt(int x)
+{
+ return x;
+}
+
+unsigned int
+acceptUInt(unsigned int x)
+{
+ return x;
+}
+
+long
+acceptLong(long x)
+{
+ return x;
+}
+
+unsigned long
+acceptULong(unsigned long x)
+{
+ return x;
+}
+
+double
+acceptDouble(double x)
+{
+ return x;
+}
diff --git a/tests/libsample/functions.h b/tests/libsample/functions.h
index 2629d3907..52a26a7a6 100644
--- a/tests/libsample/functions.h
+++ b/tests/libsample/functions.h
@@ -74,5 +74,11 @@ LIBSAMPLE_API GlobalOverloadFuncEnum overloadedFunc(double val);
LIBSAMPLE_API unsigned int doubleUnsignedInt(unsigned int value);
+LIBSAMPLE_API int acceptInt(int x);
+LIBSAMPLE_API unsigned int acceptUInt(unsigned int x);
+LIBSAMPLE_API long acceptLong(long x);
+LIBSAMPLE_API unsigned long acceptULong(unsigned long x);
+LIBSAMPLE_API double acceptDouble(double x);
+
#endif // FUNCTIONS_H
diff --git a/tests/samplebinding/implicitconv_numerical_test.py b/tests/samplebinding/implicitconv_numerical_test.py
new file mode 100755
index 000000000..d81a882c5
--- /dev/null
+++ b/tests/samplebinding/implicitconv_numerical_test.py
@@ -0,0 +1,134 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# This file is part of the Shiboken Python Bindings Generator project.
+#
+# Copyright (C) 2009 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
+
+'''Test case for inplicit converting C++ numeric types.'''
+
+import unittest
+import sys
+import sample
+
+
+class NumericTester(unittest.TestCase):
+ '''Helper class for numeric comparison testing'''
+
+ def check_value(self, source, expected, callback, desired_type=None):
+ result = callback(source)
+ self.assertEqual(result, expected)
+
+ if desired_type:
+ self.assertEqual(type(result), desired_type)
+
+
+class FloatImplicitConvert(NumericTester):
+ '''Test case for implicit converting C++ numeric types.'''
+
+ def testFloatAsInt(self):
+ '''Float as Int'''
+ self.check_value(3.14, 3, sample.acceptInt, int)
+ self.assertRaises(OverflowError, sample.acceptInt, sys.maxint + 400)
+
+ def testFloatAsLong(self):
+ '''Float as Long'''
+ #C++ longs are python ints for us
+ self.check_value(3.14, 3, sample.acceptLong, int)
+ self.assertRaises(OverflowError, sample.acceptLong, sys.maxint + 400)
+
+ def testFloatAsUInt(self):
+ '''Float as unsigned Int'''
+ self.check_value(3.14, 3, sample.acceptUInt, int)
+ self.assertRaises(OverflowError, sample.acceptUInt, -3.14)
+
+ def testFloatAsULong(self):
+ '''Float as unsigned Long'''
+ #FIXME Breaking with SystemError "bad argument to internal function"
+ self.check_value(3.14, 3, sample.acceptULong, long)
+ self.assertRaises(OverflowError, sample.acceptULong, -3.14)
+
+ def testFloatAsDouble(self):
+ '''Float as double'''
+ self.check_value(3.14, 3.14, sample.acceptDouble, float)
+
+
+class IntImplicitConvert(NumericTester):
+ '''Test case for implicit converting C++ numeric types.'''
+
+ def testIntAsInt(self):
+ '''Int as Int'''
+ self.check_value(3, 3, sample.acceptInt, int)
+
+ def testIntAsLong(self):
+ '''Int as Long'''
+ self.check_value(3, 3, sample.acceptLong, int)
+
+ # sys.maxint goes here as CPython implements int as a C long
+ self.check_value(sys.maxint, sys.maxint, sample.acceptLong, int)
+ self.check_value(-sys.maxint - 1, -sys.maxint - 1, sample.acceptLong, int)
+
+ def testIntAsUInt(self):
+ '''Int as unsigned Int'''
+ self.check_value(3, 3, sample.acceptUInt, int)
+ self.assertRaises(OverflowError, sample.acceptUInt, -3)
+
+ def testIntAsULong(self):
+ '''Int as unsigned Long'''
+ self.check_value(3, 3, sample.acceptULong, long)
+ self.assertRaises(OverflowError, sample.acceptULong, -3)
+
+ def testFloatAsDouble(self):
+ '''Float as double'''
+ self.check_value(3.14, 3.14, sample.acceptDouble, float)
+
+
+class LongImplicitConvert(NumericTester):
+ '''Test case for implicit converting C++ numeric types.'''
+
+ def testLongAsInt(self):
+ '''Long as Int'''
+ self.check_value(24224l, 24224, sample.acceptInt, int)
+ self.assertRaises(OverflowError, sample.acceptInt, sys.maxint + 20)
+
+ def testLongAsLong(self):
+ '''Long as Long'''
+ self.check_value(2405l, 2405, sample.acceptLong, int)
+ self.assertRaises(OverflowError, sample.acceptLong, sys.maxint + 20)
+
+ def testLongAsUInt(self):
+ '''Long as unsigned Int'''
+ self.check_value(260l, 260, sample.acceptUInt, int)
+ self.assertRaises(OverflowError, sample.acceptUInt, -42)
+
+ def testLongAsULong(self):
+ '''Long as unsigned Long'''
+ self.check_value(128l, 128, sample.acceptULong, long)
+ self.assertRaises(OverflowError, sample.acceptULong, -334l)
+
+ def testLongAsDouble(self):
+ '''Float as double'''
+ self.check_value(42l, 42, sample.acceptDouble, float)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index f7b963e93..9dc3d1798 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -7,6 +7,7 @@
<primitive-type name="signed int" />
<primitive-type name="char"/>
<primitive-type name="long"/>
+ <primitive-type name="unsigned long"/>
<primitive-type name="Complex" target-lang-api-name="PyComplex">
<conversion-rule file="complex_conversions.h"/>