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)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 36 | |
| 37 | def test_coerced_floats(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 38 | self.same_hash(int(1.23e300), float(1.23e300)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 39 | self.same_hash(float(0.5), complex(0.5, 0.0)) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 40 | |
| 41 | |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 42 | _default_hash = object.__hash__ |
| 43 | class DefaultHash(object): pass |
| 44 | |
| 45 | _FIXED_HASH_VALUE = 42 |
| 46 | class FixedHash(object): |
| 47 | def __hash__(self): |
| 48 | return _FIXED_HASH_VALUE |
| 49 | |
| 50 | class OnlyEquality(object): |
| 51 | def __eq__(self, other): |
| 52 | return self is other |
| 53 | |
| 54 | class OnlyInequality(object): |
| 55 | def __ne__(self, other): |
| 56 | return self is not other |
| 57 | |
| 58 | class OnlyCmp(object): |
| 59 | def __cmp__(self, other): |
| 60 | return cmp(id(self), id(other)) |
| 61 | |
| 62 | class InheritedHashWithEquality(FixedHash, OnlyEquality): pass |
| 63 | class InheritedHashWithInequality(FixedHash, OnlyInequality): pass |
| 64 | class InheritedHashWithCmp(FixedHash, OnlyCmp): pass |
| 65 | |
| 66 | class NoHash(object): |
| 67 | __hash__ = None |
| 68 | |
| 69 | class HashInheritanceTestCase(unittest.TestCase): |
| 70 | default_expected = [object(), |
| 71 | DefaultHash(), |
| 72 | OnlyInequality(), |
| 73 | ] |
| 74 | fixed_expected = [FixedHash(), |
| 75 | InheritedHashWithEquality(), |
| 76 | InheritedHashWithInequality(), |
| 77 | InheritedHashWithCmp(), |
| 78 | ] |
| 79 | error_expected = [NoHash(), |
| 80 | OnlyEquality(), |
| 81 | OnlyCmp(), |
| 82 | ] |
| 83 | |
| 84 | def test_default_hash(self): |
| 85 | for obj in self.default_expected: |
| 86 | self.assertEqual(hash(obj), _default_hash(obj)) |
| 87 | |
| 88 | def test_fixed_hash(self): |
| 89 | for obj in self.fixed_expected: |
| 90 | self.assertEqual(hash(obj), _FIXED_HASH_VALUE) |
| 91 | |
| 92 | def test_error_hash(self): |
| 93 | for obj in self.error_expected: |
| 94 | self.assertRaises(TypeError, hash, obj) |
| 95 | |
| 96 | def test_hashable(self): |
| 97 | objects = (self.default_expected + |
| 98 | self.fixed_expected) |
| 99 | for obj in objects: |
| 100 | self.assert_(isinstance(obj, Hashable), repr(obj)) |
| 101 | |
| 102 | def test_not_hashable(self): |
| 103 | for obj in self.error_expected: |
| 104 | self.assertFalse(isinstance(obj, Hashable), repr(obj)) |
| 105 | |
| 106 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 107 | def test_main(): |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 108 | support.run_unittest(HashEqualityTestCase, |
| 109 | HashInheritanceTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | if __name__ == "__main__": |
| 113 | test_main() |