diff options
Diffstat (limited to 'Lib/test/test_hash.py')
-rw-r--r-- | Lib/test/test_hash.py | 106 |
1 files changed, 40 insertions, 66 deletions
diff --git a/Lib/test/test_hash.py b/Lib/test/test_hash.py index dec7dcbd7c8..c0dd34dc7ff 100644 --- a/Lib/test/test_hash.py +++ b/Lib/test/test_hash.py @@ -3,17 +3,15 @@ # # Also test that hash implementations are inherited as expected +import datetime import os import sys -import struct -import datetime import unittest -import subprocess - -from test import test_support +from test import support +from test.script_helper import assert_python_ok from collections import Hashable -IS_64BIT = (struct.calcsize('l') == 8) +IS_64BIT = sys.maxsize > 2**32 class HashEqualityTestCase(unittest.TestCase): @@ -21,32 +19,30 @@ class HashEqualityTestCase(unittest.TestCase): def same_hash(self, *objlist): # Hash each object given and fail if # the hash values are not all the same. - hashed = map(hash, objlist) + hashed = list(map(hash, objlist)) for h in hashed[1:]: if h != hashed[0]: self.fail("hashed values differ: %r" % (objlist,)) def test_numeric_literals(self): - self.same_hash(1, 1L, 1.0, 1.0+0.0j) - self.same_hash(0, 0L, 0.0, 0.0+0.0j) - self.same_hash(-1, -1L, -1.0, -1.0+0.0j) - self.same_hash(-2, -2L, -2.0, -2.0+0.0j) + self.same_hash(1, 1, 1.0, 1.0+0.0j) + self.same_hash(0, 0.0, 0.0+0.0j) + self.same_hash(-1, -1.0, -1.0+0.0j) + self.same_hash(-2, -2.0, -2.0+0.0j) def test_coerced_integers(self): - self.same_hash(int(1), long(1), float(1), complex(1), + self.same_hash(int(1), int(1), float(1), complex(1), int('1'), float('1.0')) - self.same_hash(int(-2**31), long(-2**31), float(-2**31)) - self.same_hash(int(1-2**31), long(1-2**31), float(1-2**31)) - self.same_hash(int(2**31-1), long(2**31-1), float(2**31-1)) + self.same_hash(int(-2**31), float(-2**31)) + self.same_hash(int(1-2**31), float(1-2**31)) + self.same_hash(int(2**31-1), float(2**31-1)) # for 64-bit platforms - self.same_hash(int(2**31), long(2**31), float(2**31)) - self.same_hash(int(-2**63), long(-2**63), float(-2**63)) - self.same_hash(int(1-2**63), long(1-2**63)) - self.same_hash(int(2**63-1), long(2**63-1)) - self.same_hash(long(2**63), float(2**63)) + self.same_hash(int(2**31), float(2**31)) + self.same_hash(int(-2**63), float(-2**63)) + self.same_hash(int(2**63), float(2**63)) def test_coerced_floats(self): - self.same_hash(long(1.23e300), float(1.23e300)) + self.same_hash(int(1.23e300), float(1.23e300)) self.same_hash(float(0.5), complex(0.5, 0.0)) @@ -61,24 +57,13 @@ class FixedHash(object): class OnlyEquality(object): def __eq__(self, other): return self is other - # Trick to suppress Py3k warning in 2.x - __hash__ = None -del OnlyEquality.__hash__ class OnlyInequality(object): def __ne__(self, other): return self is not other -class OnlyCmp(object): - def __cmp__(self, other): - return cmp(id(self), id(other)) - # Trick to suppress Py3k warning in 2.x - __hash__ = None -del OnlyCmp.__hash__ - class InheritedHashWithEquality(FixedHash, OnlyEquality): pass class InheritedHashWithInequality(FixedHash, OnlyInequality): pass -class InheritedHashWithCmp(FixedHash, OnlyCmp): pass class NoHash(object): __hash__ = None @@ -86,16 +71,15 @@ class NoHash(object): class HashInheritanceTestCase(unittest.TestCase): default_expected = [object(), DefaultHash(), - OnlyEquality(), OnlyInequality(), - OnlyCmp(), ] fixed_expected = [FixedHash(), InheritedHashWithEquality(), InheritedHashWithInequality(), - InheritedHashWithCmp(), ] - error_expected = [NoHash()] + error_expected = [NoHash(), + OnlyEquality(), + ] def test_default_hash(self): for obj in self.default_expected: @@ -121,8 +105,6 @@ class HashInheritanceTestCase(unittest.TestCase): # Issue #4701: Check that some builtin types are correctly hashable -# (This test only used to fail in Python 3.0, but has been included -# in 2.x along with the lazy call to PyType_Ready in PyObject_Hash) class DefaultIterSeq(object): seq = range(10) def __len__(self): @@ -131,8 +113,8 @@ class DefaultIterSeq(object): return self.seq[index] class HashBuiltinsTestCase(unittest.TestCase): - hashes_to_check = [xrange(10), - enumerate(xrange(10)), + hashes_to_check = [range(10), + enumerate(range(10)), iter(DefaultIterSeq()), iter(lambda: 0, 0), ] @@ -152,17 +134,17 @@ class HashRandomizationTests(unittest.TestCase): def get_hash(self, repr_, seed=None): env = os.environ.copy() + env['__cleanenv'] = True # signal to assert_python not to do a copy + # of os.environ on its own if seed is not None: env['PYTHONHASHSEED'] = str(seed) else: env.pop('PYTHONHASHSEED', None) - cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)] - p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - env=env) - out, err = p.communicate() - out = test_support.strip_python_stderr(out) - return int(out.strip()) + out = assert_python_ok( + '-c', self.get_hash_command(repr_), + **env) + stdout = out[1].strip() + return int(stdout) def test_randomized_hash(self): # two runs should return different hashes @@ -205,17 +187,11 @@ class StrHashRandomizationTests(StringlikeHashRandomizationTests): def test_empty_string(self): self.assertEqual(hash(""), 0) -class UnicodeHashRandomizationTests(StringlikeHashRandomizationTests): - repr_ = repr(u'abc') +class BytesHashRandomizationTests(StringlikeHashRandomizationTests): + repr_ = repr(b'abc') def test_empty_string(self): - self.assertEqual(hash(u""), 0) - -class BufferHashRandomizationTests(StringlikeHashRandomizationTests): - repr_ = 'buffer("abc")' - - def test_empty_string(self): - self.assertEqual(hash(buffer("")), 0) + self.assertEqual(hash(b""), 0) class DatetimeTests(HashRandomizationTests): def get_hash_command(self, repr_): @@ -232,16 +208,14 @@ class DatetimeTimeTests(DatetimeTests): def test_main(): - test_support.run_unittest(HashEqualityTestCase, - HashInheritanceTestCase, - HashBuiltinsTestCase, - StrHashRandomizationTests, - UnicodeHashRandomizationTests, - BufferHashRandomizationTests, - DatetimeDateTests, - DatetimeDatetimeTests, - DatetimeTimeTests) - + support.run_unittest(HashEqualityTestCase, + HashInheritanceTestCase, + HashBuiltinsTestCase, + StrHashRandomizationTests, + BytesHashRandomizationTests, + DatetimeDateTests, + DatetimeDatetimeTests, + DatetimeTimeTests) if __name__ == "__main__": |