Author: Wim Lavrijsen <[email protected]>
Branch: cppyy-packaging
Changeset: r94861:dbc2035e00e4
Date: 2018-07-13 11:44 -0700
http://bitbucket.org/pypy/pypy/changeset/dbc2035e00e4/

Log:    more pythonizatin of std::vector and more STL tests

diff --git a/pypy/module/_cppyy/pythonify.py b/pypy/module/_cppyy/pythonify.py
--- a/pypy/module/_cppyy/pythonify.py
+++ b/pypy/module/_cppyy/pythonify.py
@@ -451,6 +451,14 @@
             return self.__real_init__(*args)
         pyclass.__init__ = vector_init
 
+        # size-up the return of data()
+        pyclass.__real_data = pyclass.data
+        def data_with_len(self):
+            arr = self.__real_data()
+            arr.reshape((len(self),))
+            return arr
+        pyclass.data = data_with_len
+
     # combine __getitem__ and __len__ to make a pythonized __getitem__
     if '__getitem__' in pyclass.__dict__ and '__len__' in pyclass.__dict__:
         pyclass._getitem__unchecked = pyclass.__getitem__
diff --git a/pypy/module/_cppyy/test/stltypes.cxx 
b/pypy/module/_cppyy/test/stltypes.cxx
--- a/pypy/module/_cppyy/test/stltypes.cxx
+++ b/pypy/module/_cppyy/test/stltypes.cxx
@@ -1,6 +1,20 @@
 #include "stltypes.h"
 
 
+//- explicit instantiations of used comparisons
+#if defined __clang__
+namespace std {
+#define ns_prefix std::
+#elif defined(__GNUC__) || defined(__GNUG__)
+namespace __gnu_cxx {
+#define ns_prefix
+#endif
+template bool ns_prefix operator==(const std::vector<int>::iterator&,
+                         const std::vector<int>::iterator&);
+template bool ns_prefix operator!=(const std::vector<int>::iterator&,
+                         const std::vector<int>::iterator&);
+}
+
 //- class with lots of std::string handling
 stringy_class::stringy_class(const char* s) : m_string(s) {}
 
@@ -9,3 +23,33 @@
 
 void stringy_class::set_string1(const std::string& s) { m_string = s; }
 void stringy_class::set_string2(std::string s) { m_string = s; }
+
+
+//- helpers for testing array
+int ArrayTest::get_pp_px(Point** p, int idx) {
+    return p[idx]->px;
+}
+
+int ArrayTest::get_pp_py(Point** p, int idx) {
+    return p[idx]->py;
+}
+
+int ArrayTest::get_pa_px(Point* p[], int idx) {
+    return p[idx]->px;
+}
+
+int ArrayTest::get_pa_py(Point* p[], int idx) {
+    return p[idx]->py;
+}
+
+
+// helpers for string testing
+std::string str_array_1[3] = {"a", "b", "c"};
+std::string str_array_2[]  = {"d", "e", "f", "g"};
+std::string str_array_3[3][2] = {{"a", "b"}, {"c", "d"}, {"e", "f"}};
+std::string str_array_4[4][2][2] = {
+     {{"a", "b"}, {"c", "d"}},
+     {{"e", "f"}, {"g", "h"}},
+     {{"i", "j"}, {"k", "l"}},
+     {{"m", "n"}, {"o", "p"}},
+};
diff --git a/pypy/module/_cppyy/test/stltypes.h 
b/pypy/module/_cppyy/test/stltypes.h
--- a/pypy/module/_cppyy/test/stltypes.h
+++ b/pypy/module/_cppyy/test/stltypes.h
@@ -37,3 +37,49 @@
    std::string operator[](double) { return "double"; }
    std::string operator[](const std::string&) { return "string"; }
 };      
