test_enum.py
Go to the documentation of this file.
1 # ruff: noqa: SIM201 SIM300 SIM202
2 from __future__ import annotations
3 
4 import pytest
5 
6 from pybind11_tests import enums as m
7 
8 
10  assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
11  assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
12  assert str(m.EOne) == "UnscopedEnum.EOne"
13  assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
14  assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
15  assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"
16 
17  # name property
18  assert m.UnscopedEnum.EOne.name == "EOne"
19  assert m.UnscopedEnum.EOne.value == 1
20  assert m.UnscopedEnum.ETwo.name == "ETwo"
21  assert m.UnscopedEnum.ETwo.value == 2
22  assert m.EOne is m.UnscopedEnum.EOne
23  # name, value readonly
24  with pytest.raises(AttributeError):
25  m.UnscopedEnum.EOne.name = ""
26  with pytest.raises(AttributeError):
27  m.UnscopedEnum.EOne.value = 10
28  # name, value returns a copy
29  # TODO: Neither the name nor value tests actually check against aliasing.
30  # Use a mutable type that has reference semantics.
31  nonaliased_name = m.UnscopedEnum.EOne.name
32  nonaliased_name = "bar" # noqa: F841
33  assert m.UnscopedEnum.EOne.name == "EOne"
34  nonaliased_value = m.UnscopedEnum.EOne.value
35  nonaliased_value = 10 # noqa: F841
36  assert m.UnscopedEnum.EOne.value == 1
37 
38  # __members__ property
39  assert m.UnscopedEnum.__members__ == {
40  "EOne": m.UnscopedEnum.EOne,
41  "ETwo": m.UnscopedEnum.ETwo,
42  "EThree": m.UnscopedEnum.EThree,
43  }
44  # __members__ readonly
45  with pytest.raises(AttributeError):
46  m.UnscopedEnum.__members__ = {}
47  # __members__ returns a copy
48  nonaliased_members = m.UnscopedEnum.__members__
49  nonaliased_members["bar"] = "baz"
50  assert m.UnscopedEnum.__members__ == {
51  "EOne": m.UnscopedEnum.EOne,
52  "ETwo": m.UnscopedEnum.ETwo,
53  "EThree": m.UnscopedEnum.EThree,
54  }
55 
56  for docstring_line in """An unscoped enumeration
57 
58 Members:
59 
60  EOne : Docstring for EOne
61 
62  ETwo : Docstring for ETwo
63 
64  EThree : Docstring for EThree""".split("\n"):
65  assert docstring_line in m.UnscopedEnum.__doc__
66 
67  # Unscoped enums will accept ==/!= int comparisons
68  y = m.UnscopedEnum.ETwo
69  assert y == 2
70  assert 2 == y
71  assert y != 3
72  assert 3 != y
73  # Compare with None
74  assert y != None # noqa: E711
75  assert not (y == None) # noqa: E711
76  # Compare with an object
77  assert y != object()
78  assert not (y == object())
79  # Compare with string
80  assert y != "2"
81  assert "2" != y
82  assert not ("2" == y)
83  assert not (y == "2")
84 
85  with pytest.raises(TypeError):
86  y < object() # noqa: B015
87 
88  with pytest.raises(TypeError):
89  y <= object() # noqa: B015
90 
91  with pytest.raises(TypeError):
92  y > object() # noqa: B015
93 
94  with pytest.raises(TypeError):
95  y >= object() # noqa: B015
96 
97  with pytest.raises(TypeError):
98  y | object()
99 
100  with pytest.raises(TypeError):
101  y & object()
102 
103  with pytest.raises(TypeError):
104  y ^ object()
105 
106  assert int(m.UnscopedEnum.ETwo) == 2
107  assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
108 
109  # order
110  assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
111  assert m.UnscopedEnum.EOne < 2
112  assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
113  assert m.UnscopedEnum.ETwo > 1
114  assert m.UnscopedEnum.ETwo <= 2
115  assert m.UnscopedEnum.ETwo >= 2
116  assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
117  assert m.UnscopedEnum.EOne <= 2
118  assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
119  assert m.UnscopedEnum.ETwo >= 1
120  assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
121  assert not (2 < m.UnscopedEnum.EOne)
122 
123  # arithmetic
124  assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
125  assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
126  assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo
127 
128 
130  assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
131  z = m.ScopedEnum.Two
132  assert m.test_scoped_enum(z) == "ScopedEnum::Two"
133 
134  # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False)
135  assert not z == 3
136  assert not 3 == z
137  assert z != 3
138  assert 3 != z
139  # Compare with None
140  assert z != None # noqa: E711
141  assert not (z == None) # noqa: E711
142  # Compare with an object
143  assert z != object()
144  assert not (z == object())
145  # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions)
146  with pytest.raises(TypeError):
147  z > 3 # noqa: B015
148  with pytest.raises(TypeError):
149  z < 3 # noqa: B015
150  with pytest.raises(TypeError):
151  z >= 3 # noqa: B015
152  with pytest.raises(TypeError):
153  z <= 3 # noqa: B015
154 
155  # order
156  assert m.ScopedEnum.Two < m.ScopedEnum.Three
157  assert m.ScopedEnum.Three > m.ScopedEnum.Two
158  assert m.ScopedEnum.Two <= m.ScopedEnum.Three
159  assert m.ScopedEnum.Two <= m.ScopedEnum.Two
160  assert m.ScopedEnum.Two >= m.ScopedEnum.Two
161  assert m.ScopedEnum.Three >= m.ScopedEnum.Two
162 
163 
165  assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
166  assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
167  assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>"
168  assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>"
169 
170  f = m.ClassWithUnscopedEnum.test_function
171  first = m.ClassWithUnscopedEnum.EFirstMode
172  second = m.ClassWithUnscopedEnum.ESecondMode
173 
174  assert f(first) == 1
175 
176  assert f(first) == f(first)
177  assert not f(first) != f(first)
178 
179  assert f(first) != f(second)
180  assert not f(first) == f(second)
181 
182  assert f(first) == int(f(first))
183  assert not f(first) != int(f(first))
184 
185  assert f(first) != int(f(second))
186  assert not f(first) == int(f(second))
187 
188  # noinspection PyDictCreation
189  x = {f(first): 1, f(second): 2}
190  x[f(first)] = 3
191  x[f(second)] = 4
192  # Hashing test
193  assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"
194 
195 
197  assert int(m.Flags.Read) == 4
198  assert int(m.Flags.Write) == 2
199  assert int(m.Flags.Execute) == 1
200  assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
201  assert int(m.Flags.Read | m.Flags.Write) == 6
202  assert int(m.Flags.Read | m.Flags.Execute) == 5
203  assert int(m.Flags.Write | m.Flags.Execute) == 3
204  assert int(m.Flags.Write | 1) == 3
205  assert ~m.Flags.Write == -3
206 
207  state = m.Flags.Read | m.Flags.Write
208  assert (state & m.Flags.Read) != 0
209  assert (state & m.Flags.Write) != 0
210  assert (state & m.Flags.Execute) == 0
211  assert (state & 1) == 0
212 
213  state2 = ~state
214  assert state2 == -7
215  assert int(state ^ state2) == -1
216 
217 
219  m.test_enum_to_int(m.Flags.Read)
220  m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
221  m.test_enum_to_int(m.ScopedCharEnum.Positive)
222  m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
223  m.test_enum_to_uint(m.Flags.Read)
224  m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
225  m.test_enum_to_uint(m.ScopedCharEnum.Positive)
226  m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
227  m.test_enum_to_long_long(m.Flags.Read)
228  m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
229  m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
230  m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
231 
232 
234  with pytest.raises(ValueError) as excinfo:
235  m.register_bad_enum()
236  assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'
237 
238 
239 def test_char_underlying_enum(): # Issue #1331/PR #1334:
240  assert type(m.ScopedCharEnum.Positive.__int__()) is int
241  assert int(m.ScopedChar16Enum.Zero) == 0
242  assert hash(m.ScopedChar32Enum.Positive) == 1
243  assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
244  assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
245  with pytest.raises(TypeError):
246  # Even if the underlying type is char, only an int can be used to construct the enum:
247  m.ScopedCharEnum("0")
248 
249 
251  assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
252  assert int(m.ScopedBoolEnum.FALSE) == 0
253  assert hash(m.ScopedBoolEnum.TRUE) == 1
254  assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
255  assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
256  # Enum could construct with a bool
257  # (bool is a strict subclass of int, and False will be converted to 0)
258  assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE
259 
260 
262  for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
263  for attr in enum_type.__dict__.values():
264  # Issue #2623/PR #2637: Add argument names to enum_ methods
265  assert "arg0" not in (attr.__doc__ or "")
266 
267 
269  for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
270  assert enum_type.__str__.__doc__.startswith("__str__")
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
test_enum.test_scoped_enum
def test_scoped_enum()
Definition: test_enum.py:129
type
Definition: pytypes.h:1525
hash
ssize_t hash(handle obj)
Definition: pytypes.h:934
object
Definition: pytypes.h:364
test_enum.test_bool_underlying_enum
def test_bool_underlying_enum()
Definition: test_enum.py:250
test_enum.test_str_signature
def test_str_signature()
Definition: test_enum.py:268
str
Definition: pytypes.h:1558
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
test_enum.test_binary_operators
def test_binary_operators()
Definition: test_enum.py:196
test_enum.test_implicit_conversion
def test_implicit_conversion()
Definition: test_enum.py:164
test_enum.test_enum_to_int
def test_enum_to_int()
Definition: test_enum.py:218
gtsam::split
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
Definition: graph-inl.h:245
test_enum.test_duplicate_enum_name
def test_duplicate_enum_name()
Definition: test_enum.py:233
test_enum.test_unscoped_enum
def test_unscoped_enum()
Definition: test_enum.py:9
test_enum.test_char_underlying_enum
def test_char_underlying_enum()
Definition: test_enum.py:239
test_enum.test_docstring_signatures
def test_docstring_signatures()
Definition: test_enum.py:261
repr
str repr(handle h)
Definition: pytypes.h:2467


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:39:27