10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport unittest
20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport sys
30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test import test_support
50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass G:
70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    'Sequence using __getitem__'
80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __init__(self, seqn):
90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.seqn = seqn
100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __getitem__(self, i):
110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return self.seqn[i]
120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass I:
140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    'Sequence using iterator protocol'
150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __init__(self, seqn):
160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.seqn = seqn
170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i = 0
180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __iter__(self):
190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return self
200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def next(self):
210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if self.i >= len(self.seqn): raise StopIteration
220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        v = self.seqn[self.i]
230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i += 1
240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return v
250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass Ig:
270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    'Sequence using iterator protocol defined with a generator'
280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __init__(self, seqn):
290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.seqn = seqn
300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i = 0
310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __iter__(self):
320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for val in self.seqn:
330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            yield val
340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass X:
360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    'Missing __getitem__ and __iter__'
370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __init__(self, seqn):
380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.seqn = seqn
390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i = 0
400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def next(self):
410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if self.i >= len(self.seqn): raise StopIteration
420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        v = self.seqn[self.i]
430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i += 1
440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return v
450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass E:
470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    'Test propagation of exceptions'
480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __init__(self, seqn):
490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.seqn = seqn
500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i = 0
510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __iter__(self):
520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return self
530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def next(self):
540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        3 // 0
550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass N:
570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    'Iterator missing next()'
580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __init__(self, seqn):
590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.seqn = seqn
600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.i = 0
610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def __iter__(self):
620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        return self
630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass EnumerateTestCase(unittest.TestCase):
650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    enum = enumerate
670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    seq, res = 'abc', [(0,'a'), (1,'b'), (2,'c')]
680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_basicfunction(self):
700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(type(self.enum(self.seq)), self.enum)
710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        e = self.enum(self.seq)
720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(iter(e), e)
730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(list(self.enum(self.seq)), self.res)
740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.enum.__doc__
750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_getitemseqn(self):
770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(list(self.enum(G(self.seq))), self.res)
780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        e = self.enum(G(''))
790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(StopIteration, e.next)
800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_iteratorseqn(self):
820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(list(self.enum(I(self.seq))), self.res)
830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        e = self.enum(I(''))
840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(StopIteration, e.next)
850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_iteratorgenerator(self):
870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(list(self.enum(Ig(self.seq))), self.res)
880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        e = self.enum(Ig(''))
890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(StopIteration, e.next)
900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_noniterable(self):
920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, self.enum, X(self.seq))
930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_illformediterable(self):
950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, list, self.enum(N(self.seq)))
960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_exception_propagation(self):
980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(ZeroDivisionError, list, self.enum(E(self.seq)))
990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_argumentcheck(self):
1010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, self.enum) # no arguments
1020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, self.enum, 1) # wrong type (not iterable)
1030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, self.enum, 'abc', 'a') # wrong type
1040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, self.enum, 'abc', 2, 3) # too many arguments
1050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    @test_support.cpython_only
1070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_tuple_reuse(self):
1080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Tests an implementation detail where tuple is reused
1090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # whenever nothing else holds a reference to it
1100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(len(set(map(id, list(enumerate(self.seq))))), len(self.seq))
1110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(len(set(map(id, enumerate(self.seq)))), min(1,len(self.seq)))
1120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass MyEnum(enumerate):
1140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    pass
1150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass SubclassTestCase(EnumerateTestCase):
1170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    enum = MyEnum
1190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TestEmpty(EnumerateTestCase):
1210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    seq, res = '', []
1230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TestBig(EnumerateTestCase):
1250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    seq = range(10,20000,2)
1270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    res = zip(range(20000), seq)
1280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TestReversed(unittest.TestCase):
1300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_simple(self):
1320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class A:
1330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __getitem__(self, i):
1340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                if i < 5:
1350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                    return str(i)
1360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                raise StopIteration
1370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __len__(self):
1380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                return 5
1390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for data in 'abc', range(5), tuple(enumerate('abc')), A(), xrange(1,17,5):
1400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(list(data)[::-1], list(reversed(data)))
1410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, reversed, {})
1420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # don't allow keyword arguments
1430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, reversed, [], a=1)
1440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_classic_class(self):
1460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class A:
1470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __reversed__(self):
1480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                return [2, 1]
1490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(list(reversed(A())), [2, 1])
1500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_xrange_optimization(self):
1520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        x = xrange(1)
1530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(type(reversed(x)), type(iter(x)))
1540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    @test_support.cpython_only
1560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_len(self):
1570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # This is an implementation detail, not an interface requirement
1580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        from test.test_iterlen import len
1590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for s in ('hello', tuple('hello'), list('hello'), xrange(5)):
1600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(len(reversed(s)), len(s))
1610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            r = reversed(s)
1620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            list(r)
1630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            self.assertEqual(len(r), 0)
1640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class SeqWithWeirdLen:
1650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            called = False
1660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __len__(self):
1670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                if not self.called:
1680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                    self.called = True
1690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                    return 10
1700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                raise ZeroDivisionError
1710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __getitem__(self, index):
1720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                return index
1730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        r = reversed(SeqWithWeirdLen())
1740c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(ZeroDivisionError, len, r)
1750c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1760c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1770c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_gc(self):
1780c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class Seq:
1790c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __len__(self):
1800c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                return 10
1810c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __getitem__(self, index):
1820c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                return index
1830c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        s = Seq()
1840c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        r = reversed(s)
1850c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        s.r = r
1860c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1870c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_args(self):
1880c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, reversed)
1890c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, reversed, [], 'extra')
1900c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
1910c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_bug1229429(self):
1920c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # this bug was never in reversed, it was in
1930c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # PyObject_CallMethod, and reversed_new calls that sometimes.
1940c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        if not hasattr(sys, "getrefcount"):
1950c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            return
1960c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        def f():
1970c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
1980c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        r = f.__reversed__ = object()
1990c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        rc = sys.getrefcount(r)
2000c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for i in range(10):
2010c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            try:
2020c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                reversed(f)
2030c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            except TypeError:
2040c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                pass
2050c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            else:
2060c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                self.fail("non-callable __reversed__ didn't raise!")
2070c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(rc, sys.getrefcount(r))
2080c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2090c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_objmethods(self):
2100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # Objects must have __len__() and __getitem__() implemented.
2110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class NoLen(object):
2120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __getitem__(self): return 1
2130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        nl = NoLen()
2140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, reversed, nl)
2150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class NoGetItem(object):
2170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            def __len__(self): return 2
2180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        ngi = NoGetItem()
2190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, reversed, ngi)
2200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass EnumerateStartTestCase(EnumerateTestCase):
2230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_basicfunction(self):
2250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        e = self.enum(self.seq)
2260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(iter(e), e)
2270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(list(self.enum(self.seq)), self.res)
2280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TestStart(EnumerateStartTestCase):
2310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    enum = lambda self, i: enumerate(i, start=11)
2330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    seq, res = 'abc', [(11, 'a'), (12, 'b'), (13, 'c')]
2340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TestLongStart(EnumerateStartTestCase):
2370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    enum = lambda self, i: enumerate(i, start=sys.maxint+1)
2390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    seq, res = 'abc', [(sys.maxint+1,'a'), (sys.maxint+2,'b'),
2400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                       (sys.maxint+3,'c')]
2410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main(verbose=None):
2440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    test_support.run_unittest(__name__)
2450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    # verify reference counting
2470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    if verbose and hasattr(sys, "gettotalrefcount"):
2480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        counts = [None] * 5
2490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        for i in xrange(len(counts)):
2500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            test_support.run_unittest(__name__)
2510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            counts[i] = sys.gettotalrefcount()
2520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        print counts
2530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
2540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == "__main__":
2550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    test_main(verbose=True)
256