# coding: iso-8859-15importrandomfrompypy.objspace.std.listobjectimportW_ListObject,SizeListStrategy,\
IntegerListStrategy,ObjectListStrategyfrompypy.interpreter.errorimportOperationErrorfrompypy.rlib.rarithmeticimportis_valid_intclassTestW_ListObject(object):deftest_is_true(self):w=self.space.wrapw_list=W_ListObject(self.space,[])assertself.space.is_true(w_list)==Falsew_list=W_ListObject(self.space,[w(5)])assertself.space.is_true(w_list)==Truew_list=W_ListObject(self.space,[w(5),w(3)])assertself.space.is_true(w_list)==Truedeftest_len(self):w=self.space.wrapw_list=W_ListObject(self.space,[])assertself.space.eq_w(self.space.len(w_list),w(0))w_list=W_ListObject(self.space,[w(5)])assertself.space.eq_w(self.space.len(w_list),w(1))w_list=W_ListObject(self.space,[w(5),w(3),w(99)]*111)assertself.space.eq_w(self.space.len(w_list),w(333))deftest_getitem(self):w=self.space.wrapw_list=W_ListObject(self.space,[w(5),w(3)])assertself.space.eq_w(self.space.getitem(w_list,w(0)),w(5))assertself.space.eq_w(self.space.getitem(w_list,w(1)),w(3))assertself.space.eq_w(self.space.getitem(w_list,w(-2)),w(5))assertself.space.eq_w(self.space.getitem(w_list,w(-1)),w(3))self.space.raises_w(self.space.w_IndexError,self.space.getitem,w_list,w(2))self.space.raises_w(self.space.w_IndexError,self.space.getitem,w_list,w(42))self.space.raises_w(self.space.w_IndexError,self.space.getitem,w_list,w(-3))deftest_getitems(self):w=self.space.wrapfrompypy.objspace.std.listobjectimportmake_range_listr=make_range_list(self.space,1,1,7)l=[w(1),w(2),w(3),w(4),w(5),w(6),w(7)]l2=r.getitems()foriinrange(7):assertself.space.eq_w(l[i],l2[i])deftest_getitems_fixedsize(self):w=self.space.wrapfrompypy.objspace.std.listobjectimportmake_range_listrangelist=make_range_list(self.space,1,1,7)emptylist=W_ListObject(self.space,[])intlist=W_ListObject(self.space,[w(1),w(2),w(3),w(4),w(5),w(6),w(7)])strlist=W_ListObject(self.space,[w('1'),w('2'),w('3'),w('4'),w('5'),w('6'),w('7')])floatlist=W_ListObject(self.space,[w(1.0),w(2.0),w(3.0),w(4.0),w(5.0),w(6.0),w(7.0)])objlist=W_ListObject(self.space,[w(1),w('2'),w(3.0),w(4),w(5),w(6),w(7)])emptylist_copy=emptylist.getitems_fixedsize()assertemptylist_copy==[]rangelist_copy=rangelist.getitems_fixedsize()intlist_copy=intlist.getitems_fixedsize()strlist_copy=strlist.getitems_fixedsize()floatlist_copy=floatlist.getitems_fixedsize()objlist_copy=objlist.getitems_fixedsize()foriinrange(7):assertself.space.eq_w(rangelist_copy[i],rangelist.getitem(i))assertself.space.eq_w(intlist_copy[i],intlist.getitem(i))assertself.space.eq_w(strlist_copy[i],strlist.getitem(i))assertself.space.eq_w(floatlist_copy[i],floatlist.getitem(i))assertself.space.eq_w(objlist_copy[i],objlist.getitem(i))emptylist_copy=emptylist.getitems_unroll()assertemptylist_copy==[]rangelist_copy=rangelist.getitems_unroll()intlist_copy=intlist.getitems_unroll()strlist_copy=strlist.getitems_unroll()floatlist_copy=floatlist.getitems_unroll()objlist_copy=objlist.getitems_unroll()foriinrange(7):assertself.space.eq_w(rangelist_copy[i],rangelist.getitem(i))assertself.space.eq_w(intlist_copy[i],intlist.getitem(i))assertself.space.eq_w(strlist_copy[i],strlist.getitem(i))assertself.space.eq_w(floatlist_copy[i],floatlist.getitem(i))assertself.space.eq_w(objlist_copy[i],objlist.getitem(i))deftest_random_getitem(self):w=self.space.wraps=list('qedx387tn3uixhvt 7fh387fymh3dh238 dwd-wq.dwq9')w_list=W_ListObject(self.space,map(w,s))keys=range(-len(s)-5,len(s)+5)choices=keys+[None]*12stepchoices=[None,None,None,1,1,-1,-1,2,-2,len(s)-1,len(s),len(s)+1,-len(s)-1,-len(s),-len(s)+1]foriinrange(40):keys.append(slice(random.choice(choices),random.choice(choices),random.choice(stepchoices)))random.shuffle(keys)forkeyinkeys:try:expected=s[key]exceptIndexError:self.space.raises_w(self.space.w_IndexError,self.space.getitem,w_list,w(key))else:w_result=self.space.getitem(w_list,w(key))assertself.space.unwrap(w_result)==expecteddeftest_iter(self):w=self.space.wrapw_list=W_ListObject(self.space,[w(5),w(3),w(99)])w_iter=self.space.iter(w_list)assertself.space.eq_w(self.space.next(w_iter),w(5))assertself.space.eq_w(self.space.next(w_iter),w(3))assertself.space.eq_w(self.space.next(w_iter),w(99))raises(OperationError,self.space.next,w_iter)raises(OperationError,self.space.next,w_iter)deftest_contains(self):w=self.space.wrapw_list=W_ListObject(self.space,[w(5),w(3),w(99)])assertself.space.eq_w(self.space.contains(w_list,w(5)),self.space.w_True)assertself.space.eq_w(self.space.contains(w_list,w(99)),self.space.w_True)assertself.space.eq_w(self.space.contains(w_list,w(11)),self.space.w_False)assertself.space.eq_w(self.space.contains(w_list,w_list),self.space.w_False)deftest_getslice(self):w=self.space.wrapdeftest1(testlist,start,stop,step,expected):w_slice=self.space.newslice(w(start),w(stop),w(step))w_list=W_ListObject(self.space,[w(i)foriintestlist])w_result=self.space.getitem(w_list,w_slice)assertself.space.unwrap(w_result)==expectedfortestlistin[[],[5,3,99]]:forstartin[-2,0,1,10]:forendin[-1,2,999]:test1(testlist,start,end,1,testlist[start:end])test1([5,7,1,4],3,1,-2,[4,])test1([5,7,1,4],3,0,-2,[4,7])test1([5,7,1,4],3,-1,-2,[])test1([5,7,1,4],-2,11,2,[1,])test1([5,7,1,4],-3,11,2,[7,4])test1([5,7,1,4],-5,11,2,[5,1])deftest_setslice(self):w=self.space.wrapdeftest1(lhslist,start,stop,rhslist,expected):w_slice=self.space.newslice(w(start),w(stop),w(1))w_lhslist=W_ListObject(self.space,[w(i)foriinlhslist])w_rhslist=W_ListObject(self.space,[w(i)foriinrhslist])self.space.setitem(w_lhslist,w_slice,w_rhslist)assertself.space.unwrap(w_lhslist)==expectedtest1([5,7,1,4],1,3,[9,8],[5,9,8,4])test1([5,7,1,4],1,3,[9],[5,9,4])test1([5,7,1,4],1,3,[9,8,6],[5,9,8,6,4])test1([5,7,1,4],1,3,[],[5,4])test1([5,7,1,4],2,2,[9],[5,7,9,1,4])test1([5,7,1,4],0,99,[9,8],[9,8])deftest_add(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(-7)]*111)assertself.space.eq_w(self.space.add(w_list1,w_list1),W_ListObject(self.space,[w(5),w(3),w(99),w(5),w(3),w(99)]))assertself.space.eq_w(self.space.add(w_list1,w_list2),W_ListObject(self.space,[w(5),w(3),w(99)]+[w(-7)]*111))assertself.space.eq_w(self.space.add(w_list1,w_list0),w_list1)assertself.space.eq_w(self.space.add(w_list0,w_list2),w_list2)deftest_mul(self):# only testing right mul at the momentw=self.space.wraparg=w(2)n=3w_lis=W_ListObject(self.space,[arg])w_lis3=W_ListObject(self.space,[arg]*n)w_res=self.space.mul(w_lis,w(n))assertself.space.eq_w(w_lis3,w_res)# commutew_res=self.space.mul(w(n),w_lis)assertself.space.eq_w(w_lis3,w_res)deftest_setitem(self):w=self.space.wrapw_list=W_ListObject(self.space,[w(5),w(3)])w_exp1=W_ListObject(self.space,[w(5),w(7)])w_exp2=W_ListObject(self.space,[w(8),w(7)])self.space.setitem(w_list,w(1),w(7))assertself.space.eq_w(w_exp1,w_list)self.space.setitem(w_list,w(-2),w(8))assertself.space.eq_w(w_exp2,w_list)self.space.raises_w(self.space.w_IndexError,self.space.setitem,w_list,w(2),w(5))self.space.raises_w(self.space.w_IndexError,self.space.setitem,w_list,w(-3),w(5))deftest_random_setitem_delitem(self):w=self.space.wraps=range(39)w_list=W_ListObject(self.space,map(w,s))expected=list(s)keys=range(-len(s)-5,len(s)+5)choices=keys+[None]*12stepchoices=[None,None,None,1,1,-1,-1,2,-2,len(s)-1,len(s),len(s)+1,-len(s)-1,-len(s),-len(s)+1]foriinrange(50):keys.append(slice(random.choice(choices),random.choice(choices),random.choice(stepchoices)))random.shuffle(keys)n=len(s)forkeyinkeys:ifrandom.random()<0.15:random.shuffle(s)w_list=W_ListObject(self.space,map(w,s))expected=list(s)try:value=expected[key]exceptIndexError:self.space.raises_w(self.space.w_IndexError,self.space.setitem,w_list,w(key),w(42))else:ifis_valid_int(value):# non-slicingifrandom.random()<0.25:# deletingself.space.delitem(w_list,w(key))delexpected[key]else:self.space.setitem(w_list,w(key),w(n))expected[key]=nn+=1else:# slice assignmentmode=random.choice(['samesize','resize','delete'])ifmode=='delete':self.space.delitem(w_list,w(key))delexpected[key]elifmode=='samesize':newvalue=range(n,n+len(value))self.space.setitem(w_list,w(key),w(newvalue))expected[key]=newvaluen+=len(newvalue)elifmode=='resize'andkey.stepisNone:newvalue=range(n,n+random.randrange(0,20))self.space.setitem(w_list,w(key),w(newvalue))expected[key]=newvaluen+=len(newvalue)assertself.space.unwrap(w_list)==expecteddeftest_eq(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(5),w(3),w(99)])w_list3=W_ListObject(self.space,[w(5),w(3),w(99),w(-1)])assertself.space.eq_w(self.space.eq(w_list0,w_list1),self.space.w_False)assertself.space.eq_w(self.space.eq(w_list1,w_list0),self.space.w_False)assertself.space.eq_w(self.space.eq(w_list1,w_list1),self.space.w_True)assertself.space.eq_w(self.space.eq(w_list1,w_list2),self.space.w_True)assertself.space.eq_w(self.space.eq(w_list2,w_list3),self.space.w_False)deftest_ne(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(5),w(3),w(99)])w_list3=W_ListObject(self.space,[w(5),w(3),w(99),w(-1)])assertself.space.eq_w(self.space.ne(w_list0,w_list1),self.space.w_True)assertself.space.eq_w(self.space.ne(w_list1,w_list0),self.space.w_True)assertself.space.eq_w(self.space.ne(w_list1,w_list1),self.space.w_False)assertself.space.eq_w(self.space.ne(w_list1,w_list2),self.space.w_False)assertself.space.eq_w(self.space.ne(w_list2,w_list3),self.space.w_True)deftest_lt(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(5),w(3),w(99)])w_list3=W_ListObject(self.space,[w(5),w(3),w(99),w(-1)])w_list4=W_ListObject(self.space,[w(5),w(3),w(9),w(-1)])assertself.space.eq_w(self.space.lt(w_list0,w_list1),self.space.w_True)assertself.space.eq_w(self.space.lt(w_list1,w_list0),self.space.w_False)assertself.space.eq_w(self.space.lt(w_list1,w_list1),self.space.w_False)assertself.space.eq_w(self.space.lt(w_list1,w_list2),self.space.w_False)assertself.space.eq_w(self.space.lt(w_list2,w_list3),self.space.w_True)assertself.space.eq_w(self.space.lt(w_list4,w_list3),self.space.w_True)deftest_ge(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(5),w(3),w(99)])w_list3=W_ListObject(self.space,[w(5),w(3),w(99),w(-1)])w_list4=W_ListObject(self.space,[w(5),w(3),w(9),w(-1)])assertself.space.eq_w(self.space.ge(w_list0,w_list1),self.space.w_False)assertself.space.eq_w(self.space.ge(w_list1,w_list0),self.space.w_True)assertself.space.eq_w(self.space.ge(w_list1,w_list1),self.space.w_True)assertself.space.eq_w(self.space.ge(w_list1,w_list2),self.space.w_True)assertself.space.eq_w(self.space.ge(w_list2,w_list3),self.space.w_False)assertself.space.eq_w(self.space.ge(w_list4,w_list3),self.space.w_False)deftest_gt(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(5),w(3),w(99)])w_list3=W_ListObject(self.space,[w(5),w(3),w(99),w(-1)])w_list4=W_ListObject(self.space,[w(5),w(3),w(9),w(-1)])assertself.space.eq_w(self.space.gt(w_list0,w_list1),self.space.w_False)assertself.space.eq_w(self.space.gt(w_list1,w_list0),self.space.w_True)assertself.space.eq_w(self.space.gt(w_list1,w_list1),self.space.w_False)assertself.space.eq_w(self.space.gt(w_list1,w_list2),self.space.w_False)assertself.space.eq_w(self.space.gt(w_list2,w_list3),self.space.w_False)assertself.space.eq_w(self.space.gt(w_list4,w_list3),self.space.w_False)deftest_le(self):w=self.space.wrapw_list0=W_ListObject(self.space,[])w_list1=W_ListObject(self.space,[w(5),w(3),w(99)])w_list2=W_ListObject(self.space,[w(5),w(3),w(99)])w_list3=W_ListObject(self.space,[w(5),w(3),w(99),w(-1)])w_list4=W_ListObject(self.space,[w(5),w(3),w(9),w(-1)])assertself.space.eq_w(self.space.le(w_list0,w_list1),self.space.w_True)assertself.space.eq_w(self.space.le(w_list1,w_list0),self.space.w_False)assertself.space.eq_w(self.space.le(w_list1,w_list1),self.space.w_True)assertself.space.eq_w(self.space.le(w_list1,w_list2),self.space.w_True)assertself.space.eq_w(self.space.le(w_list2,w_list3),self.space.w_True)assertself.space.eq_w(self.space.le(w_list4,w_list3),self.space.w_True)deftest_sizehint(self):space=self.spacew_l=space.newlist([],sizehint=10)assertisinstance(w_l.strategy,SizeListStrategy)space.call_method(w_l,'append',space.wrap(3))assertisinstance(w_l.strategy,IntegerListStrategy)w_l=space.newlist([],sizehint=10)space.call_method(w_l,'append',space.w_None)assertisinstance(w_l.strategy,ObjectListStrategy)deftest_newlist_hint(self):space=self.spacew_lst=space.newlist_hint(13)assertisinstance(w_lst.strategy,SizeListStrategy)assertw_lst.strategy.sizehint==13classAppTestW_ListObject(object):defsetup_class(cls):importsyson_cpython=(cls.runappdirectandnothasattr(sys,'pypy_translation_info'))cls.w_on_cpython=cls.space.wrap(on_cpython)cls.w_runappdirect=cls.space.wrap(cls.runappdirect)deftest_getstrategyfromlist_w(self):l0=["a","2","a",True]# this raised TypeError on ListStrategiesl1=["a","2",True,"a"]l2=[1,"2","a","a"]assertsorted(l1)==sorted(l2)deftest_notequals(self):assert[1,2,3,4]!=[1,2,5,4]deftest_contains(self):l=[]assertnotl.__contains__(2)l=[1,2,3]assertl.__contains__(2)assertnotl.__contains__("2")assertl.__contains__(1.0)l=["1","2","3"]assertl.__contains__("2")assertnotl.__contains__(2)l=range(4)assertl.__contains__(2)assertnotl.__contains__("2")l=[1,2,"3"]assertl.__contains__(2)assertnotl.__contains__("2")l=range(2,20,3)# = [2, 5, 8, 11, 14, 17]assertl.__contains__(2)assertl.__contains__(5)assertl.__contains__(8)assertl.__contains__(11)assertl.__contains__(14)assertl.__contains__(17)assertnotl.__contains__(3)assertnotl.__contains__(4)assertnotl.__contains__(7)assertnotl.__contains__(13)assertnotl.__contains__(20)l=range(2,-20,-3)# [2, -1, -4, -7, -10, -13, -16, -19]assertl.__contains__(2)assertl.__contains__(-4)assertl.__contains__(-13)assertl.__contains__(-16)assertl.__contains__(-19)assertnotl.__contains__(-17)assertnotl.__contains__(-3)assertnotl.__contains__(-20)assertnotl.__contains__(-21)deftest_call_list(self):assertlist('')==[]assertlist('abc')==['a','b','c']assertlist((1,2))==[1,2]l=[1]assertlist(l)isnotlassertlist(l)==lassertlist(range(10))==range(10)deftest_explicit_new_init(self):l=l0=list.__new__(list)l.__init__([1,2])assertlisl0assertl==[1,2]list.__init__(l,[1,2,3])assertlisl0assertl==[1,2,3]list.__init__(l,['a','b','c'])assertlisl0assertl==['a','b','c']list.__init__(l)assertl==[]deftest_explicit_new_init_more_cases(self):forassignmentin[[],(),[3],["foo"]]:l=[1,2]l.__init__(assignment)assertl==list(assignment)deftest_extend_list(self):l=l0=[1]l.extend([2])assertlisl0assertl==[1,2]l=['a']l.extend('b')assertl==['a','b']l=['a']l.extend([0])assertl==['a',0]l=range(10)l.extend([10])assertl==range(11)l=[]m=[1,2,3]l.extend(m)m[0]=5assertm==[5,2,3]assertl==[1,2,3]deftest_extend_tuple(self):l=l0=[1]l.extend((2,))assertlisl0assertl==[1,2]l=['a']l.extend(('b',))assertl==['a','b']deftest_extend_iterable(self):l=l0=[1]l.extend(iter([1,2,3,4]))assertlisl0assertl==[1,1,2,3,4]l=l0=['a']l.extend(iter(['b','c','d']))assertl==['a','b','c','d']assertlisl0l=l0=[1.2]l.extend(iter([2.3,3.4,4.5]))assertl==[1.2,2.3,3.4,4.5]assertlisl0deftest_sort(self):l=l0=[1,5,3,0]l.sort()assertlisl0assertl==[0,1,3,5]l=l0=[]l.sort()assertlisl0assertl==[]l=l0=[1]l.sort()assertlisl0assertl==[1]l=["c","a","d","b"]l.sort(reverse=True)assertl==["d","c","b","a"]l=[3.3,2.2,4.4,1.1,3.1,5.5]l.sort()assertl==[1.1,2.2,3.1,3.3,4.4,5.5]deftest_sort_cmp(self):deflencmp(a,b):returncmp(len(a),len(b))l=['a','fiver','tre','']l.sort(lencmp)assertl==['','a','tre','fiver']l=[]l.sort(lencmp)assertl==[]l=['a']l.sort(lencmp)assertl==['a']deftest_sort_key(self):deflower(x):returnx.lower()l=['a','C','b']l.sort(key=lower)assertl==['a','b','C']l=[]l.sort(key=lower)assertl==[]l=['a']l.sort(key=lower)assertl==['a']deftest_sort_reversed(self):l=range(10)l.sort(reverse=True)assertl==range(9,-1,-1)l=[]l.sort(reverse=True)assertl==[]l=[1]l.sort(reverse=True)assertl==[1]deftest_sort_cmp_key_reverse(self):deflower(x):returnx.lower()l=['a','C','b']l.sort(reverse=True,key=lower)assertl==['C','b','a']deftest_sort_simple_string(self):l=["a","d","c","b"]l.sort()assertl==["a","b","c","d"]deftest_getitem(self):l=[1,2,3,4,5,6,9]assertl[0]==1assertl[-1]==9assertl[-2]==6raises(IndexError,"l[len(l)]")raises(IndexError,"l[-len(l)-1]")l=['a','b','c']assertl[0]=='a'assertl[-1]=='c'assertl[-2]=='b'raises(IndexError,"l[len(l)]")l=[1.1,2.2,3.3]assertl[0]==1.1assertl[-1]==3.3assertl[-2]==2.2raises(IndexError,"l[len(l)]")l=[]raises(IndexError,"l[1]")deftest_setitem(self):l=[]raises(IndexError,"l[1] = 2")l=[5,3]l[0]=2assertl==[2,3]l=[5,3]l[0]="2"assertl==["2",3]deftest_delitem(self):l=[1,2,3,4,5,6,9]dell[0]assertl==[2,3,4,5,6,9]dell[-1]assertl==[2,3,4,5,6]dell[-2]assertl==[2,3,4,6]raises(IndexError,"del l[len(l)]")raises(IndexError,"del l[-len(l)-1]")l=l0=['a','b','c']dell[0]assertl==['b','c']dell[-1]assertl==['b']dell[-1]assertl==[]assertlisl0raises(IndexError,"del l[0]")l=l0=[1.1,2.2,3.3]dell[0]assertl==[2.2,3.3]dell[-1]assertl==[2.2]dell[-1]assertl==[]assertlisl0raises(IndexError,"del l[0]")l=range(10)dell[5]assertl==[0,1,2,3,4,6,7,8,9]deftest_getitem_slice(self):l=range(10)assertl[::]==ldell[::2]assertl==[1,3,5,7,9]l[-2::-1]=l[:-1]assertl==[7,5,3,1,9]dell[-1:2:-1]assertl==[7,5,3]dell[:2]assertl==[3]assertl[1:]==[]assertl[1::2]==[]assertl[::]==lassertl[0::-2]==lassertl[-1::-5]==ll=['']assertl[1:]==[]assertl[1::2]==[]assertl[::]==lassertl[0::-5]==lassertl[-1::-5]==ll.extend(['a','b'])assertl[::-1]==['b','a','']l=[1,2,3,4,5]assertl[1:0:None]==[]assertl[1:0]==[]deftest_delall(self):l=l0=[1,2,3]dell[:]assertlisl0assertl==[]l=['a','b']dell[:]assertl==[]l=[1.1,2.2]dell[:]assertl==[]l=range(5)dell[:]assertl==[]deftest_iadd(self):l=l0=[1,2,3]l+=[4,5]assertlisl0assertl==[1,2,3,4,5]l=l0=[1.1,2.2,3.3]l+=[4.4,5.5]assertlisl0assertl==[1.1,2.2,3.3,4.4,5.5]l=l0=['a','b','c']l1=l[:]l+=['d']assertlisl0assertl==['a','b','c','d']l1+=[0]assertl1==['a','b','c',0]deftest_iadd_iterable(self):l=l0=[1,2,3]l+=iter([4,5])assertlisl0assertl==[1,2,3,4,5]deftest_iadd_subclass(self):classBar(object):def__radd__(self,other):return('radd',self,other)bar=Bar()l1=[1,2,3]l1+=barassertl1==('radd',bar,[1,2,3])deftest_add_lists(self):l1=[1,2,3]l2=[4,5,6]l3=l1+l2assertl3==[1,2,3,4,5,6]l4=range(3)l5=l4+l2assertl5==[0,1,2,4,5,6]deftest_imul(self):l=l0=[4,3]l*=2assertlisl0assertl==[4,3,4,3]l*=0assertlisl0assertl==[]l=l0=[4,3]l*=(-1)assertlisl0assertl==[]l=l0=['a','b']l*=2assertlisl0assertl==['a','b','a','b']l*=0assertlisl0assertl==[]l=['a']l*=-5assertl==[]l=l0=[1.1,2.2]l*=2assertlisl0assertl==[1.1,2.2,1.1,2.2]l=range(2)l*=2assertl==[0,1,0,1]deftest_mul_errors(self):try:[1,2,3]*(3,)exceptTypeError:passdeftest_index(self):c=range(10)assertc.index(0)==0raises(ValueError,c.index,10)c=list('hello world')assertc.index('l')==2raises(ValueError,c.index,'!')assertc.index('l',3)==3assertc.index('l',4)==9raises(ValueError,c.index,'l',10)assertc.index('l',-5)==9assertc.index('l',-25)==2assertc.index('o',1,5)==4raises(ValueError,c.index,'o',1,4)assertc.index('o',1,5-11)==4raises(ValueError,c.index,'o',1,4-11)raises(TypeError,c.index,'c',0,4.3)raises(TypeError,c.index,'c',1.0,5.6)c=[0,2,4]assertc.index(0)==0raises(ValueError,c.index,3)c=[0.0,2.2,4.4]assertc.index(0)==0.0raises(ValueError,c.index,3)deftest_index_cpython_bug(self):ifself.on_cpython:skip("cpython has a bug here")c=list('hello world')assertc.index('l',None,None)==2assertc.index('l',3,None)==3assertc.index('l',None,4)==2deftest_ass_slice(self):l=range(6)l[1:3]='abc'assertl==[0,'a','b','c',3,4,5]l=[]l[:-3]=[]assertl==[]l=range(6)l[:]=[]assertl==[]l=l0=['a','b']l[1:1]=['ae']assertl==['a','ae','b']l[1:100]=['B']assertl==['a','B']l[:]=[]assertl==[]assertlisl0deftest_assign_extended_slice(self):l=l0=['a','b','c']l[::-1]=['a','b','c']assertl==['c','b','a']l[::-2]=[0,1]assertl==[1,'b',0]l[-1:5:2]=[2]assertl==[1,'b',2]l[:-1:2]=[0]assertl==[0,'b',2]assertlisl0l=[1,2,3]raises(ValueError,"l[0:2:2] = [1,2,3,4]")raises(ValueError,"l[::2] = []")l=range(6)l[::3]=('a','b')assertl==['a',1,2,'b',4,5]l=[0.0,1.1,2.2,3.3,4.4,5.5]l[::3]=('a','b')assertl==['a',1.1,2.2,'b',4.4,5.5]deftest_setslice_with_self(self):l=[1,2,3,4]l[:]=lassertl==[1,2,3,4]l=[1,2,3,4]l[0:2]=lassertl==[1,2,3,4,3,4]l=[1,2,3,4]l[0:2]=lassertl==[1,2,3,4,3,4]l=[1,2,3,4,5,6,7,8,9,10]raises(ValueError,"l[5::-1] = l")l=[1,2,3,4,5,6,7,8,9,10]raises(ValueError,"l[::2] = l")l=[1,2,3,4,5,6,7,8,9,10]l[5:]=lassertl==[1,2,3,4,5,1,2,3,4,5,6,7,8,9,10]l=[1,2,3,4,5,6]l[::-1]=lassertl==[6,5,4,3,2,1]deftest_setitem_slice_performance(self):# because of a complexity bug, this used to take forever on a# translated pypy. On CPython2.6 -A, it takes around 5 seconds.ifself.runappdirect:count=16*1024*1024else:count=1024b=[None]*countforiinrange(count):b[i:i+1]=['y']assertb==['y']*countdeftest_recursive_repr(self):l=[]assertrepr(l)=='[]'l.append(l)assertrepr(l)=='[[...]]'deftest_append(self):l=[]l.append('X')assertl==['X']l.append('Y')l.append('Z')assertl==['X','Y','Z']l=[]l.append(0)assertl==[0]forxinrange(1,5):l.append(x)assertl==range(5)l=range(4)l.append(4)assertl==range(5)l=[1,2,3]l.append("a")assertl==[1,2,3,"a"]l=[1.1,2.2,3.3]l.append(4.4)assertl==[1.1,2.2,3.3,4.4]deftest_count(self):c=list('hello')assertc.count('l')==2assertc.count('h')==1assertc.count('w')==0deftest_insert(self):c=list('hello world')c.insert(0,'X')assertc[:4]==['X','h','e','l']c.insert(2,'Y')c.insert(-2,'Z')assert''.join(c)=='XhYello worZld'ls=[1,2,3,4,5,6,7]foriinrange(5):ls.insert(0,i)assertlen(ls)==12l=[]l.insert(4,2)assertl==[2]l=[1,2,3]l.insert(0,"a")assertl==["a",1,2,3]deftest_pop(self):c=list('hello world')s=''foriinrange(11):s+=c.pop()asserts=='dlrow olleh'raises(IndexError,c.pop)assertlen(c)==0l=range(10)l.pop()assertl==range(9)l=[1.1,2.2,3.3]l.pop()assertl==[1.1,2.2]l=[]raises(IndexError,l.pop,0)deftest_pop_custom_int(self):classA(object):def__init__(self,x):self.x=xdef__int__(self):returnself.xl=range(10)x=l.pop(A(-1))assertx==9assertl==range(9)raises(TypeError,range(10).pop,1.0)deftest_pop_negative(self):l1=[1,2,3,4]l2=["1","2","3","4"]l3=range(5)l4=[1,2,3,"4"]l5=[1.1,2.2,3.3,4.4]raises(IndexError,l1.pop,-5)raises(IndexError,l2.pop,-5)raises(IndexError,l3.pop,-6)raises(IndexError,l4.pop,-5)raises(IndexError,l5.pop,-5)assertl1.pop(-2)==3assertl2.pop(-2)=="3"assertl3.pop(-2)==3assertl4.pop(-2)==3assertl5.pop(-2)==3.3deftest_remove(self):c=list('hello world')c.remove('l')assert''.join(c)=='helo world'c.remove('l')assert''.join(c)=='heo world'c.remove('l')assert''.join(c)=='heo word'raises(ValueError,c.remove,'l')assert''.join(c)=='heo word'l=range(5)l.remove(2)assertl==[0,1,3,4]l=[0,3,5]raises(ValueError,c.remove,2)l=[0.0,1.1,2.2,3.3,4.4]l.remove(2.2)assertl==[0.0,1.1,3.3,4.4]l=[0.0,3.3,5.5]raises(ValueError,c.remove,2)raises(ValueError,c.remove,2.2)deftest_reverse(self):c=list('hello world')c.reverse()assert''.join(c)=='dlrow olleh'deftest_reversed(self):assertlist(list('hello').__reversed__())==['o','l','l','e','h']assertlist(reversed(list('hello')))==['o','l','l','e','h']deftest_mutate_while_remove(self):classMean(object):def__init__(self,i):self.i=idef__eq__(self,other):ifself.i==9:dell[i-1]returnTrueelse:returnFalsel=[Mean(i)foriinrange(10)]# does not crashl.remove(None)classMean2(object):def__init__(self,i):self.i=idef__eq__(self,other):l.append(self.i)returnFalsel=[Mean2(i)foriinrange(10)]# does not crashl.remove(5)assertl[10:]==[0,1,2,3,4,6,7,8,9]deftest_mutate_while_contains(self):classMean(object):def__init__(self,i):self.i=idef__eq__(self,other):ifself.i==9==other:dell[0]returnTrueelse:returnFalsel=[Mean(i)foriinrange(10)]assertl.__contains__(9)assertnotl.__contains__(2)deftest_mutate_while_extend(self):# this used to segfault pypy-c (with py.test -A)importsysifhasattr(sys,'pypy_translation_info'):ifsys.pypy_translation_info['translation.gc']=='boehm':skip("not reliable on top of Boehm")classA(object):def__del__(self):print'del'dellst[:]foriinrange(10):keepalive=[]lst=list(str(i))*100A()whilelst:keepalive.append(lst[:])deftest___getslice__(self):l=[1,2,3,4]res=l.__getslice__(0,2)assertres==[1,2]l=[]assertl.__getslice__(0,2)==[]deftest___setslice__(self):l=[1,2,3,4]l.__setslice__(0,2,[5,6])assertl==[5,6,3,4]l=[]l.__setslice__(0,0,[3,4,5])assertl==[3,4,5]deftest___delslice__(self):l=[1,2,3,4]l.__delslice__(0,2)assertl==[3,4]deftest_unicode(self):s=u"\ufffd\ufffd\ufffd"asserts.encode("ascii","replace")=="???"asserts.encode("ascii","ignore")==""l1=[s.encode("ascii","replace")]assertl1[0]=="???"l2=[s.encode("ascii","ignore")]assertl2[0]==""l3=[s]assertl1[0].encode("ascii","replace")=="???"deftest_list_from_set(self):l=['a']l.__init__(set('b'))assertl==['b']deftest_list_from_generator(self):l=['a']g=(i*iforiinrange(5))l.__init__(g)assertl==[0,1,4,9,16]l.__init__(g)assertl==[]assertlist(g)==[]deftest_uses_custom_iterator(self):# obscure corner case: space.listview*() must not shortcut subclasses# of dicts, because the OrderedDict in the stdlib relies on this.# we extend the use case to lists and sets, i.e. all types that have# strategies, to avoid surprizes depending on the strategy.classX:passforbase,argin[(list,[]),(list,[5]),(list,['x']),(list,[X]),(list,[u'x']),(set,[]),(set,[5]),(set,['x']),(set,[X]),(set,[u'x']),(dict,[]),(dict,[(5,6)]),(dict,[('x',7)]),(dict,[(X,8)]),(dict,[(u'x',7)]),]:printbase,argclassSubClass(base):def__iter__(self):returniter("foobar")sub=SubClass(arg)assertlist(sub)==['f','o','o','b','a','r']l=[]l.extend(sub)assertl==['f','o','o','b','a','r']# test another list strategyl=['Z']l.extend(sub)assertl==['Z','f','o','o','b','a','r']classSub2(base):passassertlist(Sub2(arg))==list(base(arg))s=set()s.update(Sub2(arg))asserts==set(base(arg))deftest_comparison(self):assert([]<[])isFalseassert([]<=[])isTrueassert([]==[])isTrueassert([]!=[])isFalseassert([]>[])isFalseassert([]>=[])isTrueassert([5]<[])isFalseassert([5]<=[])isFalseassert([5]==[])isFalseassert([5]!=[])isTrueassert([5]>[])isTrueassert([5]>=[])isTrueassert([]<[5])isTrueassert([]<=[5])isTrueassert([]==[5])isFalseassert([]!=[5])isTrueassert([]>[5])isFalseassert([]>=[5])isFalseassert([4]<[5])isTrueassert([4]<=[5])isTrueassert([4]==[5])isFalseassert([4]!=[5])isTrueassert([4]>[5])isFalseassert([4]>=[5])isFalseassert([5]<[5])isFalseassert([5]<=[5])isTrueassert([5]==[5])isTrueassert([5]!=[5])isFalseassert([5]>[5])isFalseassert([5]>=[5])isTrueassert([6]<[5])isFalseassert([6]<=[5])isFalseassert([6]==[5])isFalseassert([6]!=[5])isTrueassert([6]>[5])isTrueassert([6]>=[5])isTrueN=float('nan')assert([N]<[5])isFalseassert([N]<=[5])isFalseassert([N]==[5])isFalseassert([N]!=[5])isTrueassert([N]>[5])isFalseassert([N]>=[5])isFalseassert([5]<[N])isFalseassert([5]<=[N])isFalseassert([5]==[N])isFalseassert([5]!=[N])isTrueassert([5]>[N])isFalseassert([5]>=[N])isFalsedeftest_resizelist_hint(self):import__pypy__l2=[]__pypy__.resizelist_hint(l2,100)l1=[1,2,3]l1[:]=l2assertlen(l1)==0classAppTestForRangeLists(AppTestW_ListObject):spaceconfig={"objspace.std.withrangelist":True}deftest_range_simple_backwards(self):x=range(5,1)assertx==[]deftest_range_big_start(self):x=range(1,10)x[22:0:-1]==range(1,10)deftest_range_list_invalid_slice(self):x=[1,2,3,4]assertx[10:0]==[]assertx[10:0:None]==[]x=range(1,5)assertx[10:0]==[]assertx[10:0:None]==[]assertx[0:22]==[1,2,3,4]assertx[-1:10]==[4]assertx[0:22:None]==[1,2,3,4]assertx[-1:10:None]==[4]deftest_range_backwards(self):x=range(1,10)assertx[22:-10]==[]assertx[22:-10:-1]==[9,8,7,6,5,4,3,2,1]assertx[10:3:-1]==[9,8,7,6,5]assertx[10:3:-2]==[9,7,5]assertx[1:5:-1]==[]deftest_sort_range(self):l=range(3,10,3)l.sort()assertl==[3,6,9]l.sort(reverse=True)assertl==[9,6,3]l.sort(reverse=True)assertl==[9,6,3]l.sort()assertl==[3,6,9]deftest_slice(self):l=[]l2=range(3)l.__setslice__(0,3,l2)assertl==[0,1,2]deftest_getitem(self):l=range(5)raises(IndexError,"l[-10]")deftest_append(self):l=range(5)l.append(26)assertl==[0,1,2,3,4,26]l=range(5)l.append("a")assertl==[0,1,2,3,4,"a"]l=range(5)l.append(5)assertl==[0,1,2,3,4,5]deftest_pop(self):l=range(3)assertl.pop(0)==0deftest_setitem(self):l=range(3)l[0]=1assertl==[1,1,2]deftest_inset(self):l=range(3)l.insert(1,5)assertl==[0,5,1,2]deftest_reverse(self):l=range(3)l.reverse()assertl==[2,1,0]deftest_issue1266(self):l=range(1)l.pop()# would previously crashl.append(1)assertl==[1]l=range(1)l.pop()# would previously crashl.reverse()assertl==[]deftest_issue1266_ovf(self):importsysl=range(0,sys.maxint,sys.maxint)l.append(sys.maxint)# -2 would be next in the range sequence if overflow were# allowedl.append(-2)assertl==[0,sys.maxint,-2]assert-2inll=range(-sys.maxint,sys.maxint,sys.maxint//10)item11=l[11]assertl[::11]==[-sys.maxint,item11]assertitem11inl[::11]classAppTestWithoutStrategies(object):spaceconfig={"objspace.std.withliststrategies":False}deftest_no_shared_empty_list(self):l=[]copy=l[:]copy.append({})assertcopy==[{}]notshared=l[:]assertnotshared==[]classAppTestListFastSubscr:spaceconfig={"objspace.std.optimized_list_getitem":True}deftest_getitem(self):importoperatorl=[0,1,2,3,4]foriinxrange(5):assertl[i]==iassertl[3:]==[3,4]raises(TypeError,operator.getitem,l,"str")