blob: dd62d2e18c12f87d44271110b4a009a3bcc81511 [file] [log] [blame]
Thomas Wouters104a7bc2000-08-24 20:14:10 +00001# Augmented assignment test.
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test.support import run_unittest
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004import unittest
Thomas Wouters104a7bc2000-08-24 20:14:10 +00005
6
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007class 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
Ezio Melottib3aedd42010-11-20 19:04:17 +000020 self.assertEqual(x, 3.0)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000021
Benjamin Petersonbd27aef2009-10-03 20:27:13 +000022 def test_with_unpacking(self):
23 self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec")
24
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000025 def testInList(self):
26 x = [2]
27 x[0] += 1
28 x[0] *= 2
29 x[0] **= 2
30 x[0] -= 8
31 x[0] //= 5
32 x[0] %= 3
33 x[0] &= 2
34 x[0] |= 5
35 x[0] ^= 1
36 x[0] /= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +000037 self.assertEqual(x[0], 3.0)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039 def testInDict(self):
40 x = {0: 2}
41 x[0] += 1
42 x[0] *= 2
43 x[0] **= 2
44 x[0] -= 8
45 x[0] //= 5
46 x[0] %= 3
47 x[0] &= 2
48 x[0] |= 5
49 x[0] ^= 1
50 x[0] /= 2
Ezio Melottib3aedd42010-11-20 19:04:17 +000051 self.assertEqual(x[0], 3.0)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000052
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053 def testSequences(self):
54 x = [1,2]
55 x += [3,4]
56 x *= 2
Thomas Wouters104a7bc2000-08-24 20:14:10 +000057
Ezio Melottib3aedd42010-11-20 19:04:17 +000058 self.assertEqual(x, [1, 2, 3, 4, 1, 2, 3, 4])
Thomas Wouters104a7bc2000-08-24 20:14:10 +000059
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 x = [1, 2, 3]
61 y = x
62 x[1:2] *= 2
63 y[1:2] += [1]
Thomas Wouters104a7bc2000-08-24 20:14:10 +000064
Ezio Melottib3aedd42010-11-20 19:04:17 +000065 self.assertEqual(x, [1, 2, 1, 2, 3])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000066 self.assertTrue(x is y)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000067
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 def testCustomMethods1(self):
Thomas Wouters104a7bc2000-08-24 20:14:10 +000069
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070 class aug_test:
71 def __init__(self, value):
72 self.val = value
73 def __radd__(self, val):
74 return self.val + val
75 def __add__(self, val):
76 return aug_test(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000077
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 class aug_test2(aug_test):
79 def __iadd__(self, val):
80 self.val = self.val + val
81 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +000082
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000083 class aug_test3(aug_test):
84 def __iadd__(self, val):
85 return aug_test3(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000086
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087 x = aug_test(1)
88 y = x
89 x += 10
Thomas Wouters104a7bc2000-08-24 20:14:10 +000090
Ezio Melottie9615932010-01-24 19:26:24 +000091 self.assertIsInstance(x, aug_test)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000092 self.assertTrue(y is not x)
Ezio Melottib3aedd42010-11-20 19:04:17 +000093 self.assertEqual(x.val, 11)
Jeremy Hylton4de8df92001-08-29 17:50:27 +000094
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095 x = aug_test2(2)
96 y = x
97 x += 10
Jeremy Hylton4de8df92001-08-29 17:50:27 +000098
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000099 self.assertTrue(y is x)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000100 self.assertEqual(x.val, 12)
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000101
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102 x = aug_test3(3)
103 y = x
104 x += 10
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000105
Ezio Melottie9615932010-01-24 19:26:24 +0000106 self.assertIsInstance(x, aug_test3)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000107 self.assertTrue(y is not x)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000108 self.assertEqual(x.val, 13)
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000109
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000110
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111 def testCustomMethods2(test_self):
112 output = []
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000113
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 class testall:
115 def __add__(self, val):
116 output.append("__add__ called")
117 def __radd__(self, val):
118 output.append("__radd__ called")
119 def __iadd__(self, val):
120 output.append("__iadd__ called")
121 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000122
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 def __sub__(self, val):
124 output.append("__sub__ called")
125 def __rsub__(self, val):
126 output.append("__rsub__ called")
127 def __isub__(self, val):
128 output.append("__isub__ called")
129 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000130
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 def __mul__(self, val):
132 output.append("__mul__ called")
133 def __rmul__(self, val):
134 output.append("__rmul__ called")
135 def __imul__(self, val):
136 output.append("__imul__ called")
137 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000138
Benjamin Petersond51374e2014-04-09 23:55:56 -0400139 def __matmul__(self, val):
140 output.append("__matmul__ called")
141 def __rmatmul__(self, val):
142 output.append("__rmatmul__ called")
143 def __imatmul__(self, val):
144 output.append("__imatmul__ called")
145 return self
146
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 def __floordiv__(self, val):
148 output.append("__floordiv__ called")
149 return self
150 def __ifloordiv__(self, val):
151 output.append("__ifloordiv__ called")
152 return self
153 def __rfloordiv__(self, val):
154 output.append("__rfloordiv__ called")
155 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000156
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000157 def __truediv__(self, val):
158 output.append("__truediv__ called")
159 return self
160 def __rtruediv__(self, val):
161 output.append("__rtruediv__ called")
162 return self
163 def __itruediv__(self, val):
164 output.append("__itruediv__ called")
165 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000166
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 def __mod__(self, val):
168 output.append("__mod__ called")
169 def __rmod__(self, val):
170 output.append("__rmod__ called")
171 def __imod__(self, val):
172 output.append("__imod__ called")
173 return self
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000174
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175 def __pow__(self, val):
176 output.append("__pow__ called")
177 def __rpow__(self, val):
178 output.append("__rpow__ called")
179 def __ipow__(self, val):
180 output.append("__ipow__ called")
181 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000182
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183 def __or__(self, val):
184 output.append("__or__ called")
185 def __ror__(self, val):
186 output.append("__ror__ called")
187 def __ior__(self, val):
188 output.append("__ior__ called")
189 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000190
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191 def __and__(self, val):
192 output.append("__and__ called")
193 def __rand__(self, val):
194 output.append("__rand__ called")
195 def __iand__(self, val):
196 output.append("__iand__ called")
197 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199 def __xor__(self, val):
200 output.append("__xor__ called")
201 def __rxor__(self, val):
202 output.append("__rxor__ called")
203 def __ixor__(self, val):
204 output.append("__ixor__ called")
205 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000206
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 def __rshift__(self, val):
208 output.append("__rshift__ called")
209 def __rrshift__(self, val):
210 output.append("__rrshift__ called")
211 def __irshift__(self, val):
212 output.append("__irshift__ called")
213 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000214
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 def __lshift__(self, val):
216 output.append("__lshift__ called")
217 def __rlshift__(self, val):
218 output.append("__rlshift__ called")
219 def __ilshift__(self, val):
220 output.append("__ilshift__ called")
221 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000222
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000223 x = testall()
224 x + 1
225 1 + x
226 x += 1
227
228 x - 1
229 1 - x
230 x -= 1
231
232 x * 1
233 1 * x
234 x *= 1
235
Benjamin Petersond51374e2014-04-09 23:55:56 -0400236 x @ 1
237 1 @ x
238 x @= 1
239
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 x / 1
241 1 / x
242 x /= 1
243
244 x // 1
245 1 // x
246 x //= 1
247
248 x % 1
249 1 % x
250 x %= 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
Ezio Melottib3aedd42010-11-20 19:04:17 +0000276 test_self.assertEqual(output, '''\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277__add__ called
278__radd__ called
279__iadd__ called
280__sub__ called
281__rsub__ called
282__isub__ called
283__mul__ called
284__rmul__ called
285__imul__ called
Benjamin Petersond51374e2014-04-09 23:55:56 -0400286__matmul__ called
287__rmatmul__ called
288__imatmul__ called
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289__truediv__ called
290__rtruediv__ called
291__itruediv__ called
292__floordiv__ called
293__rfloordiv__ called
294__ifloordiv__ called
295__mod__ called
296__rmod__ called
297__imod__ called
298__pow__ called
299__rpow__ called
300__ipow__ called
301__or__ called
302__ror__ called
303__ior__ called
304__and__ called
305__rand__ called
306__iand__ called
307__xor__ called
308__rxor__ called
309__ixor__ called
310__rshift__ called
311__rrshift__ called
312__irshift__ called
313__lshift__ called
314__rlshift__ called
315__ilshift__ called
316'''.splitlines())
317
318def test_main():
319 run_unittest(AugAssignTest)
320
321if __name__ == '__main__':
322 test_main()