0001 import unittest 0002 from test import test_support, seq_tests 0003 0004 class TupleTest(seq_tests.CommonTest): 0005 type2test = tuple 0006 0007 def test_constructors(self): 0008 super(TupleTest, self).test_len() 0009 # calling built-in types without argument must return empty 0010 self.assertEqual(tuple(), ()) 0011 0012 def test_truth(self): 0013 super(TupleTest, self).test_truth() 0014 self.assert_(not ()) 0015 self.assert_((42, )) 0016 0017 def test_len(self): 0018 super(TupleTest, self).test_len() 0019 self.assertEqual(len(()), 0) 0020 self.assertEqual(len((0,)), 1) 0021 self.assertEqual(len((0, 1, 2)), 3) 0022 0023 def test_iadd(self): 0024 super(TupleTest, self).test_iadd() 0025 u = (0, 1) 0026 u2 = u 0027 u += (2, 3) 0028 self.assert_(u is not u2) 0029 0030 def test_imul(self): 0031 super(TupleTest, self).test_imul() 0032 u = (0, 1) 0033 u2 = u 0034 u *= 3 0035 self.assert_(u is not u2) 0036 0037 def test_tupleresizebug(self): 0038 # Check that a specific bug in _PyTuple_Resize() is squashed. 0039 def f(): 0040 for i in range(1000): 0041 yield i 0042 self.assertEqual(list(tuple(f())), range(1000)) 0043 0044 def test_hash(self): 0045 # See SF bug 942952: Weakness in tuple hash 0046 # The hash should: 0047 # be non-commutative 0048 # should spread-out closely spaced values 0049 # should not exhibit cancellation in tuples like (x,(x,y)) 0050 # should be distinct from element hashes: hash(x)!=hash((x,)) 0051 # This test exercises those cases. 0052 # For a pure random hash and N=50, the expected number of occupied 0053 # buckets when tossing 252,600 balls into 2**32 buckets 0054 # is 252,592.6, or about 7.4 expected collisions. The 0055 # standard deviation is 2.73. On a box with 64-bit hash 0056 # codes, no collisions are expected. Here we accept no 0057 # more than 15 collisions. Any worse and the hash function 0058 # is sorely suspect. 0059 0060 N=50 0061 base = range(N) 0062 xp = [(i, j) for i in base for j in base] 0063 inps = base + [(i, j) for i in base for j in xp] + \ 0064 [(i, j) for i in xp for j in base] + xp + zip(base) 0065 collisions = len(inps) - len(set(map(hash, inps))) 0066 self.assert_(collisions <= 15) 0067 0068 def test_repr(self): 0069 l0 = tuple() 0070 l2 = (0, 1, 2) 0071 a0 = self.type2test(l0) 0072 a2 = self.type2test(l2) 0073 0074 self.assertEqual(str(a0), repr(l0)) 0075 self.assertEqual(str(a2), repr(l2)) 0076 self.assertEqual(repr(a0), "()") 0077 self.assertEqual(repr(a2), "(0, 1, 2)") 0078 0079 def test_main(): 0080 test_support.run_unittest(TupleTest) 0081 0082 if __name__=="__main__": 0083 test_main() 0084
Generated by PyXR 0.9.4