blob: 8a8f00d88d6c0883c73c33ab0175bcb1b97f5ec9 [file] [log] [blame]
Thomas Wouters104a7bc2000-08-24 20:14:10 +00001# Augmented assignment test.
2
3x = 2
4x += 1
5x *= 2
6x **= 2
7x -= 8
Guido van Rossum54e54c62001-09-04 19:14:14 +00008x //= 2
Jeremy Hylton4de8df92001-08-29 17:50:27 +00009x //= 1
Thomas Wouters104a7bc2000-08-24 20:14:10 +000010x %= 12
11x &= 2
12x |= 5
13x ^= 1
14
15print x
16
17x = [2]
18x[0] += 1
19x[0] *= 2
20x[0] **= 2
21x[0] -= 8
Guido van Rossum54e54c62001-09-04 19:14:14 +000022x[0] //= 2
Jeremy Hylton4de8df92001-08-29 17:50:27 +000023x[0] //= 2
Thomas Wouters104a7bc2000-08-24 20:14:10 +000024x[0] %= 12
25x[0] &= 2
26x[0] |= 5
27x[0] ^= 1
28
29print x
30
31x = {0: 2}
32x[0] += 1
33x[0] *= 2
34x[0] **= 2
35x[0] -= 8
Guido van Rossum54e54c62001-09-04 19:14:14 +000036x[0] //= 2
Jeremy Hylton4de8df92001-08-29 17:50:27 +000037x[0] //= 1
Thomas Wouters104a7bc2000-08-24 20:14:10 +000038x[0] %= 12
39x[0] &= 2
40x[0] |= 5
41x[0] ^= 1
42
43print x[0]
44
45x = [1,2]
46x += [3,4]
47x *= 2
48
49print x
50
51x = [1, 2, 3]
52y = x
53x[1:2] *= 2
54y[1:2] += [1]
55
56print x
57print x is y
58
59class aug_test:
Fred Drake004d5e62000-10-23 17:22:08 +000060 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 Wouters104a7bc2000-08-24 20:14:10 +000066
67
68class aug_test2(aug_test):
Fred Drake004d5e62000-10-23 17:22:08 +000069 def __iadd__(self, val):
70 self.val = self.val + val
71 return self
Thomas Wouters104a7bc2000-08-24 20:14:10 +000072
73class aug_test3(aug_test):
Fred Drake004d5e62000-10-23 17:22:08 +000074 def __iadd__(self, val):
75 return aug_test3(self.val + val)
Thomas Wouters104a7bc2000-08-24 20:14:10 +000076
77x = aug_test(1)
78y = x
79x += 10
80
81print isinstance(x, aug_test)
Fred Drake004d5e62000-10-23 17:22:08 +000082print y is not x
Thomas Wouters104a7bc2000-08-24 20:14:10 +000083print x.val
84
85x = aug_test2(2)
86y = x
87x += 10
88
89print y is x
90print x.val
91
92x = aug_test3(3)
93y = x
94x += 10
95
96print isinstance(x, aug_test3)
97print y is not x
98print x.val
99
100class testall:
101
Fred Drake004d5e62000-10-23 17:22:08 +0000102 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 Wouters104a7bc2000-08-24 20:14:10 +0000109
Fred Drake004d5e62000-10-23 17:22:08 +0000110 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 Wouters104a7bc2000-08-24 20:14:10 +0000117
Fred Drake004d5e62000-10-23 17:22:08 +0000118 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 Wouters104a7bc2000-08-24 20:14:10 +0000125
Fred Drake004d5e62000-10-23 17:22:08 +0000126 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 Wouters104a7bc2000-08-24 20:14:10 +0000133
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000134 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 Drake004d5e62000-10-23 17:22:08 +0000151 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 Wouters104a7bc2000-08-24 20:14:10 +0000158
Fred Drake004d5e62000-10-23 17:22:08 +0000159 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 Wouters104a7bc2000-08-24 20:14:10 +0000166
Fred Drake004d5e62000-10-23 17:22:08 +0000167 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 Wouters104a7bc2000-08-24 20:14:10 +0000174
Fred Drake004d5e62000-10-23 17:22:08 +0000175 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 Wouters104a7bc2000-08-24 20:14:10 +0000182
Fred Drake004d5e62000-10-23 17:22:08 +0000183 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 Wouters104a7bc2000-08-24 20:14:10 +0000190
Fred Drake004d5e62000-10-23 17:22:08 +0000191 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 Wouters104a7bc2000-08-24 20:14:10 +0000198
Fred Drake004d5e62000-10-23 17:22:08 +0000199 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 Wouters104a7bc2000-08-24 20:14:10 +0000206
207x = testall()
208x + 1
2091 + x
210x += 1
211
212x - 1
2131 - x
214x -= 1
215
216x * 1
2171 * x
218x *= 1
219
Tim Peterse93e4772001-12-11 19:20:15 +0000220if 1/2 == 0:
221 x / 1
222 1 / x
223 x /= 1
224else:
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 Wouters104a7bc2000-08-24 20:14:10 +0000230
Jeremy Hylton4de8df92001-08-29 17:50:27 +0000231x // 1
2321 // x
233x //= 1
234
Thomas Wouters104a7bc2000-08-24 20:14:10 +0000235x % 1
2361 % x
237x %= 1
238
239x ** 1
2401 ** x
241x **= 1
242
243x | 1
2441 | x
245x |= 1
246
247x & 1
2481 & x
249x &= 1
250
251x ^ 1
2521 ^ x
253x ^= 1
254
255x >> 1
2561 >> x
257x >>= 1
258
259x << 1
2601 << x
261x <<= 1