blob: 56974b8cc3b236365edc80b051ab82a2dc701c19 [file] [log] [blame]
Fred Drake13634cf2000-06-29 19:17:04 +00001# test the invariant that
2# iff a==b then hash(a)==hash(b)
3#
Nick Coghland1abd252008-07-15 15:46:38 +00004# Also test that hash implementations are inherited as expected
Fred Drake13634cf2000-06-29 19:17:04 +00005
Fred Drake97656a12001-05-18 21:45:35 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Nick Coghland1abd252008-07-15 15:46:38 +00008from collections import Hashable
Fred Drake13634cf2000-06-29 19:17:04 +00009
10
Fred Drake97656a12001-05-18 21:45:35 +000011class HashEqualityTestCase(unittest.TestCase):
12
13 def same_hash(self, *objlist):
Fred Drakeacb117e2001-05-18 21:50:02 +000014 # Hash each object given and fail if
15 # the hash values are not all the same.
Guido van Rossumc1f779c2007-07-03 08:25:58 +000016 hashed = list(map(hash, objlist))
Fred Drake97656a12001-05-18 21:45:35 +000017 for h in hashed[1:]:
18 if h != hashed[0]:
Walter Dörwald70a6b492004-02-12 17:35:32 +000019 self.fail("hashed values differ: %r" % (objlist,))
Fred Drake97656a12001-05-18 21:45:35 +000020
21 def test_numeric_literals(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000022 self.same_hash(1, 1, 1.0, 1.0+0.0j)
Thomas Woutersce272b62007-09-19 21:19:28 +000023 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 Drake97656a12001-05-18 21:45:35 +000026
27 def test_coerced_integers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000028 self.same_hash(int(1), int(1), float(1), complex(1),
Fred Drake97656a12001-05-18 21:45:35 +000029 int('1'), float('1.0'))
Thomas Woutersce272b62007-09-19 21:19:28 +000030 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 Drake97656a12001-05-18 21:45:35 +000036
37 def test_coerced_floats(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000038 self.same_hash(int(1.23e300), float(1.23e300))
Fred Drake97656a12001-05-18 21:45:35 +000039 self.same_hash(float(0.5), complex(0.5, 0.0))
Fred Drake13634cf2000-06-29 19:17:04 +000040
41
Nick Coghland1abd252008-07-15 15:46:38 +000042_default_hash = object.__hash__
43class DefaultHash(object): pass
44
45_FIXED_HASH_VALUE = 42
46class FixedHash(object):
47 def __hash__(self):
48 return _FIXED_HASH_VALUE
49
50class OnlyEquality(object):
51 def __eq__(self, other):
52 return self is other
53
54class OnlyInequality(object):
55 def __ne__(self, other):
56 return self is not other
57
58class OnlyCmp(object):
59 def __cmp__(self, other):
60 return cmp(id(self), id(other))
61
62class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
63class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
64class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
65
66class NoHash(object):
67 __hash__ = None
68
69class 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 Drake2e2be372001-09-20 21:33:42 +0000107def test_main():
Nick Coghland1abd252008-07-15 15:46:38 +0000108 support.run_unittest(HashEqualityTestCase,
109 HashInheritanceTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000110
111
112if __name__ == "__main__":
113 test_main()