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

Python code coverage for Lib/test/test_list.py

#countcontent
1n/aimport sys
2n/afrom test import list_tests
3n/aimport pickle
4n/aimport unittest
5n/a
6n/aclass ListTest(list_tests.CommonTest):
7n/a type2test = list
8n/a
9n/a def test_basic(self):
10n/a self.assertEqual(list([]), [])
11n/a l0_3 = [0, 1, 2, 3]
12n/a l0_3_bis = list(l0_3)
13n/a self.assertEqual(l0_3, l0_3_bis)
14n/a self.assertTrue(l0_3 is not l0_3_bis)
15n/a self.assertEqual(list(()), [])
16n/a self.assertEqual(list((0, 1, 2, 3)), [0, 1, 2, 3])
17n/a self.assertEqual(list(''), [])
18n/a self.assertEqual(list('spam'), ['s', 'p', 'a', 'm'])
19n/a
20n/a if sys.maxsize == 0x7fffffff:
21n/a # This test can currently only work on 32-bit machines.
22n/a # XXX If/when PySequence_Length() returns a ssize_t, it should be
23n/a # XXX re-enabled.
24n/a # Verify clearing of bug #556025.
25n/a # This assumes that the max data size (sys.maxint) == max
26n/a # address size this also assumes that the address size is at
27n/a # least 4 bytes with 8 byte addresses, the bug is not well
28n/a # tested
29n/a #
30n/a # Note: This test is expected to SEGV under Cygwin 1.3.12 or
31n/a # earlier due to a newlib bug. See the following mailing list
32n/a # thread for the details:
33n/a
34n/a # http://sources.redhat.com/ml/newlib/2002/msg00369.html
35n/a self.assertRaises(MemoryError, list, range(sys.maxsize // 2))
36n/a
37n/a # This code used to segfault in Py2.4a3
38n/a x = []
39n/a x.extend(-y for y in x)
40n/a self.assertEqual(x, [])
41n/a
42n/a def test_truth(self):
43n/a super().test_truth()
44n/a self.assertTrue(not [])
45n/a self.assertTrue([42])
46n/a
47n/a def test_identity(self):
48n/a self.assertTrue([] is not [])
49n/a
50n/a def test_len(self):
51n/a super().test_len()
52n/a self.assertEqual(len([]), 0)
53n/a self.assertEqual(len([0]), 1)
54n/a self.assertEqual(len([0, 1, 2]), 3)
55n/a
56n/a def test_overflow(self):
57n/a lst = [4, 5, 6, 7]
58n/a n = int((sys.maxsize*2+2) // len(lst))
59n/a def mul(a, b): return a * b
60n/a def imul(a, b): a *= b
61n/a self.assertRaises((MemoryError, OverflowError), mul, lst, n)
62n/a self.assertRaises((MemoryError, OverflowError), imul, lst, n)
63n/a
64n/a def test_repr_large(self):
65n/a # Check the repr of large list objects
66n/a def check(n):
67n/a l = [0] * n
68n/a s = repr(l)
69n/a self.assertEqual(s,
70n/a '[' + ', '.join(['0'] * n) + ']')
71n/a check(10) # check our checking code
72n/a check(1000000)
73n/a
74n/a def test_iterator_pickle(self):
75n/a orig = self.type2test([4, 5, 6, 7])
76n/a data = [10, 11, 12, 13, 14, 15]
77n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
78n/a # initial iterator
79n/a itorig = iter(orig)
80n/a d = pickle.dumps((itorig, orig), proto)
81n/a it, a = pickle.loads(d)
82n/a a[:] = data
83n/a self.assertEqual(type(it), type(itorig))
84n/a self.assertEqual(list(it), data)
85n/a
86n/a # running iterator
87n/a next(itorig)
88n/a d = pickle.dumps((itorig, orig), proto)
89n/a it, a = pickle.loads(d)
90n/a a[:] = data
91n/a self.assertEqual(type(it), type(itorig))
92n/a self.assertEqual(list(it), data[1:])
93n/a
94n/a # empty iterator
95n/a for i in range(1, len(orig)):
96n/a next(itorig)
97n/a d = pickle.dumps((itorig, orig), proto)
98n/a it, a = pickle.loads(d)
99n/a a[:] = data
100n/a self.assertEqual(type(it), type(itorig))
101n/a self.assertEqual(list(it), data[len(orig):])
102n/a
103n/a # exhausted iterator
104n/a self.assertRaises(StopIteration, next, itorig)
105n/a d = pickle.dumps((itorig, orig), proto)
106n/a it, a = pickle.loads(d)
107n/a a[:] = data
108n/a self.assertEqual(list(it), [])
109n/a
110n/a def test_reversed_pickle(self):
111n/a orig = self.type2test([4, 5, 6, 7])
112n/a data = [10, 11, 12, 13, 14, 15]
113n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
114n/a # initial iterator
115n/a itorig = reversed(orig)
116n/a d = pickle.dumps((itorig, orig), proto)
117n/a it, a = pickle.loads(d)
118n/a a[:] = data
119n/a self.assertEqual(type(it), type(itorig))
120n/a self.assertEqual(list(it), data[len(orig)-1::-1])
121n/a
122n/a # running iterator
123n/a next(itorig)
124n/a d = pickle.dumps((itorig, orig), proto)
125n/a it, a = pickle.loads(d)
126n/a a[:] = data
127n/a self.assertEqual(type(it), type(itorig))
128n/a self.assertEqual(list(it), data[len(orig)-2::-1])
129n/a
130n/a # empty iterator
131n/a for i in range(1, len(orig)):
132n/a next(itorig)
133n/a d = pickle.dumps((itorig, orig), proto)
134n/a it, a = pickle.loads(d)
135n/a a[:] = data
136n/a self.assertEqual(type(it), type(itorig))
137n/a self.assertEqual(list(it), [])
138n/a
139n/a # exhausted iterator
140n/a self.assertRaises(StopIteration, next, itorig)
141n/a d = pickle.dumps((itorig, orig), proto)
142n/a it, a = pickle.loads(d)
143n/a a[:] = data
144n/a self.assertEqual(list(it), [])
145n/a
146n/a def test_no_comdat_folding(self):
147n/a # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
148n/a # optimization causes failures in code that relies on distinct
149n/a # function addresses.
150n/a class L(list): pass
151n/a with self.assertRaises(TypeError):
152n/a (3,) + L([1,2])
153n/a
154n/aif __name__ == "__main__":
155n/a unittest.main()