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)) |
Fred Drake | 97656a1 | 2001-05-18 21:45:35 +0000 | [diff] [blame] | 38 | |
| 39 | def test_coerced_floats(self): |
| 40 | self.same_hash(long(1.23e300), float(1.23e300)) |
| 41 | self.same_hash(float(0.5), complex(0.5, 0.0)) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 42 | |
| 43 | |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 44 | _default_hash = object.__hash__ |
| 45 | class DefaultHash(object): pass |
| 46 | |
| 47 | _FIXED_HASH_VALUE = 42 |
| 48 | class FixedHash(object): |
| 49 | def __hash__(self): |
| 50 | return _FIXED_HASH_VALUE |
| 51 | |
| 52 | class OnlyEquality(object): |
| 53 | def __eq__(self, other): |
| 54 | return self is other |
| 55 | |
| 56 | class OnlyInequality(object): |
| 57 | def __ne__(self, other): |
| 58 | return self is not other |
| 59 | |
| 60 | class OnlyCmp(object): |
| 61 | def __cmp__(self, other): |
| 62 | return cmp(id(self), id(other)) |
| 63 | |
| 64 | class InheritedHashWithEquality(FixedHash, OnlyEquality): pass |
| 65 | class InheritedHashWithInequality(FixedHash, OnlyInequality): pass |
| 66 | class InheritedHashWithCmp(FixedHash, OnlyCmp): pass |
| 67 | |
| 68 | class NoHash(object): |
| 69 | __hash__ = None |
| 70 | |
| 71 | class HashInheritanceTestCase(unittest.TestCase): |
| 72 | default_expected = [object(), |
| 73 | DefaultHash(), |
| 74 | ] |
| 75 | fixed_expected = [FixedHash(), |
| 76 | InheritedHashWithEquality(), |
| 77 | InheritedHashWithInequality(), |
| 78 | InheritedHashWithCmp(), |
| 79 | ] |
| 80 | # TODO: Change these to expecting an exception |
| 81 | # when forward porting to Py3k |
| 82 | warning_expected = [OnlyEquality(), |
| 83 | OnlyInequality(), |
| 84 | OnlyCmp(), |
| 85 | ] |
| 86 | error_expected = [NoHash()] |
| 87 | |
| 88 | def test_default_hash(self): |
| 89 | for obj in self.default_expected: |
| 90 | self.assertEqual(hash(obj), _default_hash(obj)) |
| 91 | |
| 92 | def test_fixed_hash(self): |
| 93 | for obj in self.fixed_expected: |
| 94 | self.assertEqual(hash(obj), _FIXED_HASH_VALUE) |
| 95 | |
| 96 | def test_warning_hash(self): |
| 97 | for obj in self.warning_expected: |
| 98 | # TODO: Check for the expected Py3k warning here |
| 99 | obj_hash = hash(obj) |
| 100 | self.assertEqual(obj_hash, _default_hash(obj)) |
| 101 | |
| 102 | def test_error_hash(self): |
| 103 | for obj in self.error_expected: |
| 104 | self.assertRaises(TypeError, hash, obj) |
| 105 | |
| 106 | def test_hashable(self): |
| 107 | objects = (self.default_expected + |
| 108 | self.fixed_expected + |
| 109 | self.warning_expected) |
| 110 | for obj in objects: |
| 111 | self.assert_(isinstance(obj, Hashable), repr(obj)) |
| 112 | |
| 113 | def test_not_hashable(self): |
| 114 | for obj in self.error_expected: |
| 115 | self.assertFalse(isinstance(obj, Hashable), repr(obj)) |
| 116 | |
| 117 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 118 | def test_main(): |
Nick Coghlan | 53663a6 | 2008-07-15 14:27:37 +0000 | [diff] [blame] | 119 | test_support.run_unittest(HashEqualityTestCase, |
| 120 | HashInheritanceTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | if __name__ == "__main__": |
| 124 | test_main() |