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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 22 | def testInList(self): |
| 23 | x = [2] |
| 24 | x[0] += 1 |
| 25 | x[0] *= 2 |
| 26 | x[0] **= 2 |
| 27 | x[0] -= 8 |
| 28 | x[0] //= 5 |
| 29 | x[0] %= 3 |
| 30 | x[0] &= 2 |
| 31 | x[0] |= 5 |
| 32 | x[0] ^= 1 |
| 33 | x[0] /= 2 |
| 34 | self.assertEquals(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 35 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | def testInDict(self): |
| 37 | x = {0: 2} |
| 38 | x[0] += 1 |
| 39 | x[0] *= 2 |
| 40 | x[0] **= 2 |
| 41 | x[0] -= 8 |
| 42 | x[0] //= 5 |
| 43 | x[0] %= 3 |
| 44 | x[0] &= 2 |
| 45 | x[0] |= 5 |
| 46 | x[0] ^= 1 |
| 47 | x[0] /= 2 |
| 48 | self.assertEquals(x[0], 3.0) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 49 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 50 | def testSequences(self): |
| 51 | x = [1,2] |
| 52 | x += [3,4] |
| 53 | x *= 2 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 54 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 55 | self.assertEquals(x, [1, 2, 3, 4, 1, 2, 3, 4]) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 56 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | x = [1, 2, 3] |
| 58 | y = x |
| 59 | x[1:2] *= 2 |
| 60 | y[1:2] += [1] |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | self.assertEquals(x, [1, 2, 1, 2, 3]) |
| 63 | self.assert_(x is y) |
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 | def testCustomMethods1(self): |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 66 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 67 | class aug_test: |
| 68 | def __init__(self, value): |
| 69 | self.val = value |
| 70 | def __radd__(self, val): |
| 71 | return self.val + val |
| 72 | def __add__(self, val): |
| 73 | return aug_test(self.val + val) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 75 | class aug_test2(aug_test): |
| 76 | def __iadd__(self, val): |
| 77 | self.val = self.val + val |
| 78 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 80 | class aug_test3(aug_test): |
| 81 | def __iadd__(self, val): |
| 82 | return aug_test3(self.val + val) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 83 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 84 | x = aug_test(1) |
| 85 | y = x |
| 86 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | self.assert_(isinstance(x, aug_test)) |
| 89 | self.assert_(y is not x) |
| 90 | self.assertEquals(x.val, 11) |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 91 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 92 | x = aug_test2(2) |
| 93 | y = x |
| 94 | x += 10 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 95 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 96 | self.assert_(y is x) |
| 97 | self.assertEquals(x.val, 12) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 98 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 99 | x = aug_test3(3) |
| 100 | y = x |
| 101 | x += 10 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 102 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 103 | self.assert_(isinstance(x, aug_test3)) |
| 104 | self.assert_(y is not x) |
| 105 | self.assertEquals(x.val, 13) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 106 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 107 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | def testCustomMethods2(test_self): |
| 109 | output = [] |
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 | class testall: |
| 112 | def __add__(self, val): |
| 113 | output.append("__add__ called") |
| 114 | def __radd__(self, val): |
| 115 | output.append("__radd__ called") |
| 116 | def __iadd__(self, val): |
| 117 | output.append("__iadd__ called") |
| 118 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 119 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 120 | def __sub__(self, val): |
| 121 | output.append("__sub__ called") |
| 122 | def __rsub__(self, val): |
| 123 | output.append("__rsub__ called") |
| 124 | def __isub__(self, val): |
| 125 | output.append("__isub__ called") |
| 126 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 127 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 128 | def __mul__(self, val): |
| 129 | output.append("__mul__ called") |
| 130 | def __rmul__(self, val): |
| 131 | output.append("__rmul__ called") |
| 132 | def __imul__(self, val): |
| 133 | output.append("__imul__ called") |
| 134 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 136 | def __div__(self, val): |
| 137 | output.append("__div__ called") |
| 138 | def __rdiv__(self, val): |
| 139 | output.append("__rdiv__ called") |
| 140 | def __idiv__(self, val): |
| 141 | output.append("__idiv__ called") |
| 142 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 143 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 144 | def __floordiv__(self, val): |
| 145 | output.append("__floordiv__ called") |
| 146 | return self |
| 147 | def __ifloordiv__(self, val): |
| 148 | output.append("__ifloordiv__ called") |
| 149 | return self |
| 150 | def __rfloordiv__(self, val): |
| 151 | output.append("__rfloordiv__ called") |
| 152 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 153 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 154 | def __truediv__(self, val): |
| 155 | output.append("__truediv__ called") |
| 156 | return self |
| 157 | def __rtruediv__(self, val): |
| 158 | output.append("__rtruediv__ called") |
| 159 | return self |
| 160 | def __itruediv__(self, val): |
| 161 | output.append("__itruediv__ called") |
| 162 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 163 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 164 | def __mod__(self, val): |
| 165 | output.append("__mod__ called") |
| 166 | def __rmod__(self, val): |
| 167 | output.append("__rmod__ called") |
| 168 | def __imod__(self, val): |
| 169 | output.append("__imod__ called") |
| 170 | return self |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 171 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 172 | def __pow__(self, val): |
| 173 | output.append("__pow__ called") |
| 174 | def __rpow__(self, val): |
| 175 | output.append("__rpow__ called") |
| 176 | def __ipow__(self, val): |
| 177 | output.append("__ipow__ called") |
| 178 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 179 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 180 | def __or__(self, val): |
| 181 | output.append("__or__ called") |
| 182 | def __ror__(self, val): |
| 183 | output.append("__ror__ called") |
| 184 | def __ior__(self, val): |
| 185 | output.append("__ior__ called") |
| 186 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 187 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 188 | def __and__(self, val): |
| 189 | output.append("__and__ called") |
| 190 | def __rand__(self, val): |
| 191 | output.append("__rand__ called") |
| 192 | def __iand__(self, val): |
| 193 | output.append("__iand__ called") |
| 194 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 195 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 196 | def __xor__(self, val): |
| 197 | output.append("__xor__ called") |
| 198 | def __rxor__(self, val): |
| 199 | output.append("__rxor__ called") |
| 200 | def __ixor__(self, val): |
| 201 | output.append("__ixor__ called") |
| 202 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 203 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 204 | def __rshift__(self, val): |
| 205 | output.append("__rshift__ called") |
| 206 | def __rrshift__(self, val): |
| 207 | output.append("__rrshift__ called") |
| 208 | def __irshift__(self, val): |
| 209 | output.append("__irshift__ called") |
| 210 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 211 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 212 | def __lshift__(self, val): |
| 213 | output.append("__lshift__ called") |
| 214 | def __rlshift__(self, val): |
| 215 | output.append("__rlshift__ called") |
| 216 | def __ilshift__(self, val): |
| 217 | output.append("__ilshift__ called") |
| 218 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 219 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 220 | x = testall() |
| 221 | x + 1 |
| 222 | 1 + x |
| 223 | x += 1 |
| 224 | |
| 225 | x - 1 |
| 226 | 1 - x |
| 227 | x -= 1 |
| 228 | |
| 229 | x * 1 |
| 230 | 1 * x |
| 231 | x *= 1 |
| 232 | |
| 233 | x / 1 |
| 234 | 1 / x |
| 235 | x /= 1 |
| 236 | |
| 237 | x // 1 |
| 238 | 1 // x |
| 239 | x //= 1 |
| 240 | |
| 241 | x % 1 |
| 242 | 1 % x |
| 243 | x %= 1 |
| 244 | |
| 245 | x ** 1 |
| 246 | 1 ** x |
| 247 | x **= 1 |
| 248 | |
| 249 | x | 1 |
| 250 | 1 | x |
| 251 | x |= 1 |
| 252 | |
| 253 | x & 1 |
| 254 | 1 & x |
| 255 | x &= 1 |
| 256 | |
| 257 | x ^ 1 |
| 258 | 1 ^ x |
| 259 | x ^= 1 |
| 260 | |
| 261 | x >> 1 |
| 262 | 1 >> x |
| 263 | x >>= 1 |
| 264 | |
| 265 | x << 1 |
| 266 | 1 << x |
| 267 | x <<= 1 |
| 268 | |
| 269 | test_self.assertEquals(output, '''\ |
| 270 | __add__ called |
| 271 | __radd__ called |
| 272 | __iadd__ called |
| 273 | __sub__ called |
| 274 | __rsub__ called |
| 275 | __isub__ called |
| 276 | __mul__ called |
| 277 | __rmul__ called |
| 278 | __imul__ called |
| 279 | __truediv__ called |
| 280 | __rtruediv__ called |
| 281 | __itruediv__ called |
| 282 | __floordiv__ called |
| 283 | __rfloordiv__ called |
| 284 | __ifloordiv__ called |
| 285 | __mod__ called |
| 286 | __rmod__ called |
| 287 | __imod__ called |
| 288 | __pow__ called |
| 289 | __rpow__ called |
| 290 | __ipow__ called |
| 291 | __or__ called |
| 292 | __ror__ called |
| 293 | __ior__ called |
| 294 | __and__ called |
| 295 | __rand__ called |
| 296 | __iand__ called |
| 297 | __xor__ called |
| 298 | __rxor__ called |
| 299 | __ixor__ called |
| 300 | __rshift__ called |
| 301 | __rrshift__ called |
| 302 | __irshift__ called |
| 303 | __lshift__ called |
| 304 | __rlshift__ called |
| 305 | __ilshift__ called |
| 306 | '''.splitlines()) |
| 307 | |
| 308 | def test_main(): |
| 309 | run_unittest(AugAssignTest) |
| 310 | |
| 311 | if __name__ == '__main__': |
| 312 | test_main() |