Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Lib / test / test_hash.py
blob18ec04eecf83766b2f75af942d9a7f8cf5582c48
1 # test the invariant that
2 # iff a==b then hash(a)==hash(b)
4 # Also test that hash implementations are inherited as expected
6 import unittest
7 from test import test_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 = map(hash, objlist)
17 for h in hashed[1:]:
18 if h != hashed[0]:
19 self.fail("hashed values differ: %r" % (objlist,))
21 def test_numeric_literals(self):
22 self.same_hash(1, 1L, 1.0, 1.0+0.0j)
23 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)
27 def test_coerced_integers(self):
28 self.same_hash(int(1), long(1), float(1), complex(1),
29 int('1'), float('1.0'))
30 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))
38 self.same_hash(long(2**63), float(2**63))
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))
45 _default_hash = object.__hash__
46 class DefaultHash(object): pass
48 _FIXED_HASH_VALUE = 42
49 class FixedHash(object):
50 def __hash__(self):
51 return _FIXED_HASH_VALUE
53 class OnlyEquality(object):
54 def __eq__(self, other):
55 return self is other
56 # Trick to suppress Py3k warning in 2.x
57 __hash__ = None
58 del OnlyEquality.__hash__
60 class OnlyInequality(object):
61 def __ne__(self, other):
62 return self is not other
64 class OnlyCmp(object):
65 def __cmp__(self, other):
66 return cmp(id(self), id(other))
67 # Trick to suppress Py3k warning in 2.x
68 __hash__ = None
69 del OnlyCmp.__hash__
71 class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
72 class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
73 class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
75 class NoHash(object):
76 __hash__ = None
78 class HashInheritanceTestCase(unittest.TestCase):
79 default_expected = [object(),
80 DefaultHash(),
81 OnlyEquality(),
82 OnlyInequality(),
83 OnlyCmp(),
85 fixed_expected = [FixedHash(),
86 InheritedHashWithEquality(),
87 InheritedHashWithInequality(),
88 InheritedHashWithCmp(),
90 error_expected = [NoHash()]
92 def test_default_hash(self):
93 for obj in self.default_expected:
94 self.assertEqual(hash(obj), _default_hash(obj))
96 def test_fixed_hash(self):
97 for obj in self.fixed_expected:
98 self.assertEqual(hash(obj), _FIXED_HASH_VALUE)
100 def test_error_hash(self):
101 for obj in self.error_expected:
102 self.assertRaises(TypeError, hash, obj)
104 def test_hashable(self):
105 objects = (self.default_expected +
106 self.fixed_expected)
107 for obj in objects:
108 self.assertTrue(isinstance(obj, Hashable), repr(obj))
110 def test_not_hashable(self):
111 for obj in self.error_expected:
112 self.assertFalse(isinstance(obj, Hashable), repr(obj))
115 # 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)
118 class 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]
125 class HashBuiltinsTestCase(unittest.TestCase):
126 hashes_to_check = [xrange(10),
127 enumerate(xrange(10)),
128 iter(DefaultIterSeq()),
129 iter(lambda: 0, 0),
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))
137 def test_main():
138 test_support.run_unittest(HashEqualityTestCase,
139 HashInheritanceTestCase,
140 HashBuiltinsTestCase)
143 if __name__ == "__main__":
144 test_main()