Updated for 2.1b2 distribution.
[python/dscho.git] / Lib / test / test_augassign.py
bloba37b4e1604c5dfe993fc1ce7cb08e723f407b652
1 # Augmented assignment test.
3 x = 2
4 x += 1
5 x *= 2
6 x **= 2
7 x -= 8
8 x /= 2
9 x %= 12
10 x &= 2
11 x |= 5
12 x ^= 1
14 print x
16 x = [2]
17 x[0] += 1
18 x[0] *= 2
19 x[0] **= 2
20 x[0] -= 8
21 x[0] /= 2
22 x[0] %= 12
23 x[0] &= 2
24 x[0] |= 5
25 x[0] ^= 1
27 print x
29 x = {0: 2}
30 x[0] += 1
31 x[0] *= 2
32 x[0] **= 2
33 x[0] -= 8
34 x[0] /= 2
35 x[0] %= 12
36 x[0] &= 2
37 x[0] |= 5
38 x[0] ^= 1
40 print x[0]
42 x = [1,2]
43 x += [3,4]
44 x *= 2
46 print x
48 x = [1, 2, 3]
49 y = x
50 x[1:2] *= 2
51 y[1:2] += [1]
53 print x
54 print x is y
56 class aug_test:
57 def __init__(self, value):
58 self.val = value
59 def __radd__(self, val):
60 return self.val + val
61 def __add__(self, val):
62 return aug_test(self.val + val)
65 class aug_test2(aug_test):
66 def __iadd__(self, val):
67 self.val = self.val + val
68 return self
70 class aug_test3(aug_test):
71 def __iadd__(self, val):
72 return aug_test3(self.val + val)
74 x = aug_test(1)
75 y = x
76 x += 10
78 print isinstance(x, aug_test)
79 print y is not x
80 print x.val
82 x = aug_test2(2)
83 y = x
84 x += 10
86 print y is x
87 print x.val
89 x = aug_test3(3)
90 y = x
91 x += 10
93 print isinstance(x, aug_test3)
94 print y is not x
95 print x.val
97 class testall:
99 def __add__(self, val):
100 print "__add__ called"
101 def __radd__(self, val):
102 print "__radd__ called"
103 def __iadd__(self, val):
104 print "__iadd__ called"
105 return self
107 def __sub__(self, val):
108 print "__sub__ called"
109 def __rsub__(self, val):
110 print "__rsub__ called"
111 def __isub__(self, val):
112 print "__isub__ called"
113 return self
115 def __mul__(self, val):
116 print "__mul__ called"
117 def __rmul__(self, val):
118 print "__rmul__ called"
119 def __imul__(self, val):
120 print "__imul__ called"
121 return self
123 def __div__(self, val):
124 print "__div__ called"
125 def __rdiv__(self, val):
126 print "__rdiv__ called"
127 def __idiv__(self, val):
128 print "__idiv__ called"
129 return self
131 def __mod__(self, val):
132 print "__mod__ called"
133 def __rmod__(self, val):
134 print "__rmod__ called"
135 def __imod__(self, val):
136 print "__imod__ called"
137 return self
139 def __pow__(self, val):
140 print "__pow__ called"
141 def __rpow__(self, val):
142 print "__rpow__ called"
143 def __ipow__(self, val):
144 print "__ipow__ called"
145 return self
147 def __or__(self, val):
148 print "__or__ called"
149 def __ror__(self, val):
150 print "__ror__ called"
151 def __ior__(self, val):
152 print "__ior__ called"
153 return self
155 def __and__(self, val):
156 print "__and__ called"
157 def __rand__(self, val):
158 print "__rand__ called"
159 def __iand__(self, val):
160 print "__iand__ called"
161 return self
163 def __xor__(self, val):
164 print "__xor__ called"
165 def __rxor__(self, val):
166 print "__rxor__ called"
167 def __ixor__(self, val):
168 print "__ixor__ called"
169 return self
171 def __rshift__(self, val):
172 print "__rshift__ called"
173 def __rrshift__(self, val):
174 print "__rrshift__ called"
175 def __irshift__(self, val):
176 print "__irshift__ called"
177 return self
179 def __lshift__(self, val):
180 print "__lshift__ called"
181 def __rlshift__(self, val):
182 print "__rlshift__ called"
183 def __ilshift__(self, val):
184 print "__ilshift__ called"
185 return self
187 x = testall()
188 x + 1
189 1 + x
190 x += 1
192 x - 1
193 1 - x
194 x -= 1
196 x * 1
197 1 * x
198 x *= 1
200 x / 1
201 1 / x
202 x /= 1
204 x % 1
205 1 % x
206 x %= 1
208 x ** 1
209 1 ** x
210 x **= 1
212 x | 1
213 1 | x
214 x |= 1
216 x & 1
217 1 & x
218 x &= 1
220 x ^ 1
221 1 ^ x
222 x ^= 1
224 x >> 1
225 1 >> x
226 x >>= 1
228 x << 1
229 1 << x
230 x <<= 1