test_copy_move.py 4.4 KB

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