Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_hash.py

    r2 r391  
    44# Also test that hash implementations are inherited as expected
    55
     6import os
     7import sys
     8import struct
     9import datetime
    610import unittest
     11import subprocess
     12
    713from test import test_support
    814from collections import Hashable
     15
     16IS_64BIT = (struct.calcsize('l') == 8)
    917
    1018
     
    3644        self.same_hash(int(1-2**63), long(1-2**63))
    3745        self.same_hash(int(2**63-1), long(2**63-1))
     46        self.same_hash(long(2**63), float(2**63))
    3847
    3948    def test_coerced_floats(self):
     
    105114                   self.fixed_expected)
    106115        for obj in objects:
    107             self.assert_(isinstance(obj, Hashable), repr(obj))
     116            self.assertIsInstance(obj, Hashable)
    108117
    109118    def test_not_hashable(self):
    110119        for obj in self.error_expected:
    111             self.assertFalse(isinstance(obj, Hashable), repr(obj))
     120            self.assertNotIsInstance(obj, Hashable)
    112121
    113122
     
    134143            self.assertEqual(hash(obj), _default_hash(obj))
    135144
     145class HashRandomizationTests(unittest.TestCase):
     146
     147    # Each subclass should define a field "repr_", containing the repr() of
     148    # an object to be tested
     149
     150    def get_hash_command(self, repr_):
     151        return 'print(hash(%s))' % repr_
     152
     153    def get_hash(self, repr_, seed=None):
     154        env = os.environ.copy()
     155        if seed is not None:
     156            env['PYTHONHASHSEED'] = str(seed)
     157        else:
     158            env.pop('PYTHONHASHSEED', None)
     159        cmd_line = [sys.executable, '-c', self.get_hash_command(repr_)]
     160        p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
     161                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
     162                             env=env)
     163        out, err = p.communicate()
     164        out = test_support.strip_python_stderr(out)
     165        return int(out.strip())
     166
     167    def test_randomized_hash(self):
     168        # two runs should return different hashes
     169        run1 = self.get_hash(self.repr_, seed='random')
     170        run2 = self.get_hash(self.repr_, seed='random')
     171        self.assertNotEqual(run1, run2)
     172
     173class StringlikeHashRandomizationTests(HashRandomizationTests):
     174    def test_null_hash(self):
     175        # PYTHONHASHSEED=0 disables the randomized hash
     176        if IS_64BIT:
     177            known_hash_of_obj = 1453079729188098211
     178        else:
     179            known_hash_of_obj = -1600925533
     180
     181        # Randomization is disabled by default:
     182        self.assertEqual(self.get_hash(self.repr_), known_hash_of_obj)
     183
     184        # It can also be disabled by setting the seed to 0:
     185        self.assertEqual(self.get_hash(self.repr_, seed=0), known_hash_of_obj)
     186
     187    def test_fixed_hash(self):
     188        # test a fixed seed for the randomized hash
     189        # Note that all types share the same values:
     190        if IS_64BIT:
     191            if sys.byteorder == 'little':
     192                h = -4410911502303878509
     193            else:
     194                h = -3570150969479994130
     195        else:
     196            if sys.byteorder == 'little':
     197                h = -206076799
     198            else:
     199                h = -1024014457
     200        self.assertEqual(self.get_hash(self.repr_, seed=42), h)
     201
     202class StrHashRandomizationTests(StringlikeHashRandomizationTests):
     203    repr_ = repr('abc')
     204
     205    def test_empty_string(self):
     206        self.assertEqual(hash(""), 0)
     207
     208class UnicodeHashRandomizationTests(StringlikeHashRandomizationTests):
     209    repr_ = repr(u'abc')
     210
     211    def test_empty_string(self):
     212        self.assertEqual(hash(u""), 0)
     213
     214class BufferHashRandomizationTests(StringlikeHashRandomizationTests):
     215    repr_ = 'buffer("abc")'
     216
     217    def test_empty_string(self):
     218        self.assertEqual(hash(buffer("")), 0)
     219
     220class DatetimeTests(HashRandomizationTests):
     221    def get_hash_command(self, repr_):
     222        return 'import datetime; print(hash(%s))' % repr_
     223
     224class DatetimeDateTests(DatetimeTests):
     225    repr_ = repr(datetime.date(1066, 10, 14))
     226
     227class DatetimeDatetimeTests(DatetimeTests):
     228    repr_ = repr(datetime.datetime(1, 2, 3, 4, 5, 6, 7))
     229
     230class DatetimeTimeTests(DatetimeTests):
     231    repr_ = repr(datetime.time(0))
     232
     233
    136234def test_main():
    137235    test_support.run_unittest(HashEqualityTestCase,
    138236                              HashInheritanceTestCase,
    139                               HashBuiltinsTestCase)
     237                              HashBuiltinsTestCase,
     238                              StrHashRandomizationTests,
     239                              UnicodeHashRandomizationTests,
     240                              BufferHashRandomizationTests,
     241                              DatetimeDateTests,
     242                              DatetimeDatetimeTests,
     243                              DatetimeTimeTests)
     244
    140245
    141246
Note: See TracChangeset for help on using the changeset viewer.