[pypy-commit] pypy cppyy-packaging: more template tests
wlav
pypy.commits at gmail.com
Thu Jul 26 12:38:26 EDT 2018
Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: cppyy-packaging
Changeset: r94891:57b26d4f25d5
Date: 2018-07-25 23:32 -0700
http://bitbucket.org/pypy/pypy/changeset/57b26d4f25d5/
Log: more template tests
diff --git a/pypy/module/_cppyy/test/templates.h b/pypy/module/_cppyy/test/templates.h
--- a/pypy/module/_cppyy/test/templates.h
+++ b/pypy/module/_cppyy/test/templates.h
@@ -1,5 +1,6 @@
#include <string>
#include <sstream>
+#include <vector>
//===========================================================================
@@ -56,6 +57,18 @@
return 13;
}
+template <typename F>
+struct SomeResult {
+ F m_retval;
+};
+
+template <class I, typename O = float>
+SomeResult<O> global_get_some_result(const std::vector<I>& carrier) {
+ SomeResult<O> r{};
+ r.m_retval = O(carrier[0]);
+ return r;
+}
+
//===========================================================================
// variadic functions
diff --git a/pypy/module/_cppyy/test/test_datatypes.py b/pypy/module/_cppyy/test/test_datatypes.py
--- a/pypy/module/_cppyy/test/test_datatypes.py
+++ b/pypy/module/_cppyy/test/test_datatypes.py
@@ -61,6 +61,30 @@
#assert round(c.get_ldouble_r() + 88., 24) == 0
assert round(c.m_double + 77., 8) == 0
+ # complex<double> type
+ assert type(c.get_complex()) == complex
+ assert round(c.get_complex().real - 99., 11) == 0
+ assert round(c.get_complex().imag - 101., 11) == 0
+ assert repr(c.get_complex()) == '(99+101j)'
+ assert round(c.get_complex_cr().real - 99., 11) == 0
+ assert round(c.get_complex_cr().imag - 101., 11) == 0
+ assert round(c.get_complex_r().real - 99., 11) == 0
+ assert round(c.get_complex_r().imag - 101., 11) == 0
+ assert complex(cppyy.gbl.std.complex['double'](1, 2)) == complex(1, 2)
+
+ # complex<int> retains C++ type in all cases (but includes pythonization to
+ # resemble Python's complex more closely
+ assert type(c.get_icomplex()) == cppyy.gbl.std.complex[int]
+ assert round(c.get_icomplex().real - 121., 11) == 0
+ assert round(c.get_icomplex().imag - 141., 11) == 0
+ assert repr(c.get_icomplex()) == '(121+141j)'
+ assert round(c.get_icomplex_cr().real - 121., 11) == 0
+ assert round(c.get_icomplex_cr().imag - 141., 11) == 0
+ assert type(c.get_icomplex_r()) == cppyy.gbl.std.complex[int]
+ assert round(c.get_icomplex_r().real - 121., 11) == 0
+ assert round(c.get_icomplex_r().imag - 141., 11) == 0
+ assert complex(cppyy.gbl.std.complex['int'](1, 2)) == complex(1, 2)
+
# reading of enum types
assert c.m_enum == CppyyTestData.kNothing
assert c.m_enum == c.kNothing
diff --git a/pypy/module/_cppyy/test/test_stltypes.py b/pypy/module/_cppyy/test/test_stltypes.py
--- a/pypy/module/_cppyy/test/test_stltypes.py
+++ b/pypy/module/_cppyy/test/test_stltypes.py
@@ -210,8 +210,6 @@
vb[-1] = True
assert vb[7]
- print [x for x in vb]
-
assert [x for x in vb] == [True]+[False]*6+[True]
assert len(vb[4:8]) == 4
@@ -224,7 +222,8 @@
def setup_class(cls):
cls.w_test_dct = cls.space.newtext(test_dct)
cls.w_stlstring = cls.space.appexec([], """():
- import ctypes
+ import ctypes, _cppyy
+ _cppyy._post_import_startup()
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
def test01_string_argument_passing(self):
@@ -295,6 +294,33 @@
assert t0 == c.get_string1()
assert s == c.get_string1()
+ def test04_array_of_strings(self):
+ """Access to global arrays of strings"""
+
+ import _cppyy as cppyy
+
+ assert tuple(cppyy.gbl.str_array_1) == ('a', 'b', 'c')
+ str_array_2 = cppyy.gbl.str_array_2
+ # fix up the size
+ str_array_2.size = 4
+ assert tuple(str_array_2) == ('d', 'e', 'f', 'g')
+ assert tuple(str_array_2) == ('d', 'e', 'f', 'g')
+
+ # multi-dimensional
+ vals = ['a', 'b', 'c', 'd', 'e', 'f']
+ str_array_3 = cppyy.gbl.str_array_3
+ for i in range(3):
+ for j in range(2):
+ assert str_array_3[i][j] == vals[i*2+j]
+
+ vals = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+ 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
+ str_array_4 = cppyy.gbl.str_array_4
+ for i in range(4):
+ for j in range(2):
+ for k in range(2):
+ assert str_array_4[i][j][k] == vals[i*4+j*2+k]
+
class AppTestSTLLIST:
spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
@@ -303,7 +329,8 @@
cls.w_N = cls.space.newint(13)
cls.w_test_dct = cls.space.newtext(test_dct)
cls.w_stlstring = cls.space.appexec([], """():
- import ctypes
+ import ctypes, _cppyy
+ _cppyy._post_import_startup()
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
def test01_builtin_list_type(self):
@@ -359,7 +386,8 @@
cls.w_N = cls.space.newint(13)
cls.w_test_dct = cls.space.newtext(test_dct)
cls.w_stlstring = cls.space.appexec([], """():
- import ctypes
+ import ctypes, _cppyy
+ _cppyy._post_import_startup()
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
def test01_builtin_map_type(self):
@@ -468,7 +496,8 @@
def setup_class(cls):
cls.w_test_dct = cls.space.newtext(test_dct)
cls.w_stlstring = cls.space.appexec([], """():
- import ctypes
+ import ctypes, _cppyy
+ _cppyy._post_import_startup()
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
def test01_builtin_vector_iterators(self):
@@ -505,7 +534,8 @@
def setup_class(cls):
cls.w_test_dct = cls.space.newtext(test_dct)
cls.w_stlstring = cls.space.appexec([], """():
- import ctypes
+ import ctypes, _cppyy
+ _cppyy._post_import_startup()
return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
def test01_explicit_templates(self):
diff --git a/pypy/module/_cppyy/test/test_templates.py b/pypy/module/_cppyy/test/test_templates.py
--- a/pypy/module/_cppyy/test/test_templates.py
+++ b/pypy/module/_cppyy/test/test_templates.py
@@ -53,32 +53,53 @@
def test03_templated_function(self):
"""Templated global and static functions lookup and calls"""
- import _cppyy
+ import _cppyy as cppyy
# TODO: the following only works if something else has already
# loaded the headers associated with this template
- ggs = _cppyy.gbl.global_get_size
+ ggs = cppyy.gbl.global_get_size
assert ggs['char']() == 1
- gsf = _cppyy.gbl.global_some_foo
+ gsf = cppyy.gbl.global_some_foo
assert gsf[int](3) == 42
assert gsf(3) == 42
assert gsf(3.) == 42
- gsb = _cppyy.gbl.global_some_bar
+ gsb = cppyy.gbl.global_some_bar
assert gsb(3) == 13
assert gsb['double'](3.) == 13
# TODO: the following only works in a namespace
- nsgsb = _cppyy.gbl.SomeNS.some_bar
+ nsgsb = cppyy.gbl.SomeNS.some_bar
assert nsgsb[3]
assert nsgsb[3]() == 3
# TODO: add some static template method
+ # test forced creation of subsequent overloads
+ vector = cppyy.gbl.std.vector
+ # float in, float out
+ ggsr = cppyy.gbl.global_get_some_result['std::vector<float>']
+ assert type(ggsr(vector['float']([0.5])).m_retval) == float
+ assert ggsr(vector['float']([0.5])).m_retval == 0.5
+ # int in, float out
+ ggsr = cppyy.gbl.global_get_some_result['std::vector<int>']
+ assert type(ggsr(vector['int']([5])).m_retval) == float
+ assert ggsr(vector['int']([5])).m_retval == 5.
+ # float in, int out
+ # TODO: this now matches the earlier overload
+ #ggsr = cppyy.gbl.global_get_some_result['std::vector<float>, int']
+ #assert type(ggsr(vector['float']([0.3])).m_retval) == int
+ #assert ggsr(vector['float']([0.3])).m_retval == 0
+ # int in, int out
+ # TODO: same as above, matches earlier overload
+ #ggsr = cppyy.gbl.global_get_some_result['std::vector<int>, int']
+ #assert type(ggsr(vector['int']([5])).m_retval) == int
+ #assert ggsr(vector['int']([5])).m_retval == 5
+
def test04_variadic_function(self):
"""Call a variadic function"""
More information about the pypy-commit
mailing list