summaryrefslogtreecommitdiff
path: root/tests/run/cpp_nested_classes.pyx
blob: 50a453901df264415c8faa563042b1aa7c762815 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# tag: cpp, no-cpp-locals

cdef extern from "cpp_nested_classes_support.h":
    cdef cppclass A:
        cppclass B:
            int square(int)
            cppclass C:
                int cube(int)
        B* createB()
        ctypedef int my_int
        @staticmethod
        my_int negate(my_int)

    cdef cppclass TypedClass[T]:
        ctypedef T MyType
        struct MyStruct:
            T typed_value
            int int_value
        union MyUnion:
            T typed_value
            int int_value
        enum MyEnum:
            value

    cdef cppclass SpecializedTypedClass(TypedClass[double]):
        pass

cdef cppclass AA:
    cppclass BB:
        int square(int x):
            return x * x
        cppclass CC:
            int cube(int x):
                return x * x * x
    BB* createB():
        return new BB()
    ctypedef int my_int
    @staticmethod
    my_int negate(my_int x):
        return -x

cdef cppclass DD(AA):
    ctypedef int my_other_int

ctypedef A AliasA1
ctypedef AliasA1 AliasA2


def test_nested_classes():
    """
    >>> test_nested_classes()
    """
    cdef A a
    cdef A.B b
    assert b.square(3) == 9
    cdef A.B.C c
    assert c.cube(3) == 27

    cdef A.B *b_ptr = a.createB()
    assert b_ptr.square(4) == 16
    del b_ptr

def test_nested_defined_classes():
    """
    >>> test_nested_defined_classes()
    """
    cdef AA a
    cdef AA.BB b
    assert b.square(3) == 9
    cdef AA.BB.CC c
    assert c.cube(3) == 27

    cdef AA.BB *b_ptr = a.createB()
    assert b_ptr.square(4) == 16
    del b_ptr

def test_nested_inherited_classes():
    """
    >>> test_nested_inherited_classes()
    """
    cdef DD.BB b
    assert b.square(3) == 9

def test_nested_typedef(py_x):
    """
    >>> test_nested_typedef(5)
    """
    cdef A.my_int x = py_x
    assert A.negate(x) == -py_x

def test_nested_defined_typedef(py_x):
    """
    >>> test_nested_typedef(5)
    """
    cdef AA.my_int x = py_x
    assert AA.negate(x) == -py_x

def test_typedef_for_nested(py_x):
    """
    >>> test_typedef_for_nested(5)
    """
    cdef AliasA1.my_int x = py_x
    assert A.negate(x) == -py_x

def test_typedef_for_nested_deep(py_x):
    """
    >>> test_typedef_for_nested_deep(5)
    """
    cdef AliasA2.my_int x = py_x
    assert A.negate(x) == -py_x

def test_typed_nested_typedef(x):
    """
    >>> test_typed_nested_typedef(4)
    (4, 4.0)
    """
    cdef TypedClass[int].MyType ix = x
    cdef TypedClass[double].MyType dx = x
    return ix, dx

def test_nested_enum(TypedClass[double].MyEnum x):
    """
    >>> test_nested_enum(4)
    False
    """
    return x == 0

def test_nested_union(x):
    """
    >>> test_nested_union(2)
    2.0
    """
    cdef TypedClass[double].MyUnion u
    u.int_value = x
    assert u.int_value == x
    u.typed_value = x
    return u.typed_value

def test_nested_struct(x):
    """
    >>> test_nested_struct(2)
    2.0
    """
    cdef TypedClass[double].MyStruct s
    s.int_value = x
    assert s.int_value == x
    s.typed_value = x
    return s.typed_value



def test_typed_nested_sub_typedef(x):
    """
    >>> test_typed_nested_sub_typedef(4)
    4.0
    """
    cdef SpecializedTypedClass.MyType dx = x
    return dx

def test_nested_sub_enum(SpecializedTypedClass.MyEnum x):
    """
    >>> test_nested_sub_enum(4)
    False
    """
    return x == 0

def test_nested_sub_union(x):
    """
    >>> test_nested_sub_union(2)
    2.0
    """
    cdef SpecializedTypedClass.MyUnion u
    u.int_value = x
    assert u.int_value == x
    u.typed_value = x
    return u.typed_value

def test_nested_sub_struct(x):
    """
    >>> test_nested_sub_struct(2)
    2.0
    """
    cdef SpecializedTypedClass.MyStruct s
    s.int_value = x
    assert s.int_value == x
    s.typed_value = x
    return s.typed_value

cimport cpp_nested_names
cimport libcpp.string
from cython.operator cimport dereference as deref, preincrement as inc

def test_nested_names():
    """
    >>> test_nested_names()
    Nested
    NestedNested
    C
    y
    t
    h
    o
    n
    """
    cdef cpp_nested_names.Outer.Nested n = cpp_nested_names.Outer.get()
    cdef cpp_nested_names.Outer.Nested.NestedNested nn = n.get()
    print(n.get_str().decode('ascii'))
    print(nn.get_str().decode('ascii'))

    cdef libcpp.string.string s = "Cython"
    cdef libcpp.string.string.iterator i = s.begin()
    while i != s.end():
        print(chr(deref(i)))
        inc(i)