File: test_cpp11features.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (95 lines) | stat: -rw-r--r-- 2,721 bytes parent folder | download | duplicates (3)
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
import py, os, sys
from .support import setup_make


currpath = py.path.local(__file__).dirpath()
test_dct = str(currpath.join("cpp11featuresDict.so"))

def setup_module(mod):
    setup_make("cpp11featuresDict.so")

class AppTestCPP11FEATURES:
    spaceconfig = dict(usemodules=['_cppyy', '_rawffi', 'itertools'])

    def setup_class(cls):
        cls.w_test_dct  = cls.space.newtext(test_dct)
        cls.w_example01 = cls.space.appexec([], """():
            import ctypes, _cppyy
            _cppyy._post_import_startup()
            return ctypes.CDLL(%r, ctypes.RTLD_GLOBAL)""" % (test_dct, ))

    def test01_shared_ptr(self):
        """Usage and access of std::shared_ptr<>"""

        import _cppyy
        TestSharedPtr = _cppyy.gbl.TestSharedPtr
        create_shared_ptr_instance = _cppyy.gbl.create_shared_ptr_instance

      # proper memory accounting
        assert TestSharedPtr.s_counter == 0

        ptr1 = create_shared_ptr_instance()
        assert ptr1
        assert not not ptr1
        assert TestSharedPtr.s_counter == 1

        ptr2 = create_shared_ptr_instance()
        assert ptr2
        assert not not ptr2
        assert TestSharedPtr.s_counter == 2

        del ptr2
        import gc; gc.collect()
        assert TestSharedPtr.s_counter == 1

        del ptr1
        gc.collect()
        assert TestSharedPtr.s_counter == 0

    def test02_nullptr(self):
        """Allow the programmer to pass NULL in certain cases"""

        import _cppyy

      # test existence
        nullptr = _cppyy.nullptr
        assert not hasattr(_cppyy.gbl, 'nullptr')

      # usage is tested in datatypes.py:test15_nullptr_passing

    def test03_move(self):
        """Move construction, assignment, and methods"""

        import _cppyy

        def moveit(T):
            std = _cppyy.gbl.std

          # move constructor
            i1 = T()
            assert T.s_move_counter == 0

            i2 = T(i1)  # cctor
            assert T.s_move_counter == 0

            i3 = T(std.move(T())) # Note: in CPython can check for
                                  # ref-count == 1, so no move() needed
            assert T.s_move_counter == 1

            i4 = T(std.move(i1))
            assert T.s_move_counter == 2

          # move assignment
            i4.__assign__(i2)
            assert T.s_move_counter == 2

            i4.__assign__(std.move(T())) # same note as above move ctor
            assert T.s_move_counter == 3

            i4.__assign__(std.move(i2))
            assert T.s_move_counter == 4

      # order of moving and normal functions are reversed in 1, 2, for
      # overload resolution testing
        moveit(_cppyy.gbl.TestMoving1)
        moveit(_cppyy.gbl.TestMoving2)