2 Tests common to list and UserList.UserList
8 from test
import test_support
, seq_tests
10 class CommonTest(seq_tests
.CommonTest
):
13 # Iterable arg is optional
14 self
.assertEqual(self
.type2test([]), self
.type2test())
16 # Init clears previous values
17 a
= self
.type2test([1, 2, 3])
19 self
.assertEqual(a
, self
.type2test([]))
21 # Init overwrites previous values
22 a
= self
.type2test([1, 2, 3])
24 self
.assertEqual(a
, self
.type2test([4, 5, 6]))
26 # Mutables always return a new object
28 self
.assertNotEqual(id(a
), id(b
))
29 self
.assertEqual(a
, b
)
34 a0
= self
.type2test(l0
)
35 a2
= self
.type2test(l2
)
37 self
.assertEqual(str(a0
), str(l0
))
38 self
.assertEqual(repr(a0
), repr(l0
))
39 self
.assertEqual(repr(a2
), repr(l2
))
40 self
.assertEqual(str(a2
), "[0, 1, 2]")
41 self
.assertEqual(repr(a2
), "[0, 1, 2]")
45 self
.assertEqual(str(a2
), "[0, 1, 2, [...], 3]")
46 self
.assertEqual(repr(a2
), "[0, 1, 2, [...], 3]")
49 for i
in xrange(sys
.getrecursionlimit() + 100):
51 self
.assertRaises(RuntimeError, repr, l0
)
54 d
= self
.type2test(xrange(200))
56 d
.extend(xrange(200,400))
60 with
open(test_support
.TESTFN
, "wb") as fo
:
62 with
open(test_support
.TESTFN
, "rb") as fo
:
63 self
.assertEqual(fo
.read(), repr(d
))
65 os
.remove(test_support
.TESTFN
)
67 def test_set_subscript(self
):
68 a
= self
.type2test(range(20))
69 self
.assertRaises(ValueError, a
.__setitem
__, slice(0, 10, 0), [1,2,3])
70 self
.assertRaises(TypeError, a
.__setitem
__, slice(0, 10), 1)
71 self
.assertRaises(ValueError, a
.__setitem
__, slice(0, 10, 2), [1,2])
72 self
.assertRaises(TypeError, a
.__getitem
__, 'x', 1)
73 a
[slice(2,10,3)] = [1,2,3]
74 self
.assertEqual(a
, self
.type2test([0, 1, 1, 3, 4, 2, 6, 7, 3,
75 9, 10, 11, 12, 13, 14, 15,
78 def test_reversed(self
):
79 a
= self
.type2test(range(20))
81 self
.assertEqual(list(r
), self
.type2test(range(19, -1, -1)))
82 self
.assertRaises(StopIteration, r
.next
)
83 self
.assertEqual(list(reversed(self
.type2test())),
85 # Bug 3689: make sure list-reversed-iterator doesn't have __len__
86 self
.assertRaises(TypeError, len, reversed([1,2,3]))
88 def test_setitem(self
):
89 a
= self
.type2test([0, 1])
92 self
.assertEqual(a
, self
.type2test([0, 100]))
94 self
.assertEqual(a
, self
.type2test([0, 200]))
96 self
.assertEqual(a
, self
.type2test([100, 200]))
97 self
.assertRaises(IndexError, a
.__setitem
__, -3, 200)
98 self
.assertRaises(IndexError, a
.__setitem
__, 2, 200)
100 a
= self
.type2test([])
101 self
.assertRaises(IndexError, a
.__setitem
__, 0, 200)
102 self
.assertRaises(IndexError, a
.__setitem
__, -1, 200)
103 self
.assertRaises(TypeError, a
.__setitem
__)
105 a
= self
.type2test([0,1,2,3,4])
109 self
.assertEqual(a
, self
.type2test([1,2,3,3,4]))
113 self
.assertEqual(a
, self
.type2test([5,6,7,3,4]))
116 self
.assertEqual(a
, self
.type2test([5,6,7,88,99]))
119 self
.assertEqual(a
, self
.type2test([5,6,7,8,9]))
121 def test_delitem(self
):
122 a
= self
.type2test([0, 1])
124 self
.assertEqual(a
, [0])
126 self
.assertEqual(a
, [])
128 a
= self
.type2test([0, 1])
130 self
.assertEqual(a
, [1])
132 self
.assertEqual(a
, [])
134 a
= self
.type2test([0, 1])
135 self
.assertRaises(IndexError, a
.__delitem
__, -3)
136 self
.assertRaises(IndexError, a
.__delitem
__, 2)
138 a
= self
.type2test([])
139 self
.assertRaises(IndexError, a
.__delitem
__, 0)
141 self
.assertRaises(TypeError, a
.__delitem
__)
143 def test_setslice(self
):
145 a
= self
.type2test(l
)
147 for i
in range(-3, 4):
149 self
.assertEqual(a
, l
)
152 self
.assertEqual(a2
, a
)
154 self
.assertEqual(a
, l
)
157 self
.assertEqual(a2
, a
)
158 for j
in range(-3, 4):
160 self
.assertEqual(a
, l
)
163 self
.assertEqual(a2
, a
)
167 self
.assertEqual(aa2
, [-2, -1, 0, 1])
169 self
.assertEqual(aa2
, [])
171 a
= self
.type2test([1, 2, 3, 4, 5])
173 self
.assertEqual(a
, self
.type2test([1, 2, 3, 4, 5, 5]))
174 a
= self
.type2test([1, 2, 3, 4, 5])
176 self
.assertEqual(a
, self
.type2test([1, 1, 2, 3, 4, 5]))
177 a
= self
.type2test([1, 2, 3, 4, 5])
179 self
.assertEqual(a
, self
.type2test([1, 1, 2, 3, 4, 5, 5]))
181 a
= self
.type2test([])
182 a
[:] = tuple(range(10))
183 self
.assertEqual(a
, self
.type2test(range(10)))
185 self
.assertRaises(TypeError, a
.__setslice
__, 0, 1, 5)
186 self
.assertRaises(TypeError, a
.__setitem
__, slice(0, 1, 5))
188 self
.assertRaises(TypeError, a
.__setslice
__)
189 self
.assertRaises(TypeError, a
.__setitem
__)
191 def test_delslice(self
):
192 a
= self
.type2test([0, 1])
195 self
.assertEqual(a
, self
.type2test([]))
197 a
= self
.type2test([0, 1])
200 self
.assertEqual(a
, self
.type2test([]))
202 a
= self
.type2test([0, 1])
204 self
.assertEqual(a
, self
.type2test([1]))
206 a
= self
.type2test([0, 1])
208 self
.assertEqual(a
, self
.type2test([1]))
210 a
= self
.type2test([0, 1])
213 self
.assertEqual(a
, self
.type2test([]))
215 a
= self
.type2test([0, 1])
218 self
.assertEqual(a
, self
.type2test([]))
220 a
= self
.type2test([0, 1])
222 self
.assertEqual(a
, self
.type2test([0]))
224 a
= self
.type2test([0, 1])
226 self
.assertEqual(a
, self
.type2test([0]))
228 a
= self
.type2test([0, 1])
230 self
.assertEqual(a
, self
.type2test([]))
232 def test_append(self
):
233 a
= self
.type2test([])
237 self
.assertEqual(a
, self
.type2test([0, 1, 2]))
239 self
.assertRaises(TypeError, a
.append
)
241 def test_extend(self
):
242 a1
= self
.type2test([0])
243 a2
= self
.type2test((0, 1))
246 self
.assertEqual(a
, a1
+ a2
)
248 a
.extend(self
.type2test([]))
249 self
.assertEqual(a
, a1
+ a2
)
252 self
.assertEqual(a
, self
.type2test([0, 0, 1, 0, 0, 1]))
254 a
= self
.type2test("spam")
256 self
.assertEqual(a
, list("spameggs"))
258 self
.assertRaises(TypeError, a
.extend
, None)
260 self
.assertRaises(TypeError, a
.extend
)
262 def test_insert(self
):
263 a
= self
.type2test([0, 1, 2])
267 self
.assertEqual(a
, [-2, -1, 0, 0, 1, 2])
271 b
.insert(-200, "left")
272 b
.insert(200, "right")
273 self
.assertEqual(b
, self
.type2test(["left",-2,-1,0,0,"foo",1,2,"right"]))
275 self
.assertRaises(TypeError, a
.insert
)
278 a
= self
.type2test([-1, 0, 1])
280 self
.assertEqual(a
, [-1, 0])
282 self
.assertEqual(a
, [0])
283 self
.assertRaises(IndexError, a
.pop
, 5)
285 self
.assertEqual(a
, [])
286 self
.assertRaises(IndexError, a
.pop
)
287 self
.assertRaises(TypeError, a
.pop
, 42, 42)
288 a
= self
.type2test([0, 10, 20, 30, 40])
290 def test_remove(self
):
291 a
= self
.type2test([0, 0, 1])
293 self
.assertEqual(a
, [0, 0])
295 self
.assertEqual(a
, [0])
297 self
.assertEqual(a
, [])
299 self
.assertRaises(ValueError, a
.remove
, 0)
301 self
.assertRaises(TypeError, a
.remove
)
303 class BadExc(Exception):
307 def __eq__(self
, other
):
312 a
= self
.type2test([0, 1, 2, 3])
313 self
.assertRaises(BadExc
, a
.remove
, BadCmp())
316 def __eq__(self
, other
):
319 d
= self
.type2test('abcdefghcij')
321 self
.assertEqual(d
, self
.type2test('abdefghcij'))
323 self
.assertEqual(d
, self
.type2test('abdefghij'))
324 self
.assertRaises(ValueError, d
.remove
, 'c')
325 self
.assertEqual(d
, self
.type2test('abdefghij'))
327 # Handle comparison errors
328 d
= self
.type2test(['a', 'b', BadCmp2(), 'c'])
329 e
= self
.type2test(d
)
330 self
.assertRaises(BadExc
, d
.remove
, 'c')
331 for x
, y
in zip(d
, e
):
332 # verify that original order and values are retained.
335 def test_count(self
):
336 a
= self
.type2test([0, 1, 2])*3
337 self
.assertEqual(a
.count(0), 3)
338 self
.assertEqual(a
.count(1), 3)
339 self
.assertEqual(a
.count(3), 0)
341 self
.assertRaises(TypeError, a
.count
)
343 class BadExc(Exception):
347 def __eq__(self
, other
):
352 self
.assertRaises(BadExc
, a
.count
, BadCmp())
354 def test_index(self
):
355 u
= self
.type2test([0, 1])
356 self
.assertEqual(u
.index(0), 0)
357 self
.assertEqual(u
.index(1), 1)
358 self
.assertRaises(ValueError, u
.index
, 2)
360 u
= self
.type2test([-2, -1, 0, 0, 1, 2])
361 self
.assertEqual(u
.count(0), 2)
362 self
.assertEqual(u
.index(0), 2)
363 self
.assertEqual(u
.index(0, 2), 2)
364 self
.assertEqual(u
.index(-2, -10), 0)
365 self
.assertEqual(u
.index(0, 3), 3)
366 self
.assertEqual(u
.index(0, 3, 4), 3)
367 self
.assertRaises(ValueError, u
.index
, 2, 0, -10)
369 self
.assertRaises(TypeError, u
.index
)
371 class BadExc(Exception):
375 def __eq__(self
, other
):
380 a
= self
.type2test([0, 1, 2, 3])
381 self
.assertRaises(BadExc
, a
.index
, BadCmp())
383 a
= self
.type2test([-2, -1, 0, 0, 1, 2])
384 self
.assertEqual(a
.index(0), 2)
385 self
.assertEqual(a
.index(0, 2), 2)
386 self
.assertEqual(a
.index(0, -4), 2)
387 self
.assertEqual(a
.index(-2, -10), 0)
388 self
.assertEqual(a
.index(0, 3), 3)
389 self
.assertEqual(a
.index(0, -3), 3)
390 self
.assertEqual(a
.index(0, 3, 4), 3)
391 self
.assertEqual(a
.index(0, -3, -2), 3)
392 self
.assertEqual(a
.index(0, -4*sys
.maxint
, 4*sys
.maxint
), 2)
393 self
.assertRaises(ValueError, a
.index
, 0, 4*sys
.maxint
,-4*sys
.maxint
)
394 self
.assertRaises(ValueError, a
.index
, 2, 0, -10)
396 self
.assertRaises(ValueError, a
.index
, 2, 0, 4)
397 self
.assertEqual(a
, self
.type2test([-2, -1, 0, 1, 2]))
399 # Test modifying the list during index's iteration
401 def __init__(self
, victim
):
403 def __eq__(self
, other
):
407 a
[:] = [EvilCmp(a
) for _
in xrange(100)]
408 # This used to seg fault before patch #1005778
409 self
.assertRaises(ValueError, a
.index
, None)
411 def test_reverse(self
):
412 u
= self
.type2test([-2, -1, 0, 1, 2])
415 self
.assertEqual(u
, [2, 1, 0, -1, -2])
417 self
.assertEqual(u
, u2
)
419 self
.assertRaises(TypeError, u
.reverse
, 42)
422 with test_support
.check_py3k_warnings(
423 ("the cmp argument is not supported", DeprecationWarning)):
426 def _test_sort(self
):
427 u
= self
.type2test([1, 0])
429 self
.assertEqual(u
, [0, 1])
431 u
= self
.type2test([2,1,0,-1,-2])
433 self
.assertEqual(u
, self
.type2test([-2,-1,0,1,2]))
435 self
.assertRaises(TypeError, u
.sort
, 42, 42)
440 self
.assertEqual(u
, self
.type2test([2,1,0,-1,-2]))
442 # The following dumps core in unpatched Python 1.5:
443 def myComparison(x
,y
):
445 z
= self
.type2test(range(12))
448 self
.assertRaises(TypeError, z
.sort
, 2)
450 def selfmodifyingComparison(x
,y
):
453 self
.assertRaises(ValueError, z
.sort
, selfmodifyingComparison
)
455 self
.assertRaises(TypeError, z
.sort
, lambda x
, y
: 's')
457 self
.assertRaises(TypeError, z
.sort
, 42, 42, 42, 42)
459 def test_slice(self
):
460 u
= self
.type2test("spam")
462 self
.assertEqual(u
, list("ham"))
465 super(CommonTest
, self
).test_iadd()
466 u
= self
.type2test([0, 1])
469 self
.assert_(u
is u2
)
471 u
= self
.type2test("spam")
473 self
.assertEqual(u
, self
.type2test("spameggs"))
475 self
.assertRaises(TypeError, u
.__iadd
__, None)
478 u
= self
.type2test([0, 1])
480 self
.assertEqual(u
, self
.type2test([0, 1, 0, 1, 0, 1]))
482 self
.assertEqual(u
, self
.type2test([]))
483 s
= self
.type2test([])
486 self
.assertEqual(id(s
), oldid
)
488 def test_extendedslicing(self
):
490 a
= self
.type2test([0,1,2,3,4])
494 self
.assertEqual(a
, self
.type2test([1,3]))
495 a
= self
.type2test(range(5))
497 self
.assertEqual(a
, self
.type2test([0,2,4]))
498 a
= self
.type2test(range(5))
500 self
.assertEqual(a
, self
.type2test([0,2,3,4]))
501 a
= self
.type2test(range(10))
503 self
.assertEqual(a
, self
.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9]))
505 a
= self
.type2test(range(10))
507 self
.assertEqual(a
, self
.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
508 a
= self
.type2test(range(10))
510 self
.assertEqual(a
, self
.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
511 a
= self
.type2test(range(4))
513 self
.assertEqual(a
, self
.type2test([3, 2, 1, 0]))
514 a
= self
.type2test(range(10))
517 a
[2:3] = self
.type2test(["two", "elements"])
518 b
[slice(2,3)] = self
.type2test(["two", "elements"])
519 c
[2:3:] = self
.type2test(["two", "elements"])
520 self
.assertEqual(a
, b
)
521 self
.assertEqual(a
, c
)
522 a
= self
.type2test(range(10))
523 a
[::2] = tuple(range(5))
524 self
.assertEqual(a
, self
.type2test([0, 1, 1, 3, 2, 5, 3, 7, 4, 9]))
526 a
= self
.type2test(range(10))
529 def test_constructor_exception_handling(self
):
533 raise KeyboardInterrupt
534 self
.assertRaises(KeyboardInterrupt, list, F())