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 |
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 |
| 22 | self.assertEquals(x, 3) |
| 23 | else: |
| 24 | # new-style division (with -Qnew) |
| 25 | self.assertEquals(x, 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 43 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 60 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 61 | def testSequences(self): |
| 62 | x = [1,2] |
| 63 | x += [3,4] |
| 64 | x *= 2 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 66 | self.assertEquals(x, [1, 2, 3, 4, 1, 2, 3, 4]) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame^] | 67 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 68 | x = [1, 2, 3] |
| 69 | y = x |
| 70 | x[1:2] *= 2 |
| 71 | y[1:2] += [1] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 72 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 73 | self.assertEquals(x, [1, 2, 1, 2, 3]) |
| 74 | self.assert_(x is y) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 76 | def testCustomMethods1(self): |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame^] | 85 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 86 | class aug_test2(aug_test): |
| 87 | def __iadd__(self, val): |
| 88 | self.val = self.val + val |
| 89 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 90 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 91 | class aug_test3(aug_test): |
| 92 | def __iadd__(self, val): |
| 93 | return aug_test3(self.val + val) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame^] | 94 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 95 | x = aug_test(1) |
| 96 | y = x |
| 97 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 99 | self.assert_(isinstance(x, aug_test)) |
| 100 | self.assert_(y is not x) |
| 101 | self.assertEquals(x.val, 11) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 103 | x = aug_test2(2) |
| 104 | y = x |
| 105 | x += 10 |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame^] | 106 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 107 | self.assert_(y is x) |
| 108 | self.assertEquals(x.val, 12) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 109 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 110 | x = aug_test3(3) |
| 111 | y = x |
| 112 | x += 10 |
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 | self.assert_(isinstance(x, aug_test3)) |
| 115 | self.assert_(y is not x) |
| 116 | self.assertEquals(x.val, 13) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 117 | |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 118 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 119 | def testCustomMethods2(test_self): |
| 120 | output = [] |
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 | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 130 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 146 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 154 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 164 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 171 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 179 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 187 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 195 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 203 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 211 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 219 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 227 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 228 | x = testall() |
| 229 | x + 1 |
| 230 | 1 + x |
| 231 | x += 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 232 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 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 | 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) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 251 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 252 | x // 1 |
| 253 | 1 // x |
| 254 | x //= 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 |
| 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()) |
Anthony Baxter | 4ef3a23 | 2006-03-30 12:59:11 +0000 | [diff] [blame^] | 322 | |
Georg Brandl | 019514e | 2006-03-28 10:26:45 +0000 | [diff] [blame] | 323 | def test_main(): |
| 324 | run_unittest(AugAssignTest) |
| 325 | |
| 326 | if __name__ == '__main__': |
| 327 | test_main() |