]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_hash.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_hash.py
1 # test the invariant that
2 # iff a==b then hash(a)==hash(b)
3 #
4 # Also test that hash implementations are inherited as expected
5
6 import unittest
7 from test import test_support
8 from collections import Hashable
9
10
11 class HashEqualityTestCase(unittest.TestCase):
12
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,))
20
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)
26
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))
39
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))
43
44
45 _default_hash = object.__hash__
46 class DefaultHash(object): pass
47
48 _FIXED_HASH_VALUE = 42
49 class FixedHash(object):
50 def __hash__(self):
51 return _FIXED_HASH_VALUE
52
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__
59
60 class OnlyInequality(object):
61 def __ne__(self, other):
62 return self is not other
63
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__
70
71 class InheritedHashWithEquality(FixedHash, OnlyEquality): pass
72 class InheritedHashWithInequality(FixedHash, OnlyInequality): pass
73 class InheritedHashWithCmp(FixedHash, OnlyCmp): pass
74
75 class NoHash(object):
76 __hash__ = None
77
78 class HashInheritanceTestCase(unittest.TestCase):
79 default_expected = [object(),
80 DefaultHash(),
81 OnlyEquality(),
82 OnlyInequality(),
83 OnlyCmp(),
84 ]
85 fixed_expected = [FixedHash(),
86 InheritedHashWithEquality(),
87 InheritedHashWithInequality(),
88 InheritedHashWithCmp(),
89 ]
90 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
100 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 +
106 self.fixed_expected)
107 for obj in objects:
108 self.assertIsInstance(obj, Hashable)
109
110 def test_not_hashable(self):
111 for obj in self.error_expected:
112 self.assertNotIsInstance(obj, Hashable)
113
114
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]
124
125 class 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
137 def test_main():
138 test_support.run_unittest(HashEqualityTestCase,
139 HashInheritanceTestCase,
140 HashBuiltinsTestCase)
141
142
143 if __name__ == "__main__":
144 test_main()