0001 """ 0002 Tests common to list and UserList.UserList 0003 """ 0004 0005 import sys 0006 import os 0007 0008 import unittest 0009 from test import test_support, seq_tests 0010 0011 class CommonTest(seq_tests.CommonTest): 0012 0013 def test_init(self): 0014 # Iterable arg is optional 0015 self.assertEqual(self.type2test([]), self.type2test()) 0016 0017 # Init clears previous values 0018 a = self.type2test([1, 2, 3]) 0019 a.__init__() 0020 self.assertEqual(a, self.type2test([])) 0021 0022 # Init overwrites previous values 0023 a = self.type2test([1, 2, 3]) 0024 a.__init__([4, 5, 6]) 0025 self.assertEqual(a, self.type2test([4, 5, 6])) 0026 0027 # Mutables always return a new object 0028 b = self.type2test(a) 0029 self.assertNotEqual(id(a), id(b)) 0030 self.assertEqual(a, b) 0031 0032 def test_repr(self): 0033 l0 = [] 0034 l2 = [0, 1, 2] 0035 a0 = self.type2test(l0) 0036 a2 = self.type2test(l2) 0037 0038 self.assertEqual(str(a0), str(l0)) 0039 self.assertEqual(repr(a0), repr(l0)) 0040 self.assertEqual(`a2`, `l2`) 0041 self.assertEqual(str(a2), "[0, 1, 2]") 0042 self.assertEqual(repr(a2), "[0, 1, 2]") 0043 0044 a2.append(a2) 0045 a2.append(3) 0046 self.assertEqual(str(a2), "[0, 1, 2, [...], 3]") 0047 self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]") 0048 0049 def test_print(self): 0050 d = self.type2test(xrange(200)) 0051 d.append(d) 0052 d.extend(xrange(200,400)) 0053 d.append(d) 0054 d.append(400) 0055 try: 0056 fo = open(test_support.TESTFN, "wb") 0057 print >> fo, d, 0058 fo.close() 0059 fo = open(test_support.TESTFN, "rb") 0060 self.assertEqual(fo.read(), repr(d)) 0061 finally: 0062 fo.close() 0063 os.remove(test_support.TESTFN) 0064 0065 def test_set_subscript(self): 0066 a = self.type2test(range(20)) 0067 self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 0), [1,2,3]) 0068 self.assertRaises(TypeError, a.__setitem__, slice(0, 10), 1) 0069 self.assertRaises(ValueError, a.__setitem__, slice(0, 10, 2), [1,2]) 0070 self.assertRaises(TypeError, a.__getitem__, 'x', 1) 0071 a[slice(2,10,3)] = [1,2,3] 0072 self.assertEqual(a, self.type2test([0, 1, 1, 3, 4, 2, 6, 7, 3, 0073 9, 10, 11, 12, 13, 14, 15, 0074 16, 17, 18, 19])) 0075 0076 def test_reversed(self): 0077 a = self.type2test(range(20)) 0078 r = reversed(a) 0079 self.assertEqual(list(r), self.type2test(range(19, -1, -1))) 0080 self.assertRaises(StopIteration, r.next) 0081 self.assertEqual(list(reversed(self.type2test())), 0082 self.type2test()) 0083 0084 def test_setitem(self): 0085 a = self.type2test([0, 1]) 0086 a[0] = 0 0087 a[1] = 100 0088 self.assertEqual(a, self.type2test([0, 100])) 0089 a[-1] = 200 0090 self.assertEqual(a, self.type2test([0, 200])) 0091 a[-2] = 100 0092 self.assertEqual(a, self.type2test([100, 200])) 0093 self.assertRaises(IndexError, a.__setitem__, -3, 200) 0094 self.assertRaises(IndexError, a.__setitem__, 2, 200) 0095 0096 a = self.type2test([]) 0097 self.assertRaises(IndexError, a.__setitem__, 0, 200) 0098 self.assertRaises(IndexError, a.__setitem__, -1, 200) 0099 self.assertRaises(TypeError, a.__setitem__) 0100 0101 a = self.type2test([0,1,2,3,4]) 0102 a[0L] = 1 0103 a[1L] = 2 0104 a[2L] = 3 0105 self.assertEqual(a, self.type2test([1,2,3,3,4])) 0106 a[0] = 5 0107 a[1] = 6 0108 a[2] = 7 0109 self.assertEqual(a, self.type2test([5,6,7,3,4])) 0110 a[-2L] = 88 0111 a[-1L] = 99 0112 self.assertEqual(a, self.type2test([5,6,7,88,99])) 0113 a[-2] = 8 0114 a[-1] = 9 0115 self.assertEqual(a, self.type2test([5,6,7,8,9])) 0116 0117 def test_delitem(self): 0118 a = self.type2test([0, 1]) 0119 del a[1] 0120 self.assertEqual(a, [0]) 0121 del a[0] 0122 self.assertEqual(a, []) 0123 0124 a = self.type2test([0, 1]) 0125 del a[-2] 0126 self.assertEqual(a, [1]) 0127 del a[-1] 0128 self.assertEqual(a, []) 0129 0130 a = self.type2test([0, 1]) 0131 self.assertRaises(IndexError, a.__delitem__, -3) 0132 self.assertRaises(IndexError, a.__delitem__, 2) 0133 0134 a = self.type2test([]) 0135 self.assertRaises(IndexError, a.__delitem__, 0) 0136 0137 self.assertRaises(TypeError, a.__delitem__) 0138 0139 def test_setslice(self): 0140 l = [0, 1] 0141 a = self.type2test(l) 0142 0143 for i in range(-3, 4): 0144 a[:i] = l[:i] 0145 self.assertEqual(a, l) 0146 a2 = a[:] 0147 a2[:i] = a[:i] 0148 self.assertEqual(a2, a) 0149 a[i:] = l[i:] 0150 self.assertEqual(a, l) 0151 a2 = a[:] 0152 a2[i:] = a[i:] 0153 self.assertEqual(a2, a) 0154 for j in range(-3, 4): 0155 a[i:j] = l[i:j] 0156 self.assertEqual(a, l) 0157 a2 = a[:] 0158 a2[i:j] = a[i:j] 0159 self.assertEqual(a2, a) 0160 0161 aa2 = a2[:] 0162 aa2[:0] = [-2, -1] 0163 self.assertEqual(aa2, [-2, -1, 0, 1]) 0164 aa2[0:] = [] 0165 self.assertEqual(aa2, []) 0166 0167 a = self.type2test([1, 2, 3, 4, 5]) 0168 a[:-1] = a 0169 self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 5])) 0170 a = self.type2test([1, 2, 3, 4, 5]) 0171 a[1:] = a 0172 self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5])) 0173 a = self.type2test([1, 2, 3, 4, 5]) 0174 a[1:-1] = a 0175 self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5, 5])) 0176 0177 a = self.type2test([]) 0178 a[:] = tuple(range(10)) 0179 self.assertEqual(a, self.type2test(range(10))) 0180 0181 self.assertRaises(TypeError, a.__setslice__, 0, 1, 5) 0182 0183 self.assertRaises(TypeError, a.__setslice__) 0184 0185 def test_delslice(self): 0186 a = self.type2test([0, 1]) 0187 del a[1:2] 0188 del a[0:1] 0189 self.assertEqual(a, self.type2test([])) 0190 0191 a = self.type2test([0, 1]) 0192 del a[1L:2L] 0193 del a[0L:1L] 0194 self.assertEqual(a, self.type2test([])) 0195 0196 a = self.type2test([0, 1]) 0197 del a[-2:-1] 0198 self.assertEqual(a, self.type2test([1])) 0199 0200 a = self.type2test([0, 1]) 0201 del a[-2L:-1L] 0202 self.assertEqual(a, self.type2test([1])) 0203 0204 a = self.type2test([0, 1]) 0205 del a[1:] 0206 del a[:1] 0207 self.assertEqual(a, self.type2test([])) 0208 0209 a = self.type2test([0, 1]) 0210 del a[1L:] 0211 del a[:1L] 0212 self.assertEqual(a, self.type2test([])) 0213 0214 a = self.type2test([0, 1]) 0215 del a[-1:] 0216 self.assertEqual(a, self.type2test([0])) 0217 0218 a = self.type2test([0, 1]) 0219 del a[-1L:] 0220 self.assertEqual(a, self.type2test([0])) 0221 0222 a = self.type2test([0, 1]) 0223 del a[:] 0224 self.assertEqual(a, self.type2test([])) 0225 0226 def test_append(self): 0227 a = self.type2test([]) 0228 a.append(0) 0229 a.append(1) 0230 a.append(2) 0231 self.assertEqual(a, self.type2test([0, 1, 2])) 0232 0233 self.assertRaises(TypeError, a.append) 0234 0235 def test_extend(self): 0236 a1 = self.type2test([0]) 0237 a2 = self.type2test((0, 1)) 0238 a = a1[:] 0239 a.extend(a2) 0240 self.assertEqual(a, a1 + a2) 0241 0242 a.extend(self.type2test([])) 0243 self.assertEqual(a, a1 + a2) 0244 0245 a.extend(a) 0246 self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1])) 0247 0248 a = self.type2test("spam") 0249 a.extend("eggs") 0250 self.assertEqual(a, list("spameggs")) 0251 0252 self.assertRaises(TypeError, a.extend, None) 0253 0254 self.assertRaises(TypeError, a.extend) 0255 0256 def test_insert(self): 0257 a = self.type2test([0, 1, 2]) 0258 a.insert(0, -2) 0259 a.insert(1, -1) 0260 a.insert(2, 0) 0261 self.assertEqual(a, [-2, -1, 0, 0, 1, 2]) 0262 0263 b = a[:] 0264 b.insert(-2, "foo") 0265 b.insert(-200, "left") 0266 b.insert(200, "right") 0267 self.assertEqual(b, self.type2test(["left",-2,-1,0,0,"foo",1,2,"right"])) 0268 0269 self.assertRaises(TypeError, a.insert) 0270 0271 def test_pop(self): 0272 from decimal import Decimal 0273 a = self.type2test([-1, 0, 1]) 0274 a.pop() 0275 self.assertEqual(a, [-1, 0]) 0276 a.pop(0) 0277 self.assertEqual(a, [0]) 0278 self.assertRaises(IndexError, a.pop, 5) 0279 a.pop(0) 0280 self.assertEqual(a, []) 0281 self.assertRaises(IndexError, a.pop) 0282 self.assertRaises(TypeError, a.pop, 42, 42) 0283 a = self.type2test([0, 10, 20, 30, 40]) 0284 self.assertEqual(a.pop(Decimal(2)), 20) 0285 self.assertRaises(IndexError, a.pop, Decimal(25)) 0286 0287 def test_remove(self): 0288 a = self.type2test([0, 0, 1]) 0289 a.remove(1) 0290 self.assertEqual(a, [0, 0]) 0291 a.remove(0) 0292 self.assertEqual(a, [0]) 0293 a.remove(0) 0294 self.assertEqual(a, []) 0295 0296 self.assertRaises(ValueError, a.remove, 0) 0297 0298 self.assertRaises(TypeError, a.remove) 0299 0300 class BadExc(Exception): 0301 pass 0302 0303 class BadCmp: 0304 def __eq__(self, other): 0305 if other == 2: 0306 raise BadExc() 0307 return False 0308 0309 a = self.type2test([0, 1, 2, 3]) 0310 self.assertRaises(BadExc, a.remove, BadCmp()) 0311 0312 def test_count(self): 0313 a = self.type2test([0, 1, 2])*3 0314 self.assertEqual(a.count(0), 3) 0315 self.assertEqual(a.count(1), 3) 0316 self.assertEqual(a.count(3), 0) 0317 0318 self.assertRaises(TypeError, a.count) 0319 0320 class BadExc(Exception): 0321 pass 0322 0323 class BadCmp: 0324 def __eq__(self, other): 0325 if other == 2: 0326 raise BadExc() 0327 return False 0328 0329 self.assertRaises(BadExc, a.count, BadCmp()) 0330 0331 def test_index(self): 0332 u = self.type2test([0, 1]) 0333 self.assertEqual(u.index(0), 0) 0334 self.assertEqual(u.index(1), 1) 0335 self.assertRaises(ValueError, u.index, 2) 0336 0337 u = self.type2test([-2, -1, 0, 0, 1, 2]) 0338 self.assertEqual(u.count(0), 2) 0339 self.assertEqual(u.index(0), 2) 0340 self.assertEqual(u.index(0, 2), 2) 0341 self.assertEqual(u.index(-2, -10), 0) 0342 self.assertEqual(u.index(0, 3), 3) 0343 self.assertEqual(u.index(0, 3, 4), 3) 0344 self.assertRaises(ValueError, u.index, 2, 0, -10) 0345 0346 self.assertRaises(TypeError, u.index) 0347 0348 class BadExc(Exception): 0349 pass 0350 0351 class BadCmp: 0352 def __eq__(self, other): 0353 if other == 2: 0354 raise BadExc() 0355 return False 0356 0357 a = self.type2test([0, 1, 2, 3]) 0358 self.assertRaises(BadExc, a.index, BadCmp()) 0359 0360 a = self.type2test([-2, -1, 0, 0, 1, 2]) 0361 self.assertEqual(a.index(0), 2) 0362 self.assertEqual(a.index(0, 2), 2) 0363 self.assertEqual(a.index(0, -4), 2) 0364 self.assertEqual(a.index(-2, -10), 0) 0365 self.assertEqual(a.index(0, 3), 3) 0366 self.assertEqual(a.index(0, -3), 3) 0367 self.assertEqual(a.index(0, 3, 4), 3) 0368 self.assertEqual(a.index(0, -3, -2), 3) 0369 self.assertEqual(a.index(0, -4*sys.maxint, 4*sys.maxint), 2) 0370 self.assertRaises(ValueError, a.index, 0, 4*sys.maxint,-4*sys.maxint) 0371 self.assertRaises(ValueError, a.index, 2, 0, -10) 0372 a.remove(0) 0373 self.assertRaises(ValueError, a.index, 2, 0, 4) 0374 self.assertEqual(a, self.type2test([-2, -1, 0, 1, 2])) 0375 0376 # Test modifying the list during index's iteration 0377 class EvilCmp: 0378 def __init__(self, victim): 0379 self.victim = victim 0380 def __eq__(self, other): 0381 del self.victim[:] 0382 return False 0383 a = self.type2test() 0384 a[:] = [EvilCmp(a) for _ in xrange(100)] 0385 # This used to seg fault before patch #1005778 0386 self.assertRaises(ValueError, a.index, None) 0387 0388 def test_reverse(self): 0389 u = self.type2test([-2, -1, 0, 1, 2]) 0390 u2 = u[:] 0391 u.reverse() 0392 self.assertEqual(u, [2, 1, 0, -1, -2]) 0393 u.reverse() 0394 self.assertEqual(u, u2) 0395 0396 self.assertRaises(TypeError, u.reverse, 42) 0397 0398 def test_sort(self): 0399 u = self.type2test([1, 0]) 0400 u.sort() 0401 self.assertEqual(u, [0, 1]) 0402 0403 u = self.type2test([2,1,0,-1,-2]) 0404 u.sort() 0405 self.assertEqual(u, self.type2test([-2,-1,0,1,2])) 0406 0407 self.assertRaises(TypeError, u.sort, 42, 42) 0408 0409 def revcmp(a, b): 0410 return cmp(b, a) 0411 u.sort(revcmp) 0412 self.assertEqual(u, self.type2test([2,1,0,-1,-2])) 0413 0414 # The following dumps core in unpatched Python 1.5: 0415 def myComparison(x,y): 0416 return cmp(x%3, y%7) 0417 z = self.type2test(range(12)) 0418 z.sort(myComparison) 0419 0420 self.assertRaises(TypeError, z.sort, 2) 0421 0422 def selfmodifyingComparison(x,y): 0423 z.append(1) 0424 return cmp(x, y) 0425 self.assertRaises(ValueError, z.sort, selfmodifyingComparison) 0426 0427 self.assertRaises(TypeError, z.sort, lambda x, y: 's') 0428 0429 self.assertRaises(TypeError, z.sort, 42, 42, 42, 42) 0430 0431 def test_slice(self): 0432 u = self.type2test("spam") 0433 u[:2] = "h" 0434 self.assertEqual(u, list("ham")) 0435 0436 def test_iadd(self): 0437 super(CommonTest, self).test_iadd() 0438 u = self.type2test([0, 1]) 0439 u2 = u 0440 u += [2, 3] 0441 self.assert_(u is u2) 0442 0443 u = self.type2test("spam") 0444 u += "eggs" 0445 self.assertEqual(u, self.type2test("spameggs")) 0446 0447 self.assertRaises(TypeError, u.__iadd__, None) 0448 0449 def test_imul(self): 0450 u = self.type2test([0, 1]) 0451 u *= 3 0452 self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1])) 0453 u *= 0 0454 self.assertEqual(u, self.type2test([])) 0455 s = self.type2test([]) 0456 oldid = id(s) 0457 s *= 10 0458 self.assertEqual(id(s), oldid) 0459 0460 def test_extendedslicing(self): 0461 # subscript 0462 a = self.type2test([0,1,2,3,4]) 0463 0464 # deletion 0465 del a[::2] 0466 self.assertEqual(a, self.type2test([1,3])) 0467 a = self.type2test(range(5)) 0468 del a[1::2] 0469 self.assertEqual(a, self.type2test([0,2,4])) 0470 a = self.type2test(range(5)) 0471 del a[1::-2] 0472 self.assertEqual(a, self.type2test([0,2,3,4])) 0473 a = self.type2test(range(10)) 0474 del a[::1000] 0475 self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9])) 0476 # assignment 0477 a = self.type2test(range(10)) 0478 a[::2] = [-1]*5 0479 self.assertEqual(a, self.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])) 0480 a = self.type2test(range(10)) 0481 a[::-4] = [10]*3 0482 self.assertEqual(a, self.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) 0483 a = self.type2test(range(4)) 0484 a[::-1] = a 0485 self.assertEqual(a, self.type2test([3, 2, 1, 0])) 0486 a = self.type2test(range(10)) 0487 b = a[:] 0488 c = a[:] 0489 a[2:3] = self.type2test(["two", "elements"]) 0490 b[slice(2,3)] = self.type2test(["two", "elements"]) 0491 c[2:3:] = self.type2test(["two", "elements"]) 0492 self.assertEqual(a, b) 0493 self.assertEqual(a, c) 0494 a = self.type2test(range(10)) 0495 a[::2] = tuple(range(5)) 0496 self.assertEqual(a, self.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9])) 0497
Generated by PyXR 0.9.4