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 | d1abd25 | 2008-07-15 15:46:38 +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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +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. |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 16 | hashed = list(map(hash, objlist)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 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): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 22 | self.same_hash(1, 1, 1.0, 1.0+0.0j) |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 23 | self.same_hash(0, 0.0, 0.0+0.0j) |
| 24 | self.same_hash(-1, -1.0, -1.0+0.0j) |
| 25 | self.same_hash(-2, -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): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 28 | self.same_hash(int(1), int(1), float(1), complex(1), |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 29 | int('1'), float('1.0')) |
Thomas Wouters | ce272b6 | 2007-09-19 21:19:28 +0000 | [diff] [blame] | 30 | self.same_hash(int(-2**31), float(-2**31)) |
| 31 | self.same_hash(int(1-2**31), float(1-2**31)) |
| 32 | self.same_hash(int(2**31-1), float(2**31-1)) |
| 33 | # for 64-bit platforms |
| 34 | self.same_hash(int(2**31), float(2**31)) |
| 35 | self.same_hash(int(-2**63), float(-2**63)) |
Guilherme Polo | 887b3f2 | 2009-02-07 00:45:10 +0000 | [diff] [blame] | 36 | self.same_hash(int(2**63), float(2**63)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 37 | |
| 38 | def test_coerced_floats(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 39 | self.same_hash(int(1.23e300), float(1.23e300)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 40 | self.same_hash(float(0.5), complex(0.5, 0.0)) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 41 | |
| 42 | |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 43 | _default_hash = object.__hash__ |
| 44 | class DefaultHash(object): pass |
| 45 | |
| 46 | _FIXED_HASH_VALUE = 42 |
| 47 | class FixedHash(object): |
| 48 | def __hash__(self): |
| 49 | return _FIXED_HASH_VALUE |
| 50 | |
| 51 | class OnlyEquality(object): |
| 52 | def __eq__(self, other): |
| 53 | return self is other |
| 54 | |
| 55 | class OnlyInequality(object): |
| 56 | def __ne__(self, other): |
| 57 | return self is not other |
| 58 | |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 59 | class InheritedHashWithEquality(FixedHash, OnlyEquality): pass |
| 60 | class InheritedHashWithInequality(FixedHash, OnlyInequality): pass |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 61 | |
| 62 | class NoHash(object): |
| 63 | __hash__ = None |
| 64 | |
| 65 | class HashInheritanceTestCase(unittest.TestCase): |
| 66 | default_expected = [object(), |
| 67 | DefaultHash(), |
| 68 | OnlyInequality(), |
| 69 | ] |
| 70 | fixed_expected = [FixedHash(), |
| 71 | InheritedHashWithEquality(), |
| 72 | InheritedHashWithInequality(), |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 73 | ] |
| 74 | error_expected = [NoHash(), |
| 75 | OnlyEquality(), |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 76 | ] |
| 77 | |
| 78 | def test_default_hash(self): |
| 79 | for obj in self.default_expected: |
| 80 | self.assertEqual(hash(obj), _default_hash(obj)) |
| 81 | |
| 82 | def test_fixed_hash(self): |
| 83 | for obj in self.fixed_expected: |
| 84 | self.assertEqual(hash(obj), _FIXED_HASH_VALUE) |
| 85 | |
| 86 | def test_error_hash(self): |
| 87 | for obj in self.error_expected: |
| 88 | self.assertRaises(TypeError, hash, obj) |
| 89 | |
| 90 | def test_hashable(self): |
| 91 | objects = (self.default_expected + |
| 92 | self.fixed_expected) |
| 93 | for obj in objects: |
| 94 | self.assert_(isinstance(obj, Hashable), repr(obj)) |
| 95 | |
| 96 | def test_not_hashable(self): |
| 97 | for obj in self.error_expected: |
| 98 | self.assertFalse(isinstance(obj, Hashable), repr(obj)) |
| 99 | |
| 100 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 101 | # Issue #4701: Check that some builtin types are correctly hashable |
| 102 | class DefaultIterSeq(object): |
| 103 | seq = range(10) |
| 104 | def __len__(self): |
| 105 | return len(self.seq) |
| 106 | def __getitem__(self, index): |
| 107 | return self.seq[index] |
| 108 | |
| 109 | class HashBuiltinsTestCase(unittest.TestCase): |
| 110 | hashes_to_check = [range(10), |
| 111 | enumerate(range(10)), |
| 112 | iter(DefaultIterSeq()), |
| 113 | iter(lambda: 0, 0), |
| 114 | ] |
| 115 | |
| 116 | def test_hashes(self): |
| 117 | _default_hash = object.__hash__ |
| 118 | for obj in self.hashes_to_check: |
| 119 | self.assertEqual(hash(obj), _default_hash(obj)) |
| 120 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 121 | def test_main(): |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 122 | support.run_unittest(HashEqualityTestCase, |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 123 | HashInheritanceTestCase, |
| 124 | HashBuiltinsTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | if __name__ == "__main__": |
| 128 | test_main() |