<s> import os <EOL> import sys <EOL> import struct <EOL> import datetime <EOL> import unittest <EOL> import subprocess <EOL> from test import test_support <EOL> from collections import Hashable <EOL> IS_64BIT = ( struct . calcsize ( '<STR_LIT:l>' ) == <NUM_LIT:8> ) <EOL> class HashEqualityTestCase ( unittest . TestCase ) : <EOL> def same_hash ( self , * objlist ) : <EOL> hashed = map ( hash , objlist ) <EOL> for h in hashed [ <NUM_LIT:1> : ] : <EOL> if h != hashed [ <NUM_LIT:0> ] : <EOL> self . fail ( "<STR_LIT>" % ( objlist , ) ) <EOL> def test_numeric_literals ( self ) : <EOL> self . same_hash ( <NUM_LIT:1> , <NUM_LIT:1> L , <NUM_LIT:1.0> , <NUM_LIT:1.0> + <NUM_LIT> ) <EOL> self . same_hash ( <NUM_LIT:0> , <NUM_LIT:0> L , <NUM_LIT:0.0> , <NUM_LIT:0.0> + <NUM_LIT> ) <EOL> self . same_hash ( - <NUM_LIT:1> , - <NUM_LIT:1> L , - <NUM_LIT:1.0> , - <NUM_LIT:1.0> + <NUM_LIT> ) <EOL> self . same_hash ( - <NUM_LIT:2> , - <NUM_LIT:2> L , - <NUM_LIT> , - <NUM_LIT> + <NUM_LIT> ) <EOL> def test_coerced_integers ( self ) : <EOL> self . same_hash ( int ( <NUM_LIT:1> ) , long ( <NUM_LIT:1> ) , float ( <NUM_LIT:1> ) , complex ( <NUM_LIT:1> ) , <EOL> int ( '<STR_LIT:1>' ) , float ( '<STR_LIT:1.0>' ) ) <EOL> self . same_hash ( int ( - <NUM_LIT:2> ** <NUM_LIT> ) , long ( - <NUM_LIT:2> ** <NUM_LIT> ) , float ( - <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> self . same_hash ( int ( <NUM_LIT:1> - <NUM_LIT:2> ** <NUM_LIT> ) , long ( <NUM_LIT:1> - <NUM_LIT:2> ** <NUM_LIT> ) , float ( <NUM_LIT:1> - <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> self . same_hash ( int ( <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> ) , long ( <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> ) , float ( <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> self . same_hash ( int ( <NUM_LIT:2> ** <NUM_LIT> ) , long ( <NUM_LIT:2> ** <NUM_LIT> ) , float ( <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> self . same_hash ( int ( - <NUM_LIT:2> ** <NUM_LIT> ) , long ( - <NUM_LIT:2> ** <NUM_LIT> ) , float ( - <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> self . same_hash ( int ( <NUM_LIT:1> - <NUM_LIT:2> ** <NUM_LIT> ) , long ( <NUM_LIT:1> - <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> self . same_hash ( int ( <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> ) , long ( <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> ) ) <EOL> self . same_hash ( long ( <NUM_LIT:2> ** <NUM_LIT> ) , float ( <NUM_LIT:2> ** <NUM_LIT> ) ) <EOL> def test_coerced_floats ( self ) : <EOL> self . same_hash ( long ( <NUM_LIT> ) , float ( <NUM_LIT> ) ) <EOL> self . same_hash ( float ( <NUM_LIT:0.5> ) , complex ( <NUM_LIT:0.5> , <NUM_LIT:0.0> ) ) <EOL> _default_hash = object . __hash__ <EOL> class DefaultHash ( object ) : pass <EOL> _FIXED_HASH_VALUE = <NUM_LIT> <EOL> class FixedHash ( object ) : <EOL> def __hash__ ( self ) : <EOL> return _FIXED_HASH_VALUE <EOL> class OnlyEquality ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> return self is other <EOL> __hash__ = None <EOL> del OnlyEquality . __hash__ <EOL> class OnlyInequality ( object ) : <EOL> def __ne__ ( self , other ) : <EOL> return self is not other <EOL> class OnlyCmp ( object ) : <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( id ( self ) , id ( other ) ) <EOL> __hash__ = None <EOL> del OnlyCmp . __hash__ <EOL> class InheritedHashWithEquality ( FixedHash , OnlyEquality ) : pass <EOL> class InheritedHashWithInequality ( FixedHash , OnlyInequality ) : pass <EOL> class InheritedHashWithCmp ( FixedHash , OnlyCmp ) : pass <EOL> class NoHash ( object ) : <EOL> __hash__ = None <EOL> class HashInheritanceTestCase ( unittest . TestCase ) : <EOL> default_expected = [ object ( ) , <EOL> DefaultHash ( ) , <EOL> OnlyEquality ( ) , <EOL> OnlyInequality ( ) , <EOL> OnlyCmp ( ) , <EOL> ] <EOL> fixed_expected = [ FixedHash ( ) , <EOL> InheritedHashWithEquality ( ) , <EOL> InheritedHashWithInequality ( ) , <EOL> InheritedHashWithCmp ( ) , <EOL> ] <EOL> error_expected = [ NoHash ( ) ] <EOL> def test_default_hash ( self ) : <EOL> for obj in self . default_expected : <EOL> self . assertEqual ( hash ( obj ) , _default_hash ( obj ) ) <EOL> def test_fixed_hash ( self ) : <EOL> for obj in self . fixed_expected : <EOL> self . assertEqual ( hash ( obj ) , _FIXED_HASH_VALUE ) <EOL> def test_error_hash ( self ) : <EOL> for obj in self . error_expected : <EOL> self . assertRaises ( TypeError , hash , obj ) <EOL> def test_hashable ( self ) : <EOL> objects = ( self . default_expected + <EOL> self . fixed_expected ) <EOL> for obj in objects : <EOL> self . assertIsInstance ( obj , Hashable ) <EOL> def test_not_hashable ( self ) : <EOL> for obj in self . error_expected : <EOL> self . assertNotIsInstance ( obj , Hashable ) <EOL> class DefaultIterSeq ( object ) : <EOL> seq = range ( <NUM_LIT:10> ) <EOL> def __len__ ( self ) : <EOL> return len ( self . seq ) <EOL> def __getitem__ ( self , index ) : <EOL> return self . seq [ index ] <EOL> class HashBuiltinsTestCase ( unittest . TestCase ) : <EOL> hashes_to_check = [ xrange ( <NUM_LIT:10> ) , <EOL> enumerate ( xrange ( <NUM_LIT:10> ) ) , <EOL> iter ( DefaultIterSeq ( ) ) , <EOL> iter ( lambda : <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ] <EOL> def test_hashes ( self ) : <EOL> _default_hash = object . __hash__ <EOL> for obj in self . hashes_to_check : <EOL> self . assertEqual ( hash ( obj ) , _default_hash ( obj ) ) <EOL> class HashRandomizationTests ( unittest . TestCase ) : <EOL> def get_hash_command ( self , repr_ ) : <EOL> return '<STR_LIT>' % repr_ <EOL> def get_hash ( self , repr_ , seed = None ) : <EOL> env = os . environ . copy ( ) <EOL> if seed is not None : <EOL> env [ '<STR_LIT>' ] = str ( seed ) <EOL> else : <EOL> env . pop ( '<STR_LIT>' , None ) <EOL> cmd_line = [ sys . executable , '<STR_LIT:-c>' , self . get_hash_command ( repr_ ) ] <EOL> p = subprocess . Popen ( cmd_line , stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , stderr = subprocess . STDOUT , <EOL> env = env ) <EOL> out , err = p . communicate ( ) <EOL> out = test_support . strip_python_stderr ( out ) <EOL> return int ( out . strip ( ) ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> def test_randomized_hash ( self ) : <EOL> run1 = self . get_hash ( self . repr_ , seed = '<STR_LIT>' ) <EOL> run2 = self . get_hash ( self . repr_ , seed = '<STR_LIT>' ) <EOL> self . assertNotEqual ( run1 , run2 ) <EOL> class StringlikeHashRandomizationTests ( HashRandomizationTests ) : <EOL> def test_null_hash ( self ) : <EOL> if IS_64BIT : <EOL> known_hash_of_obj = <NUM_LIT> <EOL> else : <EOL> known_hash_of_obj = - <NUM_LIT> <EOL> self . assertEqual ( self . get_hash ( self . repr_ ) , known_hash_of_obj ) <EOL> self . assertEqual ( self . get_hash ( self . repr_ , seed = <NUM_LIT:0> ) , known_hash_of_obj ) <EOL> def test_fixed_hash ( self ) : <EOL> if IS_64BIT : <EOL> if sys . byteorder == '<STR_LIT>' : <EOL> h = - <NUM_LIT> <EOL> else : <EOL> h = - <NUM_LIT> <EOL> else : <EOL> if sys . byteorder == '<STR_LIT>' : <EOL> h = - <NUM_LIT> <EOL> else : <EOL> h = - <NUM_LIT> <EOL> self . assertEqual ( self . get_hash ( self . repr_ , seed = <NUM_LIT> ) , h ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> class StrHashRandomizationTests ( StringlikeHashRandomizationTests ) : <EOL> repr_ = repr ( '<STR_LIT:abc>' ) <EOL> def test_empty_string ( self ) : <EOL> self . assertEqual ( hash ( "<STR_LIT>" ) , <NUM_LIT:0> ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> class UnicodeHashRandomizationTests ( StringlikeHashRandomizationTests ) : <EOL> repr_ = repr ( u'<STR_LIT:abc>' ) <EOL> def test_empty_string ( self ) : <EOL> self . assertEqual ( hash ( u"<STR_LIT>" ) , <NUM_LIT:0> ) <EOL> @ unittest . skipIf ( test_support . is_jython , "<STR_LIT>" ) <EOL> class BufferHashRandomizationTests ( StringlikeHashRandomizationTests ) : <EOL> repr_ = '<STR_LIT>' <EOL> def test_empty_string ( self ) : <EOL> self . assertEqual ( hash ( buffer ( "<STR_LIT>" ) ) , <NUM_LIT:0> ) <EOL> class DatetimeTests ( HashRandomizationTests ) : <EOL> def get_hash_command ( self , repr_ ) : <EOL> return '<STR_LIT>' % repr_ <EOL> class DatetimeDateTests ( DatetimeTests ) : <EOL> repr_ = repr ( datetime . date ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> ) ) <EOL> class DatetimeDatetimeTests ( DatetimeTests ) : <EOL> repr_ = repr ( datetime . datetime ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ) ) <EOL> class DatetimeTimeTests ( DatetimeTests ) : <EOL> repr_ = repr ( datetime . time ( <NUM_LIT:0> ) ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( HashEqualityTestCase , <EOL> HashInheritanceTestCase , <EOL> HashBuiltinsTestCase , <EOL> StrHashRandomizationTests , <EOL> UnicodeHashRandomizationTests , <EOL> BufferHashRandomizationTests , <EOL> DatetimeDateTests , <EOL> DatetimeDatetimeTests , <EOL> DatetimeTimeTests ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
