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