2021-01-11 19:55:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pybind11_tests import classh_wip as m
|
|
|
|
|
|
|
|
|
2021-01-23 16:39:26 +00:00
|
|
|
def test_atyp_constructors():
|
|
|
|
e = m.atyp()
|
|
|
|
assert e.__class__.__name__ == "atyp"
|
|
|
|
e = m.atyp("")
|
|
|
|
assert e.__class__.__name__ == "atyp"
|
|
|
|
e = m.atyp("txtm")
|
|
|
|
assert e.__class__.__name__ == "atyp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast():
|
2021-01-23 16:39:26 +00:00
|
|
|
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"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load():
|
2021-01-23 16:39:26 +00:00
|
|
|
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"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast_shared_ptr():
|
2021-01-23 16:39:26 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_shmp_atyp()) == "rtrn_shmp"
|
|
|
|
assert m.get_mtxt(m.rtrn_shcp_atyp()) == "rtrn_shcp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_shared_ptr():
|
2021-01-23 16:39:26 +00:00
|
|
|
assert m.pass_shmp_atyp(m.atyp("Shmp")) == "pass_shmp:Shmp"
|
|
|
|
assert m.pass_shcp_atyp(m.atyp("Shcp")) == "pass_shcp:Shcp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast_unique_ptr():
|
2021-01-23 16:39:26 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_uqmp_atyp()) == "rtrn_uqmp"
|
|
|
|
assert m.get_mtxt(m.rtrn_uqcp_atyp()) == "rtrn_uqcp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_unique_ptr():
|
2021-01-23 16:39:26 +00:00
|
|
|
assert m.pass_uqmp_atyp(m.atyp("Uqmp")) == "pass_uqmp:Uqmp"
|
|
|
|
assert m.pass_uqcp_atyp(m.atyp("Uqcp")) == "pass_uqcp:Uqcp"
|
2021-01-12 06:06:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2021-01-23 16:39:26 +00:00
|
|
|
"pass_atyp, argm, rtrn",
|
2021-01-12 06:06:18 +00:00
|
|
|
[
|
2021-01-23 16:39:26 +00:00
|
|
|
(m.pass_uqmp_atyp, "Uqmp", "pass_uqmp:Uqmp"),
|
|
|
|
(m.pass_uqcp_atyp, "Uqcp", "pass_uqcp:Uqcp"),
|
2021-01-12 06:06:18 +00:00
|
|
|
],
|
|
|
|
)
|
2021-01-23 16:39:26 +00:00
|
|
|
def test_pass_unique_ptr_disowns(pass_atyp, argm, rtrn):
|
|
|
|
obj = m.atyp(argm)
|
|
|
|
assert pass_atyp(obj) == rtrn
|
2021-01-12 06:06:18 +00:00
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
2021-01-23 16:39:26 +00:00
|
|
|
m.pass_uqmp_atyp(obj)
|
2021-01-15 06:23:07 +00:00
|
|
|
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):
|
2021-01-16 00:47:47 +00:00
|
|
|
# Multiple roundtrips to stress-test instance registration/deregistration.
|
2021-01-23 16:39:26 +00:00
|
|
|
recycled = m.atyp("passenger")
|
2021-01-15 06:23:07 +00:00
|
|
|
for _ in range(num_round_trips):
|
2021-01-16 00:47:47 +00:00
|
|
|
id_orig = id(recycled)
|
2021-01-15 06:23:07 +00:00
|
|
|
recycled = m.unique_ptr_roundtrip(recycled)
|
|
|
|
assert m.get_mtxt(recycled) == "passenger"
|
2021-01-16 00:47:47 +00:00
|
|
|
id_rtrn = id(recycled)
|
|
|
|
# Ensure the returned object is a different Python instance.
|
|
|
|
assert id_rtrn != id_orig
|
|
|
|
id_orig = id_rtrn
|