1 # test the invariant that
2 # iff a==b then hash(a)==hash(b)
4 # Also test that hash implementations are inherited as expected
7 from test
import support
8 from collections
import Hashable
11 class HashEqualityTestCase(unittest
.TestCase
):
13 def same_hash(self
, *objlist
):
14 # Hash each object given and fail if
15 # the hash values are not all the same.
16 hashed
= list(map(hash, objlist
))
19 self
.fail("hashed values differ: %r" % (objlist
,))
21 def test_numeric_literals(self
):
22 self
.same_hash(1, 1, 1.0, 1.0+0.0j
)
23 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
)
27 def test_coerced_integers(self
):
28 self
.same_hash(int(1), int(1), float(1), complex(1),
29 int('1'), float('1.0'))
30 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))
36 self
.same_hash(int(2**63), float(2**63))
38 def test_coerced_floats(self
):
39 self
.same_hash(int(1.23e300
), float(1.23e300
))
40 self
.same_hash(float(0.5), complex(0.5, 0.0))
43 _default_hash
= object.__hash
__
44 class DefaultHash(object): pass
46 _FIXED_HASH_VALUE
= 42
47 class FixedHash(object):
49 return _FIXED_HASH_VALUE
51 class OnlyEquality(object):
52 def __eq__(self
, other
):
55 class OnlyInequality(object):
56 def __ne__(self
, other
):
57 return self
is not other
59 class InheritedHashWithEquality(FixedHash
, OnlyEquality
): pass
60 class InheritedHashWithInequality(FixedHash
, OnlyInequality
): pass
65 class HashInheritanceTestCase(unittest
.TestCase
):
66 default_expected
= [object(),
70 fixed_expected
= [FixedHash(),
71 InheritedHashWithEquality(),
72 InheritedHashWithInequality(),
74 error_expected
= [NoHash(),
78 def test_default_hash(self
):
79 for obj
in self
.default_expected
:
80 self
.assertEqual(hash(obj
), _default_hash(obj
))
82 def test_fixed_hash(self
):
83 for obj
in self
.fixed_expected
:
84 self
.assertEqual(hash(obj
), _FIXED_HASH_VALUE
)
86 def test_error_hash(self
):
87 for obj
in self
.error_expected
:
88 self
.assertRaises(TypeError, hash, obj
)
90 def test_hashable(self
):
91 objects
= (self
.default_expected
+
94 self
.assert_(isinstance(obj
, Hashable
), repr(obj
))
96 def test_not_hashable(self
):
97 for obj
in self
.error_expected
:
98 self
.assertFalse(isinstance(obj
, Hashable
), repr(obj
))
101 # Issue #4701: Check that some builtin types are correctly hashable
102 class DefaultIterSeq(object):
106 def __getitem__(self
, index
):
107 return self
.seq
[index
]
109 class HashBuiltinsTestCase(unittest
.TestCase
):
110 hashes_to_check
= [range(10),
111 enumerate(range(10)),
112 iter(DefaultIterSeq()),
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
))
122 support
.run_unittest(HashEqualityTestCase
,
123 HashInheritanceTestCase
,
124 HashBuiltinsTestCase
)
127 if __name__
== "__main__":