You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.7 KiB

  1. from pybind11_tests import ConstructorStats
  2. def test_smart_ptr(capture):
  3. # Object1
  4. from pybind11_tests import (MyObject1, make_object_1, make_object_2,
  5. print_object_1, print_object_2, print_object_3, print_object_4)
  6. for i, o in enumerate([make_object_1(), make_object_2(), MyObject1(3)], start=1):
  7. assert o.getRefCount() == 1
  8. with capture:
  9. print_object_1(o)
  10. print_object_2(o)
  11. print_object_3(o)
  12. print_object_4(o)
  13. assert capture == "MyObject1[{i}]\n".format(i=i) * 4
  14. from pybind11_tests import (make_myobject1_1, make_myobject1_2,
  15. print_myobject1_1, print_myobject1_2,
  16. print_myobject1_3, print_myobject1_4)
  17. for i, o in enumerate([make_myobject1_1(), make_myobject1_2(), MyObject1(6), 7], start=4):
  18. print(o)
  19. with capture:
  20. if not isinstance(o, int):
  21. print_object_1(o)
  22. print_object_2(o)
  23. print_object_3(o)
  24. print_object_4(o)
  25. print_myobject1_1(o)
  26. print_myobject1_2(o)
  27. print_myobject1_3(o)
  28. print_myobject1_4(o)
  29. assert capture == "MyObject1[{i}]\n".format(i=i) * (4 if isinstance(o, int) else 8)
  30. cstats = ConstructorStats.get(MyObject1)
  31. assert cstats.alive() == 0
  32. expected_values = ['MyObject1[{}]'.format(i) for i in range(1, 7)] + ['MyObject1[7]'] * 4
  33. assert cstats.values() == expected_values
  34. assert cstats.default_constructions == 0
  35. assert cstats.copy_constructions == 0
  36. # assert cstats.move_constructions >= 0 # Doesn't invoke any
  37. assert cstats.copy_assignments == 0
  38. assert cstats.move_assignments == 0
  39. # Object2
  40. from pybind11_tests import (MyObject2, make_myobject2_1, make_myobject2_2,
  41. make_myobject3_1, make_myobject3_2,
  42. print_myobject2_1, print_myobject2_2,
  43. print_myobject2_3, print_myobject2_4)
  44. for i, o in zip([8, 6, 7], [MyObject2(8), make_myobject2_1(), make_myobject2_2()]):
  45. print(o)
  46. with capture:
  47. print_myobject2_1(o)
  48. print_myobject2_2(o)
  49. print_myobject2_3(o)
  50. print_myobject2_4(o)
  51. assert capture == "MyObject2[{i}]\n".format(i=i) * 4
  52. cstats = ConstructorStats.get(MyObject2)
  53. assert cstats.alive() == 1
  54. o = None
  55. assert cstats.alive() == 0
  56. assert cstats.values() == ['MyObject2[8]', 'MyObject2[6]', 'MyObject2[7]']
  57. assert cstats.default_constructions == 0
  58. assert cstats.copy_constructions == 0
  59. # assert cstats.move_constructions >= 0 # Doesn't invoke any
  60. assert cstats.copy_assignments == 0
  61. assert cstats.move_assignments == 0
  62. # Object3
  63. from pybind11_tests import (MyObject3, print_myobject3_1, print_myobject3_2,
  64. print_myobject3_3, print_myobject3_4)
  65. for i, o in zip([9, 8, 9], [MyObject3(9), make_myobject3_1(), make_myobject3_2()]):
  66. print(o)
  67. with capture:
  68. print_myobject3_1(o)
  69. print_myobject3_2(o)
  70. print_myobject3_3(o)
  71. print_myobject3_4(o)
  72. assert capture == "MyObject3[{i}]\n".format(i=i) * 4
  73. cstats = ConstructorStats.get(MyObject3)
  74. assert cstats.alive() == 1
  75. o = None
  76. assert cstats.alive() == 0
  77. assert cstats.values() == ['MyObject3[9]', 'MyObject3[8]', 'MyObject3[9]']
  78. assert cstats.default_constructions == 0
  79. assert cstats.copy_constructions == 0
  80. # assert cstats.move_constructions >= 0 # Doesn't invoke any
  81. assert cstats.copy_assignments == 0
  82. assert cstats.move_assignments == 0
  83. # Object and ref
  84. from pybind11_tests import Object, cstats_ref
  85. cstats = ConstructorStats.get(Object)
  86. assert cstats.alive() == 0
  87. assert cstats.values() == []
  88. assert cstats.default_constructions == 10
  89. assert cstats.copy_constructions == 0
  90. # assert cstats.move_constructions >= 0 # Doesn't invoke any
  91. assert cstats.copy_assignments == 0
  92. assert cstats.move_assignments == 0
  93. cstats = cstats_ref()
  94. assert cstats.alive() == 0
  95. assert cstats.values() == ['from pointer'] * 10
  96. assert cstats.default_constructions == 30
  97. assert cstats.copy_constructions == 12
  98. # assert cstats.move_constructions >= 0 # Doesn't invoke any
  99. assert cstats.copy_assignments == 30
  100. assert cstats.move_assignments == 0
  101. def test_unique_nodelete():
  102. from pybind11_tests import MyObject4
  103. o = MyObject4(23)
  104. assert o.value == 23
  105. cstats = ConstructorStats.get(MyObject4)
  106. assert cstats.alive() == 1
  107. del o
  108. cstats = ConstructorStats.get(MyObject4)
  109. assert cstats.alive() == 1 # Leak, but that's intentional