+
+
+//- instantiations of used STL types
+namespace {
+
+    stl_like_class<int> stlc_1;
+
+} // unnamed namespace
+
+
+// comps for int only to allow testing: normal use of vector is looping over a
+// range-checked version of __getitem__
+#if defined(__clang__) && defined(__APPLE__)
+namespace std {
+#define ns_prefix std::
+#elif defined(__GNUC__) || defined(__GNUG__)
+namespace __gnu_cxx {
+#define ns_prefix
+#endif
+extern template bool ns_prefix operator==(const std::vector<int>::iterator&,
+                         const std::vector<int>::iterator&);
+extern template bool ns_prefix operator!=(const std::vector<int>::iterator&,
+                         const std::vector<int>::iterator&);
+}
+
+
+//- helpers for testing array
+namespace ArrayTest {
+
+struct Point {
+    int px, py;
+};
+
+int get_pp_px(Point** p, int idx);
+int get_pp_py(Point** p, int idx);
+int get_pa_px(Point* p[], int idx);
+int get_pa_py(Point* p[], int idx);
+
+} // namespace ArrayTest
+
+
+// helpers for string testing
+extern std::string str_array_1[3];
+extern std::string str_array_2[];
+extern std::string str_array_3[3][2];
+extern std::string str_array_4[4][2][2];
diff --git a/pypy/module/_cppyy/test/stltypes.xml 
b/pypy/module/_cppyy/test/stltypes.xml
--- a/pypy/module/_cppyy/test/stltypes.xml
+++ b/pypy/module/_cppyy/test/stltypes.xml
@@ -1,10 +1,13 @@
 <lcgdict>
 
-  <namespace name="std" />
+  <class name="just_a_class" />
+  <class name="stringy_class" />
+  <class pattern="stl_like_class<*>" />
 
-  <class name="just_a_class" />
+  <namespace name="ArrayTest" />
+  <class pattern="ArrayTest::*" />
+  <function pattern="ArrayTest::*" />
 
-  <class name="std::string" />
-  <class name="stringy_class" />
+  <variable pattern="str_array_*" />
 
 </lcgdict>
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
@@ -42,8 +42,8 @@
             assert tv1.iterator is cppyy.gbl.std.vector(p_type).iterator
 
             #----- 
-            v = tv1(); v += range(self.N)    # default args from Reflex are 
useless :/
-            if p_type == int:                # only type with == and != 
reflected in .xml
+            v = tv1(); v += range(self.N)
+            if p_type == int:
                 assert v.begin().__eq__(v.begin())
                 assert v.begin() == v.begin()
                 assert v.end() == v.end()
@@ -58,6 +58,7 @@
 
             assert v.size() == self.N
             assert len(v) == self.N
+            assert len(v.data()) == self.N
 
             #-----
             v = tv1()
@@ -502,3 +503,54 @@
         assert len(v) == N
         for i in range(N):
             assert v[i] == i
+
+
+class AppTestSTLARRAY:
+    spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
+
+    def setup_class(cls):
+        cls.w_test_dct  = cls.space.newtext(test_dct)
+        cls.w_stlarray = cls.space.appexec([], """():
+            import ctypes, _cppyy
+            _cppyy._post_import_startup()
+            return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))
+
+    def test01_array_of_basic_types(self):
+        """Usage of std::array of basic types"""
+
+        import _cppyy as cppyy
+        std = cppyy.gbl.std
+
+        a = std.array[int, 4]()
+        assert len(a) == 4
+        for i in range(len(a)):
+            a[i] = i
+            assert a[i] == i
+
+    def test02_array_of_pods(self):
+        """Usage of std::array of PODs"""
+
+        import _cppyy as cppyy
+        gbl, std = cppyy.gbl, cppyy.gbl.std
+
+        a = std.array[gbl.ArrayTest.Point, 4]()
+        assert len(a) == 4
+        for i in range(len(a)):
+            a[i].px = i
+            assert a[i].px == i
+            a[i].py = i**2
+            assert a[i].py == i**2
+
+    def test03_array_of_pointer_to_pods(self):
+        """Usage of std::array of pointer to PODs"""
+
+        import cppyy
+        from cppyy import gbl
+        from cppyy.gbl import std
+
+        ll = [gbl.ArrayTest.Point() for i in range(4)]
+        for i in range(len(ll)):
+            ll[i].px = 13*i
+            ll[i].py = 42*i
+
+        # more tests in cppyy/test/test_stltypes.py, but currently not 
supported
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to