def test_create_list_from_set(self): from pypy.objspace.std.setobject import W_SetObject from pypy.objspace.std.setobject import _initialize_set space = self.space w = space.wrap w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)]) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_l) w_set.iter = None # make sure fast path is used w_l2 = W_ListObject(space, []) space.call_method(w_l2, "__init__", w_set) w_l2.sort(False) assert space.eq_w(w_l, w_l2) w_l = W_ListObject(space, [space.wrap("a"), space.wrap("b"), space.wrap("c")]) _initialize_set(self.space, w_set, w_l) space.call_method(w_l2, "__init__", w_set) w_l2.sort(False) assert space.eq_w(w_l, w_l2)
def test_create_list_from_set(self): from pypy.objspace.std.setobject import W_SetObject from pypy.objspace.std.setobject import _initialize_set space = self.space w = space.wrap w_l = W_ListObject( space, [space.wrap(1), space.wrap(2), space.wrap(3)]) w_set = W_SetObject(self.space) _initialize_set(self.space, w_set, w_l) w_set.iter = None # make sure fast path is used w_l2 = W_ListObject(space, []) space.call_method(w_l2, "__init__", w_set) w_l2.sort(False) assert space.eq_w(w_l, w_l2) w_l = W_ListObject(space, [space.wrap("a"), space.wrap("b"), space.wrap("c")]) _initialize_set(self.space, w_set, w_l) space.call_method(w_l2, "__init__", w_set) w_l2.sort(False) assert space.eq_w(w_l, w_l2)
def test_listview(self): space = self.space s = W_SetObject(space, self.wrapped([1, 2])) assert sorted(space.listview_int(s)) == [1, 2] # s = W_SetObject(space, self.wrapped(["a", "b"], bytes=True)) assert sorted(space.listview_bytes(s)) == ["a", "b"]
def test_compare(self): s = W_SetObject(self.space) _initialize_set(self.space, s, self.word) t = W_SetObject(self.space) _initialize_set(self.space, t, self.word) assert self.space.eq_w(s, t) u = self.space.wrap(set('simsalabim')) assert self.space.eq_w(s, u)
def test_union(self): s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s2 = W_SetObject(self.space, self.wrapped([4, 5, 6, 7])) s3 = W_SetObject(self.space, self.wrapped([4, "5", "6", 7])) s4 = s1.descr_union(self.space, [s2]) s5 = s1.descr_union(self.space, [s3]) assert s4.strategy is self.space.fromcache(IntegerSetStrategy) assert s5.strategy is self.space.fromcache(ObjectSetStrategy)
def test_intersection(self): s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s2 = W_SetObject(self.space, self.wrapped([4, 5, "six", "seven"])) s3 = s1.intersect(s2) skip( "for now intersection with ObjectStrategy always results in another ObjectStrategy" ) assert s3.strategy is self.space.fromcache(IntegerSetStrategy)
def test_union(self): s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s2 = W_SetObject(self.space, self.wrapped([4, 5, 6, 7])) s3 = W_SetObject(self.space, self.wrapped([4, '5', '6', 7])) s4 = s1.descr_union(self.space, [s2]) s5 = s1.descr_union(self.space, [s3]) assert s4.strategy is self.space.fromcache(IntegerSetStrategy) assert s5.strategy is self.space.fromcache(ObjectSetStrategy)
def test_union(self): from pypy.objspace.std.setobject import set_union__Set s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5])) s2 = W_SetObject(self.space, self.wrapped([4,5,6,7])) s3 = W_SetObject(self.space, self.wrapped([4,'5','6',7])) s4 = set_union__Set(self.space, s1, [s2]) s5 = set_union__Set(self.space, s1, [s3]) assert s4.strategy is self.space.fromcache(IntegerSetStrategy) assert s5.strategy is self.space.fromcache(ObjectSetStrategy)
def test_create_set_from_list(self): from pypy.interpreter.baseobjspace import W_Root from pypy.objspace.std.setobject import BytesSetStrategy, ObjectSetStrategy, UnicodeSetStrategy from pypy.objspace.std.floatobject import W_FloatObject w = self.space.wrap wb = self.space.wrapbytes 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) assert w_set.strategy is self.space.fromcache(UnicodeSetStrategy) assert w_set.strategy.unerase(w_set.sstorage) == { u"1": None, u"2": None, u"3": 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 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_intersection_order(self): # theses tests make sure that intersection is done in the correct order # (smallest first) space = self.space a = W_SetObject(self.space) _initialize_set(self.space, a, self.space.wrap("abcdefg")) a.intersect = None b = W_SetObject(self.space) _initialize_set(self.space, b, self.space.wrap("abc")) result = a.descr_intersection(space, [b]) assert space.is_true(self.space.eq(result, W_SetObject(space, self.space.wrap("abc")))) c = W_SetObject(self.space) _initialize_set(self.space, c, self.space.wrap("e")) d = W_SetObject(self.space) _initialize_set(self.space, d, self.space.wrap("ab")) # if ordering works correct we should start with set e a.get_storage_copy = None b.get_storage_copy = None d.get_storage_copy = None result = a.descr_intersection(space, [d, c, b]) assert space.is_true(self.space.eq(result, W_SetObject(space, self.space.wrap(""))))
def test_and(self): s = W_SetObject(self.space) _initialize_set(self.space, s, self.word) t0 = W_SetObject(self.space) _initialize_set(self.space, t0, self.otherword) t1 = W_FrozensetObject(self.space, self.otherword) r0 = and__Set_Set(self.space, s, t0) r1 = and__Set_Set(self.space, s, t1) assert eq__Set_Set(self.space, r0, r1) == self.true sr = set_intersection__Set(self.space, s, [self.otherword]) assert eq__Set_Set(self.space, r0, sr) == self.true
def test_and(self): s = W_SetObject(self.space) _initialize_set(self.space, s, self.word) t0 = W_SetObject(self.space) _initialize_set(self.space, t0, self.otherword) t1 = W_FrozensetObject(self.space, self.otherword) r0 = s.descr_and(self.space, t0) r1 = s.descr_and(self.space, t1) assert r0.descr_eq(self.space, r1) == self.true sr = s.descr_intersection(self.space, [self.otherword]) assert r0.descr_eq(self.space, sr) == self.true
def test_from_list(self): s = W_SetObject(self.space, self.wrapped([1,2,3,4,5])) assert s.strategy is self.space.fromcache(IntegerSetStrategy) s = W_SetObject(self.space, self.wrapped([1,"two",3,"four",5])) assert s.strategy is self.space.fromcache(ObjectSetStrategy) s = W_SetObject(self.space) assert s.strategy is self.space.fromcache(EmptySetStrategy) s = W_SetObject(self.space, self.wrapped([])) assert s.strategy is self.space.fromcache(EmptySetStrategy)
def test_listview_str_int_on_set(self): w = self.space.wrap w_a = W_SetObject(self.space) _initialize_set(self.space, w_a, w("abcdefg")) assert sorted(self.space.listview_str(w_a)) == list("abcdefg") assert self.space.listview_int(w_a) is None w_b = W_SetObject(self.space) _initialize_set(self.space, w_b, self.space.newlist([w(1),w(2),w(3),w(4),w(5)])) assert sorted(self.space.listview_int(w_b)) == [1,2,3,4,5] assert self.space.listview_str(w_b) is None
def test_switch_to_object(self): s = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s.add(self.space.wrap("six")) assert s.strategy is self.space.fromcache(ObjectSetStrategy) s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s2 = W_SetObject(self.space, self.wrapped(["six", "seven"])) s1.update(s2) assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
def test_listview_bytes_int_on_set(self): w = self.space.wrap wb = self.space.newbytes w_a = W_SetObject(self.space) _initialize_set(self.space, w_a, wb("abcdefg")) assert sorted(self.space.listview_int(w_a)) == [97, 98, 99, 100, 101, 102, 103] assert self.space.listview_bytes(w_a) is None w_b = W_SetObject(self.space) _initialize_set(self.space, w_b, self.space.newlist([w(1),w(2),w(3),w(4),w(5)])) assert sorted(self.space.listview_int(w_b)) == [1,2,3,4,5] assert self.space.listview_bytes(w_b) is None
def _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # we might get there in non-translated versions if 'x' is # a long that fits the correct range. if is_valid_int(x): return self.newint(x) # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: return self.wraplong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): res = W_SetObject(self, self.newlist([self.wrap(item) for item in x])) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis if self.config.objspace.nofaking: raise OperationError( self.w_RuntimeError, self.wrap("nofaking enabled: refusing " "to wrap cpython value %r" % (x, ))) if isinstance(x, type(Exception)) and issubclass(x, Exception): w_result = self.wrap_exception_cls(x) if w_result is not None: return w_result from pypy.objspace.std.fake import fake_object return fake_object(self, x)
def test_discard(self): class FakeInt(object): def __init__(self, value): self.value = value def __hash__(self): return hash(self.value) def __eq__(self, other): if other == self.value: return True return False s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5])) s1.descr_discard(self.space, self.space.wrap("five")) skip("currently not supported") assert s1.strategy is self.space.fromcache(IntegerSetStrategy) set_discard__Set_ANY(self.space, s1, self.space.wrap(FakeInt(5))) assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
def test_discard(self): class FakeInt(object): def __init__(self, value): self.value = value def __hash__(self): return hash(self.value) def __eq__(self, other): if other == self.value: return True return False s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s1.descr_discard(self.space, self.space.wrap("five")) skip("currently not supported") assert s1.strategy is self.space.fromcache(IntegerSetStrategy) set_discard__Set_ANY(self.space, s1, self.space.wrap(FakeInt(5))) assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
def test_iter(self): space = self.space s = W_SetObject(space, self.wrapped([1, 2])) it = s.iter() assert isinstance(it, IntegerIteratorImplementation) assert space.unwrap(it.next()) == 1 assert space.unwrap(it.next()) == 2 # s = W_SetObject(space, self.wrapped(["a", "b"])) it = s.iter() assert isinstance(it, BytesIteratorImplementation) assert space.unwrap(it.next()) == "a" assert space.unwrap(it.next()) == "b" # s = W_SetObject(space, self.wrapped([u"a", u"b"])) it = s.iter() assert isinstance(it, UnicodeIteratorImplementation) assert space.unwrap(it.next()) == u"a" assert space.unwrap(it.next()) == u"b"
def test_integer_strategy_with_w_long(self): # tests all calls to is_plain_int1() so far space = self.space w = W_LongObject(rbigint.fromlong(42)) s1 = W_SetObject(space, self.wrapped([])) s1.add(w) assert s1.strategy is space.fromcache(IntegerSetStrategy) # s1 = W_SetObject(space, space.newlist([w])) assert s1.strategy is space.fromcache(IntegerSetStrategy)
def _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: return self.wraplong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): res = W_SetObject(self, self.newlist([self.wrap(item) for item in x])) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis raise OperationError( self.w_RuntimeError, self.wrap("refusing to wrap cpython value %r" % (x, )))
def test_from_list(self): s = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) assert s.strategy is self.space.fromcache(IntegerSetStrategy) s = W_SetObject(self.space, self.wrapped([1, "two", 3, "four", 5])) assert s.strategy is self.space.fromcache(ObjectSetStrategy) s = W_SetObject(self.space) assert s.strategy is self.space.fromcache(EmptySetStrategy) s = W_SetObject(self.space, self.wrapped([])) assert s.strategy is self.space.fromcache(EmptySetStrategy) s = W_SetObject(self.space, self.wrapped(["a", "b"], bytes=True)) assert s.strategy is self.space.fromcache(BytesSetStrategy) s = W_SetObject(self.space, self.wrapped([u"a", u"b"])) assert s.strategy is self.space.fromcache(UnicodeSetStrategy)
def descr__new__(space, w_settype, __args__): from pypy.objspace.std.setobject import W_SetObject, newset w_obj = space.allocate_instance(W_SetObject, w_settype) W_SetObject.__init__(w_obj, space) return w_obj
def test_switch_to_unicode(self): s = W_SetObject(self.space, self.wrapped([])) s.add(self.space.wrap(u"six")) assert s.strategy is self.space.fromcache(UnicodeSetStrategy)
def test_remove(self): from pypy.objspace.std.setobject import set_remove__Set_ANY s1 = W_SetObject(self.space, self.wrapped([1])) set_remove__Set_ANY(self.space, s1, self.space.wrap(1)) assert s1.strategy is self.space.fromcache(EmptySetStrategy)
def test_clear(self): s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s1.clear() assert s1.strategy is self.space.fromcache(EmptySetStrategy)
def test_remove(self): s1 = W_SetObject(self.space, self.wrapped([1])) self.space.call_method(s1, 'remove', self.space.wrap(1)) assert s1.strategy is self.space.fromcache(EmptySetStrategy)
def test_symmetric_difference(self): s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s2 = W_SetObject(self.space, self.wrapped(["six", "seven"])) s1.symmetric_difference_update(s2) assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
def test_intersection(self): s1 = W_SetObject(self.space, self.wrapped([1, 2, 3, 4, 5])) s2 = W_SetObject(self.space, self.wrapped([4, 5, "six", "seven"])) s3 = s1.intersect(s2) skip("for now intersection with ObjectStrategy always results in another ObjectStrategy") assert s3.strategy is self.space.fromcache(IntegerSetStrategy)
def newset(self, iterable_w=None): if iterable_w is None: return W_SetObject(self, None) return W_SetObject(self, self.newtuple(iterable_w))
def test_intersection_order(self): # theses tests make sure that intersection is done in the correct order # (smallest first) space = self.space a = W_SetObject(self.space) _initialize_set(self.space, a, self.space.wrap("abcdefg")) a.intersect = None b = W_SetObject(self.space) _initialize_set(self.space, b, self.space.wrap("abc")) result = a.descr_intersection(space, [b]) assert space.is_true( self.space.eq(result, W_SetObject(space, self.space.wrap("abc")))) c = W_SetObject(self.space) _initialize_set(self.space, c, self.space.wrap("e")) d = W_SetObject(self.space) _initialize_set(self.space, d, self.space.wrap("ab")) # if ordering works correct we should start with set e a.get_storage_copy = None b.get_storage_copy = None d.get_storage_copy = None result = a.descr_intersection(space, [d, c, b]) assert space.is_true( self.space.eq(result, W_SetObject(space, self.space.wrap(""))))
def test_compare(self): s = W_SetObject(self.space, None) _initialize_set(self.space, s, self.word) t = W_SetObject(self.space, None) _initialize_set(self.space, t, self.word) assert self.space.eq_w(s, t)
def descr__new__(space, w_settype, __args__): from pypy.objspace.std.setobject import W_SetObject w_obj = space.allocate_instance(W_SetObject, w_settype) W_SetObject.__init__(w_obj, space, None) return w_obj
def newset(self): from pypy.objspace.std.setobject import newset return W_SetObject(self, None)
def _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: if self.config.objspace.std.withsmalllong: from pypy.rlib.rarithmetic import r_longlong try: rx = r_longlong(x) except OverflowError: pass else: from pypy.objspace.std.smalllongobject import \ W_SmallLongObject return W_SmallLongObject(rx) return W_LongObject.fromlong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): rdict_w = r_dict(self.eq_w, self.hash_w) for item in x: rdict_w[self.wrap(item)] = None res = W_SetObject(self, rdict_w) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis if self.config.objspace.nofaking: raise OperationError(self.w_RuntimeError, self.wrap("nofaking enabled: refusing " "to wrap cpython value %r" %(x,))) if isinstance(x, type(Exception)) and issubclass(x, Exception): w_result = self.wrap_exception_cls(x) if w_result is not None: return w_result from pypy.objspace.std.fake import fake_object return fake_object(self, x)