def test_copy_list(self): l1 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) l2 = l1.clone() l2.append(self.space.wrap(4)) assert not l2 == l1.getitems()
def test_pop_without_argument_is_fast(self): space = self.space w_l = W_ListObject( space, [space.wrap(1), space.wrap(2), space.wrap(3)]) w_l.pop = None w_res = listobject.list_pop__List_ANY(space, w_l, space.w_None) # does not crash assert space.unwrap(w_res) == 3
def test_getitems_does_not_copy_object_list(self): l1 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap("two"), self.space.wrap(3)]) l2 = l1.getitems() l2.append(self.space.wrap("four")) assert l2 == l1.getitems()
def test_check_strategy(self): assert isinstance( W_ListObject(self.space, []).strategy, EmptyListStrategy) assert isinstance( W_ListObject( self.space, [self.space.wrap(1), self.space.wrap('a')]).strategy, ObjectListStrategy) assert isinstance( W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]).strategy, IntegerListStrategy) assert isinstance( W_ListObject(self.space, [self.space.wrap('a'), self.space.wrap('b')]).strategy, StringListStrategy)
def test_iter(self): w = self.space.wrap w_list = W_ListObject(self.space, [w(5), w(3), w(99)]) w_iter = self.space.iter(w_list) assert self.space.eq_w(self.space.next(w_iter), w(5)) assert self.space.eq_w(self.space.next(w_iter), w(3)) assert self.space.eq_w(self.space.next(w_iter), w(99)) raises(OperationError, self.space.next, w_iter) raises(OperationError, self.space.next, w_iter)
def test_add_of_range_and_int(self): l1 = make_range_list(self.space, 0, 1, 100) l2 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) l3 = self.space.add(l2, l1) assert l3.strategy is l2.strategy
def test_string_to_any(self): l = W_ListObject(self.space, [self.space.newbytes('a'), self.space.newbytes('b'), self.space.newbytes('c')]) assert isinstance(l.strategy, BytesListStrategy) l.append(self.space.newbytes('d')) assert isinstance(l.strategy, BytesListStrategy) l.append(self.space.wrap(3)) assert isinstance(l.strategy, ObjectListStrategy)
def test_eq(self): w = self.space.wrap w_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)]) assert self.space.eq_w(self.space.eq(w_list0, w_list1), self.space.w_False) assert self.space.eq_w(self.space.eq(w_list1, w_list0), self.space.w_False) assert self.space.eq_w(self.space.eq(w_list1, w_list1), self.space.w_True) assert self.space.eq_w(self.space.eq(w_list1, w_list2), self.space.w_True) assert self.space.eq_w(self.space.eq(w_list2, w_list3), self.space.w_False)
def test_create_set_from_list(self): from pypy.interpreter.baseobjspace import W_Root from pypy.objspace.std.setobject import BytesSetStrategy, ObjectSetStrategy from pypy.objspace.std.floatobject import W_FloatObject w = self.space.wrap wb = self.space.newbytes intstr = self.space.fromcache(IntegerSetStrategy) tmp_func = intstr.get_storage_from_list # test if get_storage_from_list is no longer used intstr.get_storage_from_list = None w_list = W_ListObject(self.space, [w(1), w(2), w(3)]) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_list) assert w_set.strategy is intstr assert intstr.unerase(w_set.sstorage) == {1:None, 2:None, 3:None} w_list = W_ListObject(self.space, [wb("1"), wb("2"), wb("3")]) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_list) assert w_set.strategy is self.space.fromcache(BytesSetStrategy) assert w_set.strategy.unerase(w_set.sstorage) == {"1":None, "2":None, "3":None} w_list = self.space.iter(W_ListObject(self.space, [w(u"1"), w(u"2"), w(u"3")])) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_list) w_list = W_ListObject(self.space, [w("1"), w(2), w("3")]) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_list) assert w_set.strategy is self.space.fromcache(ObjectSetStrategy) for item in w_set.strategy.unerase(w_set.sstorage): assert isinstance(item, W_Root) w_list = W_ListObject(self.space, [w(1.0), w(2.0), w(3.0)]) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_list) assert w_set.strategy is self.space.fromcache(ObjectSetStrategy) for item in w_set.strategy.unerase(w_set.sstorage): assert isinstance(item, W_FloatObject) # changed cached object, need to change it back for other tests to pass intstr.get_storage_from_list = tmp_func
def test_int_or_float_sort(self): space = self.space w_l = W_ListObject(space, [space.wrap(1.2), space.wrap(1), space.wrap(1.0), space.wrap(5)]) w_l.sort(False) assert [(type(x), x) for x in space.unwrap(w_l)] == [ (int, 1), (float, 1.0), (float, 1.2), (int, 5)] w_l.sort(True) assert [(type(x), x) for x in space.unwrap(w_l)] == [ (int, 5), (float, 1.2), (int, 1), (float, 1.0)]
def test_clone(self): l1 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) clone = l1.clone() assert isinstance(clone.strategy, IntegerListStrategy) clone.append(self.space.wrap(7)) assert not self.space.eq_w(l1, clone)
def test_mul_same_strategy_but_different_object(self): l1 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) l2 = l1.mul(1) assert self.space.eq_w(l1, l2) l1.setitem(0, self.space.wrap(5)) assert not self.space.eq_w(l1, l2)
def test_int_or_float_from_float(self): space = self.space w = space.wrap w_l = W_ListObject(space, [space.wrap(-42.5)]) assert isinstance(w_l.strategy, FloatListStrategy) w_l.append(w(-15)) assert isinstance(w_l.strategy, IntOrFloatListStrategy) assert space.float_w(w_l.getitem(0)) == -42.5 assert space.int_w(w_l.getitem(1)) == -15 assert space.len_w(w_l) == 2
def test_int_or_float_from_integer(self): space = self.space w = space.wrap w_l = W_ListObject(space, [space.wrap(int(-2**31))]) assert isinstance(w_l.strategy, IntegerListStrategy) w_l.append(w(-5.1)) assert isinstance(w_l.strategy, IntOrFloatListStrategy) assert space.int_w(w_l.getitem(0)) == -2**31 assert space.float_w(w_l.getitem(1)) == -5.1 assert space.len_w(w_l) == 2
def test_unpackiterable_int_list(self): space = self.space w_l = W_ListObject( space, [space.wrap(1), space.wrap(2), space.wrap(3)]) list_orig = self.space.listview_int(w_l) list_copy = self.space.unpackiterable_int(w_l) assert list_orig == list_copy == [1, 2, 3] list_copy[0] = 42 assert list_orig == [1, 2, 3]
def test_setitem(self): # This should work if test_listobject.py passes l = W_ListObject( self.space, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')]) assert self.space.eq_w(l.getitem(0), self.space.wrap('a')) l.setitem(0, self.space.wrap('d')) assert self.space.eq_w(l.getitem(0), self.space.wrap('d')) assert isinstance(l.strategy, StringListStrategy) # IntStrategy to ObjectStrategy l = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) l.setitem(0, self.space.wrap('d')) assert isinstance(l.strategy, ObjectListStrategy) # StringStrategy to ObjectStrategy l = W_ListObject( self.space, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')]) assert isinstance(l.strategy, StringListStrategy) l.setitem(0, self.space.wrap(2)) assert isinstance(l.strategy, ObjectListStrategy) # FloatStrategy to ObjectStrategy l = W_ListObject( self.space, [self.space.wrap(1.2), self.space.wrap(2.3), self.space.wrap(3.4)]) assert isinstance(l.strategy, FloatListStrategy) l.setitem(0, self.space.wrap("a")) assert isinstance(l.strategy, ObjectListStrategy)
def test_extend(self): l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.extend( W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])) assert isinstance(l.strategy, IntegerListStrategy) l = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) l.extend( W_ListObject(self.space, [ self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c') ])) assert isinstance(l.strategy, ObjectListStrategy) l = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) l.extend( W_ListObject( self.space, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)])) assert isinstance(l.strategy, IntegerListStrategy) l = W_ListObject( self.space, [self.space.wrap(1.1), self.space.wrap(2.2), self.space.wrap(3.3)]) assert isinstance(l.strategy, FloatListStrategy) l.extend( W_ListObject( self.space, [self.space.wrap(4), self.space.wrap(5), self.space.wrap(6)])) assert isinstance(l.strategy, ObjectListStrategy)
def test_unicode_to_any(self): space = self.space l = W_ListObject( space, [space.wrap(u'a'), space.wrap(u'b'), space.wrap(u'c')]) assert isinstance(l.strategy, UnicodeListStrategy) l.append(space.wrap(u'd')) assert isinstance(l.strategy, UnicodeListStrategy) l.append(space.wrap(3)) assert isinstance(l.strategy, ObjectListStrategy)
def test_int_to_any(self): l = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) l.append(self.space.wrap(4)) assert isinstance(l.strategy, IntegerListStrategy) l.append(self.space.wrap('a')) assert isinstance(l.strategy, ObjectListStrategy)
def test_empty_to_any(self): l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.append(self.space.wrap((1, 3))) assert isinstance(l.strategy, ObjectListStrategy) l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.append(self.space.wrap(1)) assert isinstance(l.strategy, IntegerListStrategy) l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.append(self.space.wrap('a')) assert isinstance(l.strategy, StringListStrategy) l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.append(self.space.wrap(1.2)) assert isinstance(l.strategy, FloatListStrategy)
def test_contains(self): w = self.space.wrap w_list = W_ListObject(self.space, [w(5), w(3), w(99)]) assert self.space.eq_w(self.space.contains(w_list, w(5)), self.space.w_True) assert self.space.eq_w(self.space.contains(w_list, w(99)), self.space.w_True) assert self.space.eq_w(self.space.contains(w_list, w(11)), self.space.w_False) assert self.space.eq_w(self.space.contains(w_list, w_list), self.space.w_False)
def test_float_to_any(self): l = W_ListObject( self.space, [self.space.wrap(1.1), self.space.wrap(2.2), self.space.wrap(3.3)]) assert isinstance(l.strategy, FloatListStrategy) l.append(self.space.wrap(4.4)) assert isinstance(l.strategy, FloatListStrategy) l.append(self.space.wrap("a")) assert isinstance(l.strategy, ObjectListStrategy)
def test_range_setslice(self): l = make_range_list(self.space, 1, 3, 5) assert isinstance(l.strategy, RangeListStrategy) l.setslice( 0, 1, 3, W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)])) assert isinstance(l.strategy, IntegerListStrategy)
def test_int_or_float_from_float_special_nan(self): from rpython.rlib import longlong2float, rarithmetic space = self.space w = space.wrap ll = rarithmetic.r_longlong(0xfffffffe12345678 - 2**64) specialnan = longlong2float.longlong2float(ll) w_l = W_ListObject(space, [space.wrap(specialnan)]) assert isinstance(w_l.strategy, FloatListStrategy) w_l.append(w(42)) assert isinstance(w_l.strategy, ObjectListStrategy) assert space.int_w(w_l.getitem(1)) == 42 assert space.len_w(w_l) == 2
def test_getitem(self): w = self.space.wrap w_list = W_ListObject(self.space, [w(5), w(3)]) assert self.space.eq_w(self.space.getitem(w_list, w(0)), w(5)) assert self.space.eq_w(self.space.getitem(w_list, w(1)), w(3)) assert self.space.eq_w(self.space.getitem(w_list, w(-2)), w(5)) assert self.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))
def test_int_or_float_from_float_int_overflow(self): if sys.maxint == 2147483647: py.test.skip("only on 64-bit") space = self.space w = space.wrap ovf1 = 2 ** 31 w_l = W_ListObject(space, [space.wrap(1.2)]) assert isinstance(w_l.strategy, FloatListStrategy) w_l.append(w(ovf1)) assert isinstance(w_l.strategy, ObjectListStrategy) assert space.float_w(w_l.getitem(0)) == 1.2 assert space.int_w(w_l.getitem(1)) == ovf1 assert space.len_w(w_l) == 2
def test_mul(self): l1 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) l2 = l1.mul(2) l3 = W_ListObject(self.space, [ self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3) ]) assert self.space.eq_w(l2, l3) l4 = make_range_list(self.space, 1, 1, 3) assert self.space.eq_w(l4, l1) l5 = l4.mul(2) assert self.space.eq_w(l5, l3)
def test_add_does_not_use_getitems(self): l1 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) l1.getitems = None l2 = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) l2.getitems = None l3 = self.space.add(l1, l2) l4 = W_ListObject(self.space, [ self.space.wrap(1), self.space.wrap(2), self.space.wrap(3), self.space.wrap(1), self.space.wrap(2), self.space.wrap(3) ]) assert self.space.eq_w(l3, l4)
def test_find_fast_on_intlist(self, monkeypatch): monkeypatch.setattr(self.space, "eq_w", None) w = self.space.wrap intlist = W_ListObject( self.space, [w(1), w(2), w(3), w(4), w(5), w(6), w(7)]) res = intlist.find(w(4), 0, 7) assert res == 3 res = intlist.find(w(4), 0, 100) assert res == 3 with py.test.raises(ValueError): intlist.find(w(4), 4, 7) with py.test.raises(ValueError): intlist.find(w(4), 0, 2)
def test_insert(self): # no change l = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) l.insert(3, self.space.wrap(4)) assert isinstance(l.strategy, IntegerListStrategy) # StringStrategy l = W_ListObject( self.space, [self.space.wrap('a'), self.space.wrap('b'), self.space.wrap('c')]) assert isinstance(l.strategy, StringListStrategy) l.insert(3, self.space.wrap(2)) assert isinstance(l.strategy, ObjectListStrategy) # IntegerStrategy l = W_ListObject( self.space, [self.space.wrap(1), self.space.wrap(2), self.space.wrap(3)]) assert isinstance(l.strategy, IntegerListStrategy) l.insert(3, self.space.wrap('d')) assert isinstance(l.strategy, ObjectListStrategy) # FloatStrategy l = W_ListObject( self.space, [self.space.wrap(1.1), self.space.wrap(2.2), self.space.wrap(3.3)]) assert isinstance(l.strategy, FloatListStrategy) l.insert(3, self.space.wrap('d')) assert isinstance(l.strategy, ObjectListStrategy) # EmptyStrategy l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.insert(0, self.space.wrap('a')) assert isinstance(l.strategy, StringListStrategy) l = W_ListObject(self.space, []) assert isinstance(l.strategy, EmptyListStrategy) l.insert(0, self.space.wrap(2)) assert isinstance(l.strategy, IntegerListStrategy)