/Lib/test/test_hash.py

http://unladen-swallow.googlecode.com/ · Python · 143 lines · 103 code · 28 blank · 12 comment · 9 complexity · c1a55326372fe00934fa6d665812c549 MD5 · raw file

  1. # test the invariant that
  2. # iff a==b then hash(a)==hash(b)
  3. #
  4. # Also test that hash implementations are inherited as expected
  5. import unittest
  6. from test import test_support
  7. from collections import Hashable
  8. class HashEqualityTestCase(unittest.TestCase):
  9. def same_hash(self, *objlist):
  10. # Hash each object given and fail if
  11. # the hash values are not all the same.
  12. hashed = map(hash, objlist)
  13. for h in hashed[1:]:
  14. if h != hashed[0]:
  15. self.fail("hashed values differ: %r" % (objlist,))
  16. def test_numeric_literals(self):
  17. self.same_hash(1, 1L, 1.0, 1.0+0.0j)
  18. self.same_hash(0, 0L, 0.0, 0.0+0.0j)
  19. self.same_hash(-1, -1L, -1.0, -1.0+0.0j)
  20. self.same_hash(-2, -2L, -2.0, -2.0+0.0j)
  21. def test_coerced_integers(self):
  22. self.same_hash(int(1), long(1), float(1), complex(1),
  23. int('1'), float('1.0'))
  24. self.same_hash(int(-2**31), long(-2**31), float(-2**31))
  25. self.same_hash(int(1-2**31), long(1-2**31), float(1-2**31))
  26. self.same_hash(int(2**31-1), long(2**31-1), float(2**31-1))
  27. # for 64-bit platforms
  28. self.same_hash(int(2**31), long(2**31), float(2**31))
  29. self.same_hash(int(-2**63), long(-2**63), float(-2**63))
  30. self.same_hash(int(1-2**63), long(1-2**63))
  31. self.same_hash(int(2**63-1), long(2**63-1))
  32. def test_coerced_floats(self):
  33. self.same_hash(long(1.23e300), float(1.23e300))
  34. self.same_hash(float(0.5), complex(0.5, 0.0))
  35. _default_hash = object.__hash__
  36. class DefaultHash(object): pass
  37. _FIXED_HASH_VALUE = 42
  38. class FixedHash(object):
  39. def __hash__(self):
  40. return _FIXED_HASH_VALUE
  41. class OnlyEquality(object):
  42. def __eq__(self, other):
  43. return self is other
  44. # Trick to suppress Py3k warning in 2.x
  45. __hash__ = None
  46. del OnlyEquality.__hash__
  47. class OnlyInequality(object):
  48. def __ne__(self, other):
  49. return self is not other
  50. class OnlyCmp(object):
  51. def __cmp__(self, other):
  52. return cmp(id(self), id(other))
  53. # Trick to suppress Py3k warning in 2.x
  54. __hash__ = None
  55. del OnlyCmp.__hash__
  56. class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
  57. class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
  58. class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
  59. class NoHash(object):
  60. __hash__ = None
  61. class HashInheritanceTestCase(unittest.TestCase):
  62. default_expected = [object(),
  63. DefaultHash(),
  64. OnlyEquality(),
  65. OnlyInequality(),
  66. OnlyCmp(),
  67. ]
  68. fixed_expected = [FixedHash(),
  69. InheritedHashWithEquality(),
  70. InheritedHashWithInequality(),
  71. InheritedHashWithCmp(),
  72. ]
  73. error_expected = [NoHash()]
  74. def test_default_hash(self):
  75. for obj in self.default_expected:
  76. self.assertEqual(hash(obj), _default_hash(obj))
  77. def test_fixed_hash(self):
  78. for obj in self.fixed_expected:
  79. self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
  80. def test_error_hash(self):
  81. for obj in self.error_expected:
  82. self.assertRaises(TypeError, hash, obj)
  83. def test_hashable(self):
  84. objects = (self.default_expected +
  85. self.fixed_expected)
  86. for obj in objects:
  87. self.assert_(isinstance(obj, Hashable), repr(obj))
  88. def test_not_hashable(self):
  89. for obj in self.error_expected:
  90. self.assertFalse(isinstance(obj, Hashable), repr(obj))
  91. # Issue #4701: Check that some builtin types are correctly hashable
  92. # (This test only used to fail in Python 3.0, but has been included
  93. # in 2.x along with the lazy call to PyType_Ready in PyObject_Hash)
  94. class DefaultIterSeq(object):
  95. seq = range(10)
  96. def __len__(self):
  97. return len(self.seq)
  98. def __getitem__(self, index):
  99. return self.seq[index]
  100. class HashBuiltinsTestCase(unittest.TestCase):
  101. hashes_to_check = [xrange(10),
  102. enumerate(xrange(10)),
  103. iter(DefaultIterSeq()),
  104. iter(lambda: 0, 0),
  105. ]
  106. def test_hashes(self):
  107. _default_hash = object.__hash__
  108. for obj in self.hashes_to_check:
  109. self.assertEqual(hash(obj), _default_hash(obj))
  110. def test_main():
  111. test_support.run_unittest(HashEqualityTestCase,
  112. HashInheritanceTestCase,
  113. HashBuiltinsTestCase)
  114. if __name__ == "__main__":
  115. test_main()