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