Skip to content

Commit aa7d99e

Browse files
committed
Adapt ConstructorStats of unittests
As we prefer copying over moving now (if not explicitly asked for otherwise), the constructor stats shift from moving towards copying.
1 parent b4dd7c4 commit aa7d99e

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

tests/test_copy_move.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ TEST_SUBMODULE(copy_move_policies, m) {
111111
// test_move_and_copy_casts
112112
m.def("move_and_copy_casts", [](py::object o) {
113113
int r = 0;
114-
r += py::cast<MoveOrCopyInt>(o).value; /* moves */
114+
r += py::cast<MoveOrCopyInt>(o).value; /* copies */
115115
r += py::cast<MoveOnlyInt>(o).value; /* moves */
116116
r += py::cast<CopyOnlyInt>(o).value; /* copies */
117-
auto m1(py::cast<MoveOrCopyInt>(o)); /* moves */
117+
auto m1(py::cast<MoveOrCopyInt>(o)); /* copies */
118118
auto m2(py::cast<MoveOnlyInt>(o)); /* moves */
119119
auto m3(py::cast<CopyOnlyInt>(o)); /* copies */
120120
r += m1.value + m2.value + m3.value;
@@ -124,7 +124,8 @@ TEST_SUBMODULE(copy_move_policies, m) {
124124

125125
// test_move_and_copy_loads
126126
m.def("move_only", [](MoveOnlyInt m) { return m.value; });
127-
m.def("move_or_copy", [](MoveOrCopyInt m) { return m.value; });
127+
m.def("move", [](MoveOrCopyInt&& m) { return m.value; });
128+
m.def("copy", [](MoveOrCopyInt m) { return m.value; });
128129
m.def("copy_only", [](CopyOnlyInt m) { return m.value; });
129130
m.def("move_pair", [](std::pair<MoveOnlyInt, MoveOrCopyInt> p) {
130131
return p.first.value + p.second.value;
@@ -184,15 +185,10 @@ TEST_SUBMODULE(copy_move_policies, m) {
184185
#ifdef PYBIND11_HAS_OPTIONAL
185186
// test_move_and_copy_load_optional
186187
m.attr("has_optional") = true;
187-
m.def("move_optional", [](std::optional<MoveOnlyInt> o) {
188-
return o->value;
189-
});
190-
m.def("move_or_copy_optional", [](std::optional<MoveOrCopyInt> o) {
191-
return o->value;
192-
});
193-
m.def("copy_optional", [](std::optional<CopyOnlyInt> o) {
194-
return o->value;
195-
});
188+
m.def("move_optional", [](std::optional<MoveOnlyInt> o) { return o->value; });
189+
m.def("mc_move_optional", [](std::optional<MoveOrCopyInt>&& o) { return o->value; });
190+
m.def("mc_copy_optional", [](std::optional<MoveOrCopyInt> o) { return o->value; });
191+
m.def("copy_optional", [](std::optional<CopyOnlyInt> o) { return o->value; });
196192
m.def("move_optional_tuple", [](std::optional<std::tuple<MoveOrCopyInt, MoveOnlyInt, CopyOnlyInt>> x) {
197193
return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value;
198194
});

tests/test_copy_move.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ def test_move_and_copy_casts():
2626
# The type move constructions/assignments below each get incremented: the move assignment comes
2727
# from the type_caster load; the move construction happens when extracting that via a cast or
2828
# loading into an argument.
29-
assert m.move_and_copy_casts(3) == 18
29+
assert m.move_and_copy_casts(3) == 6 * 3
3030
assert c_m.copy_assignments + c_m.copy_constructions == 0
3131
assert c_m.move_assignments == 2
3232
assert c_m.move_constructions >= 2
3333
assert c_mc.alive() == 0
34-
assert c_mc.copy_assignments + c_mc.copy_constructions == 0
34+
assert c_mc.copy_assignments == 0
35+
assert c_mc.copy_constructions == 2
3536
assert c_mc.move_assignments == 2
36-
assert c_mc.move_constructions >= 2
37+
assert c_mc.move_constructions == 0
3738
assert c_c.alive() == 0
3839
assert c_c.copy_assignments == 2
3940
assert c_c.copy_constructions >= 2
@@ -50,21 +51,23 @@ def test_move_and_copy_loads():
5051
)
5152

5253
assert m.move_only(10) == 10 # 1 move, c_m
53-
assert m.move_or_copy(11) == 11 # 1 move, c_mc
54+
assert m.move(10) == 10 # 0 move (just ref), c_mc
55+
assert m.copy(11) == 11 # 1 copy, c_mc
5456
assert m.copy_only(12) == 12 # 1 copy, c_c
55-
assert m.move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc move
56-
assert m.move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc move
57+
assert m.move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc copy
58+
assert m.move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc copy
5759
assert m.copy_tuple((18, 19)) == 37 # 2 c_c copies
58-
# Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
60+
# Direct constructions: 2 c_m moves, 2 c_mc copies, 1 c_c copy
5961
# Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
6062
assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
6163

6264
assert c_m.copy_assignments + c_m.copy_constructions == 0
6365
assert c_m.move_assignments == 6
6466
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
67+
assert c_mc.copy_assignments == 0
68+
assert c_mc.copy_constructions == 5
69+
assert c_mc.move_assignments == 6
70+
assert c_mc.move_constructions == 3
6871
assert c_c.copy_assignments == 4
6972
assert c_c.copy_constructions == 6
7073
assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
@@ -111,8 +114,9 @@ def test_move_and_copy_load_optional():
111114

112115
# The extra move/copy constructions below come from the std::optional move (which has to move
113116
# its arguments):
114-
assert m.move_optional(10) == 10 # c_m: 1 move assign, 2 move construct
115-
assert m.move_or_copy_optional(11) == 11 # c_mc: 1 move assign, 2 move construct
117+
assert m.move_optional(10) == 10 # c_m: 1 move assign, +1 move construct
118+
assert m.mc_move_optional(10) == 10 # c_mc: 1 move assign, +0 move construct
119+
assert m.mc_copy_optional(11) == 11 # c_mc: 1 move assign, 1 copy construct
116120
assert m.copy_optional(12) == 12 # c_c: 1 copy assign, 2 copy construct
117121
# 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
118122
# +1 move/copy construct each from moving the tuple
@@ -122,9 +126,10 @@ def test_move_and_copy_load_optional():
122126
assert c_m.copy_assignments + c_m.copy_constructions == 0
123127
assert c_m.move_assignments == 2
124128
assert c_m.move_constructions == 5
125-
assert c_mc.copy_assignments + c_mc.copy_constructions == 0
126-
assert c_mc.move_assignments == 2
127-
assert c_mc.move_constructions == 5
129+
assert c_mc.copy_assignments == 0
130+
assert c_mc.copy_constructions == 2
131+
assert c_mc.move_assignments == 3
132+
assert c_mc.move_constructions == 4
128133
assert c_c.copy_assignments == 2
129134
assert c_c.copy_constructions == 5
130135
assert c_m.alive() + c_mc.alive() + c_c.alive() == 0

tests/test_kwargs_and_defaults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_args_refcount():
244244
myval = 54321
245245
expected = refcount(myval)
246246
assert m.arg_refcount_h(myval) == expected
247-
assert m.arg_refcount_o(myval) == expected + 1
247+
assert m.arg_refcount_o(myval) == expected + 2
248248
assert m.arg_refcount_h(myval) == expected
249249
assert refcount(myval) == expected
250250

@@ -280,6 +280,6 @@ def test_args_refcount():
280280
# for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input
281281
# tuple without having to inc_ref the individual elements, but here we can't, hence the extra
282282
# refs.
283-
assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3)
283+
assert m.mixed_args_refcount(myval, myval, myval) == (exp3 + 4, exp3 + 4, exp3 + 4)
284284

285285
assert m.class_default_argument() == "<class 'decimal.Decimal'>"

0 commit comments

Comments
 (0)