112391Sjason@lowepower.comimport pytest
212391Sjason@lowepower.comfrom pybind11_tests import copy_move_policies as m
312391Sjason@lowepower.com
412391Sjason@lowepower.com
512391Sjason@lowepower.comdef test_lacking_copy_ctor():
612391Sjason@lowepower.com    with pytest.raises(RuntimeError) as excinfo:
712391Sjason@lowepower.com        m.lacking_copy_ctor.get_one()
812391Sjason@lowepower.com    assert "the object is non-copyable!" in str(excinfo.value)
912391Sjason@lowepower.com
1012391Sjason@lowepower.com
1112391Sjason@lowepower.comdef test_lacking_move_ctor():
1212391Sjason@lowepower.com    with pytest.raises(RuntimeError) as excinfo:
1312391Sjason@lowepower.com        m.lacking_move_ctor.get_one()
1412391Sjason@lowepower.com    assert "the object is neither movable nor copyable!" in str(excinfo.value)
1512391Sjason@lowepower.com
1612391Sjason@lowepower.com
1712391Sjason@lowepower.comdef test_move_and_copy_casts():
1812391Sjason@lowepower.com    """Cast some values in C++ via custom type casters and count the number of moves/copies."""
1912391Sjason@lowepower.com
2012391Sjason@lowepower.com    cstats = m.move_and_copy_cstats()
2112391Sjason@lowepower.com    c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
2212391Sjason@lowepower.com
2312391Sjason@lowepower.com    # The type move constructions/assignments below each get incremented: the move assignment comes
2412391Sjason@lowepower.com    # from the type_caster load; the move construction happens when extracting that via a cast or
2512391Sjason@lowepower.com    # loading into an argument.
2612391Sjason@lowepower.com    assert m.move_and_copy_casts(3) == 18
2712391Sjason@lowepower.com    assert c_m.copy_assignments + c_m.copy_constructions == 0
2812391Sjason@lowepower.com    assert c_m.move_assignments == 2
2912391Sjason@lowepower.com    assert c_m.move_constructions >= 2
3012391Sjason@lowepower.com    assert c_mc.alive() == 0
3112391Sjason@lowepower.com    assert c_mc.copy_assignments + c_mc.copy_constructions == 0
3212391Sjason@lowepower.com    assert c_mc.move_assignments == 2
3312391Sjason@lowepower.com    assert c_mc.move_constructions >= 2
3412391Sjason@lowepower.com    assert c_c.alive() == 0
3512391Sjason@lowepower.com    assert c_c.copy_assignments == 2
3612391Sjason@lowepower.com    assert c_c.copy_constructions >= 2
3712391Sjason@lowepower.com    assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
3812391Sjason@lowepower.com
3912391Sjason@lowepower.com
4012391Sjason@lowepower.comdef test_move_and_copy_loads():
4112391Sjason@lowepower.com    """Call some functions that load arguments via custom type casters and count the number of
4212391Sjason@lowepower.com    moves/copies."""
4312391Sjason@lowepower.com
4412391Sjason@lowepower.com    cstats = m.move_and_copy_cstats()
4512391Sjason@lowepower.com    c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
4612391Sjason@lowepower.com
4712391Sjason@lowepower.com    assert m.move_only(10) == 10  # 1 move, c_m
4812391Sjason@lowepower.com    assert m.move_or_copy(11) == 11  # 1 move, c_mc
4912391Sjason@lowepower.com    assert m.copy_only(12) == 12  # 1 copy, c_c
5012391Sjason@lowepower.com    assert m.move_pair((13, 14)) == 27  # 1 c_m move, 1 c_mc move
5112391Sjason@lowepower.com    assert m.move_tuple((15, 16, 17)) == 48  # 2 c_m moves, 1 c_mc move
5212391Sjason@lowepower.com    assert m.copy_tuple((18, 19)) == 37  # 2 c_c copies
5312391Sjason@lowepower.com    # Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
5412391Sjason@lowepower.com    # Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
5512391Sjason@lowepower.com    assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
5612391Sjason@lowepower.com
5712391Sjason@lowepower.com    assert c_m.copy_assignments + c_m.copy_constructions == 0
5812391Sjason@lowepower.com    assert c_m.move_assignments == 6
5912391Sjason@lowepower.com    assert c_m.move_constructions == 9
6012391Sjason@lowepower.com    assert c_mc.copy_assignments + c_mc.copy_constructions == 0
6112391Sjason@lowepower.com    assert c_mc.move_assignments == 5
6212391Sjason@lowepower.com    assert c_mc.move_constructions == 8
6312391Sjason@lowepower.com    assert c_c.copy_assignments == 4
6412391Sjason@lowepower.com    assert c_c.copy_constructions == 6
6512391Sjason@lowepower.com    assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
6612391Sjason@lowepower.com
6712391Sjason@lowepower.com
6812391Sjason@lowepower.com@pytest.mark.skipif(not m.has_optional, reason='no <optional>')
6912391Sjason@lowepower.comdef test_move_and_copy_load_optional():
7012391Sjason@lowepower.com    """Tests move/copy loads of std::optional arguments"""
7112391Sjason@lowepower.com
7212391Sjason@lowepower.com    cstats = m.move_and_copy_cstats()
7312391Sjason@lowepower.com    c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
7412391Sjason@lowepower.com
7512391Sjason@lowepower.com    # The extra move/copy constructions below come from the std::optional move (which has to move
7612391Sjason@lowepower.com    # its arguments):
7712391Sjason@lowepower.com    assert m.move_optional(10) == 10  # c_m: 1 move assign, 2 move construct
7812391Sjason@lowepower.com    assert m.move_or_copy_optional(11) == 11  # c_mc: 1 move assign, 2 move construct
7912391Sjason@lowepower.com    assert m.copy_optional(12) == 12  # c_c: 1 copy assign, 2 copy construct
8012391Sjason@lowepower.com    # 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
8112391Sjason@lowepower.com    # +1 move/copy construct each from moving the tuple
8212391Sjason@lowepower.com    # +1 move/copy construct each from moving the optional (which moves the tuple again)
8312391Sjason@lowepower.com    assert m.move_optional_tuple((3, 4, 5)) == 12
8412391Sjason@lowepower.com
8512391Sjason@lowepower.com    assert c_m.copy_assignments + c_m.copy_constructions == 0
8612391Sjason@lowepower.com    assert c_m.move_assignments == 2
8712391Sjason@lowepower.com    assert c_m.move_constructions == 5
8812391Sjason@lowepower.com    assert c_mc.copy_assignments + c_mc.copy_constructions == 0
8912391Sjason@lowepower.com    assert c_mc.move_assignments == 2
9012391Sjason@lowepower.com    assert c_mc.move_constructions == 5
9112391Sjason@lowepower.com    assert c_c.copy_assignments == 2
9212391Sjason@lowepower.com    assert c_c.copy_constructions == 5
9312391Sjason@lowepower.com    assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
9412391Sjason@lowepower.com
9512391Sjason@lowepower.com
9612391Sjason@lowepower.comdef test_private_op_new():
9712391Sjason@lowepower.com    """An object with a private `operator new` cannot be returned by value"""
9812391Sjason@lowepower.com
9912391Sjason@lowepower.com    with pytest.raises(RuntimeError) as excinfo:
10012391Sjason@lowepower.com        m.private_op_new_value()
10112391Sjason@lowepower.com    assert "the object is neither movable nor copyable" in str(excinfo.value)
10212391Sjason@lowepower.com
10312391Sjason@lowepower.com    assert m.private_op_new_reference().value == 1
10412391Sjason@lowepower.com
10512391Sjason@lowepower.com
10612391Sjason@lowepower.comdef test_move_fallback():
10712391Sjason@lowepower.com    """#389: rvp::move should fall-through to copy on non-movable objects"""
10812391Sjason@lowepower.com
10912391Sjason@lowepower.com    m2 = m.get_moveissue2(2)
11012391Sjason@lowepower.com    assert m2.value == 2
11112391Sjason@lowepower.com    m1 = m.get_moveissue1(1)
11212391Sjason@lowepower.com    assert m1.value == 1
113