blob: de5f3c6f6c96816cb805f6095b231c570def4e58 [file] [log] [blame]
Dean Moldovana9a37b42016-08-13 00:57:24 +02001import pytest
2
3
4def test_unscoped_enum():
5 from pybind11_tests import UnscopedEnum, EOne
6
7 assert str(UnscopedEnum.EOne) == "UnscopedEnum.EOne"
8 assert str(UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
9 assert str(EOne) == "UnscopedEnum.EOne"
10
11 # no TypeError exception for unscoped enum ==/!= int comparisons
12 y = UnscopedEnum.ETwo
13 assert y == 2
14 assert y != 3
15
16 assert int(UnscopedEnum.ETwo) == 2
17 assert str(UnscopedEnum(2)) == "UnscopedEnum.ETwo"
18
Pim Schellart90d27802016-11-16 11:28:11 -050019 # order
20 assert UnscopedEnum.EOne < UnscopedEnum.ETwo
21 assert UnscopedEnum.EOne < 2
22 assert UnscopedEnum.ETwo > UnscopedEnum.EOne
23 assert UnscopedEnum.ETwo > 1
24 assert UnscopedEnum.ETwo <= 2
25 assert UnscopedEnum.ETwo >= 2
26 assert UnscopedEnum.EOne <= UnscopedEnum.ETwo
27 assert UnscopedEnum.EOne <= 2
28 assert UnscopedEnum.ETwo >= UnscopedEnum.EOne
29 assert UnscopedEnum.ETwo >= 1
30 assert not (UnscopedEnum.ETwo < UnscopedEnum.EOne)
31 assert not (2 < UnscopedEnum.EOne)
32
Dean Moldovana9a37b42016-08-13 00:57:24 +020033
34def test_scoped_enum():
35 from pybind11_tests import ScopedEnum, test_scoped_enum
36
37 assert test_scoped_enum(ScopedEnum.Three) == "ScopedEnum::Three"
38 z = ScopedEnum.Two
39 assert test_scoped_enum(z) == "ScopedEnum::Two"
40
41 # expected TypeError exceptions for scoped enum ==/!= int comparisons
42 with pytest.raises(TypeError):
43 assert z == 2
44 with pytest.raises(TypeError):
45 assert z != 3
46
Pim Schellart90d27802016-11-16 11:28:11 -050047 # order
48 assert ScopedEnum.Two < ScopedEnum.Three
49 assert ScopedEnum.Three > ScopedEnum.Two
50 assert ScopedEnum.Two <= ScopedEnum.Three
51 assert ScopedEnum.Two <= ScopedEnum.Two
52 assert ScopedEnum.Two >= ScopedEnum.Two
53 assert ScopedEnum.Three >= ScopedEnum.Two
Dean Moldovana9a37b42016-08-13 00:57:24 +020054
Dean Moldovanbad17402016-11-20 21:21:54 +010055
Dean Moldovana9a37b42016-08-13 00:57:24 +020056def test_implicit_conversion():
57 from pybind11_tests import ClassWithUnscopedEnum
58
59 assert str(ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
60 assert str(ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
61
62 f = ClassWithUnscopedEnum.test_function
63 first = ClassWithUnscopedEnum.EFirstMode
64 second = ClassWithUnscopedEnum.ESecondMode
65
66 assert f(first) == 1
67
68 assert f(first) == f(first)
69 assert not f(first) != f(first)
70
71 assert f(first) != f(second)
72 assert not f(first) == f(second)
73
74 assert f(first) == int(f(first))
75 assert not f(first) != int(f(first))
76
77 assert f(first) != int(f(second))
78 assert not f(first) == int(f(second))
79
80 # noinspection PyDictCreation
81 x = {f(first): 1, f(second): 2}
82 x[f(first)] = 3
83 x[f(second)] = 4
84 # Hashing test
85 assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
Pim Schellart90d27802016-11-16 11:28:11 -050086
Dean Moldovanbad17402016-11-20 21:21:54 +010087
Pim Schellart90d27802016-11-16 11:28:11 -050088def test_binary_operators():
89 from pybind11_tests import Flags
90
91 assert int(Flags.Read) == 4
92 assert int(Flags.Write) == 2
93 assert int(Flags.Execute) == 1
94 assert int(Flags.Read | Flags.Write | Flags.Execute) == 7
95 assert int(Flags.Read | Flags.Write) == 6
96 assert int(Flags.Read | Flags.Execute) == 5
97 assert int(Flags.Write | Flags.Execute) == 3
98 assert int(Flags.Write | 1) == 3
99
100 state = Flags.Read | Flags.Write
101 assert (state & Flags.Read) != 0
102 assert (state & Flags.Write) != 0
103 assert (state & Flags.Execute) == 0
104 assert (state & 1) == 0
105
106 state2 = ~state
107 assert state2 == -7
108 assert int(state ^ state2) == -1