blob: 5881d03c51423191f0b3ecad76cd4ae71e65864a [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
Nick Coghland1abd252008-07-15 15:46:38 +000058class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
59class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
Nick Coghland1abd252008-07-15 15:46:38 +000060
61class NoHash(object):
62 __hash__ = None
63
64class HashInheritanceTestCase(unittest.TestCase):
65 default_expected = [object(),
66 DefaultHash(),
67 OnlyInequality(),
68 ]
69 fixed_expected = [FixedHash(),
70 InheritedHashWithEquality(),
71 InheritedHashWithInequality(),
Nick Coghland1abd252008-07-15 15:46:38 +000072 ]
73 error_expected = [NoHash(),
74 OnlyEquality(),
Nick Coghland1abd252008-07-15 15:46:38 +000075 ]
76
77 def test_default_hash(self):
78 for obj in self.default_expected:
79 self.assertEqual(hash(obj), _default_hash(obj))
80
81 def test_fixed_hash(self):
82 for obj in self.fixed_expected:
83 self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
84
85 def test_error_hash(self):
86 for obj in self.error_expected:
87 self.assertRaises(TypeError, hash, obj)
88
89 def test_hashable(self):
90 objects = (self.default_expected +
91 self.fixed_expected)
92 for obj in objects:
93 self.assert_(isinstance(obj, Hashable), repr(obj))
94
95 def test_not_hashable(self):
96 for obj in self.error_expected:
97 self.assertFalse(isinstance(obj, Hashable), repr(obj))
98
99
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000100# Issue #4701: Check that some builtin types are correctly hashable
101class DefaultIterSeq(object):
102 seq = range(10)
103 def __len__(self):
104 return len(self.seq)
105 def __getitem__(self, index):
106 return self.seq[index]
107
108class HashBuiltinsTestCase(unittest.TestCase):
109 hashes_to_check = [range(10),
110 enumerate(range(10)),
111 iter(DefaultIterSeq()),
112 iter(lambda: 0, 0),
113 ]
114
115 def test_hashes(self):
116 _default_hash = object.__hash__
117 for obj in self.hashes_to_check:
118 self.assertEqual(hash(obj), _default_hash(obj))
119
Fred Drake2e2be372001-09-20 21:33:42 +0000120def test_main():
Nick Coghland1abd252008-07-15 15:46:38 +0000121 support.run_unittest(HashEqualityTestCase,
Nick Coghlanf1f2f682008-12-30 07:29:12 +0000122 HashInheritanceTestCase,
123 HashBuiltinsTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000124
125
126if __name__ == "__main__":
127 test_main()