test_enum.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 import pytest
3 from pybind11_tests import enums as m
4 
5 
7  assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
8  assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
9  assert str(m.EOne) == "UnscopedEnum.EOne"
10 
11  # name property
12  assert m.UnscopedEnum.EOne.name == "EOne"
13  assert m.UnscopedEnum.ETwo.name == "ETwo"
14  assert m.EOne.name == "EOne"
15  # name readonly
16  with pytest.raises(AttributeError):
17  m.UnscopedEnum.EOne.name = ""
18  # name returns a copy
19  foo = m.UnscopedEnum.EOne.name
20  foo = "bar"
21  assert m.UnscopedEnum.EOne.name == "EOne"
22 
23  # __members__ property
24  assert m.UnscopedEnum.__members__ == \
25  {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, "EThree": m.UnscopedEnum.EThree}
26  # __members__ readonly
27  with pytest.raises(AttributeError):
28  m.UnscopedEnum.__members__ = {}
29  # __members__ returns a copy
30  foo = m.UnscopedEnum.__members__
31  foo["bar"] = "baz"
32  assert m.UnscopedEnum.__members__ == \
33  {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, "EThree": m.UnscopedEnum.EThree}
34 
35  for docstring_line in '''An unscoped enumeration
36 
37 Members:
38 
39  EOne : Docstring for EOne
40 
41  ETwo : Docstring for ETwo
42 
43  EThree : Docstring for EThree'''.split('\n'):
44  assert docstring_line in m.UnscopedEnum.__doc__
45 
46  # Unscoped enums will accept ==/!= int comparisons
47  y = m.UnscopedEnum.ETwo
48  assert y == 2
49  assert 2 == y
50  assert y != 3
51  assert 3 != y
52  # Compare with None
53  assert (y != None) # noqa: E711
54  assert not (y == None) # noqa: E711
55  # Compare with an object
56  assert (y != object())
57  assert not (y == object())
58  # Compare with string
59  assert y != "2"
60  assert "2" != y
61  assert not ("2" == y)
62  assert not (y == "2")
63 
64  with pytest.raises(TypeError):
65  y < object()
66 
67  with pytest.raises(TypeError):
68  y <= object()
69 
70  with pytest.raises(TypeError):
71  y > object()
72 
73  with pytest.raises(TypeError):
74  y >= object()
75 
76  with pytest.raises(TypeError):
77  y | object()
78 
79  with pytest.raises(TypeError):
80  y & object()
81 
82  with pytest.raises(TypeError):
83  y ^ object()
84 
85  assert int(m.UnscopedEnum.ETwo) == 2
86  assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
87 
88  # order
89  assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
90  assert m.UnscopedEnum.EOne < 2
91  assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
92  assert m.UnscopedEnum.ETwo > 1
93  assert m.UnscopedEnum.ETwo <= 2
94  assert m.UnscopedEnum.ETwo >= 2
95  assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
96  assert m.UnscopedEnum.EOne <= 2
97  assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
98  assert m.UnscopedEnum.ETwo >= 1
99  assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
100  assert not (2 < m.UnscopedEnum.EOne)
101 
102  # arithmetic
103  assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
104  assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
105  assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo
106 
107 
109  assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
110  z = m.ScopedEnum.Two
111  assert m.test_scoped_enum(z) == "ScopedEnum::Two"
112 
113  # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False)
114  assert not z == 3
115  assert not 3 == z
116  assert z != 3
117  assert 3 != z
118  # Compare with None
119  assert (z != None) # noqa: E711
120  assert not (z == None) # noqa: E711
121  # Compare with an object
122  assert (z != object())
123  assert not (z == object())
124  # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions)
125  with pytest.raises(TypeError):
126  z > 3
127  with pytest.raises(TypeError):
128  z < 3
129  with pytest.raises(TypeError):
130  z >= 3
131  with pytest.raises(TypeError):
132  z <= 3
133 
134  # order
135  assert m.ScopedEnum.Two < m.ScopedEnum.Three
136  assert m.ScopedEnum.Three > m.ScopedEnum.Two
137  assert m.ScopedEnum.Two <= m.ScopedEnum.Three
138  assert m.ScopedEnum.Two <= m.ScopedEnum.Two
139  assert m.ScopedEnum.Two >= m.ScopedEnum.Two
140  assert m.ScopedEnum.Three >= m.ScopedEnum.Two
141 
142 
144  assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
145  assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
146 
147  f = m.ClassWithUnscopedEnum.test_function
148  first = m.ClassWithUnscopedEnum.EFirstMode
149  second = m.ClassWithUnscopedEnum.ESecondMode
150 
151  assert f(first) == 1
152 
153  assert f(first) == f(first)
154  assert not f(first) != f(first)
155 
156  assert f(first) != f(second)
157  assert not f(first) == f(second)
158 
159  assert f(first) == int(f(first))
160  assert not f(first) != int(f(first))
161 
162  assert f(first) != int(f(second))
163  assert not f(first) == int(f(second))
164 
165  # noinspection PyDictCreation
166  x = {f(first): 1, f(second): 2}
167  x[f(first)] = 3
168  x[f(second)] = 4
169  # Hashing test
170  assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
171 
172 
174  assert int(m.Flags.Read) == 4
175  assert int(m.Flags.Write) == 2
176  assert int(m.Flags.Execute) == 1
177  assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
178  assert int(m.Flags.Read | m.Flags.Write) == 6
179  assert int(m.Flags.Read | m.Flags.Execute) == 5
180  assert int(m.Flags.Write | m.Flags.Execute) == 3
181  assert int(m.Flags.Write | 1) == 3
182  assert ~m.Flags.Write == -3
183 
184  state = m.Flags.Read | m.Flags.Write
185  assert (state & m.Flags.Read) != 0
186  assert (state & m.Flags.Write) != 0
187  assert (state & m.Flags.Execute) == 0
188  assert (state & 1) == 0
189 
190  state2 = ~state
191  assert state2 == -7
192  assert int(state ^ state2) == -1
193 
194 
196  m.test_enum_to_int(m.Flags.Read)
197  m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
198  m.test_enum_to_uint(m.Flags.Read)
199  m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
200  m.test_enum_to_long_long(m.Flags.Read)
201  m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
202 
203 
205  with pytest.raises(ValueError) as excinfo:
206  m.register_bad_enum()
207  assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'
def test_enum_to_int()
Definition: test_enum.py:195
return int(ret)+1
def test_binary_operators()
Definition: test_enum.py:173
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
Definition: graph-inl.h:255
Definition: pytypes.h:928
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
def test_duplicate_enum_name()
Definition: test_enum.py:204
def test_scoped_enum()
Definition: test_enum.py:108
def test_implicit_conversion()
Definition: test_enum.py:143
def test_unscoped_enum()
Definition: test_enum.py:6


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03