blob: 807c2c46b3638aaf7f77052385743b70e29fdc4b [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(),
Nick Coghland1abd252008-07-15 15:46:38 +000081 ]
82
83 def test_default_hash(self):
84 for obj in self.default_expected:
85 self.assertEqual(hash(obj), _default_hash(obj))
86
87 def test_fixed_hash(self):
88 for obj in self.fixed_expected:
89 self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
90
91 def test_error_hash(self):
92 for obj in self.error_expected:
93 self.assertRaises(TypeError, hash, obj)
94
95 def test_hashable(self):
96 objects = (self.default_expected +
97 self.fixed_expected)
98 for obj in objects:
99 self.assert_(isinstance(obj, Hashable), repr(obj))
100
101 def test_not_hashable(self):
102 for obj in self.error_expected:
103 self.assertFalse(isinstance(obj, Hashable), repr(obj))
104
105
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000106# Issue #4701: Check that some builtin types are correctly hashable
107class DefaultIterSeq(object):
108 seq = range(10)
109 def __len__(self):
110 return len(self.seq)
111 def __getitem__(self, index):
112 return self.seq[index]
113
114class HashBuiltinsTestCase(unittest.TestCase):
115 hashes_to_check = [range(10),
116 enumerate(range(10)),
117 iter(DefaultIterSeq()),
118 iter(lambda: 0, 0),
119 ]
120
121 def test_hashes(self):
122 _default_hash = object.__hash__
123 for obj in self.hashes_to_check:
124 self.assertEqual(hash(obj), _default_hash(obj))
125
Fred Drake2e2be372001-09-20 21:33:42 +0000126def test_main():
Nick Coghland1abd252008-07-15 15:46:38 +0000127 support.run_unittest(HashEqualityTestCase,
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000128 HashInheritanceTestCase,
129 HashBuiltinsTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000130
131
132if __name__ == "__main__":
133 test_main()