1
from test import support, seq_tests
6
class TupleTest(seq_tests.CommonTest):
9
def test_constructors(self):
10
super().test_constructors()
11
# calling built-in types without argument must return empty
12
self.assertEqual(tuple(), ())
14
t0_3_bis = tuple(t0_3)
15
self.assertTrue(t0_3 is t0_3_bis)
16
self.assertEqual(tuple([]), ())
17
self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
18
self.assertEqual(tuple(''), ())
19
self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
23
self.assertTrue(not ())
24
self.assertTrue((42, ))
28
self.assertEqual(len(()), 0)
29
self.assertEqual(len((0,)), 1)
30
self.assertEqual(len((0, 1, 2)), 3)
37
self.assertTrue(u is not u2)
44
self.assertTrue(u is not u2)
46
def test_tupleresizebug(self):
47
# Check that a specific bug in _PyTuple_Resize() is squashed.
51
self.assertEqual(list(tuple(f())), list(range(1000)))
54
# See SF bug 942952: Weakness in tuple hash
57
# should spread-out closely spaced values
58
# should not exhibit cancellation in tuples like (x,(x,y))
59
# should be distinct from element hashes: hash(x)!=hash((x,))
60
# This test exercises those cases.
61
# For a pure random hash and N=50, the expected number of occupied
62
# buckets when tossing 252,600 balls into 2**32 buckets
63
# is 252,592.6, or about 7.4 expected collisions. The
64
# standard deviation is 2.73. On a box with 64-bit hash
65
# codes, no collisions are expected. Here we accept no
66
# more than 15 collisions. Any worse and the hash function
71
xp = [(i, j) for i in base for j in base]
72
inps = base + [(i, j) for i in base for j in xp] + \
73
[(i, j) for i in xp for j in base] + xp + list(zip(base))
74
collisions = len(inps) - len(set(map(hash, inps)))
75
self.assertTrue(collisions <= 15)
80
a0 = self.type2test(l0)
81
a2 = self.type2test(l2)
83
self.assertEqual(str(a0), repr(l0))
84
self.assertEqual(str(a2), repr(l2))
85
self.assertEqual(repr(a0), "()")
86
self.assertEqual(repr(a2), "(0, 1, 2)")
88
def _not_tracked(self, t):
89
# Nested tuples can take several collections to untrack
92
self.assertFalse(gc.is_tracked(t), t)
94
def _tracked(self, t):
95
self.assertTrue(gc.is_tracked(t), t)
98
self.assertTrue(gc.is_tracked(t), t)
100
@support.cpython_only
101
def test_track_literals(self):
102
# Test GC-optimization of tuple literals
103
x, y, z = 1.5, "a", []
105
self._not_tracked(())
106
self._not_tracked((1,))
107
self._not_tracked((1, 2))
108
self._not_tracked((1, 2, "a"))
109
self._not_tracked((1, 2, (None, True, False, ()), int))
110
self._not_tracked((object(),))
111
self._not_tracked(((1, x), y, (2, 3)))
113
# Tuples with mutable elements are always tracked, even if those
114
# elements are not tracked right now.
116
self._tracked(([1],))
118
self._tracked((set(),))
119
self._tracked((x, y, z))
121
def check_track_dynamic(self, tp, always_track):
122
x, y, z = 1.5, "a", []
124
check = self._tracked if always_track else self._not_tracked
129
check(tp(obj for obj in [1, x, y]))
130
check(tp(set([1, x, y])))
131
check(tp(tuple([obj]) for obj in [1, x, y]))
132
check(tuple(tp([obj]) for obj in [1, x, y]))
134
self._tracked(tp([z]))
135
self._tracked(tp([[x, y]]))
136
self._tracked(tp([{x: y}]))
137
self._tracked(tp(obj for obj in [x, y, z]))
138
self._tracked(tp(tuple([obj]) for obj in [x, y, z]))
139
self._tracked(tuple(tp([obj]) for obj in [x, y, z]))
141
@support.cpython_only
142
def test_track_dynamic(self):
143
# Test GC-optimization of dynamically constructed tuples.
144
self.check_track_dynamic(tuple, False)
146
@support.cpython_only
147
def test_track_subtypes(self):
148
# Tuple subtypes must always be tracked
149
class MyTuple(tuple):
151
self.check_track_dynamic(MyTuple, True)
153
@support.cpython_only
154
def test_bug7466(self):
155
# Trying to untrack an unfinished tuple could crash Python
156
self._not_tracked(tuple(gc.collect() for i in range(101)))
158
def test_repr_large(self):
159
# Check the repr of large list objects
164
'(' + ', '.join(['0'] * n) + ')')
165
check(10) # check our checking code
168
def test_iterator_pickle(self):
169
# Userlist iterators don't support pickling yet since
170
# they are based on generators.
171
data = self.type2test([4, 5, 6, 7])
173
d = pickle.dumps(itorg)
175
self.assertEqual(type(itorg), type(it))
176
self.assertEqual(self.type2test(it), self.type2test(data))
181
self.assertEqual(self.type2test(it), self.type2test(data)[1:])
183
def test_reversed_pickle(self):
184
data = self.type2test([4, 5, 6, 7])
185
itorg = reversed(data)
186
d = pickle.dumps(itorg)
188
self.assertEqual(type(itorg), type(it))
189
self.assertEqual(self.type2test(it), self.type2test(reversed(data)))
194
self.assertEqual(self.type2test(it), self.type2test(reversed(data))[1:])
196
def test_no_comdat_folding(self):
197
# Issue 8847: In the PGO build, the MSVC linker's COMDAT folding
198
# optimization causes failures in code that relies on distinct
199
# function addresses.
201
with self.assertRaises(TypeError):
205
support.run_unittest(TupleTest)
207
if __name__=="__main__":