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.

107 lines
3.4 KiB

  1. import pytest
  2. from pybind11_tests import ConstructorStats
  3. def test_operator_overloading():
  4. from pybind11_tests.operators import Vector2, Vector
  5. v1 = Vector2(1, 2)
  6. v2 = Vector(3, -1)
  7. assert str(v1) == "[1.000000, 2.000000]"
  8. assert str(v2) == "[3.000000, -1.000000]"
  9. assert str(v1 + v2) == "[4.000000, 1.000000]"
  10. assert str(v1 - v2) == "[-2.000000, 3.000000]"
  11. assert str(v1 - 8) == "[-7.000000, -6.000000]"
  12. assert str(v1 + 8) == "[9.000000, 10.000000]"
  13. assert str(v1 * 8) == "[8.000000, 16.000000]"
  14. assert str(v1 / 8) == "[0.125000, 0.250000]"
  15. assert str(8 - v1) == "[7.000000, 6.000000]"
  16. assert str(8 + v1) == "[9.000000, 10.000000]"
  17. assert str(8 * v1) == "[8.000000, 16.000000]"
  18. assert str(8 / v1) == "[8.000000, 4.000000]"
  19. assert str(v1 * v2) == "[3.000000, -2.000000]"
  20. assert str(v2 / v1) == "[3.000000, -0.500000]"
  21. v1 += 2 * v2
  22. assert str(v1) == "[7.000000, 0.000000]"
  23. v1 -= v2
  24. assert str(v1) == "[4.000000, 1.000000]"
  25. v1 *= 2
  26. assert str(v1) == "[8.000000, 2.000000]"
  27. v1 /= 16
  28. assert str(v1) == "[0.500000, 0.125000]"
  29. v1 *= v2
  30. assert str(v1) == "[1.500000, -0.125000]"
  31. v2 /= v1
  32. assert str(v2) == "[2.000000, 8.000000]"
  33. cstats = ConstructorStats.get(Vector2)
  34. assert cstats.alive() == 2
  35. del v1
  36. assert cstats.alive() == 1
  37. del v2
  38. assert cstats.alive() == 0
  39. assert cstats.values() == ['[1.000000, 2.000000]', '[3.000000, -1.000000]',
  40. '[4.000000, 1.000000]', '[-2.000000, 3.000000]',
  41. '[-7.000000, -6.000000]', '[9.000000, 10.000000]',
  42. '[8.000000, 16.000000]', '[0.125000, 0.250000]',
  43. '[7.000000, 6.000000]', '[9.000000, 10.000000]',
  44. '[8.000000, 16.000000]', '[8.000000, 4.000000]',
  45. '[3.000000, -2.000000]', '[3.000000, -0.500000]',
  46. '[6.000000, -2.000000]']
  47. assert cstats.default_constructions == 0
  48. assert cstats.copy_constructions == 0
  49. assert cstats.move_constructions >= 10
  50. assert cstats.copy_assignments == 0
  51. assert cstats.move_assignments == 0
  52. def test_operators_notimplemented():
  53. """#393: need to return NotSupported to ensure correct arithmetic operator behavior"""
  54. from pybind11_tests.operators import C1, C2
  55. c1, c2 = C1(), C2()
  56. assert c1 + c1 == 11
  57. assert c2 + c2 == 22
  58. assert c2 + c1 == 21
  59. assert c1 + c2 == 12
  60. def test_nested():
  61. """#328: first member in a class can't be used in operators"""
  62. from pybind11_tests.operators import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
  63. a = NestA()
  64. b = NestB()
  65. c = NestC()
  66. a += 10
  67. assert get_NestA(a) == 13
  68. b.a += 100
  69. assert get_NestA(b.a) == 103
  70. c.b.a += 1000
  71. assert get_NestA(c.b.a) == 1003
  72. b -= 1
  73. assert get_NestB(b) == 3
  74. c.b -= 3
  75. assert get_NestB(c.b) == 1
  76. c *= 7
  77. assert get_NestC(c) == 35
  78. abase = a.as_base()
  79. assert abase.value == -2
  80. a.as_base().value += 44
  81. assert abase.value == 42
  82. assert c.b.a.as_base().value == -2
  83. c.b.a.as_base().value += 44
  84. assert c.b.a.as_base().value == 42
  85. del c
  86. pytest.gc_collect()
  87. del a # Should't delete while abase is still alive
  88. pytest.gc_collect()
  89. assert abase.value == 42
  90. del abase, b
  91. pytest.gc_collect()