ยปCore Development>Code coverage>Lib/test/test_enumerate.py

Python code coverage for Lib/test/test_enumerate.py

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