blob: f655c29eee2f6e8e2c148536118896178dd59ff3 [file] [log] [blame]
Walter Dörwald1dde95d2003-12-08 11:38:45 +00001"""
2Tests common to tuple, list and UserList.UserList
3"""
4
5import unittest
Raymond Hettinger65baa342008-02-07 00:41:02 +00006import sys
Walter Dörwald1dde95d2003-12-08 11:38:45 +00007
Walter Dörwald09a3f2c2005-03-22 22:43:28 +00008# Various iterables
9# This is used for checking the constructor (here and in test_deque.py)
10def iterfunc(seqn):
11 'Regular generator'
12 for i in seqn:
13 yield i
14
15class Sequence:
16 'Sequence using __getitem__'
17 def __init__(self, seqn):
18 self.seqn = seqn
19 def __getitem__(self, i):
20 return self.seqn[i]
21
22class IterFunc:
23 'Sequence using iterator protocol'
24 def __init__(self, seqn):
25 self.seqn = seqn
26 self.i = 0
27 def __iter__(self):
28 return self
Georg Brandla18af4e2007-04-21 15:47:16 +000029 def __next__(self):
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000030 if self.i >= len(self.seqn): raise StopIteration
31 v = self.seqn[self.i]
32 self.i += 1
33 return v
34
35class IterGen:
36 'Sequence using iterator protocol defined with a generator'
37 def __init__(self, seqn):
38 self.seqn = seqn
39 self.i = 0
40 def __iter__(self):
41 for val in self.seqn:
42 yield val
43
44class IterNextOnly:
45 'Missing __getitem__ and __iter__'
46 def __init__(self, seqn):
47 self.seqn = seqn
48 self.i = 0
Georg Brandla18af4e2007-04-21 15:47:16 +000049 def __next__(self):
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000050 if self.i >= len(self.seqn): raise StopIteration
51 v = self.seqn[self.i]
52 self.i += 1
53 return v
54
55class IterNoNext:
Georg Brandla18af4e2007-04-21 15:47:16 +000056 'Iterator missing __next__()'
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000057 def __init__(self, seqn):
58 self.seqn = seqn
59 self.i = 0
60 def __iter__(self):
61 return self
62
63class IterGenExc:
64 'Test propagation of exceptions'
65 def __init__(self, seqn):
66 self.seqn = seqn
67 self.i = 0
68 def __iter__(self):
69 return self
Georg Brandla18af4e2007-04-21 15:47:16 +000070 def __next__(self):
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000071 3 // 0
72
73class IterFuncStop:
74 'Test immediate stop'
75 def __init__(self, seqn):
76 pass
77 def __iter__(self):
78 return self
Georg Brandla18af4e2007-04-21 15:47:16 +000079 def __next__(self):
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000080 raise StopIteration
81
Raymond Hettinger736c0ab2008-03-13 02:09:15 +000082from itertools import chain
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000083def itermulti(seqn):
84 'Test multiple tiers of iterators'
Raymond Hettingera6c60372008-03-13 01:26:19 +000085 return chain(map(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
Walter Dörwald09a3f2c2005-03-22 22:43:28 +000086
Walter Dörwald1dde95d2003-12-08 11:38:45 +000087class CommonTest(unittest.TestCase):
88 # The type to be tested
89 type2test = None
90
91 def test_constructors(self):
92 l0 = []
93 l1 = [0]
94 l2 = [0, 1]
95
96 u = self.type2test()
97 u0 = self.type2test(l0)
98 u1 = self.type2test(l1)
99 u2 = self.type2test(l2)
100
101 uu = self.type2test(u)
102 uu0 = self.type2test(u0)
103 uu1 = self.type2test(u1)
104 uu2 = self.type2test(u2)
105
106 v = self.type2test(tuple(u))
107 class OtherSeq:
108 def __init__(self, initseq):
109 self.__data = initseq
110 def __len__(self):
111 return len(self.__data)
112 def __getitem__(self, i):
113 return self.__data[i]
114 s = OtherSeq(u0)
115 v0 = self.type2test(s)
116 self.assertEqual(len(v0), len(s))
117
118 s = "this is also a sequence"
119 vv = self.type2test(s)
120 self.assertEqual(len(vv), len(s))
121
Walter Dörwald09a3f2c2005-03-22 22:43:28 +0000122 # Create from various iteratables
Guido van Rossum805365e2007-05-07 22:24:25 +0000123 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
Walter Dörwald09a3f2c2005-03-22 22:43:28 +0000124 for g in (Sequence, IterFunc, IterGen,
125 itermulti, iterfunc):
126 self.assertEqual(self.type2test(g(s)), self.type2test(s))
127 self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
128 self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
129 self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
130 self.assertRaises(TypeError, self.type2test, IterNoNext(s))
131 self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))
132
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000133 def test_truth(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000134 self.assertFalse(self.type2test())
135 self.assertTrue(self.type2test([42]))
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000136
137 def test_getitem(self):
138 u = self.type2test([0, 1, 2, 3, 4])
Guido van Rossum805365e2007-05-07 22:24:25 +0000139 for i in range(len(u)):
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000140 self.assertEqual(u[i], i)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000141 self.assertEqual(u[int(i)], i)
Guido van Rossum805365e2007-05-07 22:24:25 +0000142 for i in range(-len(u), -1):
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000143 self.assertEqual(u[i], len(u)+i)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000144 self.assertEqual(u[int(i)], len(u)+i)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000145 self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
146 self.assertRaises(IndexError, u.__getitem__, len(u))
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000147 self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000148
149 u = self.type2test()
150 self.assertRaises(IndexError, u.__getitem__, 0)
151 self.assertRaises(IndexError, u.__getitem__, -1)
152
153 self.assertRaises(TypeError, u.__getitem__)
154
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000155 a = self.type2test([10, 11])
156 self.assertEqual(a[0], 10)
157 self.assertEqual(a[1], 11)
158 self.assertEqual(a[-2], 10)
159 self.assertEqual(a[-1], 11)
160 self.assertRaises(IndexError, a.__getitem__, -3)
161 self.assertRaises(IndexError, a.__getitem__, 3)
162
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000163 def test_getslice(self):
164 l = [0, 1, 2, 3, 4]
165 u = self.type2test(l)
166
167 self.assertEqual(u[0:0], self.type2test())
168 self.assertEqual(u[1:2], self.type2test([1]))
169 self.assertEqual(u[-2:-1], self.type2test([3]))
170 self.assertEqual(u[-1000:1000], u)
171 self.assertEqual(u[1000:-1000], self.type2test([]))
172 self.assertEqual(u[:], u)
173 self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
174 self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
175
176 # Extended slices
177 self.assertEqual(u[::], u)
178 self.assertEqual(u[::2], self.type2test([0, 2, 4]))
179 self.assertEqual(u[1::2], self.type2test([1, 3]))
180 self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
181 self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
182 self.assertEqual(u[3::-2], self.type2test([3, 1]))
183 self.assertEqual(u[3:3:-2], self.type2test([]))
184 self.assertEqual(u[3:2:-2], self.type2test([3]))
185 self.assertEqual(u[3:1:-2], self.type2test([3]))
186 self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
187 self.assertEqual(u[::-100], self.type2test([4]))
188 self.assertEqual(u[100:-100:], self.type2test([]))
189 self.assertEqual(u[-100:100:], u)
190 self.assertEqual(u[100:-100:-1], u[::-1])
191 self.assertEqual(u[-100:100:-1], self.type2test([]))
Guido van Rossume2a383d2007-01-15 16:59:06 +0000192 self.assertEqual(u[-100:100:2], self.type2test([0, 2, 4]))
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000193
194 # Test extreme cases with long ints
195 a = self.type2test([0,1,2,3,4])
Guido van Rossume2a383d2007-01-15 16:59:06 +0000196 self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2]))
197 self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4]))
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000198
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000199 def test_contains(self):
200 u = self.type2test([0, 1, 2])
201 for i in u:
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000202 self.assertIn(i, u)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000203 for i in min(u)-1, max(u)+1:
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000204 self.assertNotIn(i, u)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000205
206 self.assertRaises(TypeError, u.__contains__)
207
Georg Brandlbe3856d2005-08-24 09:08:57 +0000208 def test_contains_fake(self):
209 class AllEq:
210 # Sequences must use rich comparison against each item
211 # (unless "is" is true, or an earlier item answered)
212 # So instances of AllEq must be found in all non-empty sequences.
213 def __eq__(self, other):
214 return True
Nick Coghland1abd252008-07-15 15:46:38 +0000215 __hash__ = None # Can't meet hash invariant requirements
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000216 self.assertNotIn(AllEq(), self.type2test([]))
217 self.assertIn(AllEq(), self.type2test([1]))
Georg Brandlbe3856d2005-08-24 09:08:57 +0000218
219 def test_contains_order(self):
220 # Sequences must test in-order. If a rich comparison has side
221 # effects, these will be visible to tests against later members.
222 # In this test, the "side effect" is a short-circuiting raise.
223 class DoNotTestEq(Exception):
224 pass
225 class StopCompares:
226 def __eq__(self, other):
227 raise DoNotTestEq
Tim Peters9e34c042005-08-26 15:20:46 +0000228
Georg Brandlbe3856d2005-08-24 09:08:57 +0000229 checkfirst = self.type2test([1, StopCompares()])
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000230 self.assertIn(1, checkfirst)
Georg Brandlbe3856d2005-08-24 09:08:57 +0000231 checklast = self.type2test([StopCompares(), 1])
232 self.assertRaises(DoNotTestEq, checklast.__contains__, 1)
233
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000234 def test_len(self):
235 self.assertEqual(len(self.type2test()), 0)
236 self.assertEqual(len(self.type2test([])), 0)
237 self.assertEqual(len(self.type2test([0])), 1)
238 self.assertEqual(len(self.type2test([0, 1, 2])), 3)
239
240 def test_minmax(self):
241 u = self.type2test([0, 1, 2])
242 self.assertEqual(min(u), 0)
243 self.assertEqual(max(u), 2)
244
245 def test_addmul(self):
246 u1 = self.type2test([0])
247 u2 = self.type2test([0, 1])
248 self.assertEqual(u1, u1 + self.type2test())
249 self.assertEqual(u1, self.type2test() + u1)
250 self.assertEqual(u1 + self.type2test([1]), u2)
251 self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
252 self.assertEqual(self.type2test(), u2*0)
253 self.assertEqual(self.type2test(), 0*u2)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000254 self.assertEqual(self.type2test(), u2*0)
255 self.assertEqual(self.type2test(), 0*u2)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000256 self.assertEqual(u2, u2*1)
257 self.assertEqual(u2, 1*u2)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000258 self.assertEqual(u2, u2*1)
259 self.assertEqual(u2, 1*u2)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000260 self.assertEqual(u2+u2, u2*2)
261 self.assertEqual(u2+u2, 2*u2)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000262 self.assertEqual(u2+u2, u2*2)
263 self.assertEqual(u2+u2, 2*u2)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000264 self.assertEqual(u2+u2+u2, u2*3)
265 self.assertEqual(u2+u2+u2, 3*u2)
266
267 class subclass(self.type2test):
268 pass
269 u3 = subclass([0, 1])
270 self.assertEqual(u3, u3*1)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000271 self.assertIsNot(u3, u3*1)
Walter Dörwald1dde95d2003-12-08 11:38:45 +0000272
273 def test_iadd(self):
274 u = self.type2test([0, 1])
275 u += self.type2test()
276 self.assertEqual(u, self.type2test([0, 1]))
277 u += self.type2test([2, 3])
278 self.assertEqual(u, self.type2test([0, 1, 2, 3]))
279 u += self.type2test([4, 5])
280 self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
281
282 u = self.type2test("spam")
283 u += self.type2test("eggs")
284 self.assertEqual(u, self.type2test("spameggs"))
285
286 def test_imul(self):
287 u = self.type2test([0, 1])
288 u *= 3
289 self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
290
291 def test_getitemoverwriteiter(self):
292 # Verify that __getitem__ overrides are not recognized by __iter__
293 class T(self.type2test):
294 def __getitem__(self, key):
295 return str(key) + '!!!'
Georg Brandla18af4e2007-04-21 15:47:16 +0000296 self.assertEqual(next(iter(T((1,2)))), 1)
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000297
298 def test_repeat(self):
Guido van Rossum805365e2007-05-07 22:24:25 +0000299 for m in range(4):
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000300 s = tuple(range(m))
Guido van Rossum805365e2007-05-07 22:24:25 +0000301 for n in range(-3, 5):
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000302 self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
303 self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
304 self.assertEqual(id(s), id(s*1))
305
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000306 def test_bigrepeat(self):
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000307 import sys
308 if sys.maxsize <= 2147483647:
309 x = self.type2test([0])
310 x *= 2**16
311 self.assertRaises(MemoryError, x.__mul__, 2**16)
312 if hasattr(x, '__imul__'):
313 self.assertRaises(MemoryError, x.__imul__, 2**16)
Guido van Rossum3d392eb2007-11-16 00:35:22 +0000314
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000315 def test_subscript(self):
316 a = self.type2test([10, 11])
Guido van Rossume2a383d2007-01-15 16:59:06 +0000317 self.assertEqual(a.__getitem__(0), 10)
318 self.assertEqual(a.__getitem__(1), 11)
319 self.assertEqual(a.__getitem__(-2), 10)
320 self.assertEqual(a.__getitem__(-1), 11)
Raymond Hettinger5ea7e312004-09-30 07:47:20 +0000321 self.assertRaises(IndexError, a.__getitem__, -3)
322 self.assertRaises(IndexError, a.__getitem__, 3)
323 self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
324 self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
325 self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
326 self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
327 self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
328 self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
329 self.assertRaises(TypeError, a.__getitem__, 'x')
Raymond Hettinger65baa342008-02-07 00:41:02 +0000330
331 def test_count(self):
332 a = self.type2test([0, 1, 2])*3
333 self.assertEqual(a.count(0), 3)
334 self.assertEqual(a.count(1), 3)
335 self.assertEqual(a.count(3), 0)
336
337 self.assertRaises(TypeError, a.count)
338
339 class BadExc(Exception):
340 pass
341
342 class BadCmp:
343 def __eq__(self, other):
344 if other == 2:
345 raise BadExc()
346 return False
347
348 self.assertRaises(BadExc, a.count, BadCmp())
349
350 def test_index(self):
351 u = self.type2test([0, 1])
352 self.assertEqual(u.index(0), 0)
353 self.assertEqual(u.index(1), 1)
354 self.assertRaises(ValueError, u.index, 2)
355
356 u = self.type2test([-2, -1, 0, 0, 1, 2])
357 self.assertEqual(u.count(0), 2)
358 self.assertEqual(u.index(0), 2)
359 self.assertEqual(u.index(0, 2), 2)
360 self.assertEqual(u.index(-2, -10), 0)
361 self.assertEqual(u.index(0, 3), 3)
362 self.assertEqual(u.index(0, 3, 4), 3)
363 self.assertRaises(ValueError, u.index, 2, 0, -10)
364
365 self.assertRaises(TypeError, u.index)
366
367 class BadExc(Exception):
368 pass
369
370 class BadCmp:
371 def __eq__(self, other):
372 if other == 2:
373 raise BadExc()
374 return False
375
376 a = self.type2test([0, 1, 2, 3])
377 self.assertRaises(BadExc, a.index, BadCmp())
378
379 a = self.type2test([-2, -1, 0, 0, 1, 2])
380 self.assertEqual(a.index(0), 2)
381 self.assertEqual(a.index(0, 2), 2)
382 self.assertEqual(a.index(0, -4), 2)
383 self.assertEqual(a.index(-2, -10), 0)
384 self.assertEqual(a.index(0, 3), 3)
385 self.assertEqual(a.index(0, -3), 3)
386 self.assertEqual(a.index(0, 3, 4), 3)
387 self.assertEqual(a.index(0, -3, -2), 3)
388 self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2)
389 self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize)
390 self.assertRaises(ValueError, a.index, 2, 0, -10)