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