summaryrefslogtreecommitdiff
path: root/tests/run/fused_cpp.pyx
blob: 95b326904f3c76dc42523c56a2f05be7f463f9c4 (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
# tag: cpp

cimport cython
from libcpp.vector cimport vector
from libcpp.map cimport map
from libcpp.typeinfo cimport type_info
from cython.operator cimport typeid

def test_cpp_specialization(cython.floating element):
    """
    >>> import cython
    >>> test_cpp_specialization[cython.float](10.0)
    vector[float] * float 10.0
    >>> test_cpp_specialization[cython.double](10.0)
    vector[double] * double 10.0
    """
    cdef vector[cython.floating] *v = new vector[cython.floating]()
    v.push_back(element)
    print cython.typeof(v), cython.typeof(element), v.at(0)

cdef fused C:
   int
   object

cdef const type_info* tidint = &typeid(int)
def typeid_call(C x):
    """
    For GH issue 3203
    >>> typeid_call(1)
    True
    """
    cdef const type_info* a = &typeid(C)
    return a[0] == tidint[0]

cimport cython

def typeid_call2(cython.integral x):
    """
    For GH issue 3203
    >>> typeid_call2[int](1)
    True
    """
    cdef const type_info* a = &typeid(cython.integral)
    return a[0] == tidint[0]

cdef fused_ref(cython.integral& x):
    return x*2

def test_fused_ref(int x):
    """
    >>> test_fused_ref(5)
    (10, 10)
    """
    return fused_ref(x), fused_ref[int](x)

ctypedef fused nested_fused:
    vector[cython.integral]

cdef vec_of_fused(nested_fused v):
    x = v[0]
    return cython.typeof(x)

def test_nested_fused():
    """
    >>> test_nested_fused()
    int
    long
    """
    cdef vector[int] vi = [0,1]
    cdef vector[long] vl = [0,1]
    print vec_of_fused(vi)
    print vec_of_fused(vl)

ctypedef fused nested_fused2:
    map[cython.integral, cython.floating]

cdef map_of_fused(nested_fused2 m):
    for pair in m:
        return cython.typeof(pair.first), cython.typeof(pair.second)

def test_nested_fused2():
    """
    >>> test_nested_fused2()
    ('int', 'float')
    ('long', 'double')
    """
    cdef map[int, float] mif = { 0: 0.0 }
    cdef map[long, double] mld = { 0: 0.0 }
    print map_of_fused(mif)
    print map_of_fused(mld)