summaryrefslogtreecommitdiff
path: root/tests/run/cpp_scoped_enums.pyx
blob: 7a2bc912fa9b348237d2dac3eec83f89518bf61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# mode: run
# tag: cpp, cpp11

from libcpp.limits cimport numeric_limits

cdef extern from *:
    """
    enum class Enum1 {
        Item1,
        Item2
    };
    """
    cdef enum class Enum1:
        Item1
        Item2


cdef extern from * namespace "Namespace1":
    """
    namespace Namespace1 {
        enum class Enum2 {
            Item1,
            Item2
        };
    }
    """
    cdef enum class Enum2:
        Item1
        Item2


cdef enum class Enum3(int):
    a = 1
    b = 2


cdef extern from *:
    """
    enum class sorted
    {
        a = 1,
        b = 0
    };
    """
    cdef enum class Enum4 "sorted":
        a
        b


cdef extern from *:
    """
    #include <limits>

    enum class LongIntEnum : long int {
        val = std::numeric_limits<long int>::max(),
    };
    """
    enum class LongIntEnum(long int):
        val


def test_compare_enums():
    """
    >>> test_compare_enums()
    (True, True, False, False)
    """
    cdef Enum1 x, y
    x = Enum1.Item1
    y = Enum1.Item2

    return (
        x == Enum1.Item1,
        y == Enum1.Item2,
        x == Enum1.Item2,
        y == Enum1.Item1
    )

        
def test_compare_namespace_enums():
    """
    >>> test_compare_enums()
    (True, True, False, False)
    """
    cdef Enum2 z, w
    
    z = Enum2.Item1
    w = Enum2.Item2

    return (
        z == Enum2.Item1,
        w == Enum2.Item2,
        z == Enum2.Item2,
        w == Enum2.Item1
    )


def test_coerce_to_from_py_value(object i):
    """
    >>> test_coerce_to_from_py_value(1)
    (True, False)

    >>> test_coerce_to_from_py_value(2)
    (False, True)

    >>> test_coerce_to_from_py_value(3)
    (False, False)

    >>> test_coerce_to_from_py_value(11111111111111111111111111111111111111111111)
    Traceback (most recent call last):
    OverflowError: Python int too large to convert to C long
    """
    cdef Enum3 x = i
    y = Enum3.b

    return (
        x == Enum3.a,
        y == int(i)
    )


def test_reserved_cname():
    """
    >>> test_reserved_cname()
    True
    """
    cdef Enum4 x = Enum4.a
    return Enum4.a == int(1)


def test_large_enum():
    """
    >>> test_large_enum()
    True
    """
    long_max = int(numeric_limits[long].max())
    return LongIntEnum.val == long_max