<s> from java . util import ArrayList , List , Vector <EOL> from copy import copy <EOL> import unittest <EOL> import test . test_support <EOL> class CollectionProxyTest ( unittest . TestCase ) : <EOL> def _perform_op ( self , value , op_func ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return op_func ( value ) <EOL> except Exception , e : <EOL> return type ( e ) <EOL> def _arraylist_of ( self , xs ) : <EOL> """<STR_LIT>""" <EOL> a = ArrayList ( ) <EOL> a . addAll ( xs ) <EOL> return a <EOL> def check_list ( self , control , results , list_type_names , initial , test_name ) : <EOL> for result , type_name in zip ( results , list_type_names ) : <EOL> try : <EOL> len ( result ) <EOL> except : <EOL> print result <EOL> self . assertEquals ( len ( control ) , len ( result ) , "<STR_LIT>" % ( test_name , type_name ) ) <EOL> for control_value , result_value in zip ( control , result ) : <EOL> self . assertEquals ( control_value , result_value , "<STR_LIT>" % ( test_name , type_name ) ) <EOL> def _list_op_test ( self , initial_value , op_func , test_name ) : <EOL> """<STR_LIT>""" <EOL> lists = [ list ( initial_value ) , ArrayList ( initial_value ) , Vector ( initial_value ) ] <EOL> list_type_names = [ '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> results = [ self . _perform_op ( l , op_func ) for l in lists ] <EOL> self . check_list ( lists [ <NUM_LIT:0> ] , lists [ <NUM_LIT:1> : ] , list_type_names [ <NUM_LIT:1> : ] , initial_value , test_name ) <EOL> if not isinstance ( results [ <NUM_LIT:0> ] , list ) : <EOL> for r , n in zip ( results [ <NUM_LIT:1> : ] , list_type_names [ <NUM_LIT:1> : ] ) : <EOL> self . assertEquals ( results [ <NUM_LIT:0> ] , r , '<STR_LIT>' % ( test_name , n ) ) <EOL> else : <EOL> self . check_list ( results [ <NUM_LIT:0> ] , results [ <NUM_LIT:1> : ] , list_type_names [ <NUM_LIT:1> : ] , initial_value , test_name ) <EOL> def test_get_integer ( self ) : <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> for i in xrange ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> self . _list_op_test ( initial_value , lambda xs : xs [ i ] , '<STR_LIT>' % ( i , ) ) <EOL> def test_get_slice ( self ) : <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> for i in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for j in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for k in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> self . _list_op_test ( initial_value , lambda xs : xs [ i : j : k ] , '<STR_LIT>' % ( i , j , k ) ) <EOL> def test_set_integer ( self ) : <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> def make_op_func ( index ) : <EOL> def _f ( xs ) : <EOL> xs [ index ] = <NUM_LIT:100> <EOL> return _f <EOL> for i in xrange ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> self . _list_op_test ( initial_value , make_op_func ( i ) , '<STR_LIT>' % ( i , ) ) <EOL> def test_set_slice ( self ) : <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> def make_op_func ( i , j , k , v ) : <EOL> def _f ( xs ) : <EOL> xs [ i : j : k ] = v <EOL> return _f <EOL> for i in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for j in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for k in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> self . _list_op_test ( initial_value , make_op_func ( i , j , k , [ ] ) , '<STR_LIT>' % ( i , j , k ) ) <EOL> self . _list_op_test ( initial_value , make_op_func ( i , j , k , range ( <NUM_LIT:0> , <NUM_LIT:2> ) ) , '<STR_LIT>' % ( i , j , k ) ) <EOL> self . _list_op_test ( initial_value , make_op_func ( i , j , k , range ( <NUM_LIT:0> , <NUM_LIT:4> ) ) , '<STR_LIT>' % ( i , j , k ) ) <EOL> self . _list_op_test ( initial_value , make_op_func ( i , j , k , xrange ( <NUM_LIT:0> , <NUM_LIT:2> ) ) , '<STR_LIT>' % ( i , j , k ) ) <EOL> self . _list_op_test ( initial_value , make_op_func ( i , j , k , self . _arraylist_of ( range ( <NUM_LIT:0> , <NUM_LIT:2> ) ) ) , '<STR_LIT>' % ( i , j , k ) ) <EOL> self . _list_op_test ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , make_op_func ( <NUM_LIT:1> , None , None , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) , '<STR_LIT>' ) <EOL> def test_del_integer ( self ) : <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> def make_op_func ( index ) : <EOL> def _f ( xs ) : <EOL> del xs [ index ] <EOL> return _f <EOL> for i in xrange ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> self . _list_op_test ( initial_value , make_op_func ( i ) , '<STR_LIT>' % ( i , ) ) <EOL> def test_del_slice ( self ) : <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> def make_op_func ( i , j , k ) : <EOL> def _f ( xs ) : <EOL> del xs [ i : j : k ] <EOL> return _f <EOL> for i in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for j in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for k in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> self . _list_op_test ( initial_value , make_op_func ( i , j , k ) , '<STR_LIT>' % ( i , j , k ) ) <EOL> def test_len ( self ) : <EOL> jlist = ArrayList ( ) <EOL> jlist . addAll ( range ( <NUM_LIT:0> , <NUM_LIT:10> ) ) <EOL> self . assert_ ( len ( jlist ) == <NUM_LIT:10> ) <EOL> def test_iter ( self ) : <EOL> jlist = ArrayList ( ) <EOL> jlist . addAll ( range ( <NUM_LIT:0> , <NUM_LIT:10> ) ) <EOL> i = iter ( jlist ) <EOL> x = list ( i ) <EOL> self . assert_ ( x == range ( <NUM_LIT:0> , <NUM_LIT:10> ) ) <EOL> def test_override_len ( self ) : <EOL> class MyList ( ArrayList ) : <EOL> def __len__ ( self ) : <EOL> return self . size ( ) + <NUM_LIT:1> ; <EOL> m = MyList ( ) <EOL> m . addAll ( range ( <NUM_LIT:0> , <NUM_LIT:10> ) ) <EOL> self . assert_ ( len ( m ) == <NUM_LIT:11> ) <EOL> def test_override_iter ( self ) : <EOL> class MyList ( ArrayList ) : <EOL> def __iter__ ( self ) : <EOL> return iter ( self . subList ( <NUM_LIT:0> , self . size ( ) - <NUM_LIT:1> ) ) ; <EOL> m = MyList ( ) <EOL> m . addAll ( range ( <NUM_LIT:0> , <NUM_LIT:10> ) ) <EOL> i = iter ( m ) <EOL> x = list ( i ) <EOL> self . assert_ ( x == range ( <NUM_LIT:0> , <NUM_LIT:9> ) ) <EOL> def test_override_getsetdelitem ( self ) : <EOL> class MyList ( ArrayList ) : <EOL> def __getitem__ ( self , key ) : <EOL> return self . get ( key ) * <NUM_LIT:2> ; <EOL> def __setitem__ ( self , key , value ) : <EOL> return self . set ( key , value * <NUM_LIT:2> ) ; <EOL> def __delitem__ ( self , key ) : <EOL> self . add ( <NUM_LIT> ) <EOL> m = MyList ( ) <EOL> m . addAll ( range ( <NUM_LIT:0> , <NUM_LIT:10> ) ) <EOL> self . assert_ ( m [ <NUM_LIT:1> ] == <NUM_LIT:2> ) <EOL> self . assert_ ( m . get ( <NUM_LIT:1> ) == <NUM_LIT:1> ) <EOL> m [ <NUM_LIT:0> ] = <NUM_LIT:3> <EOL> self . assert_ ( m . get ( <NUM_LIT:0> ) == <NUM_LIT:6> ) <EOL> self . assert_ ( m [ <NUM_LIT:0> ] == <NUM_LIT:12> ) <EOL> del m [ <NUM_LIT:0> ] <EOL> self . assert_ ( m . size ( ) == <NUM_LIT:11> ) <EOL> self . assert_ ( m . get ( <NUM_LIT:10> ) == <NUM_LIT> ) <EOL> def test_set_slice_from_input_types ( self ) : <EOL> """<STR_LIT>""" <EOL> initial_value = range ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> def make_op_func ( i , j , k , v ) : <EOL> def _f ( xs ) : <EOL> xs [ i : j : k ] = v <EOL> return _f <EOL> for i in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for j in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> for k in [ None ] + range ( - <NUM_LIT:7> , <NUM_LIT:7> ) : <EOL> destPy = copy ( initial_value ) <EOL> destJUL = copy ( initial_value ) <EOL> destIter = copy ( initial_value ) <EOL> sourcePy = range ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> sourceJUL = self . _arraylist_of ( range ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> sourceIter = xrange ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> resultPy = self . _perform_op ( destPy , make_op_func ( i , j , k , sourcePy ) ) <EOL> resultJUL = self . _perform_op ( destJUL , make_op_func ( i , j , k , sourceJUL ) ) <EOL> resultIter = self . _perform_op ( destIter , make_op_func ( i , j , k , sourceIter ) ) <EOL> self . assertEquals ( resultPy , resultJUL ) <EOL> self . assertEquals ( resultPy , resultIter ) <EOL> self . assertEquals ( destPy , destJUL ) <EOL> self . assertEquals ( destPy , destIter ) <EOL> def test_main ( ) : <EOL> test . test_support . run_unittest ( CollectionProxyTest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
