PageRenderTime 45ms CodeModel.GetById 14ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_augassign.py

http://unladen-swallow.googlecode.com/
Python | 327 lines | 275 code | 47 blank | 5 comment | 6 complexity | 2730d9d94e42aeee9c71a044dec1e6b3 MD5 | raw file
  1# Augmented assignment test.
  2
  3from test.test_support import run_unittest
  4import unittest
  5
  6
  7class AugAssignTest(unittest.TestCase):
  8    def testBasic(self):
  9        x = 2
 10        x += 1
 11        x *= 2
 12        x **= 2
 13        x -= 8
 14        x //= 5
 15        x %= 3
 16        x &= 2
 17        x |= 5
 18        x ^= 1
 19        x /= 2
 20        if 1/2 == 0:
 21            # classic division
 22            self.assertEquals(x, 3)
 23        else:
 24            # new-style division (with -Qnew)
 25            self.assertEquals(x, 3.0)
 26
 27    def testInList(self):
 28        x = [2]
 29        x[0] += 1
 30        x[0] *= 2
 31        x[0] **= 2
 32        x[0] -= 8
 33        x[0] //= 5
 34        x[0] %= 3
 35        x[0] &= 2
 36        x[0] |= 5
 37        x[0] ^= 1
 38        x[0] /= 2
 39        if 1/2 == 0:
 40            self.assertEquals(x[0], 3)
 41        else:
 42            self.assertEquals(x[0], 3.0)
 43
 44    def testInDict(self):
 45        x = {0: 2}
 46        x[0] += 1
 47        x[0] *= 2
 48        x[0] **= 2
 49        x[0] -= 8
 50        x[0] //= 5
 51        x[0] %= 3
 52        x[0] &= 2
 53        x[0] |= 5
 54        x[0] ^= 1
 55        x[0] /= 2
 56        if 1/2 == 0:
 57            self.assertEquals(x[0], 3)
 58        else:
 59            self.assertEquals(x[0], 3.0)
 60
 61    def testSequences(self):
 62        x = [1,2]
 63        x += [3,4]
 64        x *= 2
 65
 66        self.assertEquals(x, [1, 2, 3, 4, 1, 2, 3, 4])
 67
 68        x = [1, 2, 3]
 69        y = x
 70        x[1:2] *= 2
 71        y[1:2] += [1]
 72
 73        self.assertEquals(x, [1, 2, 1, 2, 3])
 74        self.assert_(x is y)
 75
 76    def testCustomMethods1(self):
 77
 78        class aug_test:
 79            def __init__(self, value):
 80                self.val = value
 81            def __radd__(self, val):
 82                return self.val + val
 83            def __add__(self, val):
 84                return aug_test(self.val + val)
 85
 86        class aug_test2(aug_test):
 87            def __iadd__(self, val):
 88                self.val = self.val + val
 89                return self
 90
 91        class aug_test3(aug_test):
 92            def __iadd__(self, val):
 93                return aug_test3(self.val + val)
 94
 95        x = aug_test(1)
 96        y = x
 97        x += 10
 98
 99        self.assert_(isinstance(x, aug_test))
