1cimport cython
2
3from cython cimport typeof, infer_types
4
5
6def test_swap():
7    """
8    >>> test_swap()
9    """
10    a = 0
11    b = 1
12    tmp = a
13    a = b
14    b = tmp
15    assert typeof(a) == "long", typeof(a)
16    assert typeof(b) == "long", typeof(b)
17    assert typeof(tmp) == "long", typeof(tmp)
18
19def test_object_assmt():
20    """
21    >>> test_object_assmt()
22    """
23    a = 1
24    b = a
25    a = "str"
26    assert typeof(a) == "Python object", typeof(a)
27    assert typeof(b) == "long", typeof(b)
28
29
30class RAdd(object):
31    other = None
32    def __radd__(self, other):
33        self._other = other
34        return self
35    def __repr__(self):
36        return '%s(%s)' % (type(self).__name__, self._other)
37
38
39def test_inplace_assignment():
40    """
41    >>> test_inplace_assignment()
42    RAdd([1, 2, 3])
43    """
44    l = [1, 2, 3]
45    # inferred type of l is list, but assignment result is object
46    l += RAdd()
47    return l
48
49
50def test_reassignment():
51    """
52    >>> test_reassignment()
53    (1, 2, 3)
54    """
55    l = [1, 2, 3]
56    l = (1, 2, 3)
57    return l
58
59
60def test_long_vs_double(cond):
61    """
62    >>> test_long_vs_double(0)
63    """
64    assert typeof(a) == "double", typeof(a)
65    assert typeof(b) == "double", typeof(b)
66    assert typeof(c) == "double", typeof(c)
67    assert typeof(d) == "double", typeof(d)
68
69    if cond:
70        a = 1
71        b = 2
72        c = (a + b) / 2
73    else:
74        a = 1.0
75        b = 2.0
76        d = (a + b) / 2
77
78def test_double_vs_pyobject():
79    """
80    >>> test_double_vs_pyobject()
81    """
82    assert typeof(a) == "Python object", typeof(a)
83    assert typeof(b) == "Python object", typeof(b)
84    assert typeof(d) == "double", typeof(d)
85
86    a = []
87    b = []
88
89    a = 1.0
90    b = 2.0
91    d = (a + b) / 2
92
93def test_python_objects(cond):
94    """
95    >>> test_python_objects(0)
96    """
97    if cond == 1:
98        a = [1, 2, 3]
99        o_list = a
100    elif cond == 2:
101        a = set([1, 2, 3])
102        o_set = a
103    else:
104        a = {1:1, 2:2, 3:3}
105        o_dict = a
106    assert typeof(a) == "Python object", typeof(a)
107    assert typeof(o_list) == "list object", typeof(o_list)
108    assert typeof(o_dict) == "dict object", typeof(o_dict)
109    assert typeof(o_set) == "set object", typeof(o_set)
110
111# CF loops
112def test_cf_loop():
113    """
114    >>> test_cf_loop()
115    """
116    cdef int i
117    a = 0.0
118    for i in range(3):
119        a += 1
120    assert typeof(a) == "double", typeof(a)
121
122def test_cf_loop_intermediate():
123    """
124    >>> test_cf_loop()
125    """
126    cdef int i
127    a = 0
128    for i in range(3):
129        b = a
130        a = b + 1
131    assert typeof(a) == "long", typeof(a)
132    assert typeof(b) == "long", typeof(b)
133
134# Integer overflow
135def test_integer_overflow():
136    """
137    >>> test_integer_overflow()
138    """
139    a = 1
140    b = 2
141    c = a + b
142    assert typeof(a) == "Python object", typeof(a)
143    assert typeof(b) == "Python object", typeof(b)
144    assert typeof(c) == "Python object", typeof(c)
145