Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 1 | # test the invariant that |
| 2 | # iff a==b then hash(a)==hash(b) |
| 3 | # |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 4 | # Also test that hash implementations are inherited as expected |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 5 | |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 6 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 7 | from test import test_support |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 8 | from collections import Hashable |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 9 | |
| 10 | |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 11 | class HashEqualityTestCase(unittest.TestCase): |
| 12 | |
| 13 | def same_hash(self, *objlist): |
Fred Drake | acb117e | 2001-05-18 21:50:02 +0000 | [diff] [blame] | 14 | # Hash each object given and fail if |
| 15 | # the hash values are not all the same. |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 16 | hashed = map(hash, objlist) |
| 17 | for h in hashed[1:]: |
| 18 | if h != hashed[0]: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 19 | self.fail("hashed values differ: %r" % (objlist,)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 20 | |
| 21 | def test_numeric_literals(self): |
| 22 | self.same_hash(1, 1L, 1.0, 1.0+0.0j) |
Facundo Batista | d544df7 | 2007-09-19 15:10:06 +0000 | [diff] [blame] | 23 | self.same_hash(0, 0L, 0.0, 0.0+0.0j) |
| 24 | self.same_hash(-1, -1L, -1.0, -1.0+0.0j) |
| 25 | self.same_hash(-2, -2L, -2.0, -2.0+0.0j) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 26 | |
| 27 | def test_coerced_integers(self): |
| 28 | self.same_hash(int(1), long(1), float(1), complex(1), |
| 29 | int('1'), float('1.0')) |
Facundo Batista | d544df7 | 2007-09-19 15:10:06 +0000 | [diff] [blame] | 30 | self.same_hash(int(-2**31), long(-2**31), float(-2**31)) |
| 31 | self.same_hash(int(1-2**31), long(1-2**31), float(1-2**31)) |
| 32 | self.same_hash(int(2**31-1), long(2**31-1), float(2**31-1)) |
| 33 | # for 64-bit platforms |
| 34 | self.same_hash(int(2**31), long(2**31), float(2**31)) |
| 35 | self.same_hash(int(-2**63), long(-2**63), float(-2**63)) |
| 36 | self.same_hash(int(1-2**63), long(1-2**63)) |
| 37 | self.same_hash(int(2**63-1), long(2**63-1)) |
Mark Dickinson | f709ab8 | 2009-01-31 16:44:04 +0000 | [diff] [blame^] | 38 | self.same_hash(long(2**63), float(2**63)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 39 | |
| 40 | def test_coerced_floats(self): |
| 41 | self.same_hash(long(1.23e300), float(1.23e300)) |
| 42 | self.same_hash(float(0.5), complex(0.5, 0.0)) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 43 | |
| 44 | |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 45 | _default_hash = object.__hash__ |
| 46 | class DefaultHash(object): pass |
| 47 | |
| 48 | _FIXED_HASH_VALUE = 42 |
| 49 | class FixedHash(object): |
| 50 | def __hash__(self): |
| 51 | return _FIXED_HASH_VALUE |
| 52 | |
| 53 | class OnlyEquality(object): |
| 54 | def __eq__(self, other): |
| 55 | return self is other |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 56 | # Trick to suppress Py3k warning in 2.x |
| 57 | __hash__ = None |
| 58 | del OnlyEquality.__hash__ |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 59 | |
| 60 | class OnlyInequality(object): |
| 61 | def __ne__(self, other): |
| 62 | return self is not other |
| 63 | |
| 64 | class OnlyCmp(object): |
| 65 | def __cmp__(self, other): |
| 66 | return cmp(id(self), id(other)) |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 67 | # Trick to suppress Py3k warning in 2.x |
| 68 | __hash__ = None |
| 69 | del OnlyCmp.__hash__ |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 70 | |
| 71 | class InheritedHashWithEquality(FixedHash, OnlyEquality): pass |
| 72 | class InheritedHashWithInequality(FixedHash, OnlyInequality): pass |
| 73 | class InheritedHashWithCmp(FixedHash, OnlyCmp): pass |
| 74 | |
| 75 | class NoHash(object): |
| 76 | __hash__ = None |
| 77 | |
| 78 | class HashInheritanceTestCase(unittest.TestCase): |
| 79 | default_expected = [object(), |
| 80 | DefaultHash(), |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 81 | OnlyEquality(), |
| 82 | OnlyInequality(), |
| 83 | OnlyCmp(), |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 84 | ] |
| 85 | fixed_expected = [FixedHash(), |
| 86 | InheritedHashWithEquality(), |
| 87 | InheritedHashWithInequality(), |
| 88 | InheritedHashWithCmp(), |
| 89 | ] |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 90 | error_expected = [NoHash()] |
| 91 | |
| 92 | def test_default_hash(self): |
| 93 | for obj in self.default_expected: |
| 94 | self.assertEqual(hash(obj), _default_hash(obj)) |
| 95 | |
| 96 | def test_fixed_hash(self): |
| 97 | for obj in self.fixed_expected: |
| 98 | self.assertEqual(hash(obj), _FIXED_HASH_VALUE) |
| 99 | |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 100 | def test_error_hash(self): |
| 101 | for obj in self.error_expected: |
| 102 | self.assertRaises(TypeError, hash, obj) |
| 103 | |
| 104 | def test_hashable(self): |
| 105 | objects = (self.default_expected + |
Nick Coghlan | 48361f5 | 2008-08-11 15:45:58 +0000 | [diff] [blame] | 106 | self.fixed_expected) |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 107 | for obj in objects: |
| 108 | self.assert_(isinstance(obj, Hashable), repr(obj)) |
| 109 | |
| 110 | def test_not_hashable(self): |
| 111 | for obj in self.error_expected: |
| 112 | self.assertFalse(isinstance(obj, Hashable), repr(obj)) |
| 113 | |
| 114 | |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 115 | # Issue #4701: Check that some builtin types are correctly hashable |
| 116 | # (This test only used to fail in Python 3.0, but has been included |
| 117 | # in 2.x along with the lazy call to PyType_Ready in PyObject_Hash) |
| 118 | class DefaultIterSeq(object): |
| 119 | seq = range(10) |
| 120 | def __len__(self): |
| 121 | return len(self.seq) |
| 122 | def __getitem__(self, index): |
| 123 | return self.seq[index] |
| 124 | |
| 125 | class HashBuiltinsTestCase(unittest.TestCase): |
| 126 | hashes_to_check = [xrange(10), |
| 127 | enumerate(xrange(10)), |
| 128 | iter(DefaultIterSeq()), |
| 129 | iter(lambda: 0, 0), |
| 130 | ] |
| 131 | |
| 132 | def test_hashes(self): |
| 133 | _default_hash = object.__hash__ |
| 134 | for obj in self.hashes_to_check: |
| 135 | self.assertEqual(hash(obj), _default_hash(obj)) |
| 136 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 137 | def test_main(): |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 138 | test_support.run_unittest(HashEqualityTestCase, |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 139 | HashInheritanceTestCase, |
| 140 | HashBuiltinsTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
| 144 | test_main() |