summaryrefslogtreecommitdiff
path: root/tests/run/public_fused_types.srctree
blob: 54c7aa1489f2925ebde5823ea75a6536d0ac6dae (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
PYTHON setup.py build_ext --inplace
PYTHON -c "import b"

######## setup.py ########


from Cython.Build import cythonize
from distutils.core import setup

setup(
  ext_modules = cythonize("*.pyx"),
)

######## a.pxd ########

cimport cython

cdef extern from "header.h":
    ctypedef int extern_int
    ctypedef long extern_long


cdef struct mystruct_t:
    extern_int a

ctypedef union myunion_t:
    extern_long a

cdef public class MyExt [ type MyExtType, object MyExtObject ]:
    cdef unsigned char a

ctypedef char *string_t
simple_t = cython.fused_type(int, float)
less_simple_t = cython.fused_type(int, float, string_t)
struct_t = cython.fused_type(mystruct_t, myunion_t, MyExt)
builtin_t = cython.fused_type(str, unicode, bytes)

ctypedef fused fusedbunch:
    int
    long
    complex
    string_t

ctypedef fused fused1:
    short
    string_t

cdef fused fused2:
    float
    double
    string_t

cdef struct_t add_simple(struct_t obj, simple_t simple)
cdef less_simple_t add_to_simple(struct_t obj, less_simple_t simple)
cdef public_optional_args(struct_t obj, simple_t simple = *)

cdef class TestFusedExtMethods(object):
    cdef cython.floating method(self, cython.integral x, cython.floating y)
    cpdef cpdef_method(self, cython.integral x, cython.floating y)

object_t = cython.fused_type(TestFusedExtMethods, object, list)

cpdef public_cpdef(cython.integral x, cython.floating y, object_t z)

######## header.h ########

typedef int extern_int;
typedef long extern_long;

######## a.pyx ########

cimport cython

cdef struct_t add_simple(struct_t obj, simple_t simple):
     obj.a = <int> (obj.a + simple)
     return obj

cdef less_simple_t add_to_simple(struct_t obj, less_simple_t simple):
    return obj.a + simple

cdef public_optional_args(struct_t obj, simple_t simple = 6):
    return obj.a, simple

cdef class TestFusedExtMethods(object):
    cdef cython.floating method(self, cython.integral x, cython.floating y):
        if cython.integral is int:
            x += 1

        if cython.floating is double:
            y += 2.0

        return x + y

    cpdef cpdef_method(self, cython.integral x, cython.floating y):
        return cython.typeof(x), cython.typeof(y)

    def def_method(self, fused1 x, fused2 y):
        if (fused1 is string_t and fused2 is not string_t or
            not fused1 is string_t and fused2 is string_t):
            return x, y
        else:
            return <fused1> x + y

cpdef public_cpdef(cython.integral x, cython.floating y, object_t z):
    if cython.integral is int:
        pass

    return cython.typeof(x), cython.typeof(y), cython.typeof(z)


######## b.pyx ########

cimport cython
cimport a as a_cmod
from a cimport *

cdef mystruct_t mystruct
cdef myunion_t myunion
cdef MyExt myext = MyExt()

mystruct.a = 5
myunion.a = 5
myext.a = 5

assert add_simple(mystruct, 5).a == 10
assert add_simple(myunion, 5.0).a == 10.0

assert add_to_simple(mystruct, 5.0) == 10.0
assert add_to_simple(myunion, b"spamhameggs") == b"ameggs"
assert add_to_simple(myext, 5) == 10

cdef mystruct_t (*f)(mystruct_t, int)
f = add_simple
assert f(mystruct, 5).a == 10

f = <mystruct_t (*)(mystruct_t, int)> add_simple
assert f(mystruct, 5).a == 10

f = add_simple[mystruct_t, int]
assert f(mystruct, 5).a == 10

assert public_optional_args(mystruct, 5) == (5, 5)
assert public_optional_args[mystruct_t, int](mystruct) == (5, 6)

assert public_optional_args[mystruct_t, float](mystruct) == (5, 6.0)
assert public_optional_args[mystruct_t, float](mystruct, 7.0) == (5, 7.0)


cdef TestFusedExtMethods obj = TestFusedExtMethods()

cdef int x = 4
cdef float y = 5.0
cdef long a = 6
cdef double b = 7.0

cdef double (*func)(TestFusedExtMethods, long, double)

func = obj.method

result = func(obj, a, b)
assert result == 15.0, result

func = <double (*)(TestFusedExtMethods, long, double)> obj.method
assert func(obj, x, y) == 11.0

func = obj.method[long, double]
assert func(obj, a, y) == 13.0

assert obj.method(x, <double> a) == 13.0
assert obj.method[int, double](x, b) == 14.0


# Test inheritance
cdef class Subclass(TestFusedExtMethods):
    cdef cython.floating method(self, cython.integral x, cython.floating y):
        return -x -y

    cpdef cpdef_method(self, cython.integral x, cython.floating y):
        return x, y

cdef Subclass myobj = Subclass()
assert myobj.method[int, float](5, 5.0) == -10

cdef float (*meth)(Subclass, int, float)
meth = myobj.method
assert meth(myobj, 5, 5.0) == -10

meth = myobj.method[int, float]
assert meth(myobj, 5, 5.0) == -10


# Test cpdef functions and methods
cy = __import__("cython")
import a as a_mod

def ae(result, expected):
    "assert equals"
    if result != expected:
        print 'result  :', result
        print 'expected:', expected

    assert result == expected

ae(a_mod.public_cpdef[int, float, list](5, 6, [7]), ("int", "float", "list object"))

idx = cy.typeof(0), cy.typeof(0.0), cy.typeof([])
ae(a_mod.public_cpdef[idx](5, 6, [7]), ("int", "float", "list object"))

ae(a_mod.public_cpdef[cy.int, cy.double, cython.typeof(obj)](5, 6, obj), ("int", "double", "TestFusedExtMethods"))
ae(a_mod.public_cpdef[cy.int, cy.double, cython.typeof(obj)](5, 6, myobj), ("int", "double", "TestFusedExtMethods"))

ae(public_cpdef[int, float, list](5, 6, [7]), ("int", "float", "list object"))
ae(public_cpdef[int, double, TestFusedExtMethods](5, 6, obj), ("int", "double", "TestFusedExtMethods"))
ae(public_cpdef[int, double, TestFusedExtMethods](5, 6, myobj), ("int", "double", "TestFusedExtMethods"))

ae(obj.cpdef_method(10, 10.0), ("long", "double"))
ae(myobj.cpdef_method(10, 10.0), (10, 10.0))
ae(obj.cpdef_method[int, float](10, 10.0), ("int", "float"))
ae(myobj.cpdef_method[int, float](10, 10.0), (10, 10.0))

s = """\
import cython as cy

ae(obj.cpdef_method[cy.int, cy.float](10, 10.0), ("int", "float"))
ae(myobj.cpdef_method[cy.int, cy.float](10, 10.0), (10, 10.0))
"""

d = {'obj': obj, 'myobj': myobj, 'ae': ae}

exec s in d

# Test def methods
# ae(obj.def_method(12, 14.9), 26)
# ae(obj.def_method(13, "spam"), (13, "spam"))
# ae(obj.def_method[cy.short, cy.float](13, 16.3), 29)