test_copy_move.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 
5 from pybind11_tests import copy_move_policies as m
6 
7 
9  with pytest.raises(RuntimeError) as excinfo:
10  m.lacking_copy_ctor.get_one()
11  assert "is non-copyable!" in str(excinfo.value)
12 
13 
15  with pytest.raises(RuntimeError) as excinfo:
16  m.lacking_move_ctor.get_one()
17  assert "is neither movable nor copyable!" in str(excinfo.value)
18 
19 
21  """Cast some values in C++ via custom type casters and count the number of moves/copies."""
22 
23  cstats = m.move_and_copy_cstats()
24  c_m, c_mc, c_c = (
25  cstats["MoveOnlyInt"],
26  cstats["MoveOrCopyInt"],
27  cstats["CopyOnlyInt"],
28  )
29 
30  # The type move constructions/assignments below each get incremented: the move assignment comes
31  # from the type_caster load; the move construction happens when extracting that via a cast or
32  # loading into an argument.
33  assert m.move_and_copy_casts(3) == 18
34  assert c_m.copy_assignments + c_m.copy_constructions == 0
35  assert c_m.move_assignments == 2
36  assert c_m.move_constructions >= 2
37  assert c_mc.alive() == 0
38  assert c_mc.copy_assignments + c_mc.copy_constructions == 0
39  assert c_mc.move_assignments == 2
40  assert c_mc.move_constructions >= 2
41  assert c_c.alive() == 0
42  assert c_c.copy_assignments == 2
43  assert c_c.copy_constructions >= 2
44  assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
45 
46 
48  """Call some functions that load arguments via custom type casters and count the number of
49  moves/copies."""
50 
51  cstats = m.move_and_copy_cstats()
52  c_m, c_mc, c_c = (
53  cstats["MoveOnlyInt"],
54  cstats["MoveOrCopyInt"],
55  cstats["CopyOnlyInt"],
56  )
57 
58  assert m.move_only(10) == 10 # 1 move, c_m
59  assert m.move_or_copy(11) == 11 # 1 move, c_mc
60  assert m.copy_only(12) == 12 # 1 copy, c_c
61  assert m.move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc move
62  assert m.move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc move
63  assert m.copy_tuple((18, 19)) == 37 # 2 c_c copies
64  # Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
65  # Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
66  assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
67 
68  assert c_m.copy_assignments + c_m.copy_constructions == 0
69  assert c_m.move_assignments == 6
70  assert c_m.move_constructions == 9
71  assert c_mc.copy_assignments + c_mc.copy_constructions == 0
72  assert c_mc.move_assignments == 5
73  assert c_mc.move_constructions == 8
74  assert c_c.copy_assignments == 4
75  assert c_c.copy_constructions == 6
76  assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
77 
78 
79 @pytest.mark.skipif(not m.has_optional, reason="no <optional>")
81  """Tests move/copy loads of std::optional arguments"""
82 
83  cstats = m.move_and_copy_cstats()
84  c_m, c_mc, c_c = (
85  cstats["MoveOnlyInt"],
86  cstats["MoveOrCopyInt"],
87  cstats["CopyOnlyInt"],
88  )
89 
90  # The extra move/copy constructions below come from the std::optional move (which has to move
91  # its arguments):
92  assert m.move_optional(10) == 10 # c_m: 1 move assign, 2 move construct
93  assert m.move_or_copy_optional(11) == 11 # c_mc: 1 move assign, 2 move construct
94  assert m.copy_optional(12) == 12 # c_c: 1 copy assign, 2 copy construct
95  # 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
96  # +1 move/copy construct each from moving the tuple
97  # +1 move/copy construct each from moving the optional (which moves the tuple again)
98  assert m.move_optional_tuple((3, 4, 5)) == 12
99 
100  assert c_m.copy_assignments + c_m.copy_constructions == 0
101  assert c_m.move_assignments == 2
102  assert c_m.move_constructions == 5
103  assert c_mc.copy_assignments + c_mc.copy_constructions == 0
104  assert c_mc.move_assignments == 2
105  assert c_mc.move_constructions == 5
106  assert c_c.copy_assignments == 2
107  assert c_c.copy_constructions == 5
108  assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
109 
110 
112  """An object with a private `operator new` cannot be returned by value"""
113 
114  with pytest.raises(RuntimeError) as excinfo:
115  m.private_op_new_value()
116  assert "is neither movable nor copyable" in str(excinfo.value)
117 
118  assert m.private_op_new_reference().value == 1
119 
120 
122  """#389: rvp::move should fall-through to copy on non-movable objects"""
123 
124  m1 = m.get_moveissue1(1)
125  assert m1.value == 1
126  m2 = m.get_moveissue2(2)
127  assert m2.value == 2
128 
129 
131  """Make sure that cast from pytype rvalue to other pytype works"""
132 
133  value = m.get_pytype_rvalue_castissue(1.0)
134  assert value == 1
135 
136 
138  # Merely to test that this still exists and built successfully.
139  assert m.CallCastUnusualOpRefConstRef().__class__.__name__ == "UnusualOpRef"
140  assert m.CallCastUnusualOpRefMovable().__class__.__name__ == "UnusualOpRef"
test_copy_move.test_move_and_copy_load_optional
def test_move_and_copy_load_optional()
Definition: test_copy_move.py:80
test_copy_move.test_private_op_new
def test_private_op_new()
Definition: test_copy_move.py:111
test_copy_move.test_move_and_copy_casts
def test_move_and_copy_casts()
Definition: test_copy_move.py:20
test_copy_move.test_move_fallback
def test_move_fallback()
Definition: test_copy_move.py:121
str
Definition: pytypes.h:1558
test_copy_move.test_move_and_copy_loads
def test_move_and_copy_loads()
Definition: test_copy_move.py:47
test_copy_move.test_unusual_op_ref
def test_unusual_op_ref()
Definition: test_copy_move.py:137
test_copy_move.test_lacking_move_ctor
def test_lacking_move_ctor()
Definition: test_copy_move.py:14
test_copy_move.test_pytype_rvalue_cast
def test_pytype_rvalue_cast()
Definition: test_copy_move.py:130
test_copy_move.test_lacking_copy_ctor
def test_lacking_copy_ctor()
Definition: test_copy_move.py:8


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:00