test_tuple.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from test import test_support, seq_tests
  2. import gc
  3. class TupleTest(seq_tests.CommonTest):
  4. type2test = tuple
  5. def test_constructors(self):
  6. super(TupleTest, self).test_constructors()
  7. # calling built-in types without argument must return empty
  8. self.assertEqual(tuple(), ())
  9. t0_3 = (0, 1, 2, 3)
  10. t0_3_bis = tuple(t0_3)
  11. self.assertTrue(t0_3 is t0_3_bis)
  12. self.assertEqual(tuple([]), ())
  13. self.assertEqual(tuple([0, 1, 2, 3]), (0, 1, 2, 3))
  14. self.assertEqual(tuple(''), ())
  15. self.assertEqual(tuple('spam'), ('s', 'p', 'a', 'm'))
  16. def test_truth(self):
  17. super(TupleTest, self).test_truth()
  18. self.assertTrue(not ())
  19. self.assertTrue((42, ))
  20. def test_len(self):
  21. super(TupleTest, self).test_len()
  22. self.assertEqual(len(()), 0)
  23. self.assertEqual(len((0,)), 1)
  24. self.assertEqual(len((0, 1, 2)), 3)
  25. def test_iadd(self):
  26. super(TupleTest, self).test_iadd()
  27. u = (0, 1)
  28. u2 = u
  29. u += (2, 3)
  30. self.assertTrue(u is not u2)
  31. def test_imul(self):
  32. super(TupleTest, self).test_imul()
  33. u = (0, 1)
  34. u2 = u
  35. u *= 3
  36. self.assertTrue(u is not u2)
  37. def test_tupleresizebug(self):
  38. # Check that a specific bug in _PyTuple_Resize() is squashed.
  39. def f():
  40. for i in range(1000):
  41. yield i
  42. self.assertEqual(list(tuple(f())), range(1000))
  43. def test_hash(self):
  44. # See SF bug 942952: Weakness in tuple hash
  45. # The hash should:
  46. # be non-commutative
  47. # should spread-out closely spaced values
  48. # should not exhibit cancellation in tuples like (x,(x,y))
  49. # should be distinct from element hashes: hash(x)!=hash((x,))
  50. # This test exercises those cases.
  51. # For a pure random hash and N=50, the expected number of occupied
  52. # buckets when tossing 252,600 balls into 2**32 buckets
  53. # is 252,592.6, or about 7.4 expected collisions. The
  54. # standard deviation is 2.73. On a box with 64-bit hash
  55. # codes, no collisions are expected. Here we accept no
  56. # more than 15 collisions. Any worse and the hash function
  57. # is sorely suspect.
  58. N=50
  59. base = range(N)
  60. xp = [(i, j) for i in base for j in base]
  61. inps = base + [(i, j) for i in base for j in xp] + \
  62. [(i, j) for i in xp for j in base] + xp + zip(base)
  63. collisions = len(inps) - len(set(map(hash, inps)))
  64. self.assertTrue(collisions <= 15)
  65. def test_repr(self):
  66. l0 = tuple()
  67. l2 = (0, 1, 2)
  68. a0 = self.type2test(l0)
  69. a2 = self.type2test(l2)
  70. self.assertEqual(str(a0), repr(l0))
  71. self.assertEqual(str(a2), repr(l2))
  72. self.assertEqual(repr(a0), "()")
  73. self.assertEqual(repr(a2), "(0, 1, 2)")
  74. def _not_tracked(self, t):
  75. # Nested tuples can take several collections to untrack
  76. gc.collect()
  77. gc.collect()
  78. self.assertFalse(gc.is_tracked(t), t)
  79. def _tracked(self, t):
  80. self.assertTrue(gc.is_tracked(t), t)
  81. gc.collect()
  82. gc.collect()
  83. self.assertTrue(gc.is_tracked(t), t)
  84. @test_support.cpython_only
  85. def test_track_literals(self):
  86. # Test GC-optimization of tuple literals
  87. x, y, z = 1.5, "a", []
  88. self._not_tracked(())
  89. self._not_tracked((1,))
  90. self._not_tracked((1, 2))
  91. self._not_tracked((1, 2, "a"))
  92. self._not_tracked((1, 2, (None, True, False, ()), int))
  93. self._not_tracked((object(),))
  94. self._not_tracked(((1, x), y, (2, 3)))
  95. # Tuples with mutable elements are always tracked, even if those
  96. # elements are not tracked right now.
  97. self._tracked(([],))
  98. self._tracked(([1],))
  99. self._tracked(({},))
  100. self._tracked((set(),))
  101. self._tracked((x, y, z))
  102. def check_track_dynamic(self, tp, always_track):
  103. x, y, z = 1.5, "a", []
  104. check = self._tracked if always_track else self._not_tracked
  105. check(tp())
  106. check(tp([]))
  107. check(tp(set()))
  108. check(tp([1, x, y]))
  109. check(tp(obj for obj in [1, x, y]))
  110. check(tp(set([1, x, y])))
  111. check(tp(tuple([obj]) for obj in [1, x, y]))
  112. check(tuple(tp([obj]) for obj in [1, x, y]))
  113. self._tracked(tp([z]))
  114. self._tracked(tp([[x, y]]))
  115. self._tracked(tp([{x: y}]))
  116. self._tracked(tp(obj for obj in [x, y, z]))
  117. self._tracked(tp(tuple([obj]) for obj in [x, y, z]))
  118. self._tracked(tuple(tp([obj]) for obj in [x, y, z]))
  119. @test_support.cpython_only
  120. def test_track_dynamic(self):
  121. # Test GC-optimization of dynamically constructed tuples.
  122. self.check_track_dynamic(tuple, False)
  123. @test_support.cpython_only
  124. def test_track_subtypes(self):
  125. # Tuple subtypes must always be tracked
  126. class MyTuple(tuple):
  127. pass
  128. self.check_track_dynamic(MyTuple, True)
  129. @test_support.cpython_only
  130. def test_bug7466(self):
  131. # Trying to untrack an unfinished tuple could crash Python
  132. self._not_tracked(tuple(gc.collect() for i in range(101)))
  133. def test_main():
  134. test_support.run_unittest(TupleTest)
  135. if __name__=="__main__":
  136. test_main()