1""" 2Tests common to tuple, list and UserList.UserList 3""" 4 5import unittest 6import sys 7import pickle 8from test import support 9 10# Various iterables 11# This is used for checking the constructor (here and in test_deque.py) 12def iterfunc(seqn): 13 'Regular generator' 14 for i in seqn: 15 yield i 16 17class Sequence: 18 'Sequence using __getitem__' 19 def __init__(self, seqn): 20 self.seqn = seqn 21 def __getitem__(self, i): 22 return self.seqn[i] 23 24class IterFunc: 25 'Sequence using iterator protocol' 26 def __init__(self, seqn): 27 self.seqn = seqn 28 self.i = 0 29 def __iter__(self): 30 return self 31 def __next__(self): 32 if self.i >= len(self.seqn): raise StopIteration 33 v = self.seqn[self.i] 34 self.i += 1 35 return v 36 37class IterGen: 38 'Sequence using iterator protocol defined with a generator' 39 def __init__(self, seqn): 40 self.seqn = seqn 41 self.i = 0 42 def __iter__(self): 43 for val in self.seqn: 44 yield val 45 46class IterNextOnly: 47 'Missing __getitem__ and __iter__' 48 def __init__(self, seqn): 49 self.seqn = seqn 50 self.i = 0 51 def __next__(self): 52 if self.i >= len(self.seqn): raise StopIteration 53 v = self.seqn[self.i] 54 self.i += 1 55 return v 56 57class IterNoNext: 58 'Iterator missing __next__()' 59 def __init__(self, seqn): 60 self.seqn = seqn 61 self.i = 0 62 def __iter__(self): 63 return self 64 65class IterGenExc: 66 'Test propagation of exceptions' 67 def __init__(self, seqn): 68 self.seqn = seqn 69 self.i = 0 70 def __iter__(self): 71 return self 72 def __next__(self): 73 3 // 0 74 75class IterFuncStop: 76 'Test immediate stop' 77 def __init__(self, seqn): 78 pass 79 def __iter__(self): 80 return self 81 def __next__(self): 82 raise StopIteration 83 84from itertools import chain 85def itermulti(seqn): 86 'Test multiple tiers of iterators' 87 return chain(map(lambda x:x, iterfunc(IterGen(Sequence(seqn))))) 88 89class LyingTuple(tuple): 90 def __iter__(self): 91 yield 1 92 93class LyingList(list): 94 def __iter__(self): 95 yield 1 96 97class CommonTest(unittest.TestCase): 98 # The type to be tested 99 type2test = None 100 101 def test_constructors(self): 102 l0 = [] 103 l1 = [0] 104 l2 = [0, 1] 105 106 u = self.type2test() 107 u0 = self.type2test(l0) 108 u1 = self.type2test(l1) 109 u2 = self.type2test(l2) 110 111 uu = self.type2test(u) 112 uu0 = self.type2test(u0) 113 uu1 = self.type2test(u1) 114 uu2 = self.type2test(u2) 115 116 v = self.type2test(tuple(u)) 117 class OtherSeq: 118 def __init__(self, initseq): 119 self.__data = initseq 120 def __len__(self): 121 return len(self.__data) 122 def __getitem__(self, i): 123 return self.__data[i] 124 s = OtherSeq(u0) 125 v0 = self.type2test(s) 126 self.assertEqual(len(v0), len(s)) 127 128 s = "this is also a sequence" 129 vv = self.type2test(s) 130 self.assertEqual(len(vv), len(s)) 131 132 # Create from various iteratables 133 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): 134 for g in (Sequence, IterFunc, IterGen, 135 itermulti, iterfunc): 136 self.assertEqual(self.type2test(g(s)), self.type2test(s)) 137 self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test()) 138 self.assertEqual(self.type2test(c for c in "123"), self.type2test("123")) 139 self.assertRaises(TypeError, self.type2test, IterNextOnly(s)) 140 self.assertRaises(TypeError, self.type2test, IterNoNext(s)) 141 self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s)) 142 143 # Issue #23757 144 self.assertEqual(self.type2test(LyingTuple((2,))), self.type2test((1,))) 145 self.assertEqual(self.type2test(LyingList([2])), self.type2test([1])) 146 147 def test_truth(self): 148 self.assertFalse(self.type2test()) 149 self.assertTrue(self.type2test([42])) 150 151 def test_getitem(self): 152 u = self.type2test([0, 1, 2, 3, 4]) 153 for i in range(len(u)): 154 self.assertEqual(u[i], i) 155 self.assertEqual(u[int(i)], i) 156 for i in range(-len(u), -1): 157 self.assertEqual(u[i], len(u)+i) 158 self.assertEqual(u[int(i)], len(u)+i) 159 self.assertRaises(IndexError, u.__getitem__, -len(u)-1) 160 self.assertRaises(IndexError, u.__getitem__, len(u)) 161 self.assertRaises(ValueError, u.__getitem__, slice(0,10,0)) 162 163 u = self.type2test() 164 self.assertRaises(IndexError, u.__getitem__, 0) 165 self.assertRaises(IndexError, u.__getitem__, -1) 166 167 self.assertRaises(TypeError, u.__getitem__) 168 169 a = self.type2test([10, 11]) 170 self.assertEqual(a[0], 10) 171 self.assertEqual(a[1], 11) 172 self.assertEqual(a[-2], 10) 173 self.assertEqual(a[-1], 11) 174 self.assertRaises(IndexError, a.__getitem__, -3) 175 self.assertRaises(IndexError, a.__getitem__, 3) 176 177 def test_getslice(self): 178 l = [0, 1, 2, 3, 4] 179 u = self.type2test(l) 180 181 self.assertEqual(u[0:0], self.type2test()) 182 self.assertEqual(u[1:2], self.type2test([1])) 183 self.assertEqual(u[-2:-1], self.type2test([3])) 184 self.assertEqual(u[-1000:1000], u) 185 self.assertEqual(u[1000:-1000], self.type2test([])) 186 self.assertEqual(u[:], u) 187 self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4])) 188 self.assertEqual(u[None:3], self.type2test([0, 1, 2])) 189 190 # Extended slices 191 self.assertEqual(u[::], u) 192 self.assertEqual(u[::2], self.type2test([0, 2, 4])) 193 self.assertEqual(u[1::2], self.type2test([1, 3])) 194 self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0])) 195 self.assertEqual(u[::-2], self.type2test([4, 2, 0])) 196 self.assertEqual(u[3::-2], self.type2test([3, 1])) 197 self.assertEqual(u[3:3:-2], self.type2test([])) 198 self.assertEqual(u[3:2:-2], self.type2test([3])) 199 self.assertEqual(u[3:1:-2], self.type2test([3])) 200 self.assertEqual(u[3:0:-2], self.type2test([3, 1])) 201 self.assertEqual(u[::-100], self.type2test([4])) 202 self.assertEqual(u[100:-100:], self.type2test([])) 203 self.assertEqual(u[-100:100:], u) 204 self.assertEqual(u[100:-100:-1], u[::-1]) 205 self.assertEqual(u[-100:100:-1], self.type2test([])) 206 self.assertEqual(u[-100:100:2], self.type2test([0, 2, 4])) 207 208 # Test extreme cases with long ints 209 a = self.type2test([0,1,2,3,4]) 210 self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2])) 211 self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4])) 212 self.assertEqual(a[3::sys.maxsize], self.type2test([3])) 213 214 def test_contains(self): 215 u = self.type2test([0, 1, 2]) 216 for i in u: 217 self.assertIn(i, u) 218 for i in min(u)-1, max(u)+1: 219 self.assertNotIn(i, u) 220 221 self.assertRaises(TypeError, u.__contains__) 222 223 def test_contains_fake(self): 224 class AllEq: 225 # Sequences must use rich comparison against each item 226 # (unless "is" is true, or an earlier item answered) 227 # So instances of AllEq must be found in all non-empty sequences. 228 def __eq__(self, other): 229 return True 230 __hash__ = None # Can't meet hash invariant requirements 231 self.assertNotIn(AllEq(), self.type2test([])) 232 self.assertIn(AllEq(), self.type2test([1])) 233 234 def test_contains_order(self): 235 # Sequences must test in-order. If a rich comparison has side 236 # effects, these will be visible to tests against later members. 237 # In this test, the "side effect" is a short-circuiting raise. 238 class DoNotTestEq(Exception): 239 pass 240 class StopCompares: 241 def __eq__(self, other): 242 raise DoNotTestEq 243 244 checkfirst = self.type2test([1, StopCompares()]) 245 self.assertIn(1, checkfirst) 246 checklast = self.type2test([StopCompares(), 1]) 247 self.assertRaises(DoNotTestEq, checklast.__contains__, 1) 248 249 def test_len(self): 250 self.assertEqual(len(self.type2test()), 0) 251 self.assertEqual(len(self.type2test([])), 0) 252 self.assertEqual(len(self.type2test([0])), 1) 253 self.assertEqual(len(self.type2test([0, 1, 2])), 3) 254 255 def test_minmax(self): 256 u = self.type2test([0, 1, 2]) 257 self.assertEqual(min(u), 0) 258 self.assertEqual(max(u), 2) 259 260 def test_addmul(self): 261 u1 = self.type2test([0]) 262 u2 = self.type2test([0, 1]) 263 self.assertEqual(u1, u1 + self.type2test()) 264 self.assertEqual(u1, self.type2test() + u1) 265 self.assertEqual(u1 + self.type2test([1]), u2) 266 self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0])) 267 self.assertEqual(self.type2test(), u2*0) 268 self.assertEqual(self.type2test(), 0*u2) 269 self.assertEqual(self.type2test(), u2*0) 270 self.assertEqual(self.type2test(), 0*u2) 271 self.assertEqual(u2, u2*1) 272 self.assertEqual(u2, 1*u2) 273 self.assertEqual(u2, u2*1) 274 self.assertEqual(u2, 1*u2) 275 self.assertEqual(u2+u2, u2*2) 276 self.assertEqual(u2+u2, 2*u2) 277 self.assertEqual(u2+u2, u2*2) 278 self.assertEqual(u2+u2, 2*u2) 279 self.assertEqual(u2+u2+u2, u2*3) 280 self.assertEqual(u2+u2+u2, 3*u2) 281 282 class subclass(self.type2test): 283 pass 284 u3 = subclass([0, 1]) 285 self.assertEqual(u3, u3*1) 286 self.assertIsNot(u3, u3*1) 287 288 def test_iadd(self): 289 u = self.type2test([0, 1]) 290 u += self.type2test() 291 self.assertEqual(u, self.type2test([0, 1])) 292 u += self.type2test([2, 3]) 293 self.assertEqual(u, self.type2test([0, 1, 2, 3])) 294 u += self.type2test([4, 5]) 295 self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5])) 296 297 u = self.type2test("spam") 298 u += self.type2test("eggs") 299 self.assertEqual(u, self.type2test("spameggs")) 300 301 def test_imul(self): 302 u = self.type2test([0, 1]) 303 u *= 3 304 self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) 305 u *= 0 306 self.assertEqual(u, self.type2test([])) 307 308 def test_getitemoverwriteiter(self): 309 # Verify that __getitem__ overrides are not recognized by __iter__ 310 class T(self.type2test): 311 def __getitem__(self, key): 312 return str(key) + '!!!' 313 self.assertEqual(next(iter(T((1,2)))), 1) 314 315 def test_repeat(self): 316 for m in range(4): 317 s = tuple(range(m)) 318 for n in range(-3, 5): 319 self.assertEqual(self.type2test(s*n), self.type2test(s)*n) 320 self.assertEqual(self.type2test(s)*(-4), self.type2test([])) 321 self.assertEqual(id(s), id(s*1)) 322 323 def test_bigrepeat(self): 324 if sys.maxsize <= 2147483647: 325 x = self.type2test([0]) 326 x *= 2**16 327 self.assertRaises(MemoryError, x.__mul__, 2**16) 328 if hasattr(x, '__imul__'): 329 self.assertRaises(MemoryError, x.__imul__, 2**16) 330 331 def test_subscript(self): 332 a = self.type2test([10, 11]) 333 self.assertEqual(a.__getitem__(0), 10) 334 self.assertEqual(a.__getitem__(1), 11) 335 self.assertEqual(a.__getitem__(-2), 10) 336 self.assertEqual(a.__getitem__(-1), 11) 337 self.assertRaises(IndexError, a.__getitem__, -3) 338 self.assertRaises(IndexError, a.__getitem__, 3) 339 self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10])) 340 self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11])) 341 self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11])) 342 self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11])) 343 self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([])) 344 self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0)) 345 self.assertRaises(TypeError, a.__getitem__, 'x') 346 347 def test_count(self): 348 a = self.type2test([0, 1, 2])*3 349 self.assertEqual(a.count(0), 3) 350 self.assertEqual(a.count(1), 3) 351 self.assertEqual(a.count(3), 0) 352 353 self.assertRaises(TypeError, a.count) 354 355 class BadExc(Exception): 356 pass 357 358 class BadCmp: 359 def __eq__(self, other): 360 if other == 2: 361 raise BadExc() 362 return False 363 364 self.assertRaises(BadExc, a.count, BadCmp()) 365 366 def test_index(self): 367 u = self.type2test([0, 1]) 368 self.assertEqual(u.index(0), 0) 369 self.assertEqual(u.index(1), 1) 370 self.assertRaises(ValueError, u.index, 2) 371 372 u = self.type2test([-2, -1, 0, 0, 1, 2]) 373 self.assertEqual(u.count(0), 2) 374 self.assertEqual(u.index(0), 2) 375 self.assertEqual(u.index(0, 2), 2) 376 self.assertEqual(u.index(-2, -10), 0) 377 self.assertEqual(u.index(0, 3), 3) 378 self.assertEqual(u.index(0, 3, 4), 3) 379 self.assertRaises(ValueError, u.index, 2, 0, -10) 380 381 self.assertRaises(TypeError, u.index) 382 383 class BadExc(Exception): 384 pass 385 386 class BadCmp: 387 def __eq__(self, other): 388 if other == 2: 389 raise BadExc() 390 return False 391 392 a = self.type2test([0, 1, 2, 3]) 393 self.assertRaises(BadExc, a.index, BadCmp()) 394 395 a = self.type2test([-2, -1, 0, 0, 1, 2]) 396 self.assertEqual(a.index(0), 2) 397 self.assertEqual(a.index(0, 2), 2) 398 self.assertEqual(a.index(0, -4), 2) 399 self.assertEqual(a.index(-2, -10), 0) 400 self.assertEqual(a.index(0, 3), 3) 401 self.assertEqual(a.index(0, -3), 3) 402 self.assertEqual(a.index(0, 3, 4), 3) 403 self.assertEqual(a.index(0, -3, -2), 3) 404 self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2) 405 self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize) 406 self.assertRaises(ValueError, a.index, 2, 0, -10) 407 408 def test_pickle(self): 409 lst = self.type2test([4, 5, 6, 7]) 410 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 411 lst2 = pickle.loads(pickle.dumps(lst, proto)) 412 self.assertEqual(lst2, lst) 413 self.assertNotEqual(id(lst2), id(lst)) 414 415 def test_free_after_iterating(self): 416 support.check_free_after_iterating(self, iter, self.type2test) 417 support.check_free_after_iterating(self, reversed, self.type2test) 418