File indexing completed on 2025-01-18 10:17:56
0001 import pytest
0002
0003 from pybind11_tests import ConstructorStats
0004 from pybind11_tests import operators as m
0005
0006
0007 def test_operator_overloading():
0008 v1 = m.Vector2(1, 2)
0009 v2 = m.Vector(3, -1)
0010 v3 = m.Vector2(1, 2)
0011 assert v1 is not v3
0012
0013 assert str(v1) == "[1.000000, 2.000000]"
0014 assert str(v2) == "[3.000000, -1.000000]"
0015
0016 assert str(-v2) == "[-3.000000, 1.000000]"
0017
0018 assert str(v1 + v2) == "[4.000000, 1.000000]"
0019 assert str(v1 - v2) == "[-2.000000, 3.000000]"
0020 assert str(v1 - 8) == "[-7.000000, -6.000000]"
0021 assert str(v1 + 8) == "[9.000000, 10.000000]"
0022 assert str(v1 * 8) == "[8.000000, 16.000000]"
0023 assert str(v1 / 8) == "[0.125000, 0.250000]"
0024 assert str(8 - v1) == "[7.000000, 6.000000]"
0025 assert str(8 + v1) == "[9.000000, 10.000000]"
0026 assert str(8 * v1) == "[8.000000, 16.000000]"
0027 assert str(8 / v1) == "[8.000000, 4.000000]"
0028 assert str(v1 * v2) == "[3.000000, -2.000000]"
0029 assert str(v2 / v1) == "[3.000000, -0.500000]"
0030
0031 assert v1 == v3
0032 assert v1 != v2
0033 assert hash(v1) == 4
0034
0035
0036
0037 v1 += 2 * v2
0038 assert str(v1) == "[7.000000, 0.000000]"
0039 v1 -= v2
0040 assert str(v1) == "[4.000000, 1.000000]"
0041 v1 *= 2
0042 assert str(v1) == "[8.000000, 2.000000]"
0043 v1 /= 16
0044 assert str(v1) == "[0.500000, 0.125000]"
0045 v1 *= v2
0046 assert str(v1) == "[1.500000, -0.125000]"
0047 v2 /= v1
0048 assert str(v2) == "[2.000000, 8.000000]"
0049
0050 cstats = ConstructorStats.get(m.Vector2)
0051 assert cstats.alive() == 3
0052 del v1
0053 assert cstats.alive() == 2
0054 del v2
0055 assert cstats.alive() == 1
0056 del v3
0057 assert cstats.alive() == 0
0058 assert cstats.values() == [
0059 "[1.000000, 2.000000]",
0060 "[3.000000, -1.000000]",
0061 "[1.000000, 2.000000]",
0062 "[-3.000000, 1.000000]",
0063 "[4.000000, 1.000000]",
0064 "[-2.000000, 3.000000]",
0065 "[-7.000000, -6.000000]",
0066 "[9.000000, 10.000000]",
0067 "[8.000000, 16.000000]",
0068 "[0.125000, 0.250000]",
0069 "[7.000000, 6.000000]",
0070 "[9.000000, 10.000000]",
0071 "[8.000000, 16.000000]",
0072 "[8.000000, 4.000000]",
0073 "[3.000000, -2.000000]",
0074 "[3.000000, -0.500000]",
0075 "[6.000000, -2.000000]",
0076 ]
0077 assert cstats.default_constructions == 0
0078 assert cstats.copy_constructions == 0
0079 assert cstats.move_constructions >= 10
0080 assert cstats.copy_assignments == 0
0081 assert cstats.move_assignments == 0
0082
0083
0084 def test_operators_notimplemented():
0085 """#393: need to return NotSupported to ensure correct arithmetic operator behavior"""
0086
0087 c1, c2 = m.C1(), m.C2()
0088 assert c1 + c1 == 11
0089 assert c2 + c2 == 22
0090 assert c2 + c1 == 21
0091 assert c1 + c2 == 12
0092
0093
0094 def test_nested():
0095 """#328: first member in a class can't be used in operators"""
0096
0097 a = m.NestA()
0098 b = m.NestB()
0099 c = m.NestC()
0100
0101 a += 10
0102 assert m.get_NestA(a) == 13
0103 b.a += 100
0104 assert m.get_NestA(b.a) == 103
0105 c.b.a += 1000
0106 assert m.get_NestA(c.b.a) == 1003
0107 b -= 1
0108 assert m.get_NestB(b) == 3
0109 c.b -= 3
0110 assert m.get_NestB(c.b) == 1
0111 c *= 7
0112 assert m.get_NestC(c) == 35
0113
0114 abase = a.as_base()
0115 assert abase.value == -2
0116 a.as_base().value += 44
0117 assert abase.value == 42
0118 assert c.b.a.as_base().value == -2
0119 c.b.a.as_base().value += 44
0120 assert c.b.a.as_base().value == 42
0121
0122 del c
0123 pytest.gc_collect()
0124 del a
0125 pytest.gc_collect()
0126
0127 assert abase.value == 42
0128 del abase, b
0129 pytest.gc_collect()
0130
0131
0132 def test_overriding_eq_reset_hash():
0133
0134 assert m.Comparable(15) is not m.Comparable(15)
0135 assert m.Comparable(15) == m.Comparable(15)
0136
0137 with pytest.raises(TypeError) as excinfo:
0138 hash(m.Comparable(15))
0139 assert str(excinfo.value).startswith("unhashable type:")
0140
0141 for hashable in (m.Hashable, m.Hashable2):
0142 assert hashable(15) is not hashable(15)
0143 assert hashable(15) == hashable(15)
0144
0145 assert hash(hashable(15)) == 15
0146 assert hash(hashable(15)) == hash(hashable(15))
0147
0148
0149 def test_return_set_of_unhashable():
0150 with pytest.raises(TypeError) as excinfo:
0151 m.get_unhashable_HashMe_set()
0152 assert str(excinfo.value.__cause__).startswith("unhashable type:")