100        self.assert_(y is not x)
101        self.assertEquals(x.val, 11)
102
103        x = aug_test2(2)
104        y = x
105        x += 10
106
107        self.assert_(y is x)
108        self.assertEquals(x.val, 12)
109
110        x = aug_test3(3)
111        y = x
112        x += 10
113
114        self.assert_(isinstance(x, aug_test3))
115        self.assert_(y is not x)
116        self.assertEquals(x.val, 13)
117
118
119    def testCustomMethods2(test_self):
120        output = []
121
122        class testall:
123            def __add__(self, val):
124                output.append("__add__ called")
125            def __radd__(self, val):
126                output.append("__radd__ called")
127            def __iadd__(self, val):
128                output.append("__iadd__ called")
129                return self
130
131            def __sub__(self, val):
132                output.append("__sub__ called")
133            def __rsub__(self, val):
134                output.append("__rsub__ called")
135            def __isub__(self, val):
136                output.append("__isub__ called")
137                return self
138
139            def __mul__(self, val):
140                output.append("__mul__ called")
141            def __rmul__(self, val):
142                output.append("__rmul__ called")
143            def __imul__(self, val):
144                output.append("__imul__ called")
145                return self
146
147            def __div__(self, val):
148                output.append("__div__ called")
149            def __rdiv__(self, val):
150                output.append("__rdiv__ called")
151            def __idiv__(self, val):
152                output.append("__idiv__ called")
153                return self
154
155            def __floordiv__(self, val):
156                output.append("__floordiv__ called")
157                return self
158            def __ifloordiv__(self, val):
159                output.append("__ifloordiv__ called")
160                return self
161            def __rfloordiv__(self, val):
162                output.append("__rfloordiv__ called")
163                return self
164
165            def __truediv__(self, val):
166                output.append("__truediv__ called")
167                return self
168            def __itruediv__(self, val):
169                output.append("__itruediv__ called")
170                return self
171
172            def __mod__(self, val):
173                output.append("__mod__ called")
174            def __rmod__(self, val):
175                output.append("__rmod__ called")
176            def __imod__(self, val):
177                output.append("__imod__ called")
178                return self
179
180            def __pow__(self, val):
181                output.append("__pow__ called")
182            def __rpow__(self, val):
183                output.append("__rpow__ called")
184            def __ipow__(self, val):
185                output.append("__ipow__ called")
186                return self
187
188            def __or__(self, val):
189                output.append("__or__ called")
190            def __ror__(self, val):
191                output.append("__ror__ called")
192            def __ior__(self, val):
193                output.append("__ior__ called")
194                return self
195
196            def __and__(self, val):
197                output.append("__and__ called")
198            def __rand__(self, val):
199                output.append("__rand__ called")
200            def __iand__(self, val):
201                output.append("__iand__ called")
202                return self
203
204            def __xor__(self, val):
205                output.append("__xor__ called")
206            def __rxor__(self, val):
207                output.append("__rxor__ called")
208            def __ixor__(self, val):
209                output.append("__ixor__ called")
210                return self
211
212            def __rshift__(self, val):
213                output.append("__rshift__ called")
214            def __rrshift__(self, val):
215                output.append("__rrshift__ called")
216            def __irshift__(self, val):
217                output.append("__irshift__ called")
218                return self
219
220            def __lshift__(self, val):
221                output.append("__lshift__ called")
222            def __rlshift__(self, val):
223                output.append("__rlshift__ called")
224            def __ilshift__(self, val):
225                output.append("__ilshift__ called")
226                return self
227
228        x = testall()
229        x + 1
230        1 + x
231        x += 1
232
233        x - 1
234        1 - x
235        x -= 1
236
237        x * 1
238        1 * x
239        x *= 1
240
241        if 1/2 == 0:
242            x / 1
243            1 / x
244            x /= 1
245        else:
246            # True division is in effect, so "/" doesn't map to __div__ etc;
247            # but the canned expected-output file requires that those get called.
248            x.__div__(1)
249            x.__rdiv__(1)
250            x.__idiv__(1)
251
252        x // 1
253        1 // x
254        x //= 1
255
256        x % 1
257        1 % x
258        x %= 1
259
260        x ** 1
261        1 ** x
262        x **= 1
263
264        x | 1
265        1 | x
266        x |= 1
267
268        x & 1
269        1 & x
270        x &= 1
271
272        x ^ 1
273        1 ^ x
274        x ^= 1
275
276        x >> 1
277        1 >> x
278        x >>= 1
279
280        x << 1
281        1 << x
282        x <<= 1
283
284        test_self.assertEquals(output, '''\
285__add__ called
286__radd__ called
287__iadd__ called
288__sub__ called
289__rsub__ called
290__isub__ called
291__mul__ called
292__rmul__ called
293__imul__ called
294__div__ called
295__rdiv__ called
296__idiv__ called
297__floordiv__ called
298__rfloordiv__ called
299__ifloordiv__ called
300__mod__ called
301__rmod__ called
302__imod__ called
303__pow__ called
304__rpow__ called
305__ipow__ called
306__or__ called
307__ror__ called
308__ior__ called
309__and__ called
310__rand__ called
311__iand__ called
312__xor__ called
313__rxor__ called
314__ixor__ called
315__rshift__ called
316__rrshift__ called
317__irshift__ called
318__lshift__ called
319__rlshift__ called
320__ilshift__ called
321'''.splitlines())
322
323def test_main():
324    run_unittest(AugAssignTest)
325
326if __name__ == '__main__':
327    test_main()