Debian Codegraph @

pypy/module/_cppyy/test/test_boost.py

raw symbols SCIP
1 import py, os, sys
2 from pytest import mark, raises
3 from .support import setup_make
4
5 noboost = False
6 if not (os.path.exists(os.path.join(os.path.sep, 'usr', 'include', 'boost')) or \
7 os.path.exists(os.path.join(os.path.sep, 'usr', 'local', 'include', 'boost'))):
8 noboost = True
9
10
11 @mark.skipif(noboost == True, reason="boost not found")
12 class AppTestBOOSTANY:
13 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
14
15 def setup_class(cls):
16 cls.space.appexec([], """():
17 import ctypes, _cppyy
18 _cppyy._post_import_startup()
19 _cppyy.gbl.gInterpreter.Declare('#include "boost/any.hpp"')
20 """)
21
22 def test01_any_class(self):
23 """Availability of boost::any"""
24
25 import _cppyy as cppyy
26
27 assert cppyy.gbl.boost.any
28
29 std = cppyy.gbl.std
30 any = cppyy.gbl.boost.any
31
32 assert std.list[any]
33
34 def test02_any_usage(self):
35 """boost::any assignment and casting"""
36
37 import _cppyy as cppyy
38
39 assert cppyy.gbl.boost
40
41 std = cppyy.gbl.std
42 boost = cppyy.gbl.boost
43
44 val = boost.any()
45 # test both by-ref and by rvalue
46 v = std.vector[int]()
47 val.__assign__(v)
48 val.__assign__(std.move(std.vector[int](range(100))))
49 assert val.type() == cppyy.typeid(std.vector[int])
50
51 extract = boost.any_cast[std.vector[int]](val)
52 assert type(extract) is std.vector[int]
53 assert len(extract) == 100
54 extract += range(100)
55 assert len(extract) == 200
56
57 val.__assign__(std.move(extract)) # move forced
58 #assert len(extract) == 0 # not guaranteed by the standard
59
60 # TODO: we hit boost::any_cast<int>(boost::any* operand) instead
61 # of the reference version which raises
62 boost.any_cast.__useffi__ = False
63 try:
64 # raises(Exception, boost.any_cast[int], val)
65 assert not boost.any_cast[int](val)
66 except Exception:
67 # getting here is good, too ...
68 pass
69
70 extract = boost.any_cast[std.vector[int]](val)
71 assert len(extract) == 200
72
73
74 @mark.skipif(noboost == True, reason="boost not found")
75 class AppTestBOOSTOPERATORS:
76 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
77
78 def setup_class(cls):
79 cls.space.appexec([], """():
80 import ctypes, _cppyy
81 _cppyy._post_import_startup()
82 _cppyy.gbl.gInterpreter.Declare('#include "boost/operators.hpp"')
83 """)
84
85 def test01_ordered(self):
86 """ordered_field_operators as base used to crash"""
87
88 import _cppyy as cppyy
89
90 cppyy.gbl.gInterpreter.Declare('#include "gmpxx.h"')
91 cppyy.gbl.gInterpreter.Declare("""
92 namespace boost_test {
93 class Derived : boost::ordered_field_operators<Derived>, boost::ordered_field_operators<Derived, mpq_class> {};
94 }
95 """)
96
97 assert cppyy.gbl.boost_test.Derived
98
99
100 @mark.skipif(noboost == True, reason="boost not found")
101 class AppTestBOOSTVARIANT:
102 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
103
104 def setup_class(cls):
105 cls.space.appexec([], """():
106 import ctypes, _cppyy
107 _cppyy._post_import_startup()
108 _cppyy.gbl.gInterpreter.Declare('#include "boost/variant/variant.hpp"')
109 _cppyy.gbl.gInterpreter.Declare('#include "boost/variant/get.hpp"')
110 """)
111
112 def test01_variant_usage(self):
113 """boost::variant usage"""
114
115 # as posted on stackoverflow as example
116 import _cppyy as cppyy
117
118 try:
119 cpp = cppyy.gbl
120 except:
121 pass
122
123 cpp = cppyy.gbl
124 std = cpp.std
125 boost = cpp.boost
126
127 cppyy.gbl.gInterpreter.Declare("""namespace BV {
128 class A { };
129 class B { };
130 class C { }; } """)
131
132 VariantType = boost.variant['BV::A, BV::B, BV::C']
133 VariantTypeList = std.vector[VariantType]
134
135 v = VariantTypeList()
136
137 v.push_back(VariantType(cpp.BV.A()))
138 assert v.back().which() == 0
139 v.push_back(VariantType(cpp.BV.B()))
140 assert v.back().which() == 1
141 v.push_back(VariantType(cpp.BV.C()))
142 assert v.back().which() == 2
143
144 assert type(boost.get['BV::A'](v[0])) == cpp.BV.A
145 raises(Exception, boost.get['BV::B'], v[0])
146 assert type(boost.get['BV::B'](v[1])) == cpp.BV.B
147 assert type(boost.get['BV::C'](v[2])) == cpp.BV.C
148
149
150 @mark.skipif(noboost == True, reason="boost not found")
151 class AppTestBOOSTERASURE:
152 spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])
153
154 def setup_class(cls):
155 cls.space.appexec([], """():
156 import ctypes, _cppyy
157 _cppyy._post_import_startup()
158 _cppyy.gbl.gInterpreter.Declare('#include "boost/type_erasure/any.hpp"')
159 _cppyy.gbl.gInterpreter.Declare('#include "boost/type_erasure/member.hpp"')
160 """)
161
162 def test01_erasure_usage(self):
163 """boost::type_erasure usage"""
164
165 import _cppyy as cppyy
166
167 cppyy.gbl.gInterpreter.Declare("""
168 BOOST_TYPE_ERASURE_MEMBER((has_member_f), f, 0)
169
170 using LengthsInterface = boost::mpl::vector<
171 boost::type_erasure::copy_constructible<>,
172 has_member_f<std::vector<int>() const>>;
173
174 using Lengths = boost::type_erasure::any<LengthsInterface>;
175
176 struct Unerased {
177 std::vector<int> f() const { return std::vector<int>{}; }
178 };
179
180 Lengths lengths() {
181 return Unerased{};
182 }
183 """)
184
185 assert cppyy.gbl.lengths() is not None