test_class_sh_basic.py 2.77 KB
Newer Older
1
2
3
# -*- coding: utf-8 -*-
import pytest

4
from pybind11_tests import class_sh_basic as m
5
6


7
def test_atyp_constructors():
8
9
10
11
12
13
    obj = m.atyp()
    assert obj.__class__.__name__ == "atyp"
    obj = m.atyp("")
    assert obj.__class__.__name__ == "atyp"
    obj = m.atyp("txtm")
    assert obj.__class__.__name__ == "atyp"
14
15
16


def test_cast():
17
18
19
20
21
22
    assert m.get_mtxt(m.rtrn_valu_atyp()) == "rtrn_valu"
    assert m.get_mtxt(m.rtrn_rref_atyp()) == "rtrn_rref"
    assert m.get_mtxt(m.rtrn_cref_atyp()) == "rtrn_cref"
    assert m.get_mtxt(m.rtrn_mref_atyp()) == "rtrn_mref"
    assert m.get_mtxt(m.rtrn_cptr_atyp()) == "rtrn_cptr"
    assert m.get_mtxt(m.rtrn_mptr_atyp()) == "rtrn_mptr"
23
24
25


def test_load():
26
27
28
29
30
31
    assert m.pass_valu_atyp(m.atyp("Valu")) == "pass_valu:Valu"
    assert m.pass_rref_atyp(m.atyp("Rref")) == "pass_rref:Rref"
    assert m.pass_cref_atyp(m.atyp("Cref")) == "pass_cref:Cref"
    assert m.pass_mref_atyp(m.atyp("Mref")) == "pass_mref:Mref"
    assert m.pass_cptr_atyp(m.atyp("Cptr")) == "pass_cptr:Cptr"
    assert m.pass_mptr_atyp(m.atyp("Mptr")) == "pass_mptr:Mptr"
32
33
34


def test_cast_shared_ptr():
35
36
    assert m.get_mtxt(m.rtrn_shmp_atyp()) == "rtrn_shmp"
    assert m.get_mtxt(m.rtrn_shcp_atyp()) == "rtrn_shcp"
37
38
39


def test_load_shared_ptr():
40
41
    assert m.pass_shmp_atyp(m.atyp("Shmp")) == "pass_shmp:Shmp"
    assert m.pass_shcp_atyp(m.atyp("Shcp")) == "pass_shcp:Shcp"
42
43
44


def test_cast_unique_ptr():
45
46
    assert m.get_mtxt(m.rtrn_uqmp_atyp()) == "rtrn_uqmp"
    assert m.get_mtxt(m.rtrn_uqcp_atyp()) == "rtrn_uqcp"
47
48
49


def test_load_unique_ptr():
50
51
    assert m.pass_uqmp_atyp(m.atyp("Uqmp")) == "pass_uqmp:Uqmp"
    assert m.pass_uqcp_atyp(m.atyp("Uqcp")) == "pass_uqcp:Uqcp"
52
53
54


@pytest.mark.parametrize(
55
    "pass_atyp, argm, rtrn",
56
    [
57
58
        (m.pass_uqmp_atyp, "Uqmp", "pass_uqmp:Uqmp"),
        (m.pass_uqcp_atyp, "Uqcp", "pass_uqcp:Uqcp"),
59
60
    ],
)
61
62
63
def test_pass_unique_ptr_disowns(pass_atyp, argm, rtrn):
    obj = m.atyp(argm)
    assert pass_atyp(obj) == rtrn
64
    with pytest.raises(RuntimeError) as exc_info:
65
        m.pass_uqmp_atyp(obj)
66
67
68
69
70
71
72
    assert str(exc_info.value) == (
        "Missing value for wrapped C++ type:"
        " Python instance is uninitialized or was disowned."
    )


def test_unique_ptr_roundtrip(num_round_trips=1000):
73
    # Multiple roundtrips to stress-test instance registration/deregistration.
74
    recycled = m.atyp("passenger")
75
    for _ in range(num_round_trips):
76
        id_orig = id(recycled)
77
78
        recycled = m.unique_ptr_roundtrip(recycled)
        assert m.get_mtxt(recycled) == "passenger"
79
80
81
82
        id_rtrn = id(recycled)
        # Ensure the returned object is a different Python instance.
        assert id_rtrn != id_orig
        id_orig = id_rtrn
83
84
85
86
87


def test_py_type_handle_of_atyp():
    obj = m.py_type_handle_of_atyp()
    assert obj.__class__.__name__ == "pybind11_type"