test_tuple.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from test import test_support, seq_tests
  2. class TupleTest(seq_tests.CommonTest):
  3. type2test = tuple
  4. def test_constructors(self):
  5. super(TupleTest, self).test_len()
  6. # calling built-in types without argument must return empty
  7. self.assertEqual(tuple(), ())
  8. t0_3 = (0, 1, 2, 3)
  9. t0_3_bis = tuple(t0_3)
  10. self.assert_(t0_3 is t0_3_bis)
  11. self.assertEqual(tuple([]), ())
  12. self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
  13. self.assertEqual(tuple(''), ())
  14. self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
  15. def test_truth(self):
  16. super(TupleTest, self).test_truth()
  17. self.assert_(not ())
  18. self.assert_((42, ))
  19. def test_len(self):
  20. super(TupleTest, self).test_len()
  21. self.assertEqual(len(()), 0)
  22. self.assertEqual(len((0,)), 1)
  23. self.assertEqual(len((0, 1, 2)), 3)
  24. def test_iadd(self):
  25. super(TupleTest, self).test_iadd()
  26. u = (0, 1)
  27. u2 = u
  28. u += (2, 3)
  29. self.assert_(u is not u2)
  30. def test_imul(self):
  31. super(TupleTest, self).test_imul()
  32. u = (0, 1)
  33. u2 = u
  34. u *= 3
  35. self.assert_(u is not u2)
  36. def test_tupleresizebug(self):
  37. # Check that a specific bug in _PyTuple_Resize() is squashed.
  38. def f():
  39. for i in range(1000):
  40. yield i
  41. self.assertEqual(list(tuple(f())), range(1000))
  42. def test_hash(self):
  43. # See SF bug 942952: Weakness in tuple hash
  44. # The hash should:
  45. # be non-commutative
  46. # should spread-out closely spaced values
  47. # should not exhibit cancellation in tuples like (x,(x,y))
  48. # should be distinct from element hashes: hash(x)!=hash((x,))
  49. # This test exercises those cases.
  50. # For a pure random hash and N=50, the expected number of occupied
  51. # buckets when tossing 252,600 balls into 2**32 buckets
  52. # is 252,592.6, or about 7.4 expected collisions. The
  53. # standard deviation is 2.73. On a box with 64-bit hash
  54. # codes, no collisions are expected. Here we accept no
  55. # more than 15 collisions. Any worse and the hash function
  56. # is sorely suspect.
  57. N=50
  58. base = range(N)
  59. xp = [(i, j) for i in base for j in base]
  60. inps = base + [(i, j) for i in base for j in xp] + \
  61. [(i, j) for i in xp for j in base] + xp + zip(base)
  62. collisions = len(inps) - len(set(map(hash, inps)))
  63. self.assert_(collisions <= 15)
  64. def test_repr(self):
  65. l0 = tuple()
  66. l2 = (0, 1, 2)
  67. a0 = self.type2test(l0)
  68. a2 = self.type2test(l2)
  69. self.assertEqual(str(a0), repr(l0))
  70. self.assertEqual(str(a2), repr(l2))
  71. self.assertEqual(repr(a0), "()")
  72. self.assertEqual(repr(a2), "(0, 1, 2)")
  73. def test_main():
  74. test_support.run_unittest(TupleTest)
  75. if __name__=="__main__":
  76. test_main()