blob: 7ce40b95b7c76d551d62f26927e9d786ba67b41c [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 Coghlan53663a62008-07-15 14:27:37 +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
Barry Warsaw04f357c2002-07-23 19:04:11 +00007from test import test_support
Nick Coghlan53663a62008-07-15 14:27:37 +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.
Fred Drake97656a12001-05-18 21:45:35 +000016 hashed = map(hash, objlist)
17 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):
22 self.same_hash(1, 1L, 1.0, 1.0+0.0j)
Facundo Batistad544df72007-09-19 15:10:06 +000023 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 Drake97656a12001-05-18 21:45:35 +000026
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 Batistad544df72007-09-19 15:10:06 +000030 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 Drake97656a12001-05-18 21:45:35 +000038
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 Drake13634cf2000-06-29 19:17:04 +000042
43
Nick Coghlan53663a62008-07-15 14:27:37 +000044_default_hash = object.__hash__
45class DefaultHash(object): pass
46
47_FIXED_HASH_VALUE = 42
48class FixedHash(object):
49 def __hash__(self):
50 return _FIXED_HASH_VALUE
51
52class OnlyEquality(object):
53 def __eq__(self, other):
54 return self is other
Nick Coghlan48361f52008-08-11 15:45:58 +000055 # Trick to suppress Py3k warning in 2.x
56 __hash__ = None
57del OnlyEquality.__hash__
Nick Coghlan53663a62008-07-15 14:27:37 +000058
59class OnlyInequality(object):
60 def __ne__(self, other):
61 return self is not other
62
63class OnlyCmp(object):
64 def __cmp__(self, other):
65 return cmp(id(self), id(other))
Nick Coghlan48361f52008-08-11 15:45:58 +000066 # Trick to suppress Py3k warning in 2.x
67 __hash__ = None
68del OnlyCmp.__hash__
Nick Coghlan53663a62008-07-15 14:27:37 +000069
70class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
71class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
72class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
73
74class NoHash(object):
75 __hash__ = None
76
77class HashInheritanceTestCase(unittest.TestCase):
78 default_expected = [object(),
79 DefaultHash(),
Nick Coghlan48361f52008-08-11 15:45:58 +000080 OnlyEquality(),
81 OnlyInequality(),
82 OnlyCmp(),
Nick Coghlan53663a62008-07-15 14:27:37 +000083 ]
84 fixed_expected = [FixedHash(),
85 InheritedHashWithEquality(),
86 InheritedHashWithInequality(),
87 InheritedHashWithCmp(),
88 ]
Nick Coghlan53663a62008-07-15 14:27:37 +000089 error_expected = [NoHash()]
90
91 def test_default_hash(self):
92 for obj in self.default_expected:
93 self.assertEqual(hash(obj), _default_hash(obj))
94
95 def test_fixed_hash(self):
96 for obj in self.fixed_expected:
97 self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
98
Nick Coghlan53663a62008-07-15 14:27:37 +000099 def test_error_hash(self):
100 for obj in self.error_expected:
101 self.assertRaises(TypeError, hash, obj)
102
103 def test_hashable(self):
104 objects = (self.default_expected +
Nick Coghlan48361f52008-08-11 15:45:58 +0000105 self.fixed_expected)
Nick Coghlan53663a62008-07-15 14:27:37 +0000106 for obj in objects:
107 self.assert_(isinstance(obj, Hashable), repr(obj))
108
109 def test_not_hashable(self):
110 for obj in self.error_expected:
111 self.assertFalse(isinstance(obj, Hashable), repr(obj))
112
113
Nick Coghland4656402008-12-30 01:36:00 +0000114# Issue #4701: Check that some builtin types are correctly hashable
115# (This test only used to fail in Python 3.0, but has been included
116# in 2.x along with the lazy call to PyType_Ready in PyObject_Hash)
117class DefaultIterSeq(object):
118 seq = range(10)
119 def __len__(self):
120 return len(self.seq)
121 def __getitem__(self, index):
122 return self.seq[index]
123
124class HashBuiltinsTestCase(unittest.TestCase):
125 hashes_to_check = [xrange(10),
126 enumerate(xrange(10)),
127 iter(DefaultIterSeq()),
128 iter(lambda: 0, 0),
129 ]
130
131 def test_hashes(self):
132 _default_hash = object.__hash__
133 for obj in self.hashes_to_check:
134 self.assertEqual(hash(obj), _default_hash(obj))
135
Fred Drake2e2be372001-09-20 21:33:42 +0000136def test_main():
Nick Coghlan53663a62008-07-15 14:27:37 +0000137 test_support.run_unittest(HashEqualityTestCase,
Nick Coghland4656402008-12-30 01:36:00 +0000138 HashInheritanceTestCase,
139 HashBuiltinsTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000140
141
142if __name__ == "__main__":
143 test_main()