<s> import unittest <EOL> import UserDict <EOL> class BasicTestMappingProtocol ( unittest . TestCase ) : <EOL> type2test = None <EOL> def _reference ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <NUM_LIT:1> : <NUM_LIT:2> , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) } <EOL> def _empty_mapping ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . type2test ( ) <EOL> def _full_mapping ( self , data ) : <EOL> """<STR_LIT>""" <EOL> x = self . _empty_mapping ( ) <EOL> for key , value in data . items ( ) : <EOL> x [ key ] = value <EOL> return x <EOL> def __init__ ( self , * args , ** kw ) : <EOL> unittest . TestCase . __init__ ( self , * args , ** kw ) <EOL> self . reference = self . _reference ( ) . copy ( ) <EOL> key , value = self . reference . popitem ( ) <EOL> self . other = { key : value } <EOL> key , value = self . reference . popitem ( ) <EOL> self . inmapping = { key : value } <EOL> self . reference [ key ] = value <EOL> def test_read ( self ) : <EOL> p = self . _empty_mapping ( ) <EOL> p1 = dict ( p ) <EOL> d = self . _full_mapping ( self . reference ) <EOL> if d is p : <EOL> p = p1 <EOL> for key , value in self . reference . items ( ) : <EOL> self . assertEqual ( d [ key ] , value ) <EOL> knownkey = self . other . keys ( ) [ <NUM_LIT:0> ] <EOL> self . failUnlessRaises ( KeyError , lambda : d [ knownkey ] ) <EOL> self . assertEqual ( len ( p ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( d ) , len ( self . reference ) ) <EOL> for k in self . reference : <EOL> self . assert_ ( d . has_key ( k ) ) <EOL> self . assert_ ( k in d ) <EOL> for k in self . other : <EOL> self . failIf ( d . has_key ( k ) ) <EOL> self . failIf ( k in d ) <EOL> self . assertEqual ( cmp ( p , p ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( cmp ( d , d ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( cmp ( p , d ) , - <NUM_LIT:1> ) <EOL> self . assertEqual ( cmp ( d , p ) , <NUM_LIT:1> ) <EOL> if p : self . fail ( "<STR_LIT>" ) <EOL> if not d : self . fail ( "<STR_LIT>" ) <EOL> def check_iterandlist ( iter , lst , ref ) : <EOL> self . assert_ ( hasattr ( iter , '<STR_LIT>' ) ) <EOL> self . assert_ ( hasattr ( iter , '<STR_LIT>' ) ) <EOL> x = list ( iter ) <EOL> self . assert_ ( set ( x ) == set ( lst ) == set ( ref ) ) <EOL> check_iterandlist ( d . iterkeys ( ) , d . keys ( ) , self . reference . keys ( ) ) <EOL> check_iterandlist ( iter ( d ) , d . keys ( ) , self . reference . keys ( ) ) <EOL> check_iterandlist ( d . itervalues ( ) , d . values ( ) , self . reference . values ( ) ) <EOL> check_iterandlist ( d . iteritems ( ) , d . items ( ) , self . reference . items ( ) ) <EOL> key , value = d . iteritems ( ) . next ( ) <EOL> knownkey , knownvalue = self . other . iteritems ( ) . next ( ) <EOL> self . assertEqual ( d . get ( key , knownvalue ) , value ) <EOL> self . assertEqual ( d . get ( knownkey , knownvalue ) , knownvalue ) <EOL> self . failIf ( knownkey in d ) <EOL> def test_write ( self ) : <EOL> p = self . _empty_mapping ( ) <EOL> for key , value in self . reference . items ( ) : <EOL> p [ key ] = value <EOL> self . assertEqual ( p [ key ] , value ) <EOL> for key in self . reference . keys ( ) : <EOL> del p [ key ] <EOL> self . failUnlessRaises ( KeyError , lambda : p [ key ] ) <EOL> p = self . _empty_mapping ( ) <EOL> p . update ( self . reference ) <EOL> self . assertEqual ( dict ( p ) , self . reference ) <EOL> items = p . items ( ) <EOL> p = self . _empty_mapping ( ) <EOL> p . update ( items ) <EOL> self . assertEqual ( dict ( p ) , self . reference ) <EOL> d = self . _full_mapping ( self . reference ) <EOL> key , value = d . iteritems ( ) . next ( ) <EOL> knownkey , knownvalue = self . other . iteritems ( ) . next ( ) <EOL> self . assertEqual ( d . setdefault ( key , knownvalue ) , value ) <EOL> self . assertEqual ( d [ key ] , value ) <EOL> self . assertEqual ( d . setdefault ( knownkey , knownvalue ) , knownvalue ) <EOL> self . assertEqual ( d [ knownkey ] , knownvalue ) <EOL> self . assertEqual ( d . pop ( knownkey ) , knownvalue ) <EOL> self . failIf ( knownkey in d ) <EOL> self . assertRaises ( KeyError , d . pop , knownkey ) <EOL> default = <NUM_LIT> <EOL> d [ knownkey ] = knownvalue <EOL> self . assertEqual ( d . pop ( knownkey , default ) , knownvalue ) <EOL> self . failIf ( knownkey in d ) <EOL> self . assertEqual ( d . pop ( knownkey , default ) , default ) <EOL> key , value = d . popitem ( ) <EOL> self . failIf ( key in d ) <EOL> self . assertEqual ( value , self . reference [ key ] ) <EOL> p = self . _empty_mapping ( ) <EOL> self . assertRaises ( KeyError , p . popitem ) <EOL> def test_constructor ( self ) : <EOL> self . assertEqual ( self . _empty_mapping ( ) , self . _empty_mapping ( ) ) <EOL> def test_bool ( self ) : <EOL> self . assert_ ( not self . _empty_mapping ( ) ) <EOL> self . assert_ ( self . reference ) <EOL> self . assert_ ( bool ( self . _empty_mapping ( ) ) is False ) <EOL> self . assert_ ( bool ( self . reference ) is True ) <EOL> def test_keys ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( d . keys ( ) , [ ] ) <EOL> d = self . reference <EOL> self . assert_ ( self . inmapping . keys ( ) [ <NUM_LIT:0> ] in d . keys ( ) ) <EOL> self . assert_ ( self . other . keys ( ) [ <NUM_LIT:0> ] not in d . keys ( ) ) <EOL> self . assertRaises ( TypeError , d . keys , None ) <EOL> def test_values ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( d . values ( ) , [ ] ) <EOL> self . assertRaises ( TypeError , d . values , None ) <EOL> def test_items ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( d . items ( ) , [ ] ) <EOL> self . assertRaises ( TypeError , d . items , None ) <EOL> def test_len ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> def test_getitem ( self ) : <EOL> d = self . reference <EOL> self . assertEqual ( d [ self . inmapping . keys ( ) [ <NUM_LIT:0> ] ] , self . inmapping . values ( ) [ <NUM_LIT:0> ] ) <EOL> self . assertRaises ( TypeError , d . __getitem__ ) <EOL> def test_update ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( self . other ) <EOL> self . assertEqual ( d . items ( ) , self . other . items ( ) ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( ) <EOL> self . assertEqual ( d , self . _empty_mapping ( ) ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( self . other . items ( ) ) <EOL> self . assertEqual ( d . items ( ) , self . other . items ( ) ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( self . other . iteritems ( ) ) <EOL> self . assertEqual ( d . items ( ) , self . other . items ( ) ) <EOL> self . assertRaises ( ( TypeError , AttributeError ) , d . update , <NUM_LIT> ) <EOL> outerself = self <EOL> class SimpleUserDict : <EOL> def __init__ ( self ) : <EOL> self . d = outerself . reference <EOL> def keys ( self ) : <EOL> return self . d . keys ( ) <EOL> def __getitem__ ( self , i ) : <EOL> return self . d [ i ] <EOL> d . clear ( ) <EOL> d . update ( SimpleUserDict ( ) ) <EOL> i1 = d . items ( ) <EOL> i2 = self . reference . items ( ) <EOL> i1 . sort ( ) <EOL> i2 . sort ( ) <EOL> self . assertEqual ( i1 , i2 ) <EOL> class Exc ( Exception ) : pass <EOL> d = self . _empty_mapping ( ) <EOL> class FailingUserDict : <EOL> def keys ( self ) : <EOL> raise Exc <EOL> self . assertRaises ( Exc , d . update , FailingUserDict ( ) ) <EOL> d . clear ( ) <EOL> class FailingUserDict : <EOL> def keys ( self ) : <EOL> class BogonIter : <EOL> def __init__ ( self ) : <EOL> self . i = <NUM_LIT:1> <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> if self . i : <EOL> self . i = <NUM_LIT:0> <EOL> return '<STR_LIT:a>' <EOL> raise Exc <EOL> return BogonIter ( ) <EOL> def __getitem__ ( self , key ) : <EOL> return key <EOL> self . assertRaises ( Exc , d . update , FailingUserDict ( ) ) <EOL> class FailingUserDict : <EOL> def keys ( self ) : <EOL> class BogonIter : <EOL> def __init__ ( self ) : <EOL> self . i = ord ( '<STR_LIT:a>' ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> if self . i <= ord ( '<STR_LIT:z>' ) : <EOL> rtn = chr ( self . i ) <EOL> self . i += <NUM_LIT:1> <EOL> return rtn <EOL> raise StopIteration <EOL> return BogonIter ( ) <EOL> def __getitem__ ( self , key ) : <EOL> raise Exc <EOL> self . assertRaises ( Exc , d . update , FailingUserDict ( ) ) <EOL> d = self . _empty_mapping ( ) <EOL> class badseq ( object ) : <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , d . update , badseq ( ) ) <EOL> self . assertRaises ( ValueError , d . update , [ ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_get ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assert_ ( d . get ( self . other . keys ( ) [ <NUM_LIT:0> ] ) is None ) <EOL> self . assertEqual ( d . get ( self . other . keys ( ) [ <NUM_LIT:0> ] , <NUM_LIT:3> ) , <NUM_LIT:3> ) <EOL> d = self . reference <EOL> self . assert_ ( d . get ( self . other . keys ( ) [ <NUM_LIT:0> ] ) is None ) <EOL> self . assertEqual ( d . get ( self . other . keys ( ) [ <NUM_LIT:0> ] , <NUM_LIT:3> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( d . get ( self . inmapping . keys ( ) [ <NUM_LIT:0> ] ) , self . inmapping . values ( ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( d . get ( self . inmapping . keys ( ) [ <NUM_LIT:0> ] , <NUM_LIT:3> ) , self . inmapping . values ( ) [ <NUM_LIT:0> ] ) <EOL> self . assertRaises ( TypeError , d . get ) <EOL> self . assertRaises ( TypeError , d . get , None , None , None ) <EOL> def test_setdefault ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertRaises ( TypeError , d . setdefault ) <EOL> def test_popitem ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertRaises ( KeyError , d . popitem ) <EOL> self . assertRaises ( TypeError , d . popitem , <NUM_LIT> ) <EOL> def test_pop ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> k , v = self . inmapping . items ( ) [ <NUM_LIT:0> ] <EOL> d [ k ] = v <EOL> self . assertRaises ( KeyError , d . pop , self . other . keys ( ) [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( d . pop ( k ) , v ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , d . pop , k ) <EOL> class TestMappingProtocol ( BasicTestMappingProtocol ) : <EOL> def test_constructor ( self ) : <EOL> BasicTestMappingProtocol . test_constructor ( self ) <EOL> self . assert_ ( self . _empty_mapping ( ) is not self . _empty_mapping ( ) ) <EOL> self . assertEqual ( self . type2test ( x = <NUM_LIT:1> , y = <NUM_LIT:2> ) , { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } ) <EOL> def test_bool ( self ) : <EOL> BasicTestMappingProtocol . test_bool ( self ) <EOL> self . assert_ ( not self . _empty_mapping ( ) ) <EOL> self . assert_ ( self . _full_mapping ( { "<STR_LIT:x>" : "<STR_LIT:y>" } ) ) <EOL> self . assert_ ( bool ( self . _empty_mapping ( ) ) is False ) <EOL> self . assert_ ( bool ( self . _full_mapping ( { "<STR_LIT:x>" : "<STR_LIT:y>" } ) ) is True ) <EOL> def test_keys ( self ) : <EOL> BasicTestMappingProtocol . test_keys ( self ) <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( d . keys ( ) , [ ] ) <EOL> d = self . _full_mapping ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) <EOL> k = d . keys ( ) <EOL> self . assert_ ( '<STR_LIT:a>' in k ) <EOL> self . assert_ ( '<STR_LIT:b>' in k ) <EOL> self . assert_ ( '<STR_LIT:c>' not in k ) <EOL> def test_values ( self ) : <EOL> BasicTestMappingProtocol . test_values ( self ) <EOL> d = self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:2> } ) <EOL> self . assertEqual ( d . values ( ) , [ <NUM_LIT:2> ] ) <EOL> def test_items ( self ) : <EOL> BasicTestMappingProtocol . test_items ( self ) <EOL> d = self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:2> } ) <EOL> self . assertEqual ( d . items ( ) , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> def test_has_key ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assert_ ( not d . has_key ( '<STR_LIT:a>' ) ) <EOL> d = self . _full_mapping ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) <EOL> k = d . keys ( ) <EOL> k . sort ( ) <EOL> self . assertEqual ( k , [ '<STR_LIT:a>' , '<STR_LIT:b>' ] ) <EOL> self . assertRaises ( TypeError , d . has_key ) <EOL> def test_contains ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assert_ ( not ( '<STR_LIT:a>' in d ) ) <EOL> self . assert_ ( '<STR_LIT:a>' not in d ) <EOL> d = self . _full_mapping ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) <EOL> self . assert_ ( '<STR_LIT:a>' in d ) <EOL> self . assert_ ( '<STR_LIT:b>' in d ) <EOL> self . assert_ ( '<STR_LIT:c>' not in d ) <EOL> self . assertRaises ( TypeError , d . __contains__ ) <EOL> def test_len ( self ) : <EOL> BasicTestMappingProtocol . test_len ( self ) <EOL> d = self . _full_mapping ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:2> ) <EOL> def test_getitem ( self ) : <EOL> BasicTestMappingProtocol . test_getitem ( self ) <EOL> d = self . _full_mapping ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) <EOL> self . assertEqual ( d [ '<STR_LIT:a>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( d [ '<STR_LIT:b>' ] , <NUM_LIT:2> ) <EOL> d [ '<STR_LIT:c>' ] = <NUM_LIT:3> <EOL> d [ '<STR_LIT:a>' ] = <NUM_LIT:4> <EOL> self . assertEqual ( d [ '<STR_LIT:c>' ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( d [ '<STR_LIT:a>' ] , <NUM_LIT:4> ) <EOL> del d [ '<STR_LIT:b>' ] <EOL> self . assertEqual ( d , { '<STR_LIT:a>' : <NUM_LIT:4> , '<STR_LIT:c>' : <NUM_LIT:3> } ) <EOL> self . assertRaises ( TypeError , d . __getitem__ ) <EOL> def test_clear ( self ) : <EOL> d = self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> d . clear ( ) <EOL> self . assertEqual ( d , { } ) <EOL> self . assertRaises ( TypeError , d . clear , None ) <EOL> def test_update ( self ) : <EOL> BasicTestMappingProtocol . test_update ( self ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( { <NUM_LIT:1> : <NUM_LIT:100> } ) <EOL> d . update ( { <NUM_LIT:2> : <NUM_LIT:20> } ) <EOL> d . update ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> d . update ( ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( x = <NUM_LIT:100> ) <EOL> d . update ( y = <NUM_LIT:20> ) <EOL> d . update ( x = <NUM_LIT:1> , y = <NUM_LIT:2> , z = <NUM_LIT:3> ) <EOL> self . assertEqual ( d , { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> , "<STR_LIT:z>" : <NUM_LIT:3> } ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( [ ( "<STR_LIT:x>" , <NUM_LIT:100> ) , ( "<STR_LIT:y>" , <NUM_LIT:20> ) ] ) <EOL> self . assertEqual ( d , { "<STR_LIT:x>" : <NUM_LIT:100> , "<STR_LIT:y>" : <NUM_LIT:20> } ) <EOL> d = self . _empty_mapping ( ) <EOL> d . update ( [ ( "<STR_LIT:x>" , <NUM_LIT:100> ) , ( "<STR_LIT:y>" , <NUM_LIT:20> ) ] , x = <NUM_LIT:1> , y = <NUM_LIT:2> ) <EOL> self . assertEqual ( d , { "<STR_LIT:x>" : <NUM_LIT:1> , "<STR_LIT:y>" : <NUM_LIT:2> } ) <EOL> d = self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:3> , <NUM_LIT:2> : <NUM_LIT:4> } ) <EOL> d . update ( self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:4> , <NUM_LIT:5> : <NUM_LIT:6> } ) . iteritems ( ) ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:2> : <NUM_LIT:4> , <NUM_LIT:3> : <NUM_LIT:4> , <NUM_LIT:5> : <NUM_LIT:6> } ) <EOL> class SimpleUserDict : <EOL> def __init__ ( self ) : <EOL> self . d = { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } <EOL> def keys ( self ) : <EOL> return self . d . keys ( ) <EOL> def __getitem__ ( self , i ) : <EOL> return self . d [ i ] <EOL> d . clear ( ) <EOL> d . update ( SimpleUserDict ( ) ) <EOL> self . assertEqual ( d , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> def test_fromkeys ( self ) : <EOL> self . assertEqual ( self . type2test . fromkeys ( '<STR_LIT:abc>' ) , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None , '<STR_LIT:c>' : None } ) <EOL> d = self . _empty_mapping ( ) <EOL> self . assert_ ( not ( d . fromkeys ( '<STR_LIT:abc>' ) is d ) ) <EOL> self . assertEqual ( d . fromkeys ( '<STR_LIT:abc>' ) , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None , '<STR_LIT:c>' : None } ) <EOL> self . assertEqual ( d . fromkeys ( ( <NUM_LIT:4> , <NUM_LIT:5> ) , <NUM_LIT:0> ) , { <NUM_LIT:4> : <NUM_LIT:0> , <NUM_LIT:5> : <NUM_LIT:0> } ) <EOL> self . assertEqual ( d . fromkeys ( [ ] ) , { } ) <EOL> def g ( ) : <EOL> yield <NUM_LIT:1> <EOL> self . assertEqual ( d . fromkeys ( g ( ) ) , { <NUM_LIT:1> : None } ) <EOL> self . assertRaises ( TypeError , { } . fromkeys , <NUM_LIT:3> ) <EOL> class dictlike ( self . type2test ) : pass <EOL> self . assertEqual ( dictlike . fromkeys ( '<STR_LIT:a>' ) , { '<STR_LIT:a>' : None } ) <EOL> self . assertEqual ( dictlike ( ) . fromkeys ( '<STR_LIT:a>' ) , { '<STR_LIT:a>' : None } ) <EOL> self . assert_ ( dictlike . fromkeys ( '<STR_LIT:a>' ) . __class__ is dictlike ) <EOL> self . assert_ ( dictlike ( ) . fromkeys ( '<STR_LIT:a>' ) . __class__ is dictlike ) <EOL> class mydict ( self . type2test ) : <EOL> def __new__ ( cls ) : <EOL> return UserDict . UserDict ( ) <EOL> ud = mydict . fromkeys ( '<STR_LIT>' ) <EOL> self . assertEqual ( ud , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None } ) <EOL> self . assertRaises ( TypeError , dict . fromkeys ) <EOL> class Exc ( Exception ) : pass <EOL> class baddict1 ( self . type2test ) : <EOL> def __init__ ( self ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , baddict1 . fromkeys , [ <NUM_LIT:1> ] ) <EOL> class BadSeq ( object ) : <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , self . type2test . fromkeys , BadSeq ( ) ) <EOL> class baddict2 ( self . type2test ) : <EOL> def __setitem__ ( self , key , value ) : <EOL> raise Exc ( ) <EOL> self . assertRaises ( Exc , baddict2 . fromkeys , [ <NUM_LIT:1> ] ) <EOL> def test_copy ( self ) : <EOL> d = self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> self . assertEqual ( d . copy ( ) , { <NUM_LIT:1> : <NUM_LIT:1> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:3> } ) <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( d . copy ( ) , d ) <EOL> self . assert_ ( isinstance ( d . copy ( ) , d . __class__ ) ) <EOL> self . assertRaises ( TypeError , d . copy , None ) <EOL> def test_get ( self ) : <EOL> BasicTestMappingProtocol . test_get ( self ) <EOL> d = self . _empty_mapping ( ) <EOL> self . assert_ ( d . get ( '<STR_LIT:c>' ) is None ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:c>' , <NUM_LIT:3> ) , <NUM_LIT:3> ) <EOL> d = self . _full_mapping ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } ) <EOL> self . assert_ ( d . get ( '<STR_LIT:c>' ) is None ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:c>' , <NUM_LIT:3> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:a>' ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( d . get ( '<STR_LIT:a>' , <NUM_LIT:3> ) , <NUM_LIT:1> ) <EOL> def test_setdefault ( self ) : <EOL> BasicTestMappingProtocol . test_setdefault ( self ) <EOL> d = self . _empty_mapping ( ) <EOL> self . assert_ ( d . setdefault ( '<STR_LIT>' ) is None ) <EOL> d . setdefault ( '<STR_LIT>' , [ ] ) <EOL> self . assert_ ( d . setdefault ( '<STR_LIT>' ) is None ) <EOL> d . setdefault ( '<STR_LIT:key>' , [ ] ) . append ( <NUM_LIT:3> ) <EOL> self . assertEqual ( d [ '<STR_LIT:key>' ] [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> d . setdefault ( '<STR_LIT:key>' , [ ] ) . append ( <NUM_LIT:4> ) <EOL> self . assertEqual ( len ( d [ '<STR_LIT:key>' ] ) , <NUM_LIT:2> ) <EOL> def test_popitem ( self ) : <EOL> BasicTestMappingProtocol . test_popitem ( self ) <EOL> for copymode in - <NUM_LIT:1> , + <NUM_LIT:1> : <EOL> for log2size in range ( <NUM_LIT:12> ) : <EOL> size = <NUM_LIT:2> ** log2size <EOL> a = self . _empty_mapping ( ) <EOL> b = self . _empty_mapping ( ) <EOL> for i in range ( size ) : <EOL> a [ repr ( i ) ] = i <EOL> if copymode < <NUM_LIT:0> : <EOL> b [ repr ( i ) ] = i <EOL> if copymode > <NUM_LIT:0> : <EOL> b = a . copy ( ) <EOL> for i in range ( size ) : <EOL> ka , va = ta = a . popitem ( ) <EOL> self . assertEqual ( va , int ( ka ) ) <EOL> kb , vb = tb = b . popitem ( ) <EOL> self . assertEqual ( vb , int ( kb ) ) <EOL> self . assert_ ( not ( copymode < <NUM_LIT:0> and ta != tb ) ) <EOL> self . assert_ ( not a ) <EOL> self . assert_ ( not b ) <EOL> def test_pop ( self ) : <EOL> BasicTestMappingProtocol . test_pop ( self ) <EOL> d = self . _empty_mapping ( ) <EOL> k , v = '<STR_LIT:abc>' , '<STR_LIT>' <EOL> x = <NUM_LIT> L <EOL> y = <NUM_LIT> <EOL> h = self . _full_mapping ( { x : '<STR_LIT>' , y : '<STR_LIT>' } ) <EOL> self . assertEqual ( h [ x ] , h [ y ] ) <EOL> self . assertEqual ( d . pop ( k , v ) , v ) <EOL> d [ k ] = v <EOL> self . assertEqual ( d . pop ( k , <NUM_LIT:1> ) , v ) <EOL> class TestHashMappingProtocol ( TestMappingProtocol ) : <EOL> def test_getitem ( self ) : <EOL> TestMappingProtocol . test_getitem ( self ) <EOL> class Exc ( Exception ) : pass <EOL> class BadEq ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> raise Exc ( ) <EOL> d = self . _empty_mapping ( ) <EOL> d [ BadEq ( ) ] = <NUM_LIT> <EOL> self . assertRaises ( KeyError , d . __getitem__ , <NUM_LIT> ) <EOL> class BadHash ( object ) : <EOL> fail = False <EOL> def __hash__ ( self ) : <EOL> if self . fail : <EOL> raise Exc ( ) <EOL> else : <EOL> return <NUM_LIT> <EOL> d = self . _empty_mapping ( ) <EOL> x = BadHash ( ) <EOL> d [ x ] = <NUM_LIT> <EOL> x . fail = True <EOL> self . assertRaises ( Exc , d . __getitem__ , x ) <EOL> def test_fromkeys ( self ) : <EOL> TestMappingProtocol . test_fromkeys ( self ) <EOL> class mydict ( self . type2test ) : <EOL> def __new__ ( cls ) : <EOL> return UserDict . UserDict ( ) <EOL> ud = mydict . fromkeys ( '<STR_LIT>' ) <EOL> self . assertEqual ( ud , { '<STR_LIT:a>' : None , '<STR_LIT:b>' : None } ) <EOL> self . assert_ ( isinstance ( ud , UserDict . UserDict ) ) <EOL> def test_pop ( self ) : <EOL> TestMappingProtocol . test_pop ( self ) <EOL> class Exc ( Exception ) : pass <EOL> class BadHash ( object ) : <EOL> fail = False <EOL> def __hash__ ( self ) : <EOL> if self . fail : <EOL> raise Exc ( ) <EOL> else : <EOL> return <NUM_LIT> <EOL> d = self . _empty_mapping ( ) <EOL> x = BadHash ( ) <EOL> d [ x ] = <NUM_LIT> <EOL> x . fail = True <EOL> self . assertRaises ( Exc , d . pop , x ) <EOL> def test_mutatingiteration ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> d [ <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> try : <EOL> for i in d : <EOL> d [ i + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> except RuntimeError : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_repr ( self ) : <EOL> d = self . _empty_mapping ( ) <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT:{}>' ) <EOL> d [ <NUM_LIT:1> ] = <NUM_LIT:2> <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT>' ) <EOL> d = self . _empty_mapping ( ) <EOL> d [ <NUM_LIT:1> ] = d <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT>' ) <EOL> class Exc ( Exception ) : pass <EOL> class BadRepr ( object ) : <EOL> def __repr__ ( self ) : <EOL> raise Exc ( ) <EOL> d = self . _full_mapping ( { <NUM_LIT:1> : BadRepr ( ) } ) <EOL> self . assertRaises ( Exc , repr , d ) <EOL> def test_le ( self ) : <EOL> self . assert_ ( not ( self . _empty_mapping ( ) < self . _empty_mapping ( ) ) ) <EOL> self . assert_ ( not ( self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:2> } ) < self . _full_mapping ( { <NUM_LIT:1> L : <NUM_LIT:2> L } ) ) ) <EOL> class Exc ( Exception ) : pass <EOL> class BadCmp ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> raise Exc ( ) <EOL> d1 = self . _full_mapping ( { BadCmp ( ) : <NUM_LIT:1> } ) <EOL> d2 = self . _full_mapping ( { <NUM_LIT:1> : <NUM_LIT:1> } ) <EOL> try : <EOL> d1 < d2 <EOL> except Exc : <EOL> pass <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_setdefault ( self ) : <EOL> TestMappingProtocol . test_setdefault ( self ) <EOL> class Exc ( Exception ) : pass <EOL> class BadHash ( object ) : <EOL> fail = False <EOL> def __hash__ ( self ) : <EOL> if self . fail : <EOL> raise Exc ( ) <EOL> else : <EOL> return <NUM_LIT> <EOL> d = self . _empty_mapping ( ) <EOL> x = BadHash ( ) <EOL> d [ x ] = <NUM_LIT> <EOL> x . fail = True <EOL> self . assertRaises ( Exc , d . setdefault , x , [ ] ) </s>
