Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:53

0001 import pytest
0002 
0003 from pybind11_tests import copy_move_policies as m
0004 
0005 
0006 def test_lacking_copy_ctor():
0007     with pytest.raises(RuntimeError) as excinfo:
0008         m.lacking_copy_ctor.get_one()
0009     assert "is non-copyable!" in str(excinfo.value)
0010 
0011 
0012 def test_lacking_move_ctor():
0013     with pytest.raises(RuntimeError) as excinfo:
0014         m.lacking_move_ctor.get_one()
0015     assert "is neither movable nor copyable!" in str(excinfo.value)
0016 
0017 
0018 def test_move_and_copy_casts():
0019     """Cast some values in C++ via custom type casters and count the number of moves/copies."""
0020 
0021     cstats = m.move_and_copy_cstats()
0022     c_m, c_mc, c_c = (
0023         cstats["MoveOnlyInt"],
0024         cstats["MoveOrCopyInt"],
0025         cstats["CopyOnlyInt"],
0026     )
0027 
0028     # The type move constructions/assignments below each get incremented: the move assignment comes
0029     # from the type_caster load; the move construction happens when extracting that via a cast or
0030     # loading into an argument.
0031     assert m.move_and_copy_casts(3) == 18
0032     assert c_m.copy_assignments + c_m.copy_constructions == 0
0033     assert c_m.move_assignments == 2
0034     assert c_m.move_constructions >= 2
0035     assert c_mc.alive() == 0
0036     assert c_mc.copy_assignments + c_mc.copy_constructions == 0
0037     assert c_mc.move_assignments == 2
0038     assert c_mc.move_constructions >= 2
0039     assert c_c.alive() == 0
0040     assert c_c.copy_assignments == 2
0041     assert c_c.copy_constructions >= 2
0042     assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
0043 
0044 
0045 def test_move_and_copy_loads():
0046     """Call some functions that load arguments via custom type casters and count the number of
0047     moves/copies."""
0048 
0049     cstats = m.move_and_copy_cstats()
0050     c_m, c_mc, c_c = (
0051         cstats["MoveOnlyInt"],
0052         cstats["MoveOrCopyInt"],
0053         cstats["CopyOnlyInt"],
0054     )
0055 
0056     assert m.move_only(10) == 10  # 1 move, c_m
0057     assert m.move_or_copy(11) == 11  # 1 move, c_mc
0058     assert m.copy_only(12) == 12  # 1 copy, c_c
0059     assert m.move_pair((13, 14)) == 27  # 1 c_m move, 1 c_mc move
0060     assert m.move_tuple((15, 16, 17)) == 48  # 2 c_m moves, 1 c_mc move
0061     assert m.copy_tuple((18, 19)) == 37  # 2 c_c copies
0062     # Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
0063     # Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
0064     assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
0065 
0066     assert c_m.copy_assignments + c_m.copy_constructions == 0
0067     assert c_m.move_assignments == 6
0068     assert c_m.move_constructions == 9
0069     assert c_mc.copy_assignments + c_mc.copy_constructions == 0
0070     assert c_mc.move_assignments == 5
0071     assert c_mc.move_constructions == 8
0072     assert c_c.copy_assignments == 4
0073     assert c_c.copy_constructions == 6
0074     assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
0075 
0076 
0077 @pytest.mark.skipif(not m.has_optional, reason="no <optional>")
0078 def test_move_and_copy_load_optional():
0079     """Tests move/copy loads of std::optional arguments"""
0080 
0081     cstats = m.move_and_copy_cstats()
0082     c_m, c_mc, c_c = (
0083         cstats["MoveOnlyInt"],
0084         cstats["MoveOrCopyInt"],
0085         cstats["CopyOnlyInt"],
0086     )
0087 
0088     # The extra move/copy constructions below come from the std::optional move (which has to move
0089     # its arguments):
0090     assert m.move_optional(10) == 10  # c_m: 1 move assign, 2 move construct
0091     assert m.move_or_copy_optional(11) == 11  # c_mc: 1 move assign, 2 move construct
0092     assert m.copy_optional(12) == 12  # c_c: 1 copy assign, 2 copy construct
0093     # 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
0094     # +1 move/copy construct each from moving the tuple
0095     # +1 move/copy construct each from moving the optional (which moves the tuple again)
0096     assert m.move_optional_tuple((3, 4, 5)) == 12
0097 
0098     assert c_m.copy_assignments + c_m.copy_constructions == 0
0099     assert c_m.move_assignments == 2
0100     assert c_m.move_constructions == 5
0101     assert c_mc.copy_assignments + c_mc.copy_constructions == 0
0102     assert c_mc.move_assignments == 2
0103     assert c_mc.move_constructions == 5
0104     assert c_c.copy_assignments == 2
0105     assert c_c.copy_constructions == 5
0106     assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
0107 
0108 
0109 def test_private_op_new():
0110     """An object with a private `operator new` cannot be returned by value"""
0111 
0112     with pytest.raises(RuntimeError) as excinfo:
0113         m.private_op_new_value()
0114     assert "is neither movable nor copyable" in str(excinfo.value)
0115 
0116     assert m.private_op_new_reference().value == 1
0117 
0118 
0119 def test_move_fallback():
0120     """#389: rvp::move should fall-through to copy on non-movable objects"""
0121 
0122     m1 = m.get_moveissue1(1)
0123     assert m1.value == 1
0124     m2 = m.get_moveissue2(2)
0125     assert m2.value == 2
0126 
0127 
0128 def test_pytype_rvalue_cast():
0129     """Make sure that cast from pytype rvalue to other pytype works"""
0130 
0131     value = m.get_pytype_rvalue_castissue(1.0)
0132     assert value == 1