Author: Wim Lavrijsen <[email protected]>
Branch: reflex-support
Changeset: r62409:1ea72a905385
Date: 2013-03-18 14:25 -0700
http://bitbucket.org/pypy/pypy/changeset/1ea72a905385/
Log: tests for STL map and allow iterations over std::pair
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
@@ -336,9 +336,20 @@
return self.c_str() == other.c_str()
else:
return self.c_str() == other
- pyclass.__eq__ = eq
+ pyclass.__eq__ = eq
pyclass.__str__ = pyclass.c_str
+ # std::pair unpacking through iteration
+ if 'std::pair' in pyclass.__name__:
+ def getitem(self, idx):
+ if idx == 0: return self.first
+ if idx == 1: return self.second
+ raise IndexError("out of bounds")
+ def return2(self):
+ return 2
+ pyclass.__getitem__ = getitem
+ pyclass.__len__ = return2
+
_loaded_dictionaries = {}
def load_reflection_info(name):
"""Takes the name of a library containing reflection info, returns a handle
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
@@ -1,6 +1,7 @@
#include <list>
#include <map>
#include <string>
+#include <utility>
#include <vector>
//- basic example class
@@ -14,6 +15,14 @@
std::STLTYPE<TTYPE >::iterator STLTYPE##_##N##_i; \
std::STLTYPE<TTYPE >::const_iterator STLTYPE##_##N##_ci
+#define STLTYPE_INSTANTIATION2(STLTYPE, TTYPE1, TTYPE2, N) \
+ std::STLTYPE<TTYPE1, TTYPE2 > STLTYPE##_##N; \
+ std::pair<TTYPE1, TTYPE2 > STLTYPE##_##N##_p; \
+ std::pair<const TTYPE1, TTYPE2 > STLTYPE##_##N##_cp; \
+ std::STLTYPE<TTYPE1, TTYPE2 >::iterator STLTYPE##_##N##_i; \
+ std::STLTYPE<TTYPE1, TTYPE2 >::const_iterator STLTYPE##_##N##_ci
+
+
//- instantiations of used STL types
namespace {
@@ -28,9 +37,16 @@
struct _CppyyListInstances {
- STLTYPE_INSTANTIATION(list, int, 1);
- STLTYPE_INSTANTIATION(list, float, 2);
- STLTYPE_INSTANTIATION(list, double, 3);
+ STLTYPE_INSTANTIATION(list, int, 1);
+ STLTYPE_INSTANTIATION(list, float, 2);
+ STLTYPE_INSTANTIATION(list, double, 3);
+
+ };
+
+ struct _CppyyMapInstances {
+
+ STLTYPE_INSTANTIATION2(map, int, int, 1);
+ STLTYPE_INSTANTIATION2(map, std::string, int, 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
@@ -11,6 +11,11 @@
<class pattern="std::list<*>::const_iterator" />
<class name="std::__detail::_List_node_base" />
+ <class pattern="std::map<*>" />
+ <class pattern="std::pair<*>" />
+ <class pattern="std::map<*>::iterator" />
+ <class pattern="std::map<*>::const_iterator" />
+
<class pattern="__gnu_cxx::__normal_iterator<*>" />
<!-- the following are for testing, not for iteration -->
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
@@ -279,7 +279,7 @@
assert s == c.get_string1()
-class AppTestSTLSTRING:
+class AppTestSTLLIST:
spaceconfig = dict(usemodules=['cppyy'])
def setup_class(cls):
@@ -334,3 +334,92 @@
for arg in a:
pass
+
+class AppTestSTLMAP:
+ spaceconfig = dict(usemodules=['cppyy', 'itertools'])
+
+ def setup_class(cls):
+ cls.w_N = cls.space.wrap(13)
+ cls.w_test_dct = cls.space.wrap(test_dct)
+ cls.w_stlstring = cls.space.appexec([], """():
+ import cppyy, math
+ return cppyy.load_reflection_info(%r)""" % (test_dct, ))
+
+ def test01_builtin_map_type(self):
+ """Test access to a map<int,int>"""
+
+ import cppyy
+ std = cppyy.gbl.std
+
+ a = std.map(int, int)()
+ for i in range(self.N):
+ a[i] = i
+ assert a[i] == i
+
+ assert len(a) == self.N
+
+ for key, value in a:
+ assert key == value
+ assert key == self.N-1
+ assert value == self.N-1
+
+ # add a variation, just in case
+ m = std.map(int, int)()
+ for i in range(self.N):
+ m[i] = i*i
+ assert m[i] == i*i
+
+ for key, value in m:
+ assert key*key == value
+ assert key == self.N-1
+ assert value == (self.N-1)*(self.N-1)
+
+ def test02_keyed_maptype(self):
+ """Test access to a map<std::string,int>"""
+
+ import cppyy
+ std = cppyy.gbl.std
+
+ a = std.map(std.string, int)()
+ for i in range(self.N):
+ a[str(i)] = i
+ import pdb
+ pdb.set_trace()
+ assert a[str(i)] == i
+
+ assert len(a) == self.N
+
+ def test03_empty_maptype(self):
+ """Test behavior of empty map<int,int>"""
+
+ import cppyy
+ std = cppyy.gbl.std
+
+ m = std.map(int, int)()
+ for key, value in m:
+ pass
+
+ def test04_unsignedvalue_typemap_types(self):
+ """Test assignability of maps with unsigned value types"""
+
+ import cppyy, math
+ std = cppyy.gbl.std
+
+ mui = std.map(str, 'unsigned int')()
+ mui['one'] = 1
+ assert mui['one'] == 1
+ raises(ValueError, mui.__setitem__, 'minus one', -1)
+
+ # UInt_t is always 32b, sys.maxint follows system int
+ maxint32 = int(math.pow(2,31)-1)
+ mui['maxint'] = maxint32 + 3
+ assert mui['maxint'] == maxint32 + 3
+
+ mul = std.map(str, 'unsigned long')()
+ mul['two'] = 2
+ assert mul['two'] == 2
+ mul['maxint'] = maxvalue + 3
+ assert mul['maxint'] == maxvalue + 3
+
+ raises(ValueError, mul.__setitem__, 'minus two', -2)
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit