make file closing more robust
[python/dscho.git] / Lib / test / test_hash.py
blob5babc5a9a497ab0461ab4882965aa01272734252
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 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))
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, 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):
48 def __hash__(self):
49 return _FIXED_HASH_VALUE
51 class OnlyEquality(object):
52 def __eq__(self, other):
53 return self is 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
62 class NoHash(object):
63 __hash__ = None
65 class HashInheritanceTestCase(unittest.TestCase):
66 default_expected = [object(),
67 DefaultHash(),
68 OnlyInequality(),
70 fixed_expected = [FixedHash(),
71 InheritedHashWithEquality(),
72 InheritedHashWithInequality(),
74 error_expected = [NoHash(),
75 OnlyEquality(),
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 +
92 self.fixed_expected)
93 for obj in objects:
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):
103 seq = range(10)
104 def __len__(self):
105 return len(self.seq)
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()),
113 iter(lambda: 0, 0),
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))
121 def test_main():
122 support.run_unittest(HashEqualityTestCase,
123 HashInheritanceTestCase,
124 HashBuiltinsTestCase)
127 if __name__ == "__main__":
128 test_main()