1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass G: 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'Sequence using __getitem__' 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, seqn): 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.seqn = seqn 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __getitem__(self, i): 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.seqn[i] 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass I: 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'Sequence using iterator protocol' 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, seqn): 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.seqn = seqn 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i = 0 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __iter__(self): 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def next(self): 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.i >= len(self.seqn): raise StopIteration 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep v = self.seqn[self.i] 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i += 1 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return v 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Ig: 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'Sequence using iterator protocol defined with a generator' 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, seqn): 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.seqn = seqn 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i = 0 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __iter__(self): 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for val in self.seqn: 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep yield val 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass X: 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'Missing __getitem__ and __iter__' 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, seqn): 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.seqn = seqn 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i = 0 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def next(self): 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.i >= len(self.seqn): raise StopIteration 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep v = self.seqn[self.i] 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i += 1 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return v 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass E: 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'Test propagation of exceptions' 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, seqn): 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.seqn = seqn 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i = 0 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __iter__(self): 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def next(self): 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 3 // 0 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass N: 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'Iterator missing next()' 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, seqn): 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.seqn = seqn 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.i = 0 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __iter__(self): 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass EnumerateTestCase(unittest.TestCase): 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep enum = enumerate 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep seq, res = 'abc', [(0,'a'), (1,'b'), (2,'c')] 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_basicfunction(self): 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(self.enum(self.seq)), self.enum) 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e = self.enum(self.seq) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(iter(e), e) 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(self.enum(self.seq)), self.res) 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.enum.__doc__ 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_getitemseqn(self): 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(self.enum(G(self.seq))), self.res) 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e = self.enum(G('')) 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(StopIteration, e.next) 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_iteratorseqn(self): 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(self.enum(I(self.seq))), self.res) 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e = self.enum(I('')) 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(StopIteration, e.next) 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_iteratorgenerator(self): 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(self.enum(Ig(self.seq))), self.res) 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e = self.enum(Ig('')) 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(StopIteration, e.next) 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_noniterable(self): 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.enum, X(self.seq)) 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_illformediterable(self): 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, list, self.enum(N(self.seq))) 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_exception_propagation(self): 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ZeroDivisionError, list, self.enum(E(self.seq))) 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_argumentcheck(self): 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.enum) # no arguments 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.enum, 1) # wrong type (not iterable) 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.enum, 'abc', 'a') # wrong type 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, self.enum, 'abc', 2, 3) # too many arguments 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep @test_support.cpython_only 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_tuple_reuse(self): 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Tests an implementation detail where tuple is reused 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # whenever nothing else holds a reference to it 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(set(map(id, list(enumerate(self.seq))))), len(self.seq)) 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(set(map(id, enumerate(self.seq)))), min(1,len(self.seq))) 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyEnum(enumerate): 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SubclassTestCase(EnumerateTestCase): 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep enum = MyEnum 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestEmpty(EnumerateTestCase): 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep seq, res = '', [] 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestBig(EnumerateTestCase): 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep seq = range(10,20000,2) 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep res = zip(range(20000), seq) 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestReversed(unittest.TestCase): 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_simple(self): 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class A: 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __getitem__(self, i): 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if i < 5: 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return str(i) 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise StopIteration 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __len__(self): 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 5 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for data in 'abc', range(5), tuple(enumerate('abc')), A(), xrange(1,17,5): 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(data)[::-1], list(reversed(data))) 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, reversed, {}) 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # don't allow keyword arguments 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, reversed, [], a=1) 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_classic_class(self): 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class A: 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __reversed__(self): 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return [2, 1] 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(reversed(A())), [2, 1]) 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_xrange_optimization(self): 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep x = xrange(1) 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(type(reversed(x)), type(iter(x))) 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep @test_support.cpython_only 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_len(self): 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This is an implementation detail, not an interface requirement 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep from test.test_iterlen import len 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for s in ('hello', tuple('hello'), list('hello'), xrange(5)): 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(reversed(s)), len(s)) 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep r = reversed(s) 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep list(r) 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(r), 0) 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class SeqWithWeirdLen: 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep called = False 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __len__(self): 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not self.called: 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.called = True 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 10 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ZeroDivisionError 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __getitem__(self, index): 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return index 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep r = reversed(SeqWithWeirdLen()) 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(ZeroDivisionError, len, r) 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_gc(self): 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class Seq: 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __len__(self): 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 10 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __getitem__(self, index): 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return index 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s = Seq() 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep r = reversed(s) 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s.r = r 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_args(self): 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, reversed) 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, reversed, [], 'extra') 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_bug1229429(self): 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # this bug was never in reversed, it was in 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # PyObject_CallMethod, and reversed_new calls that sometimes. 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not hasattr(sys, "getrefcount"): 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def f(): 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep r = f.__reversed__ = object() 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep rc = sys.getrefcount(r) 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in range(10): 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep reversed(f) 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except TypeError: 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("non-callable __reversed__ didn't raise!") 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(rc, sys.getrefcount(r)) 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_objmethods(self): 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Objects must have __len__() and __getitem__() implemented. 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class NoLen(object): 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __getitem__(self): return 1 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep nl = NoLen() 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, reversed, nl) 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class NoGetItem(object): 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __len__(self): return 2 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ngi = NoGetItem() 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, reversed, ngi) 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass EnumerateStartTestCase(EnumerateTestCase): 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_basicfunction(self): 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e = self.enum(self.seq) 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(iter(e), e) 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(self.enum(self.seq)), self.res) 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestStart(EnumerateStartTestCase): 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep enum = lambda self, i: enumerate(i, start=11) 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep seq, res = 'abc', [(11, 'a'), (12, 'b'), (13, 'c')] 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestLongStart(EnumerateStartTestCase): 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep enum = lambda self, i: enumerate(i, start=sys.maxint+1) 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep seq, res = 'abc', [(sys.maxint+1,'a'), (sys.maxint+2,'b'), 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep (sys.maxint+3,'c')] 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(verbose=None): 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_unittest(__name__) 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # verify reference counting 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if verbose and hasattr(sys, "gettotalrefcount"): 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep counts = [None] * 5 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in xrange(len(counts)): 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_unittest(__name__) 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep counts[i] = sys.gettotalrefcount() 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print counts 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main(verbose=True) 256