4 from test
import test_support
7 'Sequence using __getitem__'
8 def __init__(self
, seqn
):
10 def __getitem__(self
, i
):
14 'Sequence using iterator protocol'
15 def __init__(self
, seqn
):
21 if self
.i
>= len(self
.seqn
): raise StopIteration
27 'Sequence using iterator protocol defined with a generator'
28 def __init__(self
, seqn
):
36 'Missing __getitem__ and __iter__'
37 def __init__(self
, seqn
):
41 if self
.i
>= len(self
.seqn
): raise StopIteration
47 'Test propagation of exceptions'
48 def __init__(self
, seqn
):
57 'Iterator missing next()'
58 def __init__(self
, seqn
):
64 class EnumerateTestCase(unittest
.TestCase
):
67 seq
, res
= 'abc', [(0,'a'), (1,'b'), (2,'c')]
69 def test_basicfunction(self
):
70 self
.assertEqual(type(self
.enum(self
.seq
)), self
.enum
)
71 e
= self
.enum(self
.seq
)
72 self
.assertEqual(iter(e
), e
)
73 self
.assertEqual(list(self
.enum(self
.seq
)), self
.res
)
76 def test_getitemseqn(self
):
77 self
.assertEqual(list(self
.enum(G(self
.seq
))), self
.res
)
79 self
.assertRaises(StopIteration, e
.next
)
81 def test_iteratorseqn(self
):
82 self
.assertEqual(list(self
.enum(I(self
.seq
))), self
.res
)
84 self
.assertRaises(StopIteration, e
.next
)
86 def test_iteratorgenerator(self
):
87 self
.assertEqual(list(self
.enum(Ig(self
.seq
))), self
.res
)
89 self
.assertRaises(StopIteration, e
.next
)
91 def test_noniterable(self
):
92 self
.assertRaises(TypeError, self
.enum
, X(self
.seq
))
94 def test_illformediterable(self
):
95 self
.assertRaises(TypeError, list, self
.enum(N(self
.seq
)))
97 def test_exception_propagation(self
):
98 self
.assertRaises(ZeroDivisionError, list, self
.enum(E(self
.seq
)))
100 def test_argumentcheck(self
):
101 self
.assertRaises(TypeError, self
.enum
) # no arguments
102 self
.assertRaises(TypeError, self
.enum
, 1) # wrong type (not iterable)
103 self
.assertRaises(TypeError, self
.enum
, 'abc', 'a') # wrong type
104 self
.assertRaises(TypeError, self
.enum
, 'abc', 2, 3) # too many arguments
106 @test_support.cpython_only
107 def test_tuple_reuse(self
):
108 # Tests an implementation detail where tuple is reused
109 # whenever nothing else holds a reference to it
110 self
.assertEqual(len(set(map(id, list(enumerate(self
.seq
))))), len(self
.seq
))
111 self
.assertEqual(len(set(map(id, enumerate(self
.seq
)))), min(1,len(self
.seq
)))
113 class MyEnum(enumerate):
116 class SubclassTestCase(EnumerateTestCase
):
120 class TestEmpty(EnumerateTestCase
):
124 class TestBig(EnumerateTestCase
):
126 seq
= range(10,20000,2)
127 res
= zip(range(20000), seq
)
129 class TestReversed(unittest
.TestCase
):
131 def test_simple(self
):
133 def __getitem__(self
, i
):
139 for data
in 'abc', range(5), tuple(enumerate('abc')), A(), xrange(1,17,5):
140 self
.assertEqual(list(data
)[::-1], list(reversed(data
)))
141 self
.assertRaises(TypeError, reversed, {})
142 # don't allow keyword arguments
143 self
.assertRaises(TypeError, reversed, [], a
=1)
145 def test_classic_class(self
):
147 def __reversed__(self
):
149 self
.assertEqual(list(reversed(A())), [2, 1])
151 def test_xrange_optimization(self
):
153 self
.assertEqual(type(reversed(x
)), type(iter(x
)))
155 @test_support.cpython_only
157 # This is an implementation detail, not an interface requirement
158 from test
.test_iterlen
import len
159 for s
in ('hello', tuple('hello'), list('hello'), xrange(5)):
160 self
.assertEqual(len(reversed(s
)), len(s
))
163 self
.assertEqual(len(r
), 0)
164 class SeqWithWeirdLen
:
170 raise ZeroDivisionError
171 def __getitem__(self
, index
):
173 r
= reversed(SeqWithWeirdLen())
174 self
.assertRaises(ZeroDivisionError, len, r
)
181 def __getitem__(self
, index
):
188 self
.assertRaises(TypeError, reversed)
189 self
.assertRaises(TypeError, reversed, [], 'extra')
191 def test_bug1229429(self
):
192 # this bug was never in reversed, it was in
193 # PyObject_CallMethod, and reversed_new calls that sometimes.
194 if not hasattr(sys
, "getrefcount"):
198 r
= f
.__reversed
__ = object()
199 rc
= sys
.getrefcount(r
)
206 self
.fail("non-callable __reversed__ didn't raise!")
207 self
.assertEqual(rc
, sys
.getrefcount(r
))
209 def test_objmethods(self
):
210 # Objects must have __len__() and __getitem__() implemented.
212 def __getitem__(self
): return 1
214 self
.assertRaises(TypeError, reversed, nl
)
216 class NoGetItem(object):
217 def __len__(self
): return 2
219 self
.assertRaises(TypeError, reversed, ngi
)
222 class EnumerateStartTestCase(EnumerateTestCase
):
224 def test_basicfunction(self
):
225 e
= self
.enum(self
.seq
)
226 self
.assertEqual(iter(e
), e
)
227 self
.assertEqual(list(self
.enum(self
.seq
)), self
.res
)
230 class TestStart(EnumerateStartTestCase
):
232 enum
= lambda self
, i
: enumerate(i
, start
=11)
233 seq
, res
= 'abc', [(11, 'a'), (12, 'b'), (13, 'c')]
236 class TestLongStart(EnumerateStartTestCase
):
238 enum
= lambda self
, i
: enumerate(i
, start
=sys
.maxint
+1)
239 seq
, res
= 'abc', [(sys
.maxint
+1,'a'), (sys
.maxint
+2,'b'),
243 def test_main(verbose
=None):
244 test_support
.run_unittest(__name__
)
246 # verify reference counting
247 if verbose
and hasattr(sys
, "gettotalrefcount"):
249 for i
in xrange(len(counts
)):
250 test_support
.run_unittest(__name__
)
251 counts
[i
] = sys
.gettotalrefcount()
254 if __name__
== "__main__":
255 test_main(verbose
=True)