<s> from collections import deque <EOL> import unittest <EOL> from test import test_support , seq_tests <EOL> import gc <EOL> import weakref <EOL> import copy <EOL> import cPickle as pickle <EOL> import random <EOL> import struct <EOL> BIG = <NUM_LIT> <EOL> def fail ( ) : <EOL> raise SyntaxError <EOL> yield <NUM_LIT:1> <EOL> class BadCmp : <EOL> def __eq__ ( self , other ) : <EOL> raise RuntimeError <EOL> class MutateCmp : <EOL> def __init__ ( self , deque , result ) : <EOL> self . deque = deque <EOL> self . result = result <EOL> def __eq__ ( self , other ) : <EOL> self . deque . clear ( ) <EOL> return self . result <EOL> class TestBasic ( unittest . TestCase ) : <EOL> def test_basics ( self ) : <EOL> d = deque ( xrange ( - <NUM_LIT> , - <NUM_LIT> ) ) <EOL> d . __init__ ( xrange ( <NUM_LIT:200> ) ) <EOL> for i in xrange ( <NUM_LIT:200> , <NUM_LIT> ) : <EOL> d . append ( i ) <EOL> for i in reversed ( xrange ( - <NUM_LIT:200> , <NUM_LIT:0> ) ) : <EOL> d . appendleft ( i ) <EOL> self . assertEqual ( list ( d ) , range ( - <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT> ) <EOL> left = [ d . popleft ( ) for i in xrange ( <NUM_LIT> ) ] <EOL> self . assertEqual ( left , range ( - <NUM_LIT:200> , <NUM_LIT:50> ) ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:50> , <NUM_LIT> ) ) <EOL> right = [ d . pop ( ) for i in xrange ( <NUM_LIT> ) ] <EOL> right . reverse ( ) <EOL> self . assertEqual ( right , range ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:50> , <NUM_LIT> ) ) <EOL> def test_maxlen ( self ) : <EOL> self . assertRaises ( ValueError , deque , '<STR_LIT:abc>' , - <NUM_LIT:1> ) <EOL> self . assertRaises ( ValueError , deque , '<STR_LIT:abc>' , - <NUM_LIT:2> ) <EOL> it = iter ( range ( <NUM_LIT:10> ) ) <EOL> d = deque ( it , maxlen = <NUM_LIT:3> ) <EOL> self . assertEqual ( list ( it ) , [ ] ) <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT>' ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:7> , <NUM_LIT:10> ) ) <EOL> self . assertEqual ( d , deque ( range ( <NUM_LIT:10> ) , <NUM_LIT:3> ) ) <EOL> d . append ( <NUM_LIT:10> ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:8> , <NUM_LIT:11> ) ) <EOL> d . appendleft ( <NUM_LIT:7> ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:7> , <NUM_LIT:10> ) ) <EOL> d . extend ( [ <NUM_LIT:10> , <NUM_LIT:11> ] ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:9> , <NUM_LIT:12> ) ) <EOL> d . extendleft ( [ <NUM_LIT:8> , <NUM_LIT:7> ] ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:7> , <NUM_LIT:10> ) ) <EOL> d = deque ( xrange ( <NUM_LIT:200> ) , maxlen = <NUM_LIT:10> ) <EOL> d . append ( d ) <EOL> test_support . unlink ( test_support . TESTFN ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:wb>" ) <EOL> try : <EOL> print >> fo , d , <EOL> fo . close ( ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:rb>" ) <EOL> self . assertEqual ( fo . read ( ) , repr ( d ) ) <EOL> finally : <EOL> fo . close ( ) <EOL> test_support . unlink ( test_support . TESTFN ) <EOL> d = deque ( range ( <NUM_LIT:10> ) , maxlen = None ) <EOL> self . assertEqual ( repr ( d ) , '<STR_LIT>' ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:wb>" ) <EOL> try : <EOL> print >> fo , d , <EOL> fo . close ( ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:rb>" ) <EOL> self . assertEqual ( fo . read ( ) , repr ( d ) ) <EOL> finally : <EOL> fo . close ( ) <EOL> test_support . unlink ( test_support . TESTFN ) <EOL> def test_maxlen_zero ( self ) : <EOL> it = iter ( range ( <NUM_LIT:100> ) ) <EOL> deque ( it , maxlen = <NUM_LIT:0> ) <EOL> self . assertEqual ( list ( it ) , [ ] ) <EOL> it = iter ( range ( <NUM_LIT:100> ) ) <EOL> d = deque ( maxlen = <NUM_LIT:0> ) <EOL> d . extend ( it ) <EOL> self . assertEqual ( list ( it ) , [ ] ) <EOL> it = iter ( range ( <NUM_LIT:100> ) ) <EOL> d = deque ( maxlen = <NUM_LIT:0> ) <EOL> d . extendleft ( it ) <EOL> self . assertEqual ( list ( it ) , [ ] ) <EOL> def test_maxlen_attribute ( self ) : <EOL> self . assertEqual ( deque ( ) . maxlen , None ) <EOL> self . assertEqual ( deque ( '<STR_LIT:abc>' ) . maxlen , None ) <EOL> self . assertEqual ( deque ( '<STR_LIT:abc>' , maxlen = <NUM_LIT:4> ) . maxlen , <NUM_LIT:4> ) <EOL> self . assertEqual ( deque ( '<STR_LIT:abc>' , maxlen = <NUM_LIT:2> ) . maxlen , <NUM_LIT:2> ) <EOL> self . assertEqual ( deque ( '<STR_LIT:abc>' , maxlen = <NUM_LIT:0> ) . maxlen , <NUM_LIT:0> ) <EOL> with self . assertRaises ( AttributeError ) : <EOL> d = deque ( '<STR_LIT:abc>' ) <EOL> d . maxlen = <NUM_LIT:10> <EOL> def test_count ( self ) : <EOL> for s in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' * <NUM_LIT> + '<STR_LIT:abc>' ) : <EOL> s = list ( s ) <EOL> d = deque ( s ) <EOL> for letter in '<STR_LIT>' : <EOL> self . assertEqual ( s . count ( letter ) , d . count ( letter ) , ( s , d , letter ) ) <EOL> self . assertRaises ( TypeError , d . count ) <EOL> self . assertRaises ( TypeError , d . count , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> class BadCompare : <EOL> def __eq__ ( self , other ) : <EOL> raise ArithmeticError <EOL> d = deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , BadCompare ( ) , <NUM_LIT:3> ] ) <EOL> self . assertRaises ( ArithmeticError , d . count , <NUM_LIT:2> ) <EOL> d = deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertRaises ( ArithmeticError , d . count , BadCompare ( ) ) <EOL> class MutatingCompare : <EOL> def __eq__ ( self , other ) : <EOL> self . d . pop ( ) <EOL> return True <EOL> m = MutatingCompare ( ) <EOL> d = deque ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , m , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> m . d = d <EOL> self . assertRaises ( RuntimeError , d . count , <NUM_LIT:3> ) <EOL> d = deque ( [ None ] * <NUM_LIT:16> ) <EOL> for i in range ( len ( d ) ) : <EOL> d . rotate ( - <NUM_LIT:1> ) <EOL> d . rotate ( <NUM_LIT:1> ) <EOL> self . assertEqual ( d . count ( <NUM_LIT:1> ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( d . count ( None ) , <NUM_LIT:16> ) <EOL> def test_comparisons ( self ) : <EOL> d = deque ( '<STR_LIT>' ) ; d . popleft ( ) <EOL> for e in [ d , deque ( '<STR_LIT:abc>' ) , deque ( '<STR_LIT>' ) , deque ( ) , list ( d ) ] : <EOL> self . assertEqual ( d == e , type ( d ) == type ( e ) and list ( d ) == list ( e ) ) <EOL> self . assertEqual ( d != e , not ( type ( d ) == type ( e ) and list ( d ) == list ( e ) ) ) <EOL> args = map ( deque , ( '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:abc>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for x in args : <EOL> for y in args : <EOL> self . assertEqual ( x == y , list ( x ) == list ( y ) , ( x , y ) ) <EOL> self . assertEqual ( x != y , list ( x ) != list ( y ) , ( x , y ) ) <EOL> self . assertEqual ( x < y , list ( x ) < list ( y ) , ( x , y ) ) <EOL> self . assertEqual ( x <= y , list ( x ) <= list ( y ) , ( x , y ) ) <EOL> self . assertEqual ( x > y , list ( x ) > list ( y ) , ( x , y ) ) <EOL> self . assertEqual ( x >= y , list ( x ) >= list ( y ) , ( x , y ) ) <EOL> self . assertEqual ( cmp ( x , y ) , cmp ( list ( x ) , list ( y ) ) , ( x , y ) ) <EOL> def test_extend ( self ) : <EOL> d = deque ( '<STR_LIT:a>' ) <EOL> self . assertRaises ( TypeError , d . extend , <NUM_LIT:1> ) <EOL> d . extend ( '<STR_LIT>' ) <EOL> self . assertEqual ( list ( d ) , list ( '<STR_LIT>' ) ) <EOL> d . extend ( d ) <EOL> self . assertEqual ( list ( d ) , list ( '<STR_LIT>' ) ) <EOL> def test_iadd ( self ) : <EOL> d = deque ( '<STR_LIT:a>' ) <EOL> d += '<STR_LIT>' <EOL> self . assertEqual ( list ( d ) , list ( '<STR_LIT>' ) ) <EOL> d += d <EOL> self . assertEqual ( list ( d ) , list ( '<STR_LIT>' ) ) <EOL> def test_extendleft ( self ) : <EOL> d = deque ( '<STR_LIT:a>' ) <EOL> self . assertRaises ( TypeError , d . extendleft , <NUM_LIT:1> ) <EOL> d . extendleft ( '<STR_LIT>' ) <EOL> self . assertEqual ( list ( d ) , list ( reversed ( '<STR_LIT>' ) ) ) <EOL> d . extendleft ( d ) <EOL> self . assertEqual ( list ( d ) , list ( '<STR_LIT>' ) ) <EOL> d = deque ( ) <EOL> d . extendleft ( range ( <NUM_LIT:1000> ) ) <EOL> self . assertEqual ( list ( d ) , list ( reversed ( range ( <NUM_LIT:1000> ) ) ) ) <EOL> self . assertRaises ( SyntaxError , d . extendleft , fail ( ) ) <EOL> def test_getitem ( self ) : <EOL> n = <NUM_LIT:200> <EOL> d = deque ( xrange ( n ) ) <EOL> l = range ( n ) <EOL> for i in xrange ( n ) : <EOL> d . popleft ( ) <EOL> l . pop ( <NUM_LIT:0> ) <EOL> if random . random ( ) < <NUM_LIT:0.5> : <EOL> d . append ( i ) <EOL> l . append ( i ) <EOL> for j in xrange ( <NUM_LIT:1> - len ( l ) , len ( l ) ) : <EOL> assert d [ j ] == l [ j ] <EOL> d = deque ( '<STR_LIT>' ) <EOL> self . assertEqual ( d [ <NUM_LIT:0> ] , '<STR_LIT:s>' ) <EOL> self . assertEqual ( d [ - <NUM_LIT:1> ] , '<STR_LIT:n>' ) <EOL> d = deque ( ) <EOL> self . assertRaises ( IndexError , d . __getitem__ , <NUM_LIT:0> ) <EOL> self . assertRaises ( IndexError , d . __getitem__ , - <NUM_LIT:1> ) <EOL> def test_setitem ( self ) : <EOL> n = <NUM_LIT:200> <EOL> d = deque ( xrange ( n ) ) <EOL> for i in xrange ( n ) : <EOL> d [ i ] = <NUM_LIT:10> * i <EOL> self . assertEqual ( list ( d ) , [ <NUM_LIT:10> * i for i in xrange ( n ) ] ) <EOL> l = list ( d ) <EOL> for i in xrange ( <NUM_LIT:1> - n , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> d [ i ] = <NUM_LIT:7> * i <EOL> l [ i ] = <NUM_LIT:7> * i <EOL> self . assertEqual ( list ( d ) , l ) <EOL> def test_delitem ( self ) : <EOL> n = <NUM_LIT> <EOL> d = deque ( xrange ( n ) ) <EOL> self . assertRaises ( IndexError , d . __delitem__ , - n - <NUM_LIT:1> ) <EOL> self . assertRaises ( IndexError , d . __delitem__ , n ) <EOL> for i in xrange ( n ) : <EOL> self . assertEqual ( len ( d ) , n - i ) <EOL> j = random . randrange ( - len ( d ) , len ( d ) ) <EOL> val = d [ j ] <EOL> self . assertIn ( val , d ) <EOL> del d [ j ] <EOL> self . assertNotIn ( val , d ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> def test_reverse ( self ) : <EOL> n = <NUM_LIT> <EOL> data = [ random . random ( ) for i in range ( n ) ] <EOL> for i in range ( n ) : <EOL> d = deque ( data [ : i ] ) <EOL> r = d . reverse ( ) <EOL> self . assertEqual ( list ( d ) , list ( reversed ( data [ : i ] ) ) ) <EOL> self . assertIs ( r , None ) <EOL> d . reverse ( ) <EOL> self . assertEqual ( list ( d ) , data [ : i ] ) <EOL> self . assertRaises ( TypeError , d . reverse , <NUM_LIT:1> ) <EOL> def test_rotate ( self ) : <EOL> s = tuple ( '<STR_LIT>' ) <EOL> n = len ( s ) <EOL> d = deque ( s ) <EOL> d . rotate ( <NUM_LIT:1> ) <EOL> self . assertEqual ( '<STR_LIT>' . join ( d ) , '<STR_LIT>' ) <EOL> d = deque ( s ) <EOL> d . rotate ( - <NUM_LIT:1> ) <EOL> self . assertEqual ( '<STR_LIT>' . join ( d ) , '<STR_LIT>' ) <EOL> d . rotate ( ) <EOL> self . assertEqual ( tuple ( d ) , s ) <EOL> for i in xrange ( n * <NUM_LIT:3> ) : <EOL> d = deque ( s ) <EOL> e = deque ( d ) <EOL> d . rotate ( i ) <EOL> for j in xrange ( i ) : <EOL> e . rotate ( <NUM_LIT:1> ) <EOL> self . assertEqual ( tuple ( d ) , tuple ( e ) ) <EOL> d . rotate ( - i ) <EOL> self . assertEqual ( tuple ( d ) , s ) <EOL> e . rotate ( n - i ) <EOL> self . assertEqual ( tuple ( e ) , s ) <EOL> for i in xrange ( n * <NUM_LIT:3> ) : <EOL> d = deque ( s ) <EOL> e = deque ( d ) <EOL> d . rotate ( - i ) <EOL> for j in xrange ( i ) : <EOL> e . rotate ( - <NUM_LIT:1> ) <EOL> self . assertEqual ( tuple ( d ) , tuple ( e ) ) <EOL> d . rotate ( i ) <EOL> self . assertEqual ( tuple ( d ) , s ) <EOL> e . rotate ( i - n ) <EOL> self . assertEqual ( tuple ( e ) , s ) <EOL> d = deque ( s ) <EOL> e = deque ( s ) <EOL> e . rotate ( BIG + <NUM_LIT> ) <EOL> dr = d . rotate <EOL> for i in xrange ( BIG + <NUM_LIT> ) : <EOL> dr ( ) <EOL> self . assertEqual ( tuple ( d ) , tuple ( e ) ) <EOL> self . assertRaises ( TypeError , d . rotate , '<STR_LIT:x>' ) <EOL> self . assertRaises ( TypeError , d . rotate , <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> d = deque ( ) <EOL> d . rotate ( ) <EOL> self . assertEqual ( d , deque ( ) ) <EOL> def test_len ( self ) : <EOL> d = deque ( '<STR_LIT>' ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:2> ) <EOL> d . popleft ( ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:1> ) <EOL> d . pop ( ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> self . assertRaises ( IndexError , d . pop ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> d . append ( '<STR_LIT:c>' ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:1> ) <EOL> d . appendleft ( '<STR_LIT:d>' ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:2> ) <EOL> d . clear ( ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> def test_underflow ( self ) : <EOL> d = deque ( ) <EOL> self . assertRaises ( IndexError , d . pop ) <EOL> self . assertRaises ( IndexError , d . popleft ) <EOL> def test_clear ( self ) : <EOL> d = deque ( xrange ( <NUM_LIT:100> ) ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:100> ) <EOL> d . clear ( ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( list ( d ) , [ ] ) <EOL> d . clear ( ) <EOL> self . assertEqual ( list ( d ) , [ ] ) <EOL> def test_remove ( self ) : <EOL> d = deque ( '<STR_LIT>' ) <EOL> d . remove ( '<STR_LIT:c>' ) <EOL> self . assertEqual ( d , deque ( '<STR_LIT>' ) ) <EOL> d . remove ( '<STR_LIT:c>' ) <EOL> self . assertEqual ( d , deque ( '<STR_LIT>' ) ) <EOL> self . assertRaises ( ValueError , d . remove , '<STR_LIT:c>' ) <EOL> self . assertEqual ( d , deque ( '<STR_LIT>' ) ) <EOL> d = deque ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , BadCmp ( ) , '<STR_LIT:c>' ] ) <EOL> e = deque ( d ) <EOL> self . assertRaises ( RuntimeError , d . remove , '<STR_LIT:c>' ) <EOL> for x , y in zip ( d , e ) : <EOL> self . assertTrue ( x is y ) <EOL> for match in ( True , False ) : <EOL> d = deque ( [ '<STR_LIT>' ] ) <EOL> d . extend ( [ MutateCmp ( d , match ) , '<STR_LIT:c>' ] ) <EOL> self . assertRaises ( IndexError , d . remove , '<STR_LIT:c>' ) <EOL> self . assertEqual ( d , deque ( ) ) <EOL> def test_repr ( self ) : <EOL> d = deque ( xrange ( <NUM_LIT:200> ) ) <EOL> e = eval ( repr ( d ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> d . append ( d ) <EOL> self . assertIn ( '<STR_LIT>' , repr ( d ) ) <EOL> def test_print ( self ) : <EOL> d = deque ( xrange ( <NUM_LIT:200> ) ) <EOL> d . append ( d ) <EOL> test_support . unlink ( test_support . TESTFN ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:wb>" ) <EOL> try : <EOL> print >> fo , d , <EOL> fo . close ( ) <EOL> fo = open ( test_support . TESTFN , "<STR_LIT:rb>" ) <EOL> self . assertEqual ( fo . read ( ) , repr ( d ) ) <EOL> finally : <EOL> fo . close ( ) <EOL> test_support . unlink ( test_support . TESTFN ) <EOL> def test_init ( self ) : <EOL> self . assertRaises ( TypeError , deque , '<STR_LIT:abc>' , <NUM_LIT:2> , <NUM_LIT:3> ) ; <EOL> self . assertRaises ( TypeError , deque , <NUM_LIT:1> ) ; <EOL> def test_hash ( self ) : <EOL> self . assertRaises ( TypeError , hash , deque ( '<STR_LIT:abc>' ) ) <EOL> def test_long_steadystate_queue_popleft ( self ) : <EOL> for size in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:100> , <NUM_LIT:1000> ) : <EOL> d = deque ( xrange ( size ) ) <EOL> append , pop = d . append , d . popleft <EOL> for i in xrange ( size , BIG ) : <EOL> append ( i ) <EOL> x = pop ( ) <EOL> if x != i - size : <EOL> self . assertEqual ( x , i - size ) <EOL> self . assertEqual ( list ( d ) , range ( BIG - size , BIG ) ) <EOL> def test_long_steadystate_queue_popright ( self ) : <EOL> for size in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:100> , <NUM_LIT:1000> ) : <EOL> d = deque ( reversed ( xrange ( size ) ) ) <EOL> append , pop = d . appendleft , d . pop <EOL> for i in xrange ( size , BIG ) : <EOL> append ( i ) <EOL> x = pop ( ) <EOL> if x != i - size : <EOL> self . assertEqual ( x , i - size ) <EOL> self . assertEqual ( list ( reversed ( list ( d ) ) ) , range ( BIG - size , BIG ) ) <EOL> def test_big_queue_popleft ( self ) : <EOL> pass <EOL> d = deque ( ) <EOL> append , pop = d . append , d . popleft <EOL> for i in xrange ( BIG ) : <EOL> append ( i ) <EOL> for i in xrange ( BIG ) : <EOL> x = pop ( ) <EOL> if x != i : <EOL> self . assertEqual ( x , i ) <EOL> def test_big_queue_popright ( self ) : <EOL> d = deque ( ) <EOL> append , pop = d . appendleft , d . pop <EOL> for i in xrange ( BIG ) : <EOL> append ( i ) <EOL> for i in xrange ( BIG ) : <EOL> x = pop ( ) <EOL> if x != i : <EOL> self . assertEqual ( x , i ) <EOL> def test_big_stack_right ( self ) : <EOL> d = deque ( ) <EOL> append , pop = d . append , d . pop <EOL> for i in xrange ( BIG ) : <EOL> append ( i ) <EOL> for i in reversed ( xrange ( BIG ) ) : <EOL> x = pop ( ) <EOL> if x != i : <EOL> self . assertEqual ( x , i ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> def test_big_stack_left ( self ) : <EOL> d = deque ( ) <EOL> append , pop = d . appendleft , d . popleft <EOL> for i in xrange ( BIG ) : <EOL> append ( i ) <EOL> for i in reversed ( xrange ( BIG ) ) : <EOL> x = pop ( ) <EOL> if x != i : <EOL> self . assertEqual ( x , i ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> def test_roundtrip_iter_init ( self ) : <EOL> d = deque ( xrange ( <NUM_LIT:200> ) ) <EOL> e = deque ( d ) <EOL> self . assertNotEqual ( id ( d ) , id ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> def test_pickle ( self ) : <EOL> d = deque ( xrange ( <NUM_LIT:200> ) ) <EOL> for i in range ( pickle . HIGHEST_PROTOCOL + <NUM_LIT:1> ) : <EOL> s = pickle . dumps ( d , i ) <EOL> e = pickle . loads ( s ) <EOL> self . assertNotEqual ( id ( d ) , id ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> def test_deepcopy ( self ) : <EOL> mut = [ <NUM_LIT:10> ] <EOL> d = deque ( [ mut ] ) <EOL> e = copy . deepcopy ( d ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> mut [ <NUM_LIT:0> ] = <NUM_LIT:11> <EOL> self . assertNotEqual ( id ( d ) , id ( e ) ) <EOL> self . assertNotEqual ( list ( d ) , list ( e ) ) <EOL> def test_copy ( self ) : <EOL> mut = [ <NUM_LIT:10> ] <EOL> d = deque ( [ mut ] ) <EOL> e = copy . copy ( d ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> mut [ <NUM_LIT:0> ] = <NUM_LIT:11> <EOL> self . assertNotEqual ( id ( d ) , id ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> def test_reversed ( self ) : <EOL> for s in ( '<STR_LIT>' , xrange ( <NUM_LIT> ) ) : <EOL> self . assertEqual ( list ( reversed ( deque ( s ) ) ) , list ( reversed ( s ) ) ) <EOL> def test_gc_doesnt_blowup ( self ) : <EOL> import gc <EOL> d = deque ( ) <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> d . append ( <NUM_LIT:1> ) <EOL> gc . collect ( ) <EOL> def test_container_iterator ( self ) : <EOL> class C ( object ) : <EOL> pass <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> obj = C ( ) <EOL> ref = weakref . ref ( obj ) <EOL> if i == <NUM_LIT:0> : <EOL> container = deque ( [ obj , <NUM_LIT:1> ] ) <EOL> else : <EOL> container = reversed ( deque ( [ obj , <NUM_LIT:1> ] ) ) <EOL> obj . x = iter ( container ) <EOL> del obj , container <EOL> gc . collect ( ) <EOL> self . assertTrue ( ref ( ) is None , "<STR_LIT>" ) <EOL> check_sizeof = test_support . check_sizeof <EOL> @ test_support . cpython_only <EOL> def test_sizeof ( self ) : <EOL> BLOCKLEN = <NUM_LIT> <EOL> basesize = test_support . calcobjsize ( '<STR_LIT>' ) <EOL> blocksize = struct . calcsize ( '<STR_LIT>' % BLOCKLEN ) <EOL> self . assertEqual ( object . __sizeof__ ( deque ( ) ) , basesize ) <EOL> check = self . check_sizeof <EOL> check ( deque ( ) , basesize + blocksize ) <EOL> check ( deque ( '<STR_LIT:a>' ) , basesize + blocksize ) <EOL> check ( deque ( '<STR_LIT:a>' * ( BLOCKLEN // <NUM_LIT:2> ) ) , basesize + blocksize ) <EOL> check ( deque ( '<STR_LIT:a>' * ( BLOCKLEN // <NUM_LIT:2> + <NUM_LIT:1> ) ) , basesize + <NUM_LIT:2> * blocksize ) <EOL> check ( deque ( '<STR_LIT:a>' * ( <NUM_LIT> * BLOCKLEN ) ) , basesize + <NUM_LIT> * blocksize ) <EOL> class TestVariousIteratorArgs ( unittest . TestCase ) : <EOL> def test_constructor ( self ) : <EOL> for s in ( "<STR_LIT>" , "<STR_LIT>" , range ( <NUM_LIT:1000> ) , ( '<STR_LIT>' , <NUM_LIT> ) , xrange ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:5> ) ) : <EOL> for g in ( seq_tests . Sequence , seq_tests . IterFunc , <EOL> seq_tests . IterGen , seq_tests . IterFuncStop , <EOL> seq_tests . itermulti , seq_tests . iterfunc ) : <EOL> self . assertEqual ( list ( deque ( g ( s ) ) ) , list ( g ( s ) ) ) <EOL> self . assertRaises ( TypeError , deque , seq_tests . IterNextOnly ( s ) ) <EOL> self . assertRaises ( TypeError , deque , seq_tests . IterNoNext ( s ) ) <EOL> self . assertRaises ( ZeroDivisionError , deque , seq_tests . IterGenExc ( s ) ) <EOL> def test_iter_with_altered_data ( self ) : <EOL> d = deque ( '<STR_LIT>' ) <EOL> it = iter ( d ) <EOL> d . pop ( ) <EOL> self . assertRaises ( RuntimeError , it . next ) <EOL> def test_runtime_error_on_empty_deque ( self ) : <EOL> d = deque ( ) <EOL> it = iter ( d ) <EOL> d . append ( <NUM_LIT:10> ) <EOL> self . assertRaises ( RuntimeError , it . next ) <EOL> class Deque ( deque ) : <EOL> pass <EOL> class DequeWithBadIter ( deque ) : <EOL> def __iter__ ( self ) : <EOL> raise TypeError <EOL> class TestSubclass ( unittest . TestCase ) : <EOL> def test_basics ( self ) : <EOL> d = Deque ( xrange ( <NUM_LIT> ) ) <EOL> d . __init__ ( xrange ( <NUM_LIT:200> ) ) <EOL> for i in xrange ( <NUM_LIT:200> , <NUM_LIT> ) : <EOL> d . append ( i ) <EOL> for i in reversed ( xrange ( - <NUM_LIT:200> , <NUM_LIT:0> ) ) : <EOL> d . appendleft ( i ) <EOL> self . assertEqual ( list ( d ) , range ( - <NUM_LIT:200> , <NUM_LIT> ) ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT> ) <EOL> left = [ d . popleft ( ) for i in xrange ( <NUM_LIT> ) ] <EOL> self . assertEqual ( left , range ( - <NUM_LIT:200> , <NUM_LIT:50> ) ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:50> , <NUM_LIT> ) ) <EOL> right = [ d . pop ( ) for i in xrange ( <NUM_LIT> ) ] <EOL> right . reverse ( ) <EOL> self . assertEqual ( right , range ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertEqual ( list ( d ) , range ( <NUM_LIT:50> , <NUM_LIT> ) ) <EOL> d . clear ( ) <EOL> self . assertEqual ( len ( d ) , <NUM_LIT:0> ) <EOL> def test_copy_pickle ( self ) : <EOL> d = Deque ( '<STR_LIT:abc>' ) <EOL> e = d . __copy__ ( ) <EOL> self . assertEqual ( type ( d ) , type ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> e = Deque ( d ) <EOL> self . assertEqual ( type ( d ) , type ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> for proto in range ( pickle . HIGHEST_PROTOCOL + <NUM_LIT:1> ) : <EOL> s = pickle . dumps ( d , proto ) <EOL> e = pickle . loads ( s ) <EOL> self . assertNotEqual ( id ( d ) , id ( e ) ) <EOL> self . assertEqual ( type ( d ) , type ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> d = Deque ( '<STR_LIT>' , maxlen = <NUM_LIT:4> ) <EOL> e = d . __copy__ ( ) <EOL> self . assertEqual ( type ( d ) , type ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> e = Deque ( d ) <EOL> self . assertEqual ( type ( d ) , type ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> for proto in range ( pickle . HIGHEST_PROTOCOL + <NUM_LIT:1> ) : <EOL> s = pickle . dumps ( d , proto ) <EOL> e = pickle . loads ( s ) <EOL> self . assertNotEqual ( id ( d ) , id ( e ) ) <EOL> self . assertEqual ( type ( d ) , type ( e ) ) <EOL> self . assertEqual ( list ( d ) , list ( e ) ) <EOL> def test_weakref ( self ) : <EOL> d = deque ( '<STR_LIT>' ) <EOL> p = weakref . proxy ( d ) <EOL> self . assertEqual ( str ( p ) , str ( d ) ) <EOL> d = None <EOL> self . assertRaises ( ReferenceError , str , p ) <EOL> def test_strange_subclass ( self ) : <EOL> class X ( deque ) : <EOL> def __iter__ ( self ) : <EOL> return iter ( [ ] ) <EOL> d1 = X ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d2 = X ( [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) <EOL> d1 == d2 <EOL> class SubclassWithKwargs ( deque ) : <EOL> def __init__ ( self , newarg = <NUM_LIT:1> ) : <EOL> deque . __init__ ( self ) <EOL> class TestSubclassWithKwargs ( unittest . TestCase ) : <EOL> def test_subclass_with_kwargs ( self ) : <EOL> SubclassWithKwargs ( newarg = <NUM_LIT:1> ) <EOL> libreftest = """<STR_LIT>""" <EOL> __test__ = { '<STR_LIT>' : libreftest } <EOL> def test_main ( verbose = None ) : <EOL> import sys <EOL> test_classes = ( <EOL> TestBasic , <EOL> TestVariousIteratorArgs , <EOL> TestSubclass , <EOL> TestSubclassWithKwargs , <EOL> ) <EOL> test_support . run_unittest ( * test_classes ) <EOL> if verbose and hasattr ( sys , "<STR_LIT>" ) : <EOL> import gc <EOL> counts = [ None ] * <NUM_LIT:5> <EOL> for i in xrange ( len ( counts ) ) : <EOL> test_support . run_unittest ( * test_classes ) <EOL> gc . collect ( ) <EOL> counts [ i ] = sys . gettotalrefcount ( ) <EOL> print counts <EOL> from test import test_deque <EOL> test_support . run_doctest ( test_deque , verbose ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( verbose = True ) </s>
