Blame external/pybind11/tests/test_copy_move.py

Packit 534379
import pytest
Packit 534379
from pybind11_tests import copy_move_policies as m
Packit 534379
Packit 534379
Packit 534379
def test_lacking_copy_ctor():
Packit 534379
    with pytest.raises(RuntimeError) as excinfo:
Packit 534379
        m.lacking_copy_ctor.get_one()
Packit 534379
    assert "the object is non-copyable!" in str(excinfo.value)
Packit 534379
Packit 534379
Packit 534379
def test_lacking_move_ctor():
Packit 534379
    with pytest.raises(RuntimeError) as excinfo:
Packit 534379
        m.lacking_move_ctor.get_one()
Packit 534379
    assert "the object is neither movable nor copyable!" in str(excinfo.value)
Packit 534379
Packit 534379
Packit 534379
def test_move_and_copy_casts():
Packit 534379
    """Cast some values in C++ via custom type casters and count the number of moves/copies."""
Packit 534379
Packit 534379
    cstats = m.move_and_copy_cstats()
Packit 534379
    c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
Packit 534379
Packit 534379
    # The type move constructions/assignments below each get incremented: the move assignment comes
Packit 534379
    # from the type_caster load; the move construction happens when extracting that via a cast or
Packit 534379
    # loading into an argument.
Packit 534379
    assert m.move_and_copy_casts(3) == 18
Packit 534379
    assert c_m.copy_assignments + c_m.copy_constructions == 0
Packit 534379
    assert c_m.move_assignments == 2
Packit 534379
    assert c_m.move_constructions >= 2
Packit 534379
    assert c_mc.alive() == 0
Packit 534379
    assert c_mc.copy_assignments + c_mc.copy_constructions == 0
Packit 534379
    assert c_mc.move_assignments == 2
Packit 534379
    assert c_mc.move_constructions >= 2
Packit 534379
    assert c_c.alive() == 0
Packit 534379
    assert c_c.copy_assignments == 2
Packit 534379
    assert c_c.copy_constructions >= 2
Packit 534379
    assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
Packit 534379
Packit 534379
Packit 534379
def test_move_and_copy_loads():
Packit 534379
    """Call some functions that load arguments via custom type casters and count the number of
Packit 534379
    moves/copies."""
Packit 534379
Packit 534379
    cstats = m.move_and_copy_cstats()
Packit 534379
    c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
Packit 534379
Packit 534379
    assert m.move_only(10) == 10  # 1 move, c_m
Packit 534379
    assert m.move_or_copy(11) == 11  # 1 move, c_mc
Packit 534379
    assert m.copy_only(12) == 12  # 1 copy, c_c
Packit 534379
    assert m.move_pair((13, 14)) == 27  # 1 c_m move, 1 c_mc move
Packit 534379
    assert m.move_tuple((15, 16, 17)) == 48  # 2 c_m moves, 1 c_mc move
Packit 534379
    assert m.copy_tuple((18, 19)) == 37  # 2 c_c copies
Packit 534379
    # Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
Packit 534379
    # Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
Packit 534379
    assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
Packit 534379
Packit 534379
    assert c_m.copy_assignments + c_m.copy_constructions == 0
Packit 534379
    assert c_m.move_assignments == 6
Packit 534379
    assert c_m.move_constructions == 9
Packit 534379
    assert c_mc.copy_assignments + c_mc.copy_constructions == 0
Packit 534379
    assert c_mc.move_assignments == 5
Packit 534379
    assert c_mc.move_constructions == 8
Packit 534379
    assert c_c.copy_assignments == 4
Packit 534379
    assert c_c.copy_constructions == 6
Packit 534379
    assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
Packit 534379
Packit 534379
Packit 534379
@pytest.mark.skipif(not m.has_optional, reason='no <optional>')
Packit 534379
def test_move_and_copy_load_optional():
Packit 534379
    """Tests move/copy loads of std::optional arguments"""
Packit 534379
Packit 534379
    cstats = m.move_and_copy_cstats()
Packit 534379
    c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
Packit 534379
Packit 534379
    # The extra move/copy constructions below come from the std::optional move (which has to move
Packit 534379
    # its arguments):
Packit 534379
    assert m.move_optional(10) == 10  # c_m: 1 move assign, 2 move construct
Packit 534379
    assert m.move_or_copy_optional(11) == 11  # c_mc: 1 move assign, 2 move construct
Packit 534379
    assert m.copy_optional(12) == 12  # c_c: 1 copy assign, 2 copy construct
Packit 534379
    # 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
Packit 534379
    # +1 move/copy construct each from moving the tuple
Packit 534379
    # +1 move/copy construct each from moving the optional (which moves the tuple again)
Packit 534379
    assert m.move_optional_tuple((3, 4, 5)) == 12
Packit 534379
Packit 534379
    assert c_m.copy_assignments + c_m.copy_constructions == 0
Packit 534379
    assert c_m.move_assignments == 2
Packit 534379
    assert c_m.move_constructions == 5
Packit 534379
    assert c_mc.copy_assignments + c_mc.copy_constructions == 0
Packit 534379
    assert c_mc.move_assignments == 2
Packit 534379
    assert c_mc.move_constructions == 5
Packit 534379
    assert c_c.copy_assignments == 2
Packit 534379
    assert c_c.copy_constructions == 5
Packit 534379
    assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
Packit 534379
Packit 534379
Packit 534379
def test_private_op_new():
Packit 534379
    """An object with a private `operator new` cannot be returned by value"""
Packit 534379
Packit 534379
    with pytest.raises(RuntimeError) as excinfo:
Packit 534379
        m.private_op_new_value()
Packit 534379
    assert "the object is neither movable nor copyable" in str(excinfo.value)
Packit 534379
Packit 534379
    assert m.private_op_new_reference().value == 1
Packit 534379
Packit 534379
Packit 534379
def test_move_fallback():
Packit 534379
    """#389: rvp::move should fall-through to copy on non-movable objects"""
Packit 534379
Packit 534379
    m2 = m.get_moveissue2(2)
Packit 534379
    assert m2.value == 2
Packit 534379
    m1 = m.get_moveissue1(1)
Packit 534379
    assert m1.value == 1