blob: fa0fb2378f27fdd7c8ccc52b9ee44e7da8f9e9d6 [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))
Mark Dickinsonf709ab82009-01-31 16:44:04 +000038 self.same_hash(long(2**63), float(2**63))
Fred Drake97656a12001-05-18 21:45:35 +000039
40 def test_coerced_floats(self):
41 self.same_hash(long(1.23e300), float(1.23e300))
42 self.same_hash(float(0.5), complex(0.5, 0.0))
Fred Drake13634cf2000-06-29 19:17:04 +000043
44
Nick Coghlan53663a62008-07-15 14:27:37 +000045_default_hash = object.__hash__
46class DefaultHash(object): pass
47
48_FIXED_HASH_VALUE = 42
49class FixedHash(object):
50 def __hash__(self):
51 return _FIXED_HASH_VALUE
52
53class OnlyEquality(object):
54 def __eq__(self, other):
55 return self is other
Nick Coghlan48361f52008-08-11 15:45:58 +000056 # Trick to suppress Py3k warning in 2.x
57 __hash__ = None
58del OnlyEquality.__hash__
Nick Coghlan53663a62008-07-15 14:27:37 +000059
60class OnlyInequality(object):
61 def __ne__(self, other):
62 return self is not other
63
64class OnlyCmp(object):
65 def __cmp__(self, other):
66 return cmp(id(self), id(other))
Nick Coghlan48361f52008-08-11 15:45:58 +000067 # Trick to suppress Py3k warning in 2.x
68 __hash__ = None
69del OnlyCmp.__hash__
Nick Coghlan53663a62008-07-15 14:27:37 +000070
71class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
72class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
73class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
74
75class NoHash(object):
76 __hash__ = None
77
78class HashInheritanceTestCase(unittest.TestCase):
79 default_expected = [object(),
80 DefaultHash(),
Nick Coghlan48361f52008-08-11 15:45:58 +000081 OnlyEquality(),
82 OnlyInequality(),
83 OnlyCmp(),
Nick Coghlan53663a62008-07-15 14:27:37 +000084 ]
85 fixed_expected = [FixedHash(),
86 InheritedHashWithEquality(),
87 InheritedHashWithInequality(),
88 InheritedHashWithCmp(),
89 ]
Nick Coghlan53663a62008-07-15 14:27:37 +000090 error_expected = [NoHash()]
91
92 def test_default_hash(self):
93 for obj in self.default_expected:
94 self.assertEqual(hash(obj), _default_hash(obj))
95
96 def test_fixed_hash(self):
97 for obj in self.fixed_expected:
98 self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
99
Nick Coghlan53663a62008-07-15 14:27:37 +0000100 def test_error_hash(self):
101 for obj in self.error_expected:
102 self.assertRaises(TypeError, hash, obj)
103
104 def test_hashable(self):
105 objects = (self.default_expected +
Nick Coghlan48361f52008-08-11 15:45:58 +0000106 self.fixed_expected)
Nick Coghlan53663a62008-07-15 14:27:37 +0000107 for obj in objects:
108 self.assert_(isinstance(obj, Hashable), repr(obj))
109
110 def test_not_hashable(self):
111 for obj in self.error_expected:
112 self.assertFalse(isinstance(obj, Hashable), repr(obj))
113
114
Nick Coghlan180e4002008-12-30 01:18:48 +0000115# Issue #4701: Check that some builtin types are correctly hashable
116# (This test only used to fail in Python 3.0, but has been included
117# in 2.x along with the lazy call to PyType_Ready in PyObject_Hash)
118class DefaultIterSeq(object):
119 seq = range(10)
120 def __len__(self):
121 return len(self.seq)
122 def __getitem__(self, index):
123 return self.seq[index]
124
125class HashBuiltinsTestCase(unittest.TestCase):
126 hashes_to_check = [xrange(10),
127 enumerate(xrange(10)),
128 iter(DefaultIterSeq()),
129 iter(lambda: 0, 0),
130 ]
131
132 def test_hashes(self):
133 _default_hash = object.__hash__
134 for obj in self.hashes_to_check:
135 self.assertEqual(hash(obj), _default_hash(obj))
136
Fred Drake2e2be372001-09-20 21:33:42 +0000137def test_main():
Nick Coghlan53663a62008-07-15 14:27:37 +0000138 test_support.run_unittest(HashEqualityTestCase,
Nick Coghlan180e4002008-12-30 01:18:48 +0000139 HashInheritanceTestCase,
140 HashBuiltinsTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000141
142
143if __name__ == "__main__":
144 test_main()