blob: fa608b9a52dba6b97cf966c7e17a34234b39ad81 [file] [log] [blame]
Barry Warsawa09ec191996-12-18 19:56:22 +00001import operator
Fred Drakecd112f52001-08-11 03:21:35 +00002import unittest
Barry Warsawa09ec191996-12-18 19:56:22 +00003
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Barry Warsawa09ec191996-12-18 19:56:22 +00005
Armin Rigofd163f92005-12-29 15:59:19 +00006class Seq1:
7 def __init__(self, lst):
8 self.lst = lst
9 def __len__(self):
10 return len(self.lst)
11 def __getitem__(self, i):
12 return self.lst[i]
13 def __add__(self, other):
14 return self.lst + other.lst
15 def __mul__(self, other):
16 return self.lst * other
17 def __rmul__(self, other):
18 return other * self.lst
19
20class Seq2(object):
21 def __init__(self, lst):
22 self.lst = lst
23 def __len__(self):
24 return len(self.lst)
25 def __getitem__(self, i):
26 return self.lst[i]
27 def __add__(self, other):
28 return self.lst + other.lst
29 def __mul__(self, other):
30 return self.lst * other
31 def __rmul__(self, other):
32 return other * self.lst
33
Barry Warsawa09ec191996-12-18 19:56:22 +000034
Fred Drakecd112f52001-08-11 03:21:35 +000035class OperatorTestCase(unittest.TestCase):
36 def test_lt(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000037 self.assertRaises(TypeError, operator.lt)
38 self.assertRaises(TypeError, operator.lt, 1j, 2j)
39 self.assertFalse(operator.lt(1, 0))
40 self.assertFalse(operator.lt(1, 0.0))
41 self.assertFalse(operator.lt(1, 1))
42 self.assertFalse(operator.lt(1, 1.0))
43 self.assertTrue(operator.lt(1, 2))
44 self.assertTrue(operator.lt(1, 2.0))
Fred Drakecd112f52001-08-11 03:21:35 +000045
46 def test_le(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000047 self.assertRaises(TypeError, operator.le)
48 self.assertRaises(TypeError, operator.le, 1j, 2j)
49 self.assertFalse(operator.le(1, 0))
50 self.assertFalse(operator.le(1, 0.0))
51 self.assertTrue(operator.le(1, 1))
52 self.assertTrue(operator.le(1, 1.0))
53 self.assertTrue(operator.le(1, 2))
54 self.assertTrue(operator.le(1, 2.0))
Fred Drakecd112f52001-08-11 03:21:35 +000055
56 def test_eq(self):
Walter Dörwaldbb9c7392004-11-01 17:10:19 +000057 class C(object):
58 def __eq__(self, other):
59 raise SyntaxError
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000060 self.assertRaises(TypeError, operator.eq)
61 self.assertRaises(SyntaxError, operator.eq, C(), C())
62 self.assertFalse(operator.eq(1, 0))
63 self.assertFalse(operator.eq(1, 0.0))
64 self.assertTrue(operator.eq(1, 1))
65 self.assertTrue(operator.eq(1, 1.0))
66 self.assertFalse(operator.eq(1, 2))
67 self.assertFalse(operator.eq(1, 2.0))
Fred Drakecd112f52001-08-11 03:21:35 +000068
69 def test_ne(self):
Walter Dörwaldbb9c7392004-11-01 17:10:19 +000070 class C(object):
71 def __ne__(self, other):
72 raise SyntaxError
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000073 self.assertRaises(TypeError, operator.ne)
74 self.assertRaises(SyntaxError, operator.ne, C(), C())
75 self.assertTrue(operator.ne(1, 0))
76 self.assertTrue(operator.ne(1, 0.0))
77 self.assertFalse(operator.ne(1, 1))
78 self.assertFalse(operator.ne(1, 1.0))
79 self.assertTrue(operator.ne(1, 2))
80 self.assertTrue(operator.ne(1, 2.0))
Fred Drakecd112f52001-08-11 03:21:35 +000081
82 def test_ge(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000083 self.assertRaises(TypeError, operator.ge)
84 self.assertRaises(TypeError, operator.ge, 1j, 2j)
85 self.assertTrue(operator.ge(1, 0))
86 self.assertTrue(operator.ge(1, 0.0))
87 self.assertTrue(operator.ge(1, 1))
88 self.assertTrue(operator.ge(1, 1.0))
89 self.assertFalse(operator.ge(1, 2))
90 self.assertFalse(operator.ge(1, 2.0))
Fred Drakecd112f52001-08-11 03:21:35 +000091
92 def test_gt(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 self.assertRaises(TypeError, operator.gt)
94 self.assertRaises(TypeError, operator.gt, 1j, 2j)
95 self.assertTrue(operator.gt(1, 0))
96 self.assertTrue(operator.gt(1, 0.0))
97 self.assertFalse(operator.gt(1, 1))
98 self.assertFalse(operator.gt(1, 1.0))
99 self.assertFalse(operator.gt(1, 2))
100 self.assertFalse(operator.gt(1, 2.0))
Fred Drakecd112f52001-08-11 03:21:35 +0000101
102 def test_abs(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000103 self.assertRaises(TypeError, operator.abs)
104 self.assertRaises(TypeError, operator.abs, None)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000105 self.assertEqual(operator.abs(-1), 1)
106 self.assertEqual(operator.abs(1), 1)
Fred Drakecd112f52001-08-11 03:21:35 +0000107
108 def test_add(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000109 self.assertRaises(TypeError, operator.add)
110 self.assertRaises(TypeError, operator.add, None, None)
111 self.assertTrue(operator.add(3, 4) == 7)
Fred Drakecd112f52001-08-11 03:21:35 +0000112
113 def test_bitwise_and(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000114 self.assertRaises(TypeError, operator.and_)
115 self.assertRaises(TypeError, operator.and_, None, None)
116 self.assertTrue(operator.and_(0xf, 0xa) == 0xa)
Fred Drakecd112f52001-08-11 03:21:35 +0000117
118 def test_concat(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000119 self.assertRaises(TypeError, operator.concat)
120 self.assertRaises(TypeError, operator.concat, None, None)
121 self.assertTrue(operator.concat('py', 'thon') == 'python')
122 self.assertTrue(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
123 self.assertTrue(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7])
124 self.assertTrue(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7])
125 self.assertRaises(TypeError, operator.concat, 13, 29)
Fred Drakecd112f52001-08-11 03:21:35 +0000126
127 def test_countOf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000128 self.assertRaises(TypeError, operator.countOf)
129 self.assertRaises(TypeError, operator.countOf, None, None)
130 self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
131 self.assertTrue(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)
Fred Drakecd112f52001-08-11 03:21:35 +0000132
133 def test_delitem(self):
134 a = [4, 3, 2, 1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000135 self.assertRaises(TypeError, operator.delitem, a)
136 self.assertRaises(TypeError, operator.delitem, a, None)
137 self.assertTrue(operator.delitem(a, 1) is None)
138 self.assertTrue(a == [4, 2, 1])
Fred Drakecd112f52001-08-11 03:21:35 +0000139
Fred Drakecd112f52001-08-11 03:21:35 +0000140 def test_floordiv(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000141 self.assertRaises(TypeError, operator.floordiv, 5)
142 self.assertRaises(TypeError, operator.floordiv, None, None)
143 self.assertTrue(operator.floordiv(5, 2) == 2)
Fred Drakecd112f52001-08-11 03:21:35 +0000144
145 def test_truediv(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000146 self.assertRaises(TypeError, operator.truediv, 5)
147 self.assertRaises(TypeError, operator.truediv, None, None)
148 self.assertTrue(operator.truediv(5, 2) == 2.5)
Fred Drakecd112f52001-08-11 03:21:35 +0000149
150 def test_getitem(self):
151 a = range(10)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000152 self.assertRaises(TypeError, operator.getitem)
153 self.assertRaises(TypeError, operator.getitem, a, None)
154 self.assertTrue(operator.getitem(a, 2) == 2)
Fred Drakecd112f52001-08-11 03:21:35 +0000155
Fred Drakecd112f52001-08-11 03:21:35 +0000156 def test_indexOf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000157 self.assertRaises(TypeError, operator.indexOf)
158 self.assertRaises(TypeError, operator.indexOf, None, None)
159 self.assertTrue(operator.indexOf([4, 3, 2, 1], 3) == 1)
Fred Drakecd112f52001-08-11 03:21:35 +0000160 self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
161
162 def test_invert(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000163 self.assertRaises(TypeError, operator.invert)
164 self.assertRaises(TypeError, operator.invert, None)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000165 self.assertEqual(operator.inv(4), -5)
Fred Drakecd112f52001-08-11 03:21:35 +0000166
Fred Drakecd112f52001-08-11 03:21:35 +0000167 def test_lshift(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000168 self.assertRaises(TypeError, operator.lshift)
169 self.assertRaises(TypeError, operator.lshift, None, 42)
170 self.assertTrue(operator.lshift(5, 1) == 10)
171 self.assertTrue(operator.lshift(5, 0) == 5)
Fred Drakecd112f52001-08-11 03:21:35 +0000172 self.assertRaises(ValueError, operator.lshift, 2, -1)
173
174 def test_mod(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000175 self.assertRaises(TypeError, operator.mod)
176 self.assertRaises(TypeError, operator.mod, None, 42)
177 self.assertTrue(operator.mod(5, 2) == 1)
Fred Drakecd112f52001-08-11 03:21:35 +0000178
179 def test_mul(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000180 self.assertRaises(TypeError, operator.mul)
181 self.assertRaises(TypeError, operator.mul, None, None)
182 self.assertTrue(operator.mul(5, 2) == 10)
Fred Drakecd112f52001-08-11 03:21:35 +0000183
184 def test_neg(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000185 self.assertRaises(TypeError, operator.neg)
186 self.assertRaises(TypeError, operator.neg, None)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000187 self.assertEqual(operator.neg(5), -5)
188 self.assertEqual(operator.neg(-5), 5)
189 self.assertEqual(operator.neg(0), 0)
190 self.assertEqual(operator.neg(-0), 0)
Fred Drakecd112f52001-08-11 03:21:35 +0000191
192 def test_bitwise_or(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000193 self.assertRaises(TypeError, operator.or_)
194 self.assertRaises(TypeError, operator.or_, None, None)
195 self.assertTrue(operator.or_(0xa, 0x5) == 0xf)
Fred Drakecd112f52001-08-11 03:21:35 +0000196
197 def test_pos(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000198 self.assertRaises(TypeError, operator.pos)
199 self.assertRaises(TypeError, operator.pos, None)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000200 self.assertEqual(operator.pos(5), 5)
201 self.assertEqual(operator.pos(-5), -5)
202 self.assertEqual(operator.pos(0), 0)
203 self.assertEqual(operator.pos(-0), 0)
Fred Drakecd112f52001-08-11 03:21:35 +0000204
Raymond Hettinger5959c552002-08-19 03:19:09 +0000205 def test_pow(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000206 self.assertRaises(TypeError, operator.pow)
207 self.assertRaises(TypeError, operator.pow, None, None)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000208 self.assertEqual(operator.pow(3,5), 3**5)
209 self.assertEqual(operator.__pow__(3,5), 3**5)
Raymond Hettinger5959c552002-08-19 03:19:09 +0000210 self.assertRaises(TypeError, operator.pow, 1)
211 self.assertRaises(TypeError, operator.pow, 1, 2, 3)
212
Fred Drakecd112f52001-08-11 03:21:35 +0000213 def test_rshift(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000214 self.assertRaises(TypeError, operator.rshift)
215 self.assertRaises(TypeError, operator.rshift, None, 42)
216 self.assertTrue(operator.rshift(5, 1) == 2)
217 self.assertTrue(operator.rshift(5, 0) == 5)
Fred Drakecd112f52001-08-11 03:21:35 +0000218 self.assertRaises(ValueError, operator.rshift, 2, -1)
219
220 def test_contains(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000221 self.assertRaises(TypeError, operator.contains)
222 self.assertRaises(TypeError, operator.contains, None, None)
223 self.assertTrue(operator.contains(range(4), 2))
224 self.assertFalse(operator.contains(range(4), 5))
Fred Drakecd112f52001-08-11 03:21:35 +0000225
226 def test_setitem(self):
Guido van Rossum805365e2007-05-07 22:24:25 +0000227 a = list(range(3))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000228 self.assertRaises(TypeError, operator.setitem, a)
229 self.assertRaises(TypeError, operator.setitem, a, None, None)
230 self.assertTrue(operator.setitem(a, 0, 2) is None)
231 self.assertTrue(a == [2, 1, 2])
Fred Drakecd112f52001-08-11 03:21:35 +0000232 self.assertRaises(IndexError, operator.setitem, a, 4, 2)
233
Fred Drakecd112f52001-08-11 03:21:35 +0000234 def test_sub(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000235 self.assertRaises(TypeError, operator.sub)
236 self.assertRaises(TypeError, operator.sub, None, None)
237 self.assertTrue(operator.sub(5, 2) == 3)
Fred Drakecd112f52001-08-11 03:21:35 +0000238
239 def test_truth(self):
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000240 class C(object):
Jack Diederich4dafcc42006-11-28 19:15:13 +0000241 def __bool__(self):
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000242 raise SyntaxError
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000243 self.assertRaises(TypeError, operator.truth)
244 self.assertRaises(SyntaxError, operator.truth, C())
245 self.assertTrue(operator.truth(5))
246 self.assertTrue(operator.truth([0]))
247 self.assertFalse(operator.truth(0))
248 self.assertFalse(operator.truth([]))
Fred Drakecd112f52001-08-11 03:21:35 +0000249
250 def test_bitwise_xor(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000251 self.assertRaises(TypeError, operator.xor)
252 self.assertRaises(TypeError, operator.xor, None, None)
253 self.assertTrue(operator.xor(0xb, 0xc) == 0x7)
Fred Drakecd112f52001-08-11 03:21:35 +0000254
Raymond Hettinger9543b342003-01-18 23:22:20 +0000255 def test_is(self):
256 a = b = 'xyzpdq'
257 c = a[:3] + b[3:]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000258 self.assertRaises(TypeError, operator.is_)
259 self.assertTrue(operator.is_(a, b))
260 self.assertFalse(operator.is_(a,c))
Raymond Hettinger9543b342003-01-18 23:22:20 +0000261
262 def test_is_not(self):
263 a = b = 'xyzpdq'
264 c = a[:3] + b[3:]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000265 self.assertRaises(TypeError, operator.is_not)
266 self.assertFalse(operator.is_not(a, b))
267 self.assertTrue(operator.is_not(a,c))
Fred Drakecd112f52001-08-11 03:21:35 +0000268
Raymond Hettinger166958b2003-12-01 13:18:39 +0000269 def test_attrgetter(self):
270 class A:
271 pass
272 a = A()
273 a.name = 'arthur'
274 f = operator.attrgetter('name')
275 self.assertEqual(f(a), 'arthur')
276 f = operator.attrgetter('rank')
277 self.assertRaises(AttributeError, f, a)
Antoine Pitroue9745712010-10-31 15:26:04 +0000278 self.assertRaises(TypeError, operator.attrgetter, 2)
Raymond Hettinger166958b2003-12-01 13:18:39 +0000279 self.assertRaises(TypeError, operator.attrgetter)
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000280
281 # multiple gets
282 record = A()
283 record.x = 'X'
284 record.y = 'Y'
285 record.z = 'Z'
286 self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))
Antoine Pitroue9745712010-10-31 15:26:04 +0000287 self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y'))
Raymond Hettinger166958b2003-12-01 13:18:39 +0000288
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000289 class C(object):
Christian Heimes9bd667a2008-01-20 15:14:11 +0000290 def __getattr__(self, name):
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000291 raise SyntaxError
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000292 self.assertRaises(SyntaxError, operator.attrgetter('foo'), C())
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000293
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000294 # recursive gets
295 a = A()
296 a.name = 'arthur'
297 a.child = A()
298 a.child.name = 'thomas'
299 f = operator.attrgetter('child.name')
300 self.assertEqual(f(a), 'thomas')
301 self.assertRaises(AttributeError, f, a.child)
302 f = operator.attrgetter('name', 'child.name')
303 self.assertEqual(f(a), ('arthur', 'thomas'))
304 f = operator.attrgetter('name', 'child.name', 'child.child.name')
305 self.assertRaises(AttributeError, f, a)
Antoine Pitroue9745712010-10-31 15:26:04 +0000306 f = operator.attrgetter('child.')
307 self.assertRaises(AttributeError, f, a)
308 f = operator.attrgetter('.child')
309 self.assertRaises(AttributeError, f, a)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000310
311 a.child.child = A()
312 a.child.child.name = 'johnson'
313 f = operator.attrgetter('child.child.name')
314 self.assertEqual(f(a), 'johnson')
315 f = operator.attrgetter('name', 'child.name', 'child.child.name')
316 self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))
317
Raymond Hettinger166958b2003-12-01 13:18:39 +0000318 def test_itemgetter(self):
319 a = 'ABCDE'
320 f = operator.itemgetter(2)
321 self.assertEqual(f(a), 'C')
322 f = operator.itemgetter(10)
323 self.assertRaises(IndexError, f, a)
324
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000325 class C(object):
Christian Heimes9bd667a2008-01-20 15:14:11 +0000326 def __getitem__(self, name):
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000327 raise SyntaxError
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000328 self.assertRaises(SyntaxError, operator.itemgetter(42), C())
Walter Dörwaldbb9c7392004-11-01 17:10:19 +0000329
Raymond Hettinger166958b2003-12-01 13:18:39 +0000330 f = operator.itemgetter('name')
331 self.assertRaises(TypeError, f, a)
332 self.assertRaises(TypeError, operator.itemgetter)
Raymond Hettinger166958b2003-12-01 13:18:39 +0000333
334 d = dict(key='val')
335 f = operator.itemgetter('key')
336 self.assertEqual(f(d), 'val')
337 f = operator.itemgetter('nonkey')
338 self.assertRaises(KeyError, f, d)
339
340 # example used in the docs
341 inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
342 getcount = operator.itemgetter(1)
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000343 self.assertEqual(list(map(getcount, inventory)), [3, 2, 5, 1])
Raymond Hettinger64958a12003-12-17 20:43:33 +0000344 self.assertEqual(sorted(inventory, key=getcount),
Raymond Hettinger166958b2003-12-01 13:18:39 +0000345 [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])
346
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000347 # multiple gets
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000348 data = list(map(str, range(20)))
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000349 self.assertEqual(operator.itemgetter(2,10,5)(data), ('2', '10', '5'))
350 self.assertRaises(TypeError, operator.itemgetter(2, 'x', 5), data)
Fred Drake2e2be372001-09-20 21:33:42 +0000351
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000352 def test_methodcaller(self):
353 self.assertRaises(TypeError, operator.methodcaller)
354 class A:
355 def foo(self, *args, **kwds):
356 return args[0] + args[1]
357 def bar(self, f=42):
358 return f
359 a = A()
360 f = operator.methodcaller('foo')
361 self.assertRaises(IndexError, f, a)
362 f = operator.methodcaller('foo', 1, 2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000363 self.assertEqual(f(a), 3)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000364 f = operator.methodcaller('bar')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000365 self.assertEqual(f(a), 42)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000366 self.assertRaises(TypeError, f, a, a)
367 f = operator.methodcaller('bar', f=5)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000368 self.assertEqual(f(a), 5)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000369
Armin Rigof5bd3b42005-12-29 16:50:42 +0000370 def test_inplace(self):
371 class C(object):
372 def __iadd__ (self, other): return "iadd"
373 def __iand__ (self, other): return "iand"
Armin Rigof5bd3b42005-12-29 16:50:42 +0000374 def __ifloordiv__(self, other): return "ifloordiv"
375 def __ilshift__ (self, other): return "ilshift"
376 def __imod__ (self, other): return "imod"
377 def __imul__ (self, other): return "imul"
378 def __ior__ (self, other): return "ior"
379 def __ipow__ (self, other): return "ipow"
380 def __irshift__ (self, other): return "irshift"
381 def __isub__ (self, other): return "isub"
382 def __itruediv__ (self, other): return "itruediv"
383 def __ixor__ (self, other): return "ixor"
384 def __getitem__(self, other): return 5 # so that C is a sequence
385 c = C()
386 self.assertEqual(operator.iadd (c, 5), "iadd")
387 self.assertEqual(operator.iand (c, 5), "iand")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000388 self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
389 self.assertEqual(operator.ilshift (c, 5), "ilshift")
390 self.assertEqual(operator.imod (c, 5), "imod")
391 self.assertEqual(operator.imul (c, 5), "imul")
392 self.assertEqual(operator.ior (c, 5), "ior")
393 self.assertEqual(operator.ipow (c, 5), "ipow")
394 self.assertEqual(operator.irshift (c, 5), "irshift")
395 self.assertEqual(operator.isub (c, 5), "isub")
396 self.assertEqual(operator.itruediv (c, 5), "itruediv")
397 self.assertEqual(operator.ixor (c, 5), "ixor")
398 self.assertEqual(operator.iconcat (c, c), "iadd")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000399 self.assertEqual(operator.__iadd__ (c, 5), "iadd")
400 self.assertEqual(operator.__iand__ (c, 5), "iand")
Armin Rigof5bd3b42005-12-29 16:50:42 +0000401 self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
402 self.assertEqual(operator.__ilshift__ (c, 5), "ilshift")
403 self.assertEqual(operator.__imod__ (c, 5), "imod")
404 self.assertEqual(operator.__imul__ (c, 5), "imul")
405 self.assertEqual(operator.__ior__ (c, 5), "ior")
406 self.assertEqual(operator.__ipow__ (c, 5), "ipow")
407 self.assertEqual(operator.__irshift__ (c, 5), "irshift")
408 self.assertEqual(operator.__isub__ (c, 5), "isub")
409 self.assertEqual(operator.__itruediv__ (c, 5), "itruediv")
410 self.assertEqual(operator.__ixor__ (c, 5), "ixor")
411 self.assertEqual(operator.__iconcat__ (c, c), "iadd")
Fred Drake2e2be372001-09-20 21:33:42 +0000412
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000413def test_main(verbose=None):
414 import sys
415 test_classes = (
416 OperatorTestCase,
417 )
418
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000419 support.run_unittest(*test_classes)
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000420
421 # verify reference counting
422 if verbose and hasattr(sys, "gettotalrefcount"):
423 import gc
424 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000425 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000426 support.run_unittest(*test_classes)
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000427 gc.collect()
428 counts[i] = sys.gettotalrefcount()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000429 print(counts)
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000430
Fred Drake2e2be372001-09-20 21:33:42 +0000431if __name__ == "__main__":
Raymond Hettinger984f9bb2005-03-09 16:38:48 +0000432 test_main(verbose=True)