1cimport cython
2cimport check_fused_types_pxd
3
4import math
5
6ctypedef char *string_t
7
8fused_t = cython.fused_type(int, long, float, string_t)
9other_t = cython.fused_type(int, long)
10base_t = cython.fused_type(short, int)
11
12# complex_t = cython.fused_type(cython.floatcomplex, cython.doublecomplex)
13cdef fused complex_t:
14    float complex
15    double complex
16
17ctypedef base_t **base_t_p_p
18
19# ctypedef cython.fused_type(char, base_t_p_p, fused_t, complex_t) composed_t
20cdef fused composed_t:
21    char
22    int
23    float
24    string_t
25    cython.pp_int
26    float complex
27    double complex
28    int complex
29    long complex
30
31
32cdef func(fused_t a, other_t b):
33    cdef int int_a
34    cdef string_t string_a
35    cdef other_t other_a
36
37    if fused_t is other_t:
38        print 'fused_t is other_t'
39        other_a = a
40
41    if fused_t is int:
42        print 'fused_t is int'
43        int_a = a
44
45    if fused_t is string_t:
46        print 'fused_t is string_t'
47        string_a = a
48
49    if fused_t in check_fused_types_pxd.unresolved_t:
50        print 'fused_t in unresolved_t'
51
52    if int in check_fused_types_pxd.unresolved_t:
53        print 'int in unresolved_t'
54
55    if string_t in check_fused_types_pxd.unresolved_t:
56        print 'string_t in unresolved_t'
57
58
59def test_int_int():
60    """
61    >>> test_int_int()
62    fused_t is other_t
63    fused_t is int
64    fused_t in unresolved_t
65    int in unresolved_t
66    """
67    cdef int x = 1
68    cdef int y = 2
69
70    func(x, y)
71
72def test_int_long():
73    """
74    >>> test_int_long()
75    fused_t is int
76    fused_t in unresolved_t
77    int in unresolved_t
78    """
79    cdef int x = 1
80    cdef long y = 2
81
82    func(x, y)
83
84def test_float_int():
85    """
86    >>> test_float_int()
87    fused_t in unresolved_t
88    int in unresolved_t
89    """
90    cdef float x = 1
91    cdef int y = 2
92
93    func(x, y)
94
95def test_string_int():
96    """
97    >>> test_string_int()
98    fused_t is string_t
99    int in unresolved_t
100    """
101    cdef string_t x = b"spam"
102    cdef int y = 2
103
104    func(x, y)
105
106
107cdef if_then_else(fused_t a, other_t b):
108    cdef other_t other_a
109    cdef string_t string_a
110    cdef fused_t specific_a
111
112    if fused_t is other_t:
113        print 'fused_t is other_t'
114        other_a = a
115    elif fused_t is string_t:
116        print 'fused_t is string_t'
117        string_a = a
118    else:
119        print 'none of the above'
120        specific_a = a
121
122def test_if_then_else_long_long():
123    """
124    >>> test_if_then_else_long_long()
125    fused_t is other_t
126    """
127    cdef long x = 0, y = 0
128    if_then_else(x, y)
129
130def test_if_then_else_string_int():
131    """
132    >>> test_if_then_else_string_int()
133    fused_t is string_t
134    """
135    cdef string_t x = b"spam"
136    cdef int y = 0
137    if_then_else(x, y)
138
139def test_if_then_else_float_int():
140    """
141    >>> test_if_then_else_float_int()
142    none of the above
143    """
144    cdef float x = 0.0
145    cdef int y = 1
146    if_then_else(x, y)
147
148
149cdef composed_t composed(composed_t x, composed_t y):
150    if composed_t in base_t_p_p or composed_t is string_t:
151        if string_t == composed_t:
152            print x.decode('ascii'), y.decode('ascii')
153        else:
154            print x[0][0], y[0][0]
155
156        return x
157    elif composed_t == string_t:
158        print 'this is never executed'
159    elif list():
160        print 'neither is this one'
161    else:
162        if composed_t not in complex_t:
163            print 'not a complex number'
164            print <int> x, <int> y
165        else:
166            print 'it is a complex number'
167            print x.real, x.imag
168
169        return x + y
170
171def test_composed_types():
172    """
173    >>> test_composed_types()
174    it is a complex number
175    0.5 0.6
176    9 4
177    <BLANKLINE>
178    not a complex number
179    7 8
180    15
181    <BLANKLINE>
182    7 8
183    <BLANKLINE>
184    spam eggs
185    spam
186    """
187    cdef double complex a = 0.5 + 0.6j, b = 0.4 -0.2j, result
188    cdef int c = 7, d = 8
189    cdef int *cp = &c, *dp = &d
190    cdef string_t e = "spam", f = "eggs"
191
192    result = composed(a, b)
193    print int(math.ceil(result.real * 10)), int(math.ceil(result.imag * 10))
194    print
195
196    print composed(c, d)
197    print
198
199    composed(&cp, &dp)
200    print
201
202    print composed(e, f).decode('ascii')
203
204