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

Python code coverage for Lib/test/test_tuple.py

#countcontent
1n/afrom test import support, seq_tests
2n/aimport unittest
3n/a
4n/aimport gc
5n/aimport pickle
6n/a
7n/aclass TupleTest(seq_tests.CommonTest):
8n/a type2test = tuple
9n/a
10n/a def test_getitem_error(self):
11n/a msg = "tuple indices must be integers or slices"
12n/a with self.assertRaisesRegex(TypeError, msg):
13n/a ()['a']
14n/a
15n/a def test_constructors(self):
16n/a super().test_constructors()
17n/a # calling built-in types without argument must return empty
18n/a self.assertEqual(tuple(), ())
19n/a t0_3 = (0, 1, 2, 3)
20n/a t0_3_bis = tuple(t0_3)
21n/a self.assertTrue(t0_3 is t0_3_bis)
22n/a self.assertEqual(tuple([]), ())
23n/a self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
24n/a self.assertEqual(tuple(''), ())
25n/a self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
26n/a
27n/a def test_truth(self):
28n/a super().test_truth()
29n/a self.assertTrue(not ())
30n/a self.assertTrue((42, ))
31n/a
32n/a def test_len(self):
33n/a super().test_len()
34n/a self.assertEqual(len(()), 0)
35n/a self.assertEqual(len((0,)), 1)
36n/a self.assertEqual(len((0, 1, 2)), 3)
37n/a
38n/a def test_iadd(self):
39n/a super().test_iadd()
40n/a u = (0, 1)
41n/a u2 = u
42n/a u += (2, 3)
43n/a self.assertTrue(u is not u2)
44n/a
45n/a def test_imul(self):
46n/a super().test_imul()
47n/a u = (0, 1)
48n/a u2 = u
49n/a u *= 3
50n/a self.assertTrue(u is not u2)
51n/a
52n/a def test_tupleresizebug(self):
53n/a # Check that a specific bug in _PyTuple_Resize() is squashed.
54n/a def f():
55n/a for i in range(1000):
56n/a yield i
57n/a self.assertEqual(list(tuple(f())), list(range(1000)))
58n/a
59n/a def test_hash(self):
60n/a # See SF bug 942952: Weakness in tuple hash
61n/a # The hash should:
62n/a # be non-commutative
63n/a # should spread-out closely spaced values
64n/a # should not exhibit cancellation in tuples like (x,(x,y))
65n/a # should be distinct from element hashes: hash(x)!=hash((x,))
66n/a # This test exercises those cases.
67n/a # For a pure random hash and N=50, the expected number of occupied
68n/a # buckets when tossing 252,600 balls into 2**32 buckets
69n/a # is 252,592.6, or about 7.4 expected collisions. The
70n/a # standard deviation is 2.73. On a box with 64-bit hash
71n/a # codes, no collisions are expected. Here we accept no
72n/a # more than 15 collisions. Any worse and the hash function
73n/a # is sorely suspect.
74n/a
75n/a N=50
76n/a base = list(range(N))
77n/a xp = [(i, j) for i in base for j in base]
78n/a inps = base + [(i, j) for i in base for j in xp] + \
79n/a [(i, j) for i in xp for j in base] + xp + list(zip(base))
80n/a collisions = len(inps) - len(set(map(hash, inps)))
81n/a self.assertTrue(collisions <= 15)
82n/a
83n/a def test_repr(self):
84n/a l0 = tuple()
85n/a l2 = (0, 1, 2)
86n/a a0 = self.type2test(l0)
87n/a a2 = self.type2test(l2)
88n/a
89n/a self.assertEqual(str(a0), repr(l0))
90n/a self.assertEqual(str(a2), repr(l2))
91n/a self.assertEqual(repr(a0), "()")
92n/a self.assertEqual(repr(a2), "(0, 1, 2)")
93n/a
94n/a def _not_tracked(self, t):
95n/a # Nested tuples can take several collections to untrack
96n/a gc.collect()
97n/a gc.collect()
98n/a self.assertFalse(gc.is_tracked(t), t)
99n/a
100n/a def _tracked(self, t):
101n/a self.assertTrue(gc.is_tracked(t), t)
102n/a gc.collect()
103n/a gc.collect()
104n/a self.assertTrue(gc.is_tracked(t), t)
105n/a
106n/a @support.cpython_only
107n/a def test_track_literals(self):
108n/a # Test GC-optimization of tuple literals
109n/a x, y, z = 1.5, "a", []
110n/a
111n/a self._not_tracked(())
112n/a self._not_tracked((1,))
113n/a self._not_tracked((1, 2))
114n/a self._not_tracked((1, 2, "a"))
115n/a self._not_tracked((1, 2, (None, True, False, ()), int))
116n/a self._not_tracked((object(),))
117n/a self._not_tracked(((1, x), y, (2, 3)))
118n/a
119n/a # Tuples with mutable elements are always tracked, even if those
120n/a # elements are not tracked right now.
121n/a self._tracked(([],))
122n/a self._tracked(([1],))
123n/a self._tracked(({},))
124n/a self._tracked((set(),))
125n/a self._tracked((x, y, z))
126n/a
127n/a def check_track_dynamic(self, tp, always_track):
128n/a x, y, z = 1.5, "a", []
129n/a
130n/a check = self._tracked if always_track else self._not_tracked
131n/a check(tp())
132n/a check(tp([]))
133n/a check(tp(set()))
134n/a check(tp([1, x, y]))
135n/a check(tp(obj for obj in [1, x, y]))
136n/a check(tp(set([1, x, y])))
137n/a check(tp(tuple([obj]) for obj in [1, x, y]))
138n/a check(tuple(tp([obj]) for obj in [1, x, y]))
139n/a
140n/a self._tracked(tp([z]))
141n/a self._tracked(tp([[x, y]]))
142n/a self._tracked(tp([{x: y}]))
143n/a self._tracked(tp(obj for obj in [x, y, z]))
144n/a self._tracked(tp(tuple([obj]) for obj in [x, y, z]))
145n/a self._tracked(tuple(tp([obj]) for obj in [x, y, z]))
146n/a
147n/a @support.cpython_only
148n/a def test_track_dynamic(self):
149n/a # Test GC-optimization of dynamically constructed tuples.
150n/a self.check_track_dynamic(tuple, False)
151n/a
152n/a @support.cpython_only
153n/a def test_track_subtypes(self):
154n/a # Tuple subtypes must always be tracked
155n/a class MyTuple(tuple):
156n/a pass
157n/a self.check_track_dynamic(MyTuple, True)
158n/a
159n/a @support.cpython_only
160n/a def test_bug7466(self):
161n/a # Trying to untrack an unfinished tuple could crash Python
162n/a self._not_tracked(tuple(gc.collect() for i in range(101)))
163n/a
164n/a def test_repr_large(self):
165n/a # Check the repr of large list objects
166n/a def check(n):
167n/a l = (0,) * n
168n/a s = repr(l)
169n/a self.assertEqual(s,
170n/a '(' + ', '.join(['0'] * n) + ')')
171n/a check(10) # check our checking code
172n/a check(1000000)
173n/a
174n/a def test_iterator_pickle(self):
175n/a # Userlist iterators don't support pickling yet since
176n/a # they are based on generators.
177n/a data = self.type2test([4, 5, 6, 7])
178n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
179n/a itorg = iter(data)
180n/a d = pickle.dumps(itorg, proto)
181n/a it = pickle.loads(d)
182n/a self.assertEqual(type(itorg), type(it))
183n/a self.assertEqual(self.type2test(it), self.type2test(data))
184n/a
185n/a it = pickle.loads(d)
186n/a next(it)
187n/a d = pickle.dumps(it, proto)
188n/a self.assertEqual(self.type2test(it), self.type2test(data)[1:])
189n/a
190n/a def test_reversed_pickle(self):
191n/a data = self.type2test([4, 5, 6, 7])
192n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
193n/a itorg = reversed(data)
194n/a d = pickle.dumps(itorg, proto)
195n/a it = pickle.loads(d)
196n/a self.assertEqual(type(itorg), type(it))
197n/a self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
198n/a
199n/a it = pickle.loads(d)
200n/a next(it)
201n/a d = pickle.dumps(it, proto)
202n/a self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
203n/a
204n/a def test_no_comdat_folding(self):
205n/a # Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
206n/a # optimization causes failures in code that relies on distinct
207n/a # function addresses.
208n/a class T(tuple): pass
209n/a with self.assertRaises(TypeError):
210n/a [3,] + T((1,2))
211n/a
212n/a def test_lexicographic_ordering(self):
213n/a # Issue 21100
214n/a a = self.type2test([1, 2])
215n/a b = self.type2test([1, 2, 0])
216n/a c = self.type2test([1, 3])
217n/a self.assertLess(a, b)
218n/a self.assertLess(b, c)
219n/a
220n/aif __name__ == "__main__":
221n/a unittest.main()