1
from test import support, seq_tests
3
class TupleTest(seq_tests.CommonTest):
6
def test_constructors(self):
8
# calling built-in types without argument must return empty
9
self.assertEqual(tuple(), ())
11
t0_3_bis = tuple(t0_3)
12
self.assert_(t0_3 is t0_3_bis)
13
self.assertEqual(tuple([]), ())
14
self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
15
self.assertEqual(tuple(''), ())
16
self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
25
self.assertEqual(len(()), 0)
26
self.assertEqual(len((0,)), 1)
27
self.assertEqual(len((0, 1, 2)), 3)
34
self.assert_(u is not u2)
41
self.assert_(u is not u2)
43
def test_tupleresizebug(self):
44
# Check that a specific bug in _PyTuple_Resize() is squashed.
48
self.assertEqual(list(tuple(f())), list(range(1000)))
51
# See SF bug 942952: Weakness in tuple hash
54
# should spread-out closely spaced values
55
# should not exhibit cancellation in tuples like (x,(x,y))
56
# should be distinct from element hashes: hash(x)!=hash((x,))
57
# This test exercises those cases.
58
# For a pure random hash and N=50, the expected number of occupied
59
# buckets when tossing 252,600 balls into 2**32 buckets
60
# is 252,592.6, or about 7.4 expected collisions. The
61
# standard deviation is 2.73. On a box with 64-bit hash
62
# codes, no collisions are expected. Here we accept no
63
# more than 15 collisions. Any worse and the hash function
68
xp = [(i, j) for i in base for j in base]
69
inps = base + [(i, j) for i in base for j in xp] + \
70
[(i, j) for i in xp for j in base] + xp + list(zip(base))
71
collisions = len(inps) - len(set(map(hash, inps)))
72
self.assert_(collisions <= 15)
77
a0 = self.type2test(l0)
78
a2 = self.type2test(l2)
80
self.assertEqual(str(a0), repr(l0))
81
self.assertEqual(str(a2), repr(l2))
82
self.assertEqual(repr(a0), "()")
83
self.assertEqual(repr(a2), "(0, 1, 2)")
86
support.run_unittest(TupleTest)
88
if __name__=="__main__":