<s> import random , sys <EOL> from pypy . rpython . lltypesystem import lltype , llmemory <EOL> from pypy . rpython . memory import support , lldict <EOL> class TestLLAddressDict : <EOL> def setup_method ( self , meth ) : <EOL> lldict . alloc_count = <NUM_LIT:0> <EOL> def test_basics ( self ) : <EOL> d = lldict . newdict ( ) <EOL> d . add ( intaddr ( <NUM_LIT> ) ) <EOL> d . setitem ( intaddr ( <NUM_LIT> ) , intaddr ( <NUM_LIT> ) ) <EOL> assert not d . contains ( intaddr ( <NUM_LIT> ) ) <EOL> assert d . contains ( intaddr ( <NUM_LIT> ) ) <EOL> assert d . contains ( intaddr ( <NUM_LIT> ) ) <EOL> assert not d . contains ( intaddr ( <NUM_LIT> ) ) <EOL> assert d . get ( intaddr ( <NUM_LIT> ) ) == llmemory . NULL <EOL> assert d . get ( intaddr ( <NUM_LIT> ) ) == llmemory . NULL <EOL> assert d . get ( intaddr ( <NUM_LIT> ) ) == intaddr ( <NUM_LIT> ) <EOL> assert d . get ( intaddr ( <NUM_LIT> ) ) == llmemory . NULL <EOL> assert d . length ( ) == <NUM_LIT:2> <EOL> d . delete ( ) <EOL> assert lldict . alloc_count == <NUM_LIT:0> <EOL> def test_foreach ( self ) : <EOL> d = lldict . newdict ( ) <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> d . setitem ( intaddr ( <NUM_LIT> * i ) , intaddr ( i ) ) <EOL> result = [ ] <EOL> d . foreach ( lambda key , value , arg : result . append ( ( key , value , arg ) ) , <EOL> "<STR_LIT>" ) <EOL> assert len ( result ) == <NUM_LIT:30> <EOL> seen = { } <EOL> for key , value , arg in result : <EOL> assert key . intval == <NUM_LIT> * value . intval <EOL> assert arg == "<STR_LIT>" <EOL> seen [ key . intval ] = True <EOL> assert len ( seen ) == <NUM_LIT:30> <EOL> d . delete ( ) <EOL> assert lldict . alloc_count == <NUM_LIT:0> <EOL> def test_copy_and_update ( self ) : <EOL> d = lldict . newdict ( <NUM_LIT:3> ) <EOL> d . setitem ( intaddr ( <NUM_LIT> ) , intaddr ( <NUM_LIT> ) ) <EOL> d . insertclean ( intaddr ( <NUM_LIT> ) , intaddr ( <NUM_LIT> ) ) <EOL> d . setitem ( intaddr ( <NUM_LIT> ) , intaddr ( <NUM_LIT> ) ) <EOL> def surviving ( key ) : <EOL> return key . intval != <NUM_LIT> <EOL> def updated_address ( key ) : <EOL> return intaddr ( { <NUM_LIT> : <NUM_LIT> , <NUM_LIT> : <NUM_LIT> } [ key . intval ] ) <EOL> d2 = support . copy_and_update ( d , surviving , updated_address ) <EOL> d . delete ( ) <EOL> assert d2 . length ( ) == <NUM_LIT:2> <EOL> assert d2 . get ( intaddr ( <NUM_LIT> ) ) == llmemory . NULL <EOL> assert d2 . get ( intaddr ( <NUM_LIT> ) ) == intaddr ( <NUM_LIT> ) <EOL> assert d2 . get ( intaddr ( <NUM_LIT> ) ) == llmemory . NULL <EOL> assert d2 . get ( intaddr ( <NUM_LIT> ) ) == intaddr ( <NUM_LIT> ) <EOL> d2 . delete ( ) <EOL> assert lldict . alloc_count == <NUM_LIT:0> <EOL> def test_clear ( self ) : <EOL> d = lldict . newdict ( ) <EOL> d . setitem ( intaddr ( <NUM_LIT> ) , intaddr ( <NUM_LIT> ) ) <EOL> d . clear ( ) <EOL> assert d . length ( ) == <NUM_LIT:0> <EOL> assert not d . contains ( intaddr ( <NUM_LIT> ) ) <EOL> d . delete ( ) <EOL> assert lldict . alloc_count == <NUM_LIT:0> <EOL> def test_random ( self ) : <EOL> for i in range ( <NUM_LIT:8> ) + range ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:10> ) : <EOL> examples = { } <EOL> lst = [ ] <EOL> for j in range ( i ) : <EOL> if j % <NUM_LIT> == <NUM_LIT> : <EOL> intval = random . choice ( lst ) <EOL> else : <EOL> intval = random . randrange ( - sys . maxint , sys . maxint ) or <NUM_LIT:1> <EOL> lst . append ( intval ) <EOL> examples [ intval ] = True <EOL> d = lldict . newdict ( ) <EOL> for intval in lst : <EOL> d . setitem ( intaddr ( intval ) , intaddr ( - intval ) ) <EOL> for intval in lst : <EOL> assert d . contains ( intaddr ( intval ) ) <EOL> assert d . get ( intaddr ( intval ) , "<STR_LIT>" ) . intval == - intval <EOL> for intval in lst : <EOL> for j in range ( intval - <NUM_LIT:5> , intval + <NUM_LIT:5> ) : <EOL> if j not in examples : <EOL> assert not d . contains ( intaddr ( j ) ) <EOL> assert not d . contains ( llmemory . NULL ) <EOL> d . delete ( ) <EOL> assert lldict . alloc_count == <NUM_LIT:0> <EOL> class intaddr ( object ) : <EOL> _TYPE = llmemory . Address <EOL> def __init__ ( self , intval ) : <EOL> self . intval = intval <EOL> def _cast_to_int ( self ) : <EOL> return self . intval <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . intval & ( sys . maxint * <NUM_LIT:2> + <NUM_LIT:1> ) , ) <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , intaddr ) and self . intval == other . intval <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) </s>
