Home | History | Annotate | Download | only in test
      1 """
      2 Tests common to list and UserList.UserList
      3 """
      4 
      5 import sys
      6 import os
      7 
      8 from test import test_support, seq_tests
      9 
     10 class CommonTest(seq_tests.CommonTest):
     11 
     12     def test_init(self):
     13         # Iterable arg is optional
     14         self.assertEqual(self.type2test([]), self.type2test())
     15 
     16         # Init clears previous values
     17         a = self.type2test([1, 2, 3])
     18         a.__init__()
     19         self.assertEqual(a, self.type2test([]))
     20 
     21         # Init overwrites previous values
     22         a = self.type2test([1, 2, 3])
     23         a.__init__([4, 5, 6])
     24         self.assertEqual(a, self.type2test([4, 5, 6]))
     25 
     26         # Mutables always return a new object
     27         b = self.type2test(a)
     28         self.assertNotEqual(id(a), id(b))
     29         self.assertEqual(a, b)
     30 
     31     def test_repr(self):
     32         l0 = []
     33         l2 = [0, 1, 2]
     34         a0 = self.type2test(l0)
     35         a2 = self.type2test(l2)
     36 
     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]")
     42 
     43         a2.append(a2)
     44         a2.append(3)
     45         self.assertEqual(str(a2), "[0, 1, 2, [...], 3]")
     46         self.assertEqual(repr(a2), "[0, 1, 2, [...], 3]")
     47 
     48         l0 = []
     49         for i in xrange(sys.getrecursionlimit() + 100):
     50             l0 = [l0]
     51         self.assertRaises(RuntimeError, repr, l0)
     52 
     53     def test_print(self):
     54         d = self.type2test(xrange(200))
     55         d.append(d)
     56         d.extend(xrange(200,400))
     57         d.append(d)
     58         d.append(400)
     59         try:
     60             with open(test_support.TESTFN, "wb") as fo:
     61                 print >> fo, d,
     62             with open(test_support.TESTFN, "rb") as fo:
     63                 self.assertEqual(fo.read(), repr(d))
     64         finally:
     65             os.remove(test_support.TESTFN)
     66 
     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,
     76                                             16, 17, 18, 19]))
     77 
     78     def test_reversed(self):
     79         a = self.type2test(range(20))
     80         r = reversed(a)
     81         self.assertEqual(list(r), self.type2test(range(19, -1, -1)))
     82         self.assertRaises(StopIteration, r.next)
     83         self.assertEqual(list(reversed(self.type2test())),
     84                          self.type2test())
     85         # Bug 3689: make sure list-reversed-iterator doesn't have __len__
     86         self.assertRaises(TypeError, len, reversed([1,2,3]))
     87 
     88     def test_setitem(self):
     89         a = self.type2test([0, 1])
     90         a[0] = 0
     91         a[1] = 100
     92         self.assertEqual(a, self.type2test([0, 100]))
     93         a[-1] = 200
     94         self.assertEqual(a, self.type2test([0, 200]))
     95         a[-2] = 100
     96         self.assertEqual(a, self.type2test([100, 200]))
     97         self.assertRaises(IndexError, a.__setitem__, -3, 200)
     98         self.assertRaises(IndexError, a.__setitem__, 2, 200)
     99 
    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__)
    104 
    105         a = self.type2test([0,1,2,3,4])
    106         a[0L] = 1
    107         a[1L] = 2
    108         a[2L] = 3
    109         self.assertEqual(a, self.type2test([1,2,3,3,4]))
    110         a[0] = 5
    111         a[1] = 6
    112         a[2] = 7
    113         self.assertEqual(a, self.type2test([5,6,7,3,4]))
    114         a[-2L] = 88
    115         a[-1L] = 99
    116         self.assertEqual(a, self.type2test([5,6,7,88,99]))
    117         a[-2] = 8
    118         a[-1] = 9
    119         self.assertEqual(a, self.type2test([5,6,7,8,9]))
    120 
    121     def test_delitem(self):
    122         a = self.type2test([0, 1])
    123         del a[1]
    124         self.assertEqual(a, [0])
    125         del a[0]
    126         self.assertEqual(a, [])
    127 
    128         a = self.type2test([0, 1])
    129         del a[-2]
    130         self.assertEqual(a, [1])
    131         del a[-1]
    132         self.assertEqual(a, [])
    133 
    134         a = self.type2test([0, 1])
    135         self.assertRaises(IndexError, a.__delitem__, -3)
    136         self.assertRaises(IndexError, a.__delitem__, 2)
    137 
    138         a = self.type2test([])
    139         self.assertRaises(IndexError, a.__delitem__, 0)
    140 
    141         self.assertRaises(TypeError, a.__delitem__)
    142 
    143     def test_setslice(self):
    144         l = [0, 1]
    145         a = self.type2test(l)
    146 
    147         for i in range(-3, 4):
    148             a[:i] = l[:i]
    149             self.assertEqual(a, l)
    150             a2 = a[:]
    151             a2[:i] = a[:i]
    152             self.assertEqual(a2, a)
    153             a[i:] = l[i:]
    154             self.assertEqual(a, l)
    155             a2 = a[:]
    156             a2[i:] = a[i:]
    157             self.assertEqual(a2, a)
    158             for j in range(-3, 4):
    159                 a[i:j] = l[i:j]
    160                 self.assertEqual(a, l)
    161                 a2 = a[:]
    162                 a2[i:j] = a[i:j]
    163                 self.assertEqual(a2, a)
    164 
    165         aa2 = a2[:]
    166         aa2[:0] = [-2, -1]
    167         self.assertEqual(aa2, [-2, -1, 0, 1])
    168         aa2[0:] = []
    169         self.assertEqual(aa2, [])
    170 
    171         a = self.type2test([1, 2, 3, 4, 5])
    172         a[:-1] = a
    173         self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 5]))
    174         a = self.type2test([1, 2, 3, 4, 5])
    175         a[1:] = a
    176         self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5]))
    177         a = self.type2test([1, 2, 3, 4, 5])
    178         a[1:-1] = a
    179         self.assertEqual(a, self.type2test([1, 1, 2, 3, 4, 5, 5]))
    180 
    181         a = self.type2test([])
    182         a[:] = tuple(range(10))
    183         self.assertEqual(a, self.type2test(range(10)))
    184 
    185         self.assertRaises(TypeError, a.__setslice__, 0, 1, 5)
    186         self.assertRaises(TypeError, a.__setitem__, slice(0, 1, 5))
    187 
    188         self.assertRaises(TypeError, a.__setslice__)
    189         self.assertRaises(TypeError, a.__setitem__)
    190 
    191     def test_delslice(self):
    192         a = self.type2test([0, 1])
    193         del a[1:2]
    194         del a[0:1]
    195         self.assertEqual(a, self.type2test([]))
    196 
    197         a = self.type2test([0, 1])
    198         del a[1L:2L]
    199         del a[0L:1L]
    200         self.assertEqual(a, self.type2test([]))
    201 
    202         a = self.type2test([0, 1])
    203         del a[-2:-1]
    204         self.assertEqual(a, self.type2test([1]))
    205 
    206         a = self.type2test([0, 1])
    207         del a[-2L:-1L]
    208         self.assertEqual(a, self.type2test([1]))
    209 
    210         a = self.type2test([0, 1])
    211         del a[1:]
    212         del a[:1]
    213         self.assertEqual(a, self.type2test([]))
    214 
    215         a = self.type2test([0, 1])
    216         del a[1L:]
    217         del a[:1L]
    218         self.assertEqual(a, self.type2test([]))
    219 
    220         a = self.type2test([0, 1])
    221         del a[-1:]
    222         self.assertEqual(a, self.type2test([0]))
    223 
    224         a = self.type2test([0, 1])
    225         del a[-1L:]
    226         self.assertEqual(a, self.type2test([0]))
    227 
    228         a = self.type2test([0, 1])
    229         del a[:]
    230         self.assertEqual(a, self.type2test([]))
    231 
    232     def test_append(self):
    233         a = self.type2test([])
    234         a.append(0)
    235         a.append(1)
    236         a.append(2)
    237         self.assertEqual(a, self.type2test([0, 1, 2]))
    238 
    239         self.assertRaises(TypeError, a.append)
    240 
    241     def test_extend(self):
    242         a1 = self.type2test([0])
    243         a2 = self.type2test((0, 1))
    244         a = a1[:]
    245         a.extend(a2)
    246         self.assertEqual(a, a1 + a2)
    247 
    248         a.extend(self.type2test([]))
    249         self.assertEqual(a, a1 + a2)
    250 
    251         a.extend(a)
    252         self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1]))
    253 
    254         a = self.type2test("spam")
    255         a.extend("eggs")
    256         self.assertEqual(a, list("spameggs"))
    257 
    258         self.assertRaises(TypeError, a.extend, None)
    259 
    260         self.assertRaises(TypeError, a.extend)
    261 
    262     def test_insert(self):
    263         a = self.type2test([0, 1, 2])
    264         a.insert(0, -2)
    265         a.insert(1, -1)
    266         a.insert(2, 0)
    267         self.assertEqual(a, [-2, -1, 0, 0, 1, 2])
    268 
    269         b = a[:]
    270         b.insert(-2, "foo")
    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"]))
    274 
    275         self.assertRaises(TypeError, a.insert)
    276 
    277     def test_pop(self):
    278         a = self.type2test([-1, 0, 1])
    279         a.pop()
    280         self.assertEqual(a, [-1, 0])
    281         a.pop(0)
    282         self.assertEqual(a, [0])
    283         self.assertRaises(IndexError, a.pop, 5)
    284         a.pop(0)
    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])
    289 
    290     def test_remove(self):
    291         a = self.type2test([0, 0, 1])
    292         a.remove(1)
    293         self.assertEqual(a, [0, 0])
    294         a.remove(0)
    295         self.assertEqual(a, [0])
    296         a.remove(0)
    297         self.assertEqual(a, [])
    298 
    299         self.assertRaises(ValueError, a.remove, 0)
    300 
    301         self.assertRaises(TypeError, a.remove)
    302 
    303         class BadExc(Exception):
    304             pass
    305 
    306         class BadCmp:
    307             def __eq__(self, other):
    308                 if other == 2:
    309                     raise BadExc()
    310                 return False
    311 
    312         a = self.type2test([0, 1, 2, 3])
    313         self.assertRaises(BadExc, a.remove, BadCmp())
    314 
    315         class BadCmp2:
    316             def __eq__(self, other):
    317                 raise BadExc()
    318 
    319         d = self.type2test('abcdefghcij')
    320         d.remove('c')
    321         self.assertEqual(d, self.type2test('abdefghcij'))
    322         d.remove('c')
    323         self.assertEqual(d, self.type2test('abdefghij'))
    324         self.assertRaises(ValueError, d.remove, 'c')
    325         self.assertEqual(d, self.type2test('abdefghij'))
    326 
    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.
    333             self.assertIs(x, y)
    334 
    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)
    340 
    341         self.assertRaises(TypeError, a.count)
    342 
    343         class BadExc(Exception):
    344             pass
    345 
    346         class BadCmp:
    347             def __eq__(self, other):
    348                 if other == 2:
    349                     raise BadExc()
    350                 return False
    351 
    352         self.assertRaises(BadExc, a.count, BadCmp())
    353 
    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)
    359 
    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)
    368 
    369         self.assertRaises(TypeError, u.index)
    370 
    371         class BadExc(Exception):
    372             pass
    373 
    374         class BadCmp:
    375             def __eq__(self, other):
    376                 if other == 2:
    377                     raise BadExc()
    378                 return False
    379 
    380         a = self.type2test([0, 1, 2, 3])
    381         self.assertRaises(BadExc, a.index, BadCmp())
    382 
    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)
    395         a.remove(0)
    396         self.assertRaises(ValueError, a.index, 2, 0, 4)
    397         self.assertEqual(a, self.type2test([-2, -1, 0, 1, 2]))
    398 
    399         # Test modifying the list during index's iteration
    400         class EvilCmp:
    401             def __init__(self, victim):
    402                 self.victim = victim
    403             def __eq__(self, other):
    404                 del self.victim[:]
    405                 return False
    406         a = self.type2test()
    407         a[:] = [EvilCmp(a) for _ in xrange(100)]
    408         # This used to seg fault before patch #1005778
    409         self.assertRaises(ValueError, a.index, None)
    410 
    411     def test_reverse(self):
    412         u = self.type2test([-2, -1, 0, 1, 2])
    413         u2 = u[:]
    414         u.reverse()
    415         self.assertEqual(u, [2, 1, 0, -1, -2])
    416         u.reverse()
    417         self.assertEqual(u, u2)
    418 
    419         self.assertRaises(TypeError, u.reverse, 42)
    420 
    421     def test_sort(self):
    422         with test_support.check_py3k_warnings(
    423                 ("the cmp argument is not supported", DeprecationWarning)):
    424             self._test_sort()
    425 
    426     def _test_sort(self):
    427         u = self.type2test([1, 0])
    428         u.sort()
    429         self.assertEqual(u, [0, 1])
    430 
    431         u = self.type2test([2,1,0,-1,-2])
    432         u.sort()
    433         self.assertEqual(u, self.type2test([-2,-1,0,1,2]))
    434 
    435         self.assertRaises(TypeError, u.sort, 42, 42)
    436 
    437         def revcmp(a, b):
    438             return cmp(b, a)
    439         u.sort(revcmp)
    440         self.assertEqual(u, self.type2test([2,1,0,-1,-2]))
    441 
    442         # The following dumps core in unpatched Python 1.5:
    443         def myComparison(x,y):
    444             return cmp(x%3, y%7)
    445         z = self.type2test(range(12))
    446         z.sort(myComparison)
    447 
    448         self.assertRaises(TypeError, z.sort, 2)
    449 
    450         def selfmodifyingComparison(x,y):
    451             z.append(1)
    452             return cmp(x, y)
    453         self.assertRaises(ValueError, z.sort, selfmodifyingComparison)
    454 
    455         self.assertRaises(TypeError, z.sort, lambda x, y: 's')
    456 
    457         self.assertRaises(TypeError, z.sort, 42, 42, 42, 42)
    458 
    459     def test_slice(self):
    460         u = self.type2test("spam")
    461         u[:2] = "h"
    462         self.assertEqual(u, list("ham"))
    463 
    464     def test_iadd(self):
    465         super(CommonTest, self).test_iadd()
    466         u = self.type2test([0, 1])
    467         u2 = u
    468         u += [2, 3]
    469         self.assertIs(u, u2)
    470 
    471         u = self.type2test("spam")
    472         u += "eggs"
    473         self.assertEqual(u, self.type2test("spameggs"))
    474 
    475         self.assertRaises(TypeError, u.__iadd__, None)
    476 
    477     def test_imul(self):
    478         u = self.type2test([0, 1])
    479         u *= 3
    480         self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
    481         u *= 0
    482         self.assertEqual(u, self.type2test([]))
    483         s = self.type2test([])
    484         oldid = id(s)
    485         s *= 10
    486         self.assertEqual(id(s), oldid)
    487 
    488     def test_extendedslicing(self):
    489         #  subscript
    490         a = self.type2test([0,1,2,3,4])
    491 
    492         #  deletion
    493         del a[::2]
    494         self.assertEqual(a, self.type2test([1,3]))
    495         a = self.type2test(range(5))
    496         del a[1::2]
    497         self.assertEqual(a, self.type2test([0,2,4]))
    498         a = self.type2test(range(5))
    499         del a[1::-2]
    500         self.assertEqual(a, self.type2test([0,2,3,4]))
    501         a = self.type2test(range(10))
    502         del a[::1000]
    503         self.assertEqual(a, self.type2test([1, 2, 3, 4, 5, 6, 7, 8, 9]))
    504         #  assignment
    505         a = self.type2test(range(10))
    506         a[::2] = [-1]*5
    507         self.assertEqual(a, self.type2test([-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]))
    508         a = self.type2test(range(10))
    509         a[::-4] = [10]*3
    510         self.assertEqual(a, self.type2test([0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]))
    511         a = self.type2test(range(4))
    512         a[::-1] = a
    513         self.assertEqual(a, self.type2test([3, 2, 1, 0]))
    514         a = self.type2test(range(10))
    515         b = a[:]
    516         c = a[:]
    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]))
    525         # test issue7788
    526         a = self.type2test(range(10))
    527         del a[9::1<<333]
    528 
    529     def test_constructor_exception_handling(self):
    530         # Bug #1242657
    531         class F(object):
    532             def __iter__(self):
    533                 raise KeyboardInterrupt
    534         self.assertRaises(KeyboardInterrupt, list, F())
    535