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

Python code coverage for Lib/test/seq_tests.py

#countcontent
1n/a"""
2n/aTests common to tuple, list and UserList.UserList
3n/a"""
4n/a
5n/aimport unittest
6n/aimport sys
7n/aimport pickle
8n/afrom test import support
9n/a
10n/a# Various iterables
11n/a# This is used for checking the constructor (here and in test_deque.py)
12n/adef iterfunc(seqn):
13n/a 'Regular generator'
14n/a for i in seqn:
15n/a yield i
16n/a
17n/aclass Sequence:
18n/a 'Sequence using __getitem__'
19n/a def __init__(self, seqn):
20n/a self.seqn = seqn
21n/a def __getitem__(self, i):
22n/a return self.seqn[i]
23n/a
24n/aclass IterFunc:
25n/a 'Sequence using iterator protocol'
26n/a def __init__(self, seqn):
27n/a self.seqn = seqn
28n/a self.i = 0
29n/a def __iter__(self):
30n/a return self
31n/a def __next__(self):
32n/a if self.i >= len(self.seqn): raise StopIteration
33n/a v = self.seqn[self.i]
34n/a self.i += 1
35n/a return v
36n/a
37n/aclass IterGen:
38n/a 'Sequence using iterator protocol defined with a generator'
39n/a def __init__(self, seqn):
40n/a self.seqn = seqn
41n/a self.i = 0
42n/a def __iter__(self):
43n/a for val in self.seqn:
44n/a yield val
45n/a
46n/aclass IterNextOnly:
47n/a 'Missing __getitem__ and __iter__'
48n/a def __init__(self, seqn):
49n/a self.seqn = seqn
50n/a self.i = 0
51n/a def __next__(self):
52n/a if self.i >= len(self.seqn): raise StopIteration
53n/a v = self.seqn[self.i]
54n/a self.i += 1
55n/a return v
56n/a
57n/aclass IterNoNext:
58n/a 'Iterator missing __next__()'
59n/a def __init__(self, seqn):
60n/a self.seqn = seqn
61n/a self.i = 0
62n/a def __iter__(self):
63n/a return self
64n/a
65n/aclass IterGenExc:
66n/a 'Test propagation of exceptions'
67n/a def __init__(self, seqn):
68n/a self.seqn = seqn
69n/a self.i = 0
70n/a def __iter__(self):
71n/a return self
72n/a def __next__(self):
73n/a 3 // 0
74n/a
75n/aclass IterFuncStop:
76n/a 'Test immediate stop'
77n/a def __init__(self, seqn):
78n/a pass
79n/a def __iter__(self):
80n/a return self
81n/a def __next__(self):
82n/a raise StopIteration
83n/a
84n/afrom itertools import chain
85n/adef itermulti(seqn):
86n/a 'Test multiple tiers of iterators'
87n/a return chain(map(lambda x:x, iterfunc(IterGen(Sequence(seqn)))))
88n/a
89n/aclass LyingTuple(tuple):
90n/a def __iter__(self):
91n/a yield 1
92n/a
93n/aclass LyingList(list):
94n/a def __iter__(self):
95n/a yield 1
96n/a
97n/aclass CommonTest(unittest.TestCase):
98n/a # The type to be tested
99n/a type2test = None
100n/a
101n/a def test_constructors(self):
102n/a l0 = []
103n/a l1 = [0]
104n/a l2 = [0, 1]
105n/a
106n/a u = self.type2test()
107n/a u0 = self.type2test(l0)
108n/a u1 = self.type2test(l1)
109n/a u2 = self.type2test(l2)
110n/a
111n/a uu = self.type2test(u)
112n/a uu0 = self.type2test(u0)
113n/a uu1 = self.type2test(u1)
114n/a uu2 = self.type2test(u2)
115n/a
116n/a v = self.type2test(tuple(u))
117n/a class OtherSeq:
118n/a def __init__(self, initseq):
119n/a self.__data = initseq
120n/a def __len__(self):
121n/a return len(self.__data)
122n/a def __getitem__(self, i):
123n/a return self.__data[i]
124n/a s = OtherSeq(u0)
125n/a v0 = self.type2test(s)
126n/a self.assertEqual(len(v0), len(s))
127n/a
128n/a s = "this is also a sequence"
129n/a vv = self.type2test(s)
130n/a self.assertEqual(len(vv), len(s))
131n/a
132n/a # Create from various iteratables
133n/a for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
134n/a for g in (Sequence, IterFunc, IterGen,
135n/a itermulti, iterfunc):
136n/a self.assertEqual(self.type2test(g(s)), self.type2test(s))
137n/a self.assertEqual(self.type2test(IterFuncStop(s)), self.type2test())
138n/a self.assertEqual(self.type2test(c for c in "123"), self.type2test("123"))
139n/a self.assertRaises(TypeError, self.type2test, IterNextOnly(s))
140n/a self.assertRaises(TypeError, self.type2test, IterNoNext(s))
141n/a self.assertRaises(ZeroDivisionError, self.type2test, IterGenExc(s))
142n/a
143n/a # Issue #23757
144n/a self.assertEqual(self.type2test(LyingTuple((2,))), self.type2test((1,)))
145n/a self.assertEqual(self.type2test(LyingList([2])), self.type2test([1]))
146n/a
147n/a def test_truth(self):
148n/a self.assertFalse(self.type2test())
149n/a self.assertTrue(self.type2test([42]))
150n/a
151n/a def test_getitem(self):
152n/a u = self.type2test([0, 1, 2, 3, 4])
153n/a for i in range(len(u)):
154n/a self.assertEqual(u[i], i)
155n/a self.assertEqual(u[int(i)], i)
156n/a for i in range(-len(u), -1):
157n/a self.assertEqual(u[i], len(u)+i)
158n/a self.assertEqual(u[int(i)], len(u)+i)
159n/a self.assertRaises(IndexError, u.__getitem__, -len(u)-1)
160n/a self.assertRaises(IndexError, u.__getitem__, len(u))
161n/a self.assertRaises(ValueError, u.__getitem__, slice(0,10,0))
162n/a
163n/a u = self.type2test()
164n/a self.assertRaises(IndexError, u.__getitem__, 0)
165n/a self.assertRaises(IndexError, u.__getitem__, -1)
166n/a
167n/a self.assertRaises(TypeError, u.__getitem__)
168n/a
169n/a a = self.type2test([10, 11])
170n/a self.assertEqual(a[0], 10)
171n/a self.assertEqual(a[1], 11)
172n/a self.assertEqual(a[-2], 10)
173n/a self.assertEqual(a[-1], 11)
174n/a self.assertRaises(IndexError, a.__getitem__, -3)
175n/a self.assertRaises(IndexError, a.__getitem__, 3)
176n/a
177n/a def test_getslice(self):
178n/a l = [0, 1, 2, 3, 4]
179n/a u = self.type2test(l)
180n/a
181n/a self.assertEqual(u[0:0], self.type2test())
182n/a self.assertEqual(u[1:2], self.type2test([1]))
183n/a self.assertEqual(u[-2:-1], self.type2test([3]))
184n/a self.assertEqual(u[-1000:1000], u)
185n/a self.assertEqual(u[1000:-1000], self.type2test([]))
186n/a self.assertEqual(u[:], u)
187n/a self.assertEqual(u[1:None], self.type2test([1, 2, 3, 4]))
188n/a self.assertEqual(u[None:3], self.type2test([0, 1, 2]))
189n/a
190n/a # Extended slices
191n/a self.assertEqual(u[::], u)
192n/a self.assertEqual(u[::2], self.type2test([0, 2, 4]))
193n/a self.assertEqual(u[1::2], self.type2test([1, 3]))
194n/a self.assertEqual(u[::-1], self.type2test([4, 3, 2, 1, 0]))
195n/a self.assertEqual(u[::-2], self.type2test([4, 2, 0]))
196n/a self.assertEqual(u[3::-2], self.type2test([3, 1]))
197n/a self.assertEqual(u[3:3:-2], self.type2test([]))
198n/a self.assertEqual(u[3:2:-2], self.type2test([3]))
199n/a self.assertEqual(u[3:1:-2], self.type2test([3]))
200n/a self.assertEqual(u[3:0:-2], self.type2test([3, 1]))
201n/a self.assertEqual(u[::-100], self.type2test([4]))
202n/a self.assertEqual(u[100:-100:], self.type2test([]))
203n/a self.assertEqual(u[-100:100:], u)
204n/a self.assertEqual(u[100:-100:-1], u[::-1])
205n/a self.assertEqual(u[-100:100:-1], self.type2test([]))
206n/a self.assertEqual(u[-100:100:2], self.type2test([0, 2, 4]))
207n/a
208n/a # Test extreme cases with long ints
209n/a a = self.type2test([0,1,2,3,4])
210n/a self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2]))
211n/a self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4]))
212n/a
213n/a def test_contains(self):
214n/a u = self.type2test([0, 1, 2])
215n/a for i in u:
216n/a self.assertIn(i, u)
217n/a for i in min(u)-1, max(u)+1:
218n/a self.assertNotIn(i, u)
219n/a
220n/a self.assertRaises(TypeError, u.__contains__)
221n/a
222n/a def test_contains_fake(self):
223n/a class AllEq:
224n/a # Sequences must use rich comparison against each item
225n/a # (unless "is" is true, or an earlier item answered)
226n/a # So instances of AllEq must be found in all non-empty sequences.
227n/a def __eq__(self, other):
228n/a return True
229n/a __hash__ = None # Can't meet hash invariant requirements
230n/a self.assertNotIn(AllEq(), self.type2test([]))
231n/a self.assertIn(AllEq(), self.type2test([1]))
232n/a
233n/a def test_contains_order(self):
234n/a # Sequences must test in-order. If a rich comparison has side
235n/a # effects, these will be visible to tests against later members.
236n/a # In this test, the "side effect" is a short-circuiting raise.
237n/a class DoNotTestEq(Exception):
238n/a pass
239n/a class StopCompares:
240n/a def __eq__(self, other):
241n/a raise DoNotTestEq
242n/a
243n/a checkfirst = self.type2test([1, StopCompares()])
244n/a self.assertIn(1, checkfirst)
245n/a checklast = self.type2test([StopCompares(), 1])
246n/a self.assertRaises(DoNotTestEq, checklast.__contains__, 1)
247n/a
248n/a def test_len(self):
249n/a self.assertEqual(len(self.type2test()), 0)
250n/a self.assertEqual(len(self.type2test([])), 0)
251n/a self.assertEqual(len(self.type2test([0])), 1)
252n/a self.assertEqual(len(self.type2test([0, 1, 2])), 3)
253n/a
254n/a def test_minmax(self):
255n/a u = self.type2test([0, 1, 2])
256n/a self.assertEqual(min(u), 0)
257n/a self.assertEqual(max(u), 2)
258n/a
259n/a def test_addmul(self):
260n/a u1 = self.type2test([0])
261n/a u2 = self.type2test([0, 1])
262n/a self.assertEqual(u1, u1 + self.type2test())
263n/a self.assertEqual(u1, self.type2test() + u1)
264n/a self.assertEqual(u1 + self.type2test([1]), u2)
265n/a self.assertEqual(self.type2test([-1]) + u1, self.type2test([-1, 0]))
266n/a self.assertEqual(self.type2test(), u2*0)
267n/a self.assertEqual(self.type2test(), 0*u2)
268n/a self.assertEqual(self.type2test(), u2*0)
269n/a self.assertEqual(self.type2test(), 0*u2)
270n/a self.assertEqual(u2, u2*1)
271n/a self.assertEqual(u2, 1*u2)
272n/a self.assertEqual(u2, u2*1)
273n/a self.assertEqual(u2, 1*u2)
274n/a self.assertEqual(u2+u2, u2*2)
275n/a self.assertEqual(u2+u2, 2*u2)
276n/a self.assertEqual(u2+u2, u2*2)
277n/a self.assertEqual(u2+u2, 2*u2)
278n/a self.assertEqual(u2+u2+u2, u2*3)
279n/a self.assertEqual(u2+u2+u2, 3*u2)
280n/a
281n/a class subclass(self.type2test):
282n/a pass
283n/a u3 = subclass([0, 1])
284n/a self.assertEqual(u3, u3*1)
285n/a self.assertIsNot(u3, u3*1)
286n/a
287n/a def test_iadd(self):
288n/a u = self.type2test([0, 1])
289n/a u += self.type2test()
290n/a self.assertEqual(u, self.type2test([0, 1]))
291n/a u += self.type2test([2, 3])
292n/a self.assertEqual(u, self.type2test([0, 1, 2, 3]))
293n/a u += self.type2test([4, 5])
294n/a self.assertEqual(u, self.type2test([0, 1, 2, 3, 4, 5]))
295n/a
296n/a u = self.type2test("spam")
297n/a u += self.type2test("eggs")
298n/a self.assertEqual(u, self.type2test("spameggs"))
299n/a
300n/a def test_imul(self):
301n/a u = self.type2test([0, 1])
302n/a u *= 3
303n/a self.assertEqual(u, self.type2test([0, 1, 0, 1, 0, 1]))
304n/a
305n/a def test_getitemoverwriteiter(self):
306n/a # Verify that __getitem__ overrides are not recognized by __iter__
307n/a class T(self.type2test):
308n/a def __getitem__(self, key):
309n/a return str(key) + '!!!'
310n/a self.assertEqual(next(iter(T((1,2)))), 1)
311n/a
312n/a def test_repeat(self):
313n/a for m in range(4):
314n/a s = tuple(range(m))
315n/a for n in range(-3, 5):
316n/a self.assertEqual(self.type2test(s*n), self.type2test(s)*n)
317n/a self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
318n/a self.assertEqual(id(s), id(s*1))
319n/a
320n/a def test_bigrepeat(self):
321n/a if sys.maxsize <= 2147483647:
322n/a x = self.type2test([0])
323n/a x *= 2**16
324n/a self.assertRaises(MemoryError, x.__mul__, 2**16)
325n/a if hasattr(x, '__imul__'):
326n/a self.assertRaises(MemoryError, x.__imul__, 2**16)
327n/a
328n/a def test_subscript(self):
329n/a a = self.type2test([10, 11])
330n/a self.assertEqual(a.__getitem__(0), 10)
331n/a self.assertEqual(a.__getitem__(1), 11)
332n/a self.assertEqual(a.__getitem__(-2), 10)
333n/a self.assertEqual(a.__getitem__(-1), 11)
334n/a self.assertRaises(IndexError, a.__getitem__, -3)
335n/a self.assertRaises(IndexError, a.__getitem__, 3)
336n/a self.assertEqual(a.__getitem__(slice(0,1)), self.type2test([10]))
337n/a self.assertEqual(a.__getitem__(slice(1,2)), self.type2test([11]))
338n/a self.assertEqual(a.__getitem__(slice(0,2)), self.type2test([10, 11]))
339n/a self.assertEqual(a.__getitem__(slice(0,3)), self.type2test([10, 11]))
340n/a self.assertEqual(a.__getitem__(slice(3,5)), self.type2test([]))
341n/a self.assertRaises(ValueError, a.__getitem__, slice(0, 10, 0))
342n/a self.assertRaises(TypeError, a.__getitem__, 'x')
343n/a
344n/a def test_count(self):
345n/a a = self.type2test([0, 1, 2])*3
346n/a self.assertEqual(a.count(0), 3)
347n/a self.assertEqual(a.count(1), 3)
348n/a self.assertEqual(a.count(3), 0)
349n/a
350n/a self.assertRaises(TypeError, a.count)
351n/a
352n/a class BadExc(Exception):
353n/a pass
354n/a
355n/a class BadCmp:
356n/a def __eq__(self, other):
357n/a if other == 2:
358n/a raise BadExc()
359n/a return False
360n/a
361n/a self.assertRaises(BadExc, a.count, BadCmp())
362n/a
363n/a def test_index(self):
364n/a u = self.type2test([0, 1])
365n/a self.assertEqual(u.index(0), 0)
366n/a self.assertEqual(u.index(1), 1)
367n/a self.assertRaises(ValueError, u.index, 2)
368n/a
369n/a u = self.type2test([-2, -1, 0, 0, 1, 2])
370n/a self.assertEqual(u.count(0), 2)
371n/a self.assertEqual(u.index(0), 2)
372n/a self.assertEqual(u.index(0, 2), 2)
373n/a self.assertEqual(u.index(-2, -10), 0)
374n/a self.assertEqual(u.index(0, 3), 3)
375n/a self.assertEqual(u.index(0, 3, 4), 3)
376n/a self.assertRaises(ValueError, u.index, 2, 0, -10)
377n/a
378n/a self.assertRaises(TypeError, u.index)
379n/a
380n/a class BadExc(Exception):
381n/a pass
382n/a
383n/a class BadCmp:
384n/a def __eq__(self, other):
385n/a if other == 2:
386n/a raise BadExc()
387n/a return False
388n/a
389n/a a = self.type2test([0, 1, 2, 3])
390n/a self.assertRaises(BadExc, a.index, BadCmp())
391n/a
392n/a a = self.type2test([-2, -1, 0, 0, 1, 2])
393n/a self.assertEqual(a.index(0), 2)
394n/a self.assertEqual(a.index(0, 2), 2)
395n/a self.assertEqual(a.index(0, -4), 2)
396n/a self.assertEqual(a.index(-2, -10), 0)
397n/a self.assertEqual(a.index(0, 3), 3)
398n/a self.assertEqual(a.index(0, -3), 3)
399n/a self.assertEqual(a.index(0, 3, 4), 3)
400n/a self.assertEqual(a.index(0, -3, -2), 3)
401n/a self.assertEqual(a.index(0, -4*sys.maxsize, 4*sys.maxsize), 2)
402n/a self.assertRaises(ValueError, a.index, 0, 4*sys.maxsize,-4*sys.maxsize)
403n/a self.assertRaises(ValueError, a.index, 2, 0, -10)
404n/a
405n/a def test_pickle(self):
406n/a lst = self.type2test([4, 5, 6, 7])
407n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
408n/a lst2 = pickle.loads(pickle.dumps(lst, proto))
409n/a self.assertEqual(lst2, lst)
410n/a self.assertNotEqual(id(lst2), id(lst))
411n/a
412n/a def test_free_after_iterating(self):
413n/a support.check_free_after_iterating(self, iter, self.type2test)
414n/a support.check_free_after_iterating(self, reversed, self.type2test)