+++ /dev/null
-# test the invariant that\r
-# iff a==b then hash(a)==hash(b)\r
-#\r
-# Also test that hash implementations are inherited as expected\r
-\r
-import unittest\r
-from test import test_support\r
-from collections import Hashable\r
-\r
-\r
-class HashEqualityTestCase(unittest.TestCase):\r
-\r
- def same_hash(self, *objlist):\r
- # Hash each object given and fail if\r
- # the hash values are not all the same.\r
- hashed = map(hash, objlist)\r
- for h in hashed[1:]:\r
- if h != hashed[0]:\r
- self.fail("hashed values differ: %r" % (objlist,))\r
-\r
- def test_numeric_literals(self):\r
- self.same_hash(1, 1L, 1.0, 1.0+0.0j)\r
- self.same_hash(0, 0L, 0.0, 0.0+0.0j)\r
- self.same_hash(-1, -1L, -1.0, -1.0+0.0j)\r
- self.same_hash(-2, -2L, -2.0, -2.0+0.0j)\r
-\r
- def test_coerced_integers(self):\r
- self.same_hash(int(1), long(1), float(1), complex(1),\r
- int('1'), float('1.0'))\r
- self.same_hash(int(-2**31), long(-2**31), float(-2**31))\r
- self.same_hash(int(1-2**31), long(1-2**31), float(1-2**31))\r
- self.same_hash(int(2**31-1), long(2**31-1), float(2**31-1))\r
- # for 64-bit platforms\r
- self.same_hash(int(2**31), long(2**31), float(2**31))\r
- self.same_hash(int(-2**63), long(-2**63), float(-2**63))\r
- self.same_hash(int(1-2**63), long(1-2**63))\r
- self.same_hash(int(2**63-1), long(2**63-1))\r
- self.same_hash(long(2**63), float(2**63))\r
-\r
- def test_coerced_floats(self):\r
- self.same_hash(long(1.23e300), float(1.23e300))\r
- self.same_hash(float(0.5), complex(0.5, 0.0))\r
-\r
-\r
-_default_hash = object.__hash__\r
-class DefaultHash(object): pass\r
-\r
-_FIXED_HASH_VALUE = 42\r
-class FixedHash(object):\r
- def __hash__(self):\r
- return _FIXED_HASH_VALUE\r
-\r
-class OnlyEquality(object):\r
- def __eq__(self, other):\r
- return self is other\r
- # Trick to suppress Py3k warning in 2.x\r
- __hash__ = None\r
-del OnlyEquality.__hash__\r
-\r
-class OnlyInequality(object):\r
- def __ne__(self, other):\r
- return self is not other\r
-\r
-class OnlyCmp(object):\r
- def __cmp__(self, other):\r
- return cmp(id(self), id(other))\r
- # Trick to suppress Py3k warning in 2.x\r
- __hash__ = None\r
-del OnlyCmp.__hash__\r
-\r
-class InheritedHashWithEquality(FixedHash, OnlyEquality): pass\r
-class InheritedHashWithInequality(FixedHash, OnlyInequality): pass\r
-class InheritedHashWithCmp(FixedHash, OnlyCmp): pass\r
-\r
-class NoHash(object):\r
- __hash__ = None\r
-\r
-class HashInheritanceTestCase(unittest.TestCase):\r
- default_expected = [object(),\r
- DefaultHash(),\r
- OnlyEquality(),\r
- OnlyInequality(),\r
- OnlyCmp(),\r
- ]\r
- fixed_expected = [FixedHash(),\r
- InheritedHashWithEquality(),\r
- InheritedHashWithInequality(),\r
- InheritedHashWithCmp(),\r
- ]\r
- error_expected = [NoHash()]\r
-\r
- def test_default_hash(self):\r
- for obj in self.default_expected:\r
- self.assertEqual(hash(obj), _default_hash(obj))\r
-\r
- def test_fixed_hash(self):\r
- for obj in self.fixed_expected:\r
- self.assertEqual(hash(obj), _FIXED_HASH_VALUE)\r
-\r
- def test_error_hash(self):\r
- for obj in self.error_expected:\r
- self.assertRaises(TypeError, hash, obj)\r
-\r
- def test_hashable(self):\r
- objects = (self.default_expected +\r
- self.fixed_expected)\r
- for obj in objects:\r
- self.assertIsInstance(obj, Hashable)\r
-\r
- def test_not_hashable(self):\r
- for obj in self.error_expected:\r
- self.assertNotIsInstance(obj, Hashable)\r
-\r
-\r
-# Issue #4701: Check that some builtin types are correctly hashable\r
-# (This test only used to fail in Python 3.0, but has been included\r
-# in 2.x along with the lazy call to PyType_Ready in PyObject_Hash)\r
-class DefaultIterSeq(object):\r
- seq = range(10)\r
- def __len__(self):\r
- return len(self.seq)\r
- def __getitem__(self, index):\r
- return self.seq[index]\r
-\r
-class HashBuiltinsTestCase(unittest.TestCase):\r
- hashes_to_check = [xrange(10),\r
- enumerate(xrange(10)),\r
- iter(DefaultIterSeq()),\r
- iter(lambda: 0, 0),\r
- ]\r
-\r
- def test_hashes(self):\r
- _default_hash = object.__hash__\r
- for obj in self.hashes_to_check:\r
- self.assertEqual(hash(obj), _default_hash(obj))\r
-\r
-def test_main():\r
- test_support.run_unittest(HashEqualityTestCase,\r
- HashInheritanceTestCase,\r
- HashBuiltinsTestCase)\r
-\r
-\r
-if __name__ == "__main__":\r
- test_main()\r