2 from test
import test_support
, seq_tests
4 class TupleTest(seq_tests
.CommonTest
):
7 def test_constructors(self
):
8 super(TupleTest
, self
).test_len()
9 # calling built-in types without argument must return empty
10 self
.assertEqual(tuple(), ())
13 super(TupleTest
, self
).test_truth()
18 super(TupleTest
, self
).test_len()
19 self
.assertEqual(len(()), 0)
20 self
.assertEqual(len((0,)), 1)
21 self
.assertEqual(len((0, 1, 2)), 3)
24 super(TupleTest
, self
).test_iadd()
28 self
.assert_(u
is not u2
)
31 super(TupleTest
, self
).test_imul()
35 self
.assert_(u
is not u2
)
37 def test_tupleresizebug(self
):
38 # Check that a specific bug in _PyTuple_Resize() is squashed.
42 self
.assertEqual(list(tuple(f())), range(1000))
45 # See SF bug 942952: Weakness in tuple hash
48 # should spread-out closely spaced values
49 # should not exhibit cancellation in tuples like (x,(x,y))
50 # should be distinct from element hashes: hash(x)!=hash((x,))
51 # This test exercises those cases.
52 # For a pure random hash and N=50, the expected number of occupied
53 # buckets when tossing 252,600 balls into 2**32 buckets
54 # is 252,592.6, or about 7.4 expected collisions. The
55 # standard deviation is 2.73. On a box with 64-bit hash
56 # codes, no collisions are expected. Here we accept no
57 # more than 15 collisions. Any worse and the hash function
62 xp
= [(i
, j
) for i
in base
for j
in base
]
63 inps
= base
+ [(i
, j
) for i
in base
for j
in xp
] + \
64 [(i
, j
) for i
in xp
for j
in base
] + xp
+ zip(base
)
65 collisions
= len(inps
) - len(set(map(hash, inps
)))
66 self
.assert_(collisions
<= 15)
69 test_support
.run_unittest(TupleTest
)
71 if __name__
=="__main__":