Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 1 | # Augmented assignment test. |
| 2 | |
| 3 | x = 2 |
| 4 | x += 1 |
| 5 | x *= 2 |
| 6 | x **= 2 |
| 7 | x -= 8 |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 8 | x //= 2 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 9 | x //= 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 10 | x %= 12 |
| 11 | x &= 2 |
| 12 | x |= 5 |
| 13 | x ^= 1 |
| 14 | |
| 15 | print x |
| 16 | |
| 17 | x = [2] |
| 18 | x[0] += 1 |
| 19 | x[0] *= 2 |
| 20 | x[0] **= 2 |
| 21 | x[0] -= 8 |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 22 | x[0] //= 2 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 23 | x[0] //= 2 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 24 | x[0] %= 12 |
| 25 | x[0] &= 2 |
| 26 | x[0] |= 5 |
| 27 | x[0] ^= 1 |
| 28 | |
| 29 | print x |
| 30 | |
| 31 | x = {0: 2} |
| 32 | x[0] += 1 |
| 33 | x[0] *= 2 |
| 34 | x[0] **= 2 |
| 35 | x[0] -= 8 |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 36 | x[0] //= 2 |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 37 | x[0] //= 1 |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 38 | x[0] %= 12 |
| 39 | x[0] &= 2 |
| 40 | x[0] |= 5 |
| 41 | x[0] ^= 1 |
| 42 | |
| 43 | print x[0] |
| 44 | |
| 45 | x = [1,2] |
| 46 | x += [3,4] |
| 47 | x *= 2 |
| 48 | |
| 49 | print x |
| 50 | |
| 51 | x = [1, 2, 3] |
| 52 | y = x |
| 53 | x[1:2] *= 2 |
| 54 | y[1:2] += [1] |
| 55 | |
| 56 | print x |
| 57 | print x is y |
| 58 | |
| 59 | class aug_test: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 60 | def __init__(self, value): |
| 61 | self.val = value |
| 62 | def __radd__(self, val): |
| 63 | return self.val + val |
| 64 | def __add__(self, val): |
| 65 | return aug_test(self.val + val) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | class aug_test2(aug_test): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 69 | def __iadd__(self, val): |
| 70 | self.val = self.val + val |
| 71 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 72 | |
| 73 | class aug_test3(aug_test): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 74 | def __iadd__(self, val): |
| 75 | return aug_test3(self.val + val) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 76 | |
| 77 | x = aug_test(1) |
| 78 | y = x |
| 79 | x += 10 |
| 80 | |
| 81 | print isinstance(x, aug_test) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 82 | print y is not x |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 83 | print x.val |
| 84 | |
| 85 | x = aug_test2(2) |
| 86 | y = x |
| 87 | x += 10 |
| 88 | |
| 89 | print y is x |
| 90 | print x.val |
| 91 | |
| 92 | x = aug_test3(3) |
| 93 | y = x |
| 94 | x += 10 |
| 95 | |
| 96 | print isinstance(x, aug_test3) |
| 97 | print y is not x |
| 98 | print x.val |
| 99 | |
| 100 | class testall: |
| 101 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 102 | def __add__(self, val): |
| 103 | print "__add__ called" |
| 104 | def __radd__(self, val): |
| 105 | print "__radd__ called" |
| 106 | def __iadd__(self, val): |
| 107 | print "__iadd__ called" |
| 108 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 109 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 110 | def __sub__(self, val): |
| 111 | print "__sub__ called" |
| 112 | def __rsub__(self, val): |
| 113 | print "__rsub__ called" |
| 114 | def __isub__(self, val): |
| 115 | print "__isub__ called" |
| 116 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 117 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 118 | def __mul__(self, val): |
| 119 | print "__mul__ called" |
| 120 | def __rmul__(self, val): |
| 121 | print "__rmul__ called" |
| 122 | def __imul__(self, val): |
| 123 | print "__imul__ called" |
| 124 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 125 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 126 | def __div__(self, val): |
| 127 | print "__div__ called" |
| 128 | def __rdiv__(self, val): |
| 129 | print "__rdiv__ called" |
| 130 | def __idiv__(self, val): |
| 131 | print "__idiv__ called" |
| 132 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 133 | |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 134 | def __floordiv__(self, val): |
| 135 | print "__floordiv__ called" |
| 136 | return self |
| 137 | def __ifloordiv__(self, val): |
| 138 | print "__ifloordiv__ called" |
| 139 | return self |
| 140 | def __rfloordiv__(self, val): |
| 141 | print "__rfloordiv__ called" |
| 142 | return self |
| 143 | |
| 144 | def __truediv__(self, val): |
| 145 | print "__truediv__ called" |
| 146 | return self |
| 147 | def __itruediv__(self, val): |
| 148 | print "__itruediv__ called" |
| 149 | return self |
| 150 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 151 | def __mod__(self, val): |
| 152 | print "__mod__ called" |
| 153 | def __rmod__(self, val): |
| 154 | print "__rmod__ called" |
| 155 | def __imod__(self, val): |
| 156 | print "__imod__ called" |
| 157 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 158 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 159 | def __pow__(self, val): |
| 160 | print "__pow__ called" |
| 161 | def __rpow__(self, val): |
| 162 | print "__rpow__ called" |
| 163 | def __ipow__(self, val): |
| 164 | print "__ipow__ called" |
| 165 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 166 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 167 | def __or__(self, val): |
| 168 | print "__or__ called" |
| 169 | def __ror__(self, val): |
| 170 | print "__ror__ called" |
| 171 | def __ior__(self, val): |
| 172 | print "__ior__ called" |
| 173 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 174 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 175 | def __and__(self, val): |
| 176 | print "__and__ called" |
| 177 | def __rand__(self, val): |
| 178 | print "__rand__ called" |
| 179 | def __iand__(self, val): |
| 180 | print "__iand__ called" |
| 181 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 182 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 183 | def __xor__(self, val): |
| 184 | print "__xor__ called" |
| 185 | def __rxor__(self, val): |
| 186 | print "__rxor__ called" |
| 187 | def __ixor__(self, val): |
| 188 | print "__ixor__ called" |
| 189 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 190 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 191 | def __rshift__(self, val): |
| 192 | print "__rshift__ called" |
| 193 | def __rrshift__(self, val): |
| 194 | print "__rrshift__ called" |
| 195 | def __irshift__(self, val): |
| 196 | print "__irshift__ called" |
| 197 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 198 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 199 | def __lshift__(self, val): |
| 200 | print "__lshift__ called" |
| 201 | def __rlshift__(self, val): |
| 202 | print "__rlshift__ called" |
| 203 | def __ilshift__(self, val): |
| 204 | print "__ilshift__ called" |
| 205 | return self |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 206 | |
| 207 | x = testall() |
| 208 | x + 1 |
| 209 | 1 + x |
| 210 | x += 1 |
| 211 | |
| 212 | x - 1 |
| 213 | 1 - x |
| 214 | x -= 1 |
| 215 | |
| 216 | x * 1 |
| 217 | 1 * x |
| 218 | x *= 1 |
| 219 | |
Tim Peters | e93e477 | 2001-12-11 19:20:15 +0000 | [diff] [blame] | 220 | if 1/2 == 0: |
| 221 | x / 1 |
| 222 | 1 / x |
| 223 | x /= 1 |
| 224 | else: |
| 225 | # True division is in effect, so "/" doesn't map to __div__ etc; |
| 226 | # but the canned expected-output file requires that those get called. |
| 227 | x.__div__(1) |
| 228 | x.__rdiv__(1) |
| 229 | x.__idiv__(1) |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 230 | |
Jeremy Hylton | 4de8df9 | 2001-08-29 17:50:27 +0000 | [diff] [blame] | 231 | x // 1 |
| 232 | 1 // x |
| 233 | x //= 1 |
| 234 | |
Thomas Wouters | 104a7bc | 2000-08-24 20:14:10 +0000 | [diff] [blame] | 235 | x % 1 |
| 236 | 1 % x |
| 237 | x %= 1 |
| 238 | |
| 239 | x ** 1 |
| 240 | 1 ** x |
| 241 | x **= 1 |
| 242 | |
| 243 | x | 1 |
| 244 | 1 | x |
| 245 | x |= 1 |
| 246 | |
| 247 | x & 1 |
| 248 | 1 & x |
| 249 | x &= 1 |
| 250 | |
| 251 | x ^ 1 |
| 252 | 1 ^ x |
| 253 | x ^= 1 |
| 254 | |
| 255 | x >> 1 |
| 256 | 1 >> x |
| 257 | x >>= 1 |
| 258 | |
| 259 | x << 1 |
| 260 | 1 << x |
| 261 | x <<= 1 |