blob: fea1025e910550d0359bf809672ff3c5ddf58e79 [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))
Guilherme Polo887b3f22009-02-07 00:45:10 +000036 self.same_hash(int(2**63), float(2**63))
Fred Drake97656a12001-05-18 21:45:35 +000037
38 def test_coerced_floats(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000039 self.same_hash(int(1.23e300), float(1.23e300))
Fred Drake97656a12001-05-18 21:45:35 +000040 self.same_hash(float(0.5), complex(0.5, 0.0))
Fred Drake13634cf2000-06-29 19:17:04 +000041
42
Nick Coghland1abd252008-07-15 15:46:38 +000043_default_hash = object.__hash__
44class DefaultHash(object): pass
45
46_FIXED_HASH_VALUE = 42
47class FixedHash(object):
48 def __hash__(self):
49 return _FIXED_HASH_VALUE
50
51class OnlyEquality(object):
52 def __eq__(self, other):
53 return self is other
54
55class OnlyInequality(object):
56 def __ne__(self, other):
57 return self is not other
58
Nick Coghland1abd252008-07-15 15:46:38 +000059class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
60class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
Nick Coghland1abd252008-07-15 15:46:38 +000061
62class NoHash(object):
63 __hash__ = None
64
65class HashInheritanceTestCase(unittest.TestCase):
66 default_expected = [object(),
67 DefaultHash(),
68 OnlyInequality(),
69 ]
70 fixed_expected = [FixedHash(),
71 InheritedHashWithEquality(),
72 InheritedHashWithInequality(),
Nick Coghland1abd252008-07-15 15:46:38 +000073 ]
74 error_expected = [NoHash(),
75 OnlyEquality(),
Nick Coghland1abd252008-07-15 15:46:38 +000076 ]
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:
Ezio Melottie9615932010-01-24 19:26:24 +000094 self.assertIsInstance(obj, Hashable)
Nick Coghland1abd252008-07-15 15:46:38 +000095
96 def test_not_hashable(self):
97 for obj in self.error_expected:
Ezio Melottie9615932010-01-24 19:26:24 +000098 self.assertNotIsInstance(obj, Hashable)
Nick Coghland1abd252008-07-15 15:46:38 +000099
100
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000101# Issue #4701: Check that some builtin types are correctly hashable
102class 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
109class 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 Drake2e2be372001-09-20 21:33:42 +0000121def test_main():
Nick Coghland1abd252008-07-15 15:46:38 +0000122 support.run_unittest(HashEqualityTestCase,
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000123 HashInheritanceTestCase,
124 HashBuiltinsTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000125
126
127if __name__ == "__main__":
128 test_main()