Remove trailing whitespace.
[cython.git] / tests / run / inplace.pyx
1 cimport cython
2
3 def f(a,b):
4     """
5     >>> str(f(5, 7))
6     '29509034655744'
7     """
8     a += b
9     a *= b
10     a **= b
11     return a
12
13 def g(int a, int b):
14     """
15     >>> g(13, 4)
16     32
17     """
18     a -= b
19     a /= b
20     a <<= b
21     return a
22
23 def h(double a, double b):
24     """
25     >>> h(56, 7)
26     105.0
27     """
28     a /= b
29     a += b
30     a *= b
31     return a
32
33 from libc cimport stdlib
34
35 def arrays():
36     """
37     >>> arrays()
38     19
39     """
40     cdef char* buf = <char*>stdlib.malloc(10)
41     cdef int i = 2
42     cdef object j = 2
43     buf[2] = 0
44     buf[i] += 2
45     buf[2] *= 10
46     buf[j] -= 1
47     print buf[2]
48     stdlib.free(buf)
49
50 cdef class A:
51     cdef attr
52     cdef int attr2
53     cdef char* buf
54     def __init__(self):
55         self.attr = 3
56         self.attr2 = 3
57
58 class B:
59     attr = 3
60
61 def attributes():
62     """
63     >>> attributes()
64     26 26 26
65     """
66     cdef A a = A()
67     b = B()
68     a.attr += 10
69     a.attr *= 2
70     a.attr2 += 10
71     a.attr2 *= 2
72     b.attr += 10
73     b.attr *= 2
74     print a.attr, a.attr2, b.attr
75
76 def get_2(): return 2
77 cdef int identity(int value): return value
78
79 def smoketest():
80     """
81     >>> smoketest()
82     10
83     """
84     cdef char* buf = <char*>stdlib.malloc(10)
85     cdef A a = A()
86     a.buf = buf
87     a.buf[identity(1)] = 0
88     (a.buf + identity(4) - <int>(2*get_2() - 1))[get_2() - 2*identity(1)] += 10
89     print a.buf[1]
90     stdlib.free(buf)
91
92
93 def side_effect(x):
94     print u"side effect", x
95     return x
96
97 cdef int c_side_effect(int x):
98     print u"c side effect", x
99     return x
100
101 def test_side_effects():
102     """
103     >>> test_side_effects()
104     side effect 1
105     c side effect 2
106     side effect 3
107     c side effect 4
108     ([0, 11, 102, 3, 4], [0, 1, 2, 13, 104])
109     """
110     cdef object a = list(range(5))
111     a[side_effect(1)] += 10
112     a[c_side_effect(2)] += 100
113     cdef int i
114     cdef int b[5]
115     for i from 0 <= i < 5:
116         b[i] = i
117     b[side_effect(3)] += 10
118     b[c_side_effect(4)] += 100
119     return a, [b[i] for i from 0 <= i < 5]
120
121 @cython.cdivision(True)
122 def test_inplace_cdivision(int a, int b):
123     """
124     >>> test_inplace_cdivision(13, 10)
125     3
126     >>> test_inplace_cdivision(13, -10)
127     3
128     >>> test_inplace_cdivision(-13, 10)
129     -3
130     >>> test_inplace_cdivision(-13, -10)
131     -3
132     """
133     a %= b
134     return a
135
136 @cython.cdivision(False)
137 def test_inplace_pydivision(int a, int b):
138     """
139     >>> test_inplace_pydivision(13, 10)
140     3
141     >>> test_inplace_pydivision(13, -10)
142     -7
143     >>> test_inplace_pydivision(-13, 10)
144     7
145     >>> test_inplace_pydivision(-13, -10)
146     -3
147     """
148     a %= b
149     return a
150
151 def test_complex_inplace(double complex x, double complex y):
152     """
153     >>> test_complex_inplace(1, 1)
154     (2+0j)
155     >>> test_complex_inplace(2, 3)
156     (15+0j)
157     >>> test_complex_inplace(2+3j, 4+5j)
158     (-16+62j)
159     """
160     x += y
161     x *= y
162     return x
163
164
165 # The following is more subtle than one might expect.
166
167 cdef struct Inner:
168     int x
169
170 cdef struct Aa:
171     int value
172     Inner inner
173
174 cdef struct NestedA:
175     Aa a
176
177 cdef struct ArrayOfA:
178     Aa[10] a
179
180 def nested_struct_assignment():
181     """
182     >>> nested_struct_assignment()
183     """
184     cdef NestedA nested
185     nested.a.value = 2
186     nested.a.value += 3
187     assert nested.a.value == 5
188
189     nested.a.inner.x = 5
190     nested.a.inner.x += 10
191     assert nested.a.inner.x == 15
192
193 def nested_array_assignment():
194     """
195     >>> nested_array_assignment()
196     c side effect 0
197     c side effect 1
198     """
199     cdef ArrayOfA array
200     array.a[0].value = 2
201     array.a[c_side_effect(0)].value += 3
202     assert array.a[0].value == 5
203
204     array.a[1].inner.x = 5
205     array.a[c_side_effect(1)].inner.x += 10
206     assert array.a[1].inner.x == 15
207
208 cdef class VerboseDict(object):
209     cdef name
210     cdef dict dict
211     def __init__(self, name, **kwds):
212         self.name = name
213         self.dict = kwds
214     def __getitem__(self, key):
215         print self.name, "__getitem__", key
216         return self.dict[key]
217     def __setitem__(self, key, value):
218         print self.name, "__setitem__", key, value
219         self.dict[key] = value
220     def __repr__(self):
221         return repr(self.name)
222
223 def deref_and_increment(o, key):
224     """
225     >>> deref_and_increment({'a': 1}, 'a')
226     side effect a
227     >>> v = VerboseDict('v', a=10)
228     >>> deref_and_increment(v, 'a')
229     side effect a
230     v __getitem__ a
231     v __setitem__ a 11
232     """
233     o[side_effect(key)] += 1
234
235 def double_deref_and_increment(o, key1, key2):
236     """
237     >>> v = VerboseDict('v', a=10)
238     >>> w = VerboseDict('w', vkey=v)
239     >>> double_deref_and_increment(w, 'vkey', 'a')
240     side effect vkey
241     w __getitem__ vkey
242     side effect a
243     v __getitem__ a
244     v __setitem__ a 11
245     """
246     o[side_effect(key1)][side_effect(key2)] += 1
247
248 def conditional_inplace(value, a, condition, b):
249     """
250     >>> conditional_inplace([1, 2, 3], [100], True, [200])
251     [1, 2, 3, 100]
252     >>> conditional_inplace([1, 2, 3], [100], False, [200])
253     [1, 2, 3, 200]
254     """
255     value += a if condition else b
256     return value