test_operator_overloading.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import pytest
  2. from pybind11_tests import operators as m
  3. from pybind11_tests import ConstructorStats
  4. def test_operator_overloading():
  5. v1 = m.Vector2(1, 2)
  6. v2 = m.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. assert hash(v1) == 4
  34. cstats = ConstructorStats.get(m.Vector2)
  35. assert cstats.alive() == 2
  36. del v1
  37. assert cstats.alive() == 1
  38. del v2
  39. assert cstats.alive() == 0
  40. assert cstats.values() == ['[1.000000, 2.000000]', '[3.000000, -1.000000]',
  41. '[4.000000, 1.000000]', '[-2.000000, 3.000000]',
  42. '[-7.000000, -6.000000]', '[9.000000, 10.000000]',
  43. '[8.000000, 16.000000]', '[0.125000, 0.250000]',
  44. '[7.000000, 6.000000]', '[9.000000, 10.000000]',
  45. '[8.000000, 16.000000]', '[8.000000, 4.000000]',
  46. '[3.000000, -2.000000]', '[3.000000, -0.500000]',
  47. '[6.000000, -2.000000]']
  48. assert cstats.default_constructions == 0
  49. assert cstats.copy_constructions == 0
  50. assert cstats.move_constructions >= 10
  51. assert cstats.copy_assignments == 0
  52. assert cstats.move_assignments == 0
  53. def test_operators_notimplemented():
  54. """#393: need to return NotSupported to ensure correct arithmetic operator behavior"""
  55. c1, c2 = m.C1(), m.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. a = m.NestA()
  63. b = m.NestB()
  64. c = m.NestC()
  65. a += 10
  66. assert m.get_NestA(a) == 13
  67. b.a += 100
  68. assert m.get_NestA(b.a) == 103
  69. c.b.a += 1000
  70. assert m.get_NestA(c.b.a) == 1003
  71. b -= 1
  72. assert m.get_NestB(b) == 3
  73. c.b -= 3
  74. assert m.get_NestB(c.b) == 1
  75. c *= 7
  76. assert m.get_NestC(c) == 35
  77. abase = a.as_base()
  78. assert abase.value == -2
  79. a.as_base().value += 44
  80. assert abase.value == 42
  81. assert c.b.a.as_base().value == -2
  82. c.b.a.as_base().value += 44
  83. assert c.b.a.as_base().value == 42
  84. del c
  85. pytest.gc_collect()
  86. del a # Shouldn't delete while abase is still alive
  87. pytest.gc_collect()
  88. assert abase.value == 42
  89. del abase, b
  90. pytest.gc_collect()