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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03