Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 1 | # Augmented assignment test. |
| 2 | |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 3 | from test.test_support import run_unittest, check_py3k_warnings |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 4 | import unittest |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 5 | |
| 6 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 22 | self.assertEqual(x, 3) |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 23 | else: |
| 24 | # new-style division (with -Qnew) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 25 | self.assertEqual(x, 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 26 | |
Benjamin Peterson | 7adbb5a | 2009-10-03 20:23:24 +0000 | [diff] [blame] | 27 | def test_with_unpacking(self): |
| 28 | self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec") |
| 29 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 43 | self.assertEqual(x[0], 3) |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 44 | else: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 45 | self.assertEqual(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 46 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 60 | self.assertEqual(x[0], 3) |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 61 | else: |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 62 | self.assertEqual(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 63 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 64 | def testSequences(self): |
| 65 | x = [1,2] |
| 66 | x += [3,4] |
| 67 | x *= 2 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 68 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 69 | self.assertEqual(x, [1, 2, 3, 4, 1, 2, 3, 4]) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame] | 70 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 71 | x = [1, 2, 3] |
| 72 | y = x |
| 73 | x[1:2] *= 2 |
| 74 | y[1:2] += [1] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 75 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 76 | self.assertEqual(x, [1, 2, 1, 2, 3]) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 77 | self.assertTrue(x is y) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 78 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 79 | def testCustomMethods1(self): |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 80 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame] | 88 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 89 | class aug_test2(aug_test): |
| 90 | def __iadd__(self, val): |
| 91 | self.val = self.val + val |
| 92 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 93 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 94 | class aug_test3(aug_test): |
| 95 | def __iadd__(self, val): |
| 96 | return aug_test3(self.val + val) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame] | 97 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 98 | x = aug_test(1) |
| 99 | y = x |
| 100 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 101 | |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 102 | self.assertIsInstance(x, aug_test) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 103 | self.assertTrue(y is not x) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 104 | self.assertEqual(x.val, 11) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 105 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 106 | x = aug_test2(2) |
| 107 | y = x |
| 108 | x += 10 |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame] | 109 | |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 110 | self.assertTrue(y is x) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 111 | self.assertEqual(x.val, 12) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 113 | x = aug_test3(3) |
| 114 | y = x |
| 115 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 116 | |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 117 | self.assertIsInstance(x, aug_test3) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 118 | self.assertTrue(y is not x) |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 119 | self.assertEqual(x.val, 13) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 120 | |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 121 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 122 | def testCustomMethods2(test_self): |
| 123 | output = [] |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 124 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 133 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 141 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 149 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 157 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 182 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 190 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 198 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 206 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 214 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 222 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 230 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 231 | x = testall() |
| 232 | x + 1 |
| 233 | 1 + x |
| 234 | x += 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 235 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 236 | x - 1 |
| 237 | 1 - x |
| 238 | x -= 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 239 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 240 | x * 1 |
| 241 | 1 * x |
| 242 | x *= 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 243 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 254 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 255 | x // 1 |
| 256 | 1 // x |
| 257 | x //= 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 258 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 259 | x % 1 |
| 260 | 1 % x |
| 261 | x %= 1 |
| 262 | |
| 263 | x ** 1 |
| 264 | 1 ** x |
| 265 | x **= 1 |
| 266 | |
| 267 | x | 1 |
| 268 | 1 | x |
| 269 | x |= 1 |
| 270 | |
| 271 | x & 1 |
| 272 | 1 & x |
| 273 | x &= 1 |
| 274 | |
| 275 | x ^ 1 |
| 276 | 1 ^ x |
| 277 | x ^= 1 |
| 278 | |
| 279 | x >> 1 |
| 280 | 1 >> x |
| 281 | x >>= 1 |
| 282 | |
| 283 | x << 1 |
| 284 | 1 << x |
| 285 | x <<= 1 |
| 286 | |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 287 | test_self.assertEqual(output, '''\ |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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()) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame] | 325 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 326 | def test_main(): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 327 | with check_py3k_warnings(("classic int division", DeprecationWarning)): |
| 328 | run_unittest(AugAssignTest) |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 329 | |
| 330 | if __name__ == '__main__': |
| 331 | test_main() |