File: test_boost.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (185 lines) | stat: -rw-r--r-- 5,722 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import py, os, sys
from pytest import mark, raises
from .support import setup_make

noboost = False
if not (os.path.exists(os.path.join(os.path.sep, 'usr', 'include', 'boost')) or \
        os.path.exists(os.path.join(os.path.sep, 'usr', 'local', 'include', 'boost'))):
    noboost = True


@mark.skipif(noboost == True, reason="boost not found")
class AppTestBOOSTANY:
    spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])

    def setup_class(cls):
        cls.space.appexec([], """():
            import ctypes, _cppyy
            _cppyy._post_import_startup()
            _cppyy.gbl.gInterpreter.Declare('#include "boost/any.hpp"')
        """)

    def test01_any_class(self):
        """Availability of boost::any"""

        import _cppyy as cppyy

        assert cppyy.gbl.boost.any

        std = cppyy.gbl.std
        any = cppyy.gbl.boost.any

        assert std.list[any]

    def test02_any_usage(self):
        """boost::any assignment and casting"""

        import _cppyy as cppyy

        assert cppyy.gbl.boost

        std = cppyy.gbl.std
        boost = cppyy.gbl.boost

        val = boost.any()
        # test both by-ref and by rvalue
        v = std.vector[int]()
        val.__assign__(v)
        val.__assign__(std.move(std.vector[int](range(100))))
        assert val.type() == cppyy.typeid(std.vector[int])

        extract = boost.any_cast[std.vector[int]](val)
        assert type(extract) is std.vector[int]
        assert len(extract) == 100
        extract += range(100)
        assert len(extract) == 200

        val.__assign__(std.move(extract))   # move forced
        #assert len(extract) == 0      # not guaranteed by the standard

        # TODO: we hit boost::any_cast<int>(boost::any* operand) instead
        # of the reference version which raises
        boost.any_cast.__useffi__ = False
        try:
          # raises(Exception, boost.any_cast[int], val)
            assert not boost.any_cast[int](val)
        except Exception:
          # getting here is good, too ...
            pass

        extract = boost.any_cast[std.vector[int]](val)
        assert len(extract) == 200


@mark.skipif(noboost == True, reason="boost not found")
class AppTestBOOSTOPERATORS:
    spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])

    def setup_class(cls):
        cls.space.appexec([], """():
            import ctypes, _cppyy
            _cppyy._post_import_startup()
            _cppyy.gbl.gInterpreter.Declare('#include "boost/operators.hpp"')
        """)

    def test01_ordered(self):
        """ordered_field_operators as base used to crash"""

        import _cppyy as cppyy

        cppyy.gbl.gInterpreter.Declare('#include "gmpxx.h"')
        cppyy.gbl.gInterpreter.Declare("""
            namespace boost_test {
               class Derived : boost::ordered_field_operators<Derived>, boost::ordered_field_operators<Derived, mpq_class> {};
            }
        """)

        assert cppyy.gbl.boost_test.Derived


@mark.skipif(noboost == True, reason="boost not found")
class AppTestBOOSTVARIANT:
    spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])

    def setup_class(cls):
        cls.space.appexec([], """():
            import ctypes, _cppyy
            _cppyy._post_import_startup()
            _cppyy.gbl.gInterpreter.Declare('#include "boost/variant/variant.hpp"')
            _cppyy.gbl.gInterpreter.Declare('#include "boost/variant/get.hpp"')
        """)

    def test01_variant_usage(self):
        """boost::variant usage"""

      # as posted on stackoverflow as example
        import _cppyy as cppyy

        try:
            cpp   = cppyy.gbl
        except:
            pass

        cpp   = cppyy.gbl
        std   = cpp.std
        boost = cpp.boost

        cppyy.gbl.gInterpreter.Declare("""namespace BV {
          class A { };
          class B { };
          class C { }; } """)

        VariantType = boost.variant['BV::A, BV::B, BV::C']
        VariantTypeList = std.vector[VariantType]

        v = VariantTypeList()

        v.push_back(VariantType(cpp.BV.A()))
        assert v.back().which() == 0
        v.push_back(VariantType(cpp.BV.B()))
        assert v.back().which() == 1
        v.push_back(VariantType(cpp.BV.C()))
        assert v.back().which() == 2

        assert type(boost.get['BV::A'](v[0])) == cpp.BV.A
        raises(Exception, boost.get['BV::B'], v[0])
        assert type(boost.get['BV::B'](v[1])) == cpp.BV.B
        assert type(boost.get['BV::C'](v[2])) == cpp.BV.C


@mark.skipif(noboost == True, reason="boost not found")
class AppTestBOOSTERASURE:
    spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])

    def setup_class(cls):
        cls.space.appexec([], """():
            import ctypes, _cppyy
            _cppyy._post_import_startup()
            _cppyy.gbl.gInterpreter.Declare('#include "boost/type_erasure/any.hpp"')
            _cppyy.gbl.gInterpreter.Declare('#include "boost/type_erasure/member.hpp"')
        """)

    def test01_erasure_usage(self):
        """boost::type_erasure usage"""

        import _cppyy as cppyy

        cppyy.gbl.gInterpreter.Declare("""
            BOOST_TYPE_ERASURE_MEMBER((has_member_f), f, 0)

            using LengthsInterface = boost::mpl::vector<
                boost::type_erasure::copy_constructible<>,
                has_member_f<std::vector<int>() const>>;

            using Lengths = boost::type_erasure::any<LengthsInterface>;

            struct Unerased {
                std::vector<int> f() const { return std::vector<int>{}; }
            };

            Lengths lengths() {
                return Unerased{};
            }
        """)

        assert cppyy.gbl.lengths() is not None