test_operator_overloading.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 
5 from pybind11_tests import ConstructorStats
6 from pybind11_tests import operators as m
7 
8 
10  v1 = m.Vector2(1, 2)
11  v2 = m.Vector(3, -1)
12  v3 = m.Vector2(1, 2) # Same value as v1, but different instance.
13  assert v1 is not v3
14 
15  assert str(v1) == "[1.000000, 2.000000]"
16  assert str(v2) == "[3.000000, -1.000000]"
17 
18  assert str(-v2) == "[-3.000000, 1.000000]"
19 
20  assert str(v1 + v2) == "[4.000000, 1.000000]"
21  assert str(v1 - v2) == "[-2.000000, 3.000000]"
22  assert str(v1 - 8) == "[-7.000000, -6.000000]"
23  assert str(v1 + 8) == "[9.000000, 10.000000]"
24  assert str(v1 * 8) == "[8.000000, 16.000000]"
25  assert str(v1 / 8) == "[0.125000, 0.250000]"
26  assert str(8 - v1) == "[7.000000, 6.000000]"
27  assert str(8 + v1) == "[9.000000, 10.000000]"
28  assert str(8 * v1) == "[8.000000, 16.000000]"
29  assert str(8 / v1) == "[8.000000, 4.000000]"
30  assert str(v1 * v2) == "[3.000000, -2.000000]"
31  assert str(v2 / v1) == "[3.000000, -0.500000]"
32 
33  assert v1 == v3
34  assert v1 != v2
35  assert hash(v1) == 4
36  # TODO(eric.cousineau): Make this work.
37  # assert abs(v1) == "abs(Vector2)"
38 
39  v1 += 2 * v2
40  assert str(v1) == "[7.000000, 0.000000]"
41  v1 -= v2
42  assert str(v1) == "[4.000000, 1.000000]"
43  v1 *= 2
44  assert str(v1) == "[8.000000, 2.000000]"
45  v1 /= 16
46  assert str(v1) == "[0.500000, 0.125000]"
47  v1 *= v2
48  assert str(v1) == "[1.500000, -0.125000]"
49  v2 /= v1
50  assert str(v2) == "[2.000000, 8.000000]"
51 
52  cstats = ConstructorStats.get(m.Vector2)
53  assert cstats.alive() == 3
54  del v1
55  assert cstats.alive() == 2
56  del v2
57  assert cstats.alive() == 1
58  del v3
59  assert cstats.alive() == 0
60  assert cstats.values() == [
61  "[1.000000, 2.000000]",
62  "[3.000000, -1.000000]",
63  "[1.000000, 2.000000]",
64  "[-3.000000, 1.000000]",
65  "[4.000000, 1.000000]",
66  "[-2.000000, 3.000000]",
67  "[-7.000000, -6.000000]",
68  "[9.000000, 10.000000]",
69  "[8.000000, 16.000000]",
70  "[0.125000, 0.250000]",
71  "[7.000000, 6.000000]",
72  "[9.000000, 10.000000]",
73  "[8.000000, 16.000000]",
74  "[8.000000, 4.000000]",
75  "[3.000000, -2.000000]",
76  "[3.000000, -0.500000]",
77  "[6.000000, -2.000000]",
78  ]
79  assert cstats.default_constructions == 0
80  assert cstats.copy_constructions == 0
81  assert cstats.move_constructions >= 10
82  assert cstats.copy_assignments == 0
83  assert cstats.move_assignments == 0
84 
85 
87  """#393: need to return NotSupported to ensure correct arithmetic operator behavior"""
88 
89  c1, c2 = m.C1(), m.C2()
90  assert c1 + c1 == 11
91  assert c2 + c2 == 22
92  assert c2 + c1 == 21
93  assert c1 + c2 == 12
94 
95 
97  """#328: first member in a class can't be used in operators"""
98 
99  a = m.NestA()
100  b = m.NestB()
101  c = m.NestC()
102 
103  a += 10
104  assert m.get_NestA(a) == 13
105  b.a += 100
106  assert m.get_NestA(b.a) == 103
107  c.b.a += 1000
108  assert m.get_NestA(c.b.a) == 1003
109  b -= 1
110  assert m.get_NestB(b) == 3
111  c.b -= 3
112  assert m.get_NestB(c.b) == 1
113  c *= 7
114  assert m.get_NestC(c) == 35
115 
116  abase = a.as_base()
117  assert abase.value == -2
118  a.as_base().value += 44
119  assert abase.value == 42
120  assert c.b.a.as_base().value == -2
121  c.b.a.as_base().value += 44
122  assert c.b.a.as_base().value == 42
123 
124  del c
125  pytest.gc_collect()
126  del a # Shouldn't delete while abase is still alive
127  pytest.gc_collect()
128 
129  assert abase.value == 42
130  del abase, b
131  pytest.gc_collect()
132 
133 
135  assert m.Comparable(15) is not m.Comparable(15)
136  assert m.Comparable(15) == m.Comparable(15)
137 
138  with pytest.raises(TypeError) as excinfo:
139  hash(m.Comparable(15))
140  assert str(excinfo.value).startswith("unhashable type:")
141 
142  for hashable in (m.Hashable, m.Hashable2):
143  assert hashable(15) is not hashable(15)
144  assert hashable(15) == hashable(15)
145 
146  assert hash(hashable(15)) == 15
147  assert hash(hashable(15)) == hash(hashable(15))
148 
149 
151  with pytest.raises(TypeError) as excinfo:
152  m.get_unhashable_HashMe_set()
153  assert str(excinfo.value.__cause__).startswith("unhashable type:")
test_operator_overloading.test_operator_overloading
def test_operator_overloading()
Definition: test_operator_overloading.py:9
test_operator_overloading.test_operators_notimplemented
def test_operators_notimplemented()
Definition: test_operator_overloading.py:86
hash
ssize_t hash(handle obj)
Definition: pytypes.h:934
test_operator_overloading.test_nested
def test_nested()
Definition: test_operator_overloading.py:96
test_operator_overloading.test_return_set_of_unhashable
def test_return_set_of_unhashable()
Definition: test_operator_overloading.py:150
test_operator_overloading.test_overriding_eq_reset_hash
def test_overriding_eq_reset_hash()
Definition: test_operator_overloading.py:134
str
Definition: pytypes.h:1558
ConstructorStats::get
static ConstructorStats & get(std::type_index type)
Definition: constructor_stats.h:163


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:15:58