def moveit(T): from cppyy.gbl import std # move constructor i1 = T() assert T.s_move_counter == 0 i2 = T(i1) # cctor assert T.s_move_counter == 0 if is_pypy or 0x3000000 <= sys.hexversion: i3 = T(std.move(T())) # can't check ref-count else: i3 = T(T()) # should call move, not memoized cctor assert T.s_move_counter == 1 i3 = T(std.move(T())) # both move and ref-count assert T.s_move_counter == 2 i4 = T(std.move(i1)) assert T.s_move_counter == 3 # move assignment i4.__assign__(i2) assert T.s_move_counter == 3 if is_pypy or 0x3000000 <= sys.hexversion: i4.__assign__(std.move(T())) # can't check ref-count else: i4.__assign__(T()) assert T.s_move_counter == 4 i4.__assign__(std.move(i2)) assert T.s_move_counter == 5
def test06_variadic_sfinae(self): """Attribute testing through SFINAE""" import cppyy cppyy.gbl.AttrTesting # load from cppyy.gbl.AttrTesting import Obj1, Obj2, has_var1, call_has_var1 from cppyy.gbl.std import move assert has_var1(Obj1()) == hasattr(Obj1(), 'var1') assert has_var1(Obj2()) == hasattr(Obj2(), 'var1') assert has_var1(3) == hasattr(3, 'var1') assert has_var1("aap") == hasattr("aap", 'var1') assert call_has_var1(move(Obj1())) == True assert call_has_var1(move(Obj2())) == False
def test02_any_usage(self): """boost::any assignment and casting""" if self.disable: import warnings warnings.warn("no boost/any.hpp found, skipping test02_any_usage") return import cppyy assert cppyy.gbl.boost from cppyy.gbl import std, boost val = boost.any() # test both by-ref and by rvalue v = std.vector[int]() val.__assign__(v) val.__assign__(std.move(std.vector[int](range(100)))) assert val.type() == cppyy.typeid(std.vector[int]) extract = boost.any_cast[std.vector[int]](val) assert type(extract) is std.vector[int] assert len(extract) == 100 extract += range(100) assert len(extract) == 200 val.__assign__(std.move(extract)) # move forced #assert len(extract) == 0 # not guaranteed by the standard # TODO: we hit boost::any_cast<int>(boost::any* operand) instead # of the reference version which raises boost.any_cast.__useffi__ = False try: # raises(Exception, boost.any_cast[int], val) assert not boost.any_cast[int](val) except Exception: # getting here is good, too ... pass extract = boost.any_cast[std.vector[int]](val) assert len(extract) == 200
def test02_any_usage(self): """boost::any assignment and casting""" import cppyy assert cppyy.gbl.boost from cppyy.gbl import std, boost val = boost.any() val.__assign__(std.vector[int]()) assert val.type() == cppyy.typeid(std.vector[int]) extract = boost.any_cast[std.vector[int]](std.move(val)) assert type(extract) is std.vector[int] extract += range(100) val.__assign__(std.move(extract)) assert len(extract) == 0 raises(Exception, boost.any_cast[int], std.move(val)) extract = boost.any_cast[std.vector[int]](std.move(val)) assert len(extract) == 100
def test02_any_usage(self): """boost::any assignment and casting""" import cppyy assert cppyy.gbl.boost from cppyy.gbl import std, boost val = boost.any() # test both by-ref and by rvalue v = std.vector[int]() val.__assign__(v) val.__assign__(std.move(std.vector[int](range(100)))) assert val.type() == cppyy.typeid(std.vector[int]) extract = boost.any_cast[std.vector[int]](val) assert type(extract) is std.vector[int] assert len(extract) == 100 extract += range(100) assert len(extract) == 200 val.__assign__(std.move(extract)) # move forced #assert len(extract) == 0 # not guaranteed by the standard # TODO: we hit boost::any_cast<int>(boost::any* operand) instead # of the reference version which raises boost.any_cast.__useffi__ = False try: # raises(Exception, boost.any_cast[int], val) assert not boost.any_cast[int](val) except Exception: # getting here is good, too ... pass extract = boost.any_cast[std.vector[int]](val) assert len(extract) == 200