move sections
[python/dscho.git] / Lib / test / test_augassign.py
blob0292a958f24028d5cbb24feef11798cb9e61a964
1 # Augmented assignment test.
3 from test.test_support import run_unittest, check_py3k_warnings
4 import unittest
7 class 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)
27 def test_with_unpacking(self):
28 self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec")
30 def testInList(self):
31 x = [2]
32 x[0] += 1
33 x[0] *= 2
34 x[0] **= 2
35 x[0] -= 8
36 x[0] //= 5
37 x[0] %= 3
38 x[0] &= 2
39 x[0] |= 5
40 x[0] ^= 1
41 x[0] /= 2
42 if 1/2 == 0:
43 self.assertEquals(x[0], 3)
44 else:
45 self.assertEquals(x[0], 3.0)
47 def testInDict(self):
48 x = {0: 2}
49 x[0] += 1
50 x[0] *= 2
51 x[0] **= 2
52 x[0] -= 8
53 x[0] //= 5
54 x[0] %= 3
55 x[0] &= 2
56 x[0] |= 5
57 x[0] ^= 1
58 x[0] /= 2
59 if 1/2 == 0:
60 self.assertEquals(x[0], 3)
61 else:
62 self.assertEquals(x[0], 3.0)
64 def testSequences(self):
65 x = [1,2]
66 x += [3,4]
67 x *= 2
69 self.assertEquals(x, [1, 2, 3, 4, 1, 2, 3, 4])
71 x = [1, 2, 3]
72 y = x
73 x[1:2] *= 2
74 y[1:2] += [1]
76 self.assertEquals(x, [1, 2, 1, 2, 3])
77 self.assertTrue(x is y)
79 def testCustomMethods1(self):
81 class aug_test:
82 def __init__(self, value):
83 self.val = value
84 def __radd__(self, val):
85 return self.val + val
86 def __add__(self, val):
87 return aug_test(self.val + val)
89 class aug_test2(aug_test):
90 def __iadd__(self, val):
91 self.val = self.val + val
92 return self
94 class aug_test3(aug_test):
95 def __iadd__(self, val):
96 return aug_test3(self.val + val)
98 x = aug_test(1)
99 y = x
100 x += 10
102 self.assertIsInstance(x, aug_test)
103 self.assertTrue(y is not x)
104 self.assertEquals(x.val, 11)
106 x = aug_test2(2)
107 y = x
108 x += 10
110 self.assertTrue(y is x)
111 self.assertEquals(x.val, 12)
113 x = aug_test3(3)
114 y = x
115 x += 10
117 self.assertIsInstance(x, aug_test3)
118 self.assertTrue(y is not x)
119 self.assertEquals(x.val, 13)
122 def testCustomMethods2(test_self):
123 output = []
125 class testall:
126 def __add__(self, val):
127 output.append("__add__ called")
128 def __radd__(self, val):
129 output.append("__radd__ called")
130 def __iadd__(self, val):
131 output.append("__iadd__ called")
132 return self
134 def __sub__(self, val):
135 output.append("__sub__ called")
136 def __rsub__(self, val):
137 output.append("__rsub__ called")
138 def __isub__(self, val):
139 output.append("__isub__ called")
140 return self
142 def __mul__(self, val):
143 output.append("__mul__ called")
144 def __rmul__(self, val):
145 output.append("__rmul__ called")
146 def __imul__(self, val):
147 output.append("__imul__ called")
148 return self
150 def __div__(self, val):
151 output.append("__div__ called")
152 def __rdiv__(self, val):
153 output.append("__rdiv__ called")
154 def __idiv__(self, val):
155 output.append("__idiv__ called")
156 return self
158 def __floordiv__(self, val):
159 output.append("__floordiv__ called")
160 return self
161 def __ifloordiv__(self, val):
162 output.append("__ifloordiv__ called")
163 return self
164 def __rfloordiv__(self, val):
165 output.append("__rfloordiv__ called")
166 return self
168 def __truediv__(self, val):
169 output.append("__truediv__ called")
170 return self
171 def __itruediv__(self, val):
172 output.append("__itruediv__ called")
173 return self
175 def __mod__(self, val):
176 output.append("__mod__ called")
177 def __rmod__(self, val):
178 output.append("__rmod__ called")
179 def __imod__(self, val):
180 output.append("__imod__ called")
181 return self
183 def __pow__(self, val):
184 output.append("__pow__ called")
185 def __rpow__(self, val):
186 output.append("__rpow__ called")
187 def __ipow__(self, val):
188 output.append("__ipow__ called")
189 return self
191 def __or__(self, val):
192 output.append("__or__ called")
193 def __ror__(self, val):
194 output.append("__ror__ called")
195 def __ior__(self, val):
196 output.append("__ior__ called")
197 return self
199 def __and__(self, val):
200 output.append("__and__ called")
201 def __rand__(self, val):
202 output.append("__rand__ called")
203 def __iand__(self, val):
204 output.append("__iand__ called")
205 return self
207 def __xor__(self, val):
208 output.append("__xor__ called")
209 def __rxor__(self, val):
210 output.append("__rxor__ called")
211 def __ixor__(self, val):
212 output.append("__ixor__ called")
213 return self
215 def __rshift__(self, val):
216 output.append("__rshift__ called")
217 def __rrshift__(self, val):
218 output.append("__rrshift__ called")
219 def __irshift__(self, val):
220 output.append("__irshift__ called")
221 return self
223 def __lshift__(self, val):
224 output.append("__lshift__ called")
225 def __rlshift__(self, val):
226 output.append("__rlshift__ called")
227 def __ilshift__(self, val):
228 output.append("__ilshift__ called")
229 return self
231 x = testall()
232 x + 1
233 1 + x
234 x += 1
236 x - 1
237 1 - x
238 x -= 1
240 x * 1
241 1 * x
242 x *= 1
244 if 1/2 == 0:
245 x / 1
246 1 / x
247 x /= 1
248 else:
249 # True division is in effect, so "/" doesn't map to __div__ etc;
250 # but the canned expected-output file requires that those get called.
251 x.__div__(1)
252 x.__rdiv__(1)
253 x.__idiv__(1)
255 x // 1
256 1 // x
257 x //= 1
259 x % 1
260 1 % x
261 x %= 1
263 x ** 1
264 1 ** x
265 x **= 1
267 x | 1
268 1 | x
269 x |= 1
271 x & 1
272 1 & x
273 x &= 1
275 x ^ 1
276 1 ^ x
277 x ^= 1
279 x >> 1
280 1 >> x
281 x >>= 1
283 x << 1
284 1 << x
285 x <<= 1
287 test_self.assertEquals(output, '''\
288 __add__ called
289 __radd__ called
290 __iadd__ called
291 __sub__ called
292 __rsub__ called
293 __isub__ called
294 __mul__ called
295 __rmul__ called
296 __imul__ called
297 __div__ called
298 __rdiv__ called
299 __idiv__ called
300 __floordiv__ called
301 __rfloordiv__ called
302 __ifloordiv__ called
303 __mod__ called
304 __rmod__ called
305 __imod__ called
306 __pow__ called
307 __rpow__ called
308 __ipow__ called
309 __or__ called
310 __ror__ called
311 __ior__ called
312 __and__ called
313 __rand__ called
314 __iand__ called
315 __xor__ called
316 __rxor__ called
317 __ixor__ called
318 __rshift__ called
319 __rrshift__ called
320 __irshift__ called
321 __lshift__ called
322 __rlshift__ called
323 __ilshift__ called
324 '''.splitlines())
326 def test_main():
327 with check_py3k_warnings(("classic int division", DeprecationWarning)):
328 run_unittest(AugAssignTest)
330 if __name__ == '__main__':
331 test_main()