test_copy_move.py 4.5 KB

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