Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 1 | # Augmented assignment test. |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import run_unittest |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4 | import unittest |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 5 | |
| 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | self.assertEquals(x, 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 21 | |
Benjamin Peterson | bd27aef | 2009-10-03 20:27:13 +0000 | [diff] [blame] | 22 | def test_with_unpacking(self): |
| 23 | self.assertRaises(SyntaxError, compile, "x, b += 3", "<test>", "exec") |
| 24 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 25 | 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 |
| 37 | self.assertEquals(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 38 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | 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 |
| 51 | self.assertEquals(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 52 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 53 | def testSequences(self): |
| 54 | x = [1,2] |
| 55 | x += [3,4] |
| 56 | x *= 2 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | self.assertEquals(x, [1, 2, 3, 4, 1, 2, 3, 4]) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 59 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 60 | x = [1, 2, 3] |
| 61 | y = x |
| 62 | x[1:2] *= 2 |
| 63 | y[1:2] += [1] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 65 | self.assertEquals(x, [1, 2, 1, 2, 3]) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 66 | self.assertTrue(x is y) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | def testCustomMethods1(self): |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 69 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 70 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | class aug_test2(aug_test): |
| 79 | def __iadd__(self, val): |
| 80 | self.val = self.val + val |
| 81 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 83 | class aug_test3(aug_test): |
| 84 | def __iadd__(self, val): |
| 85 | return aug_test3(self.val + val) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 87 | x = aug_test(1) |
| 88 | y = x |
| 89 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 90 | |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 91 | self.assertIsInstance(x, aug_test) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 92 | self.assertTrue(y is not x) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | self.assertEquals(x.val, 11) |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 94 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 95 | x = aug_test2(2) |
| 96 | y = x |
| 97 | x += 10 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 98 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 99 | self.assertTrue(y is x) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 100 | self.assertEquals(x.val, 12) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 101 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 102 | x = aug_test3(3) |
| 103 | y = x |
| 104 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 105 | |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 106 | self.assertIsInstance(x, aug_test3) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 107 | self.assertTrue(y is not x) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | self.assertEquals(x.val, 13) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 109 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 110 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | def testCustomMethods2(test_self): |
| 112 | output = [] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 113 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 114 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 122 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 123 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 130 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 131 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 138 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 139 | def __div__(self, val): |
| 140 | output.append("__div__ called") |
| 141 | def __rdiv__(self, val): |
| 142 | output.append("__rdiv__ called") |
| 143 | def __idiv__(self, val): |
| 144 | output.append("__idiv__ called") |
| 145 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 146 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 147 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 156 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 157 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 166 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 167 | 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 Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 174 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 175 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 182 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 183 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 190 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 191 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 198 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 199 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 206 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 207 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 214 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 215 | 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 Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 222 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 223 | 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 | |
| 236 | x / 1 |
| 237 | 1 / x |
| 238 | x /= 1 |
| 239 | |
| 240 | 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 | test_self.assertEquals(output, '''\ |
| 273 | __add__ called |
| 274 | __radd__ called |
| 275 | __iadd__ called |
| 276 | __sub__ called |
| 277 | __rsub__ called |
| 278 | __isub__ called |
| 279 | __mul__ called |
| 280 | __rmul__ called |
| 281 | __imul__ called |
| 282 | __truediv__ called |
| 283 | __rtruediv__ called |
| 284 | __itruediv__ called |
| 285 | __floordiv__ called |
| 286 | __rfloordiv__ called |
| 287 | __ifloordiv__ called |
| 288 | __mod__ called |
| 289 | __rmod__ called |
| 290 | __imod__ called |
| 291 | __pow__ called |
| 292 | __rpow__ called |
| 293 | __ipow__ called |
| 294 | __or__ called |
| 295 | __ror__ called |
| 296 | __ior__ called |
| 297 | __and__ called |
| 298 | __rand__ called |
| 299 | __iand__ called |
| 300 | __xor__ called |
| 301 | __rxor__ called |
| 302 | __ixor__ called |
| 303 | __rshift__ called |
| 304 | __rrshift__ called |
| 305 | __irshift__ called |
| 306 | __lshift__ called |
| 307 | __rlshift__ called |
| 308 | __ilshift__ called |
| 309 | '''.splitlines()) |
| 310 | |
| 311 | def test_main(): |
| 312 | run_unittest(AugAssignTest) |
| 313 | |
| 314 | if __name__ == '__main__': |
| 315 | test_main() |