2021-01-11 19:55:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pybind11_tests import classh_wip as m
|
|
|
|
|
|
|
|
|
2021-01-11 23:58:14 +00:00
|
|
|
def test_mpty_constructors():
|
2021-01-11 19:55:03 +00:00
|
|
|
e = m.mpty()
|
|
|
|
assert e.__class__.__name__ == "mpty"
|
2021-01-11 23:58:14 +00:00
|
|
|
e = m.mpty("")
|
|
|
|
assert e.__class__.__name__ == "mpty"
|
|
|
|
e = m.mpty("txtm")
|
|
|
|
assert e.__class__.__name__ == "mpty"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast():
|
2021-01-12 22:24:31 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_valu()) == "rtrn_valu"
|
2021-01-14 18:29:23 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_rref()) == "rtrn_rref"
|
2021-01-12 21:45:48 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_cref()) == "rtrn_cref"
|
|
|
|
assert m.get_mtxt(m.rtrn_mpty_mref()) == "rtrn_mref"
|
2021-01-12 21:26:22 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_cptr()) == "rtrn_cptr"
|
2021-01-12 21:45:48 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_mptr()) == "rtrn_mptr"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load():
|
2021-01-12 04:38:11 +00:00
|
|
|
assert m.pass_mpty_valu(m.mpty("Valu")) == "pass_valu:Valu"
|
|
|
|
assert m.pass_mpty_rref(m.mpty("Rref")) == "pass_rref:Rref"
|
|
|
|
assert m.pass_mpty_cref(m.mpty("Cref")) == "pass_cref:Cref"
|
|
|
|
assert m.pass_mpty_mref(m.mpty("Mref")) == "pass_mref:Mref"
|
|
|
|
assert m.pass_mpty_cptr(m.mpty("Cptr")) == "pass_cptr:Cptr"
|
|
|
|
assert m.pass_mpty_mptr(m.mpty("Mptr")) == "pass_mptr:Mptr"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast_shared_ptr():
|
2021-01-13 18:48:08 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_shmp()) == "rtrn_shmp"
|
|
|
|
assert m.get_mtxt(m.rtrn_mpty_shcp()) == "rtrn_shcp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_shared_ptr():
|
2021-01-12 05:27:23 +00:00
|
|
|
assert m.pass_mpty_shmp(m.mpty("Shmp")) == "pass_shmp:Shmp"
|
|
|
|
assert m.pass_mpty_shcp(m.mpty("Shcp")) == "pass_shcp:Shcp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cast_unique_ptr():
|
2021-01-13 20:00:26 +00:00
|
|
|
assert m.get_mtxt(m.rtrn_mpty_uqmp()) == "rtrn_uqmp"
|
|
|
|
assert m.get_mtxt(m.rtrn_mpty_uqcp()) == "rtrn_uqcp"
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_unique_ptr():
|
2021-01-12 06:06:18 +00:00
|
|
|
assert m.pass_mpty_uqmp(m.mpty("Uqmp")) == "pass_uqmp:Uqmp"
|
|
|
|
assert m.pass_mpty_uqcp(m.mpty("Uqcp")) == "pass_uqcp:Uqcp"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"pass_mpty, argm, rtrn",
|
|
|
|
[
|
|
|
|
(m.pass_mpty_uqmp, "Uqmp", "pass_uqmp:Uqmp"),
|
|
|
|
(m.pass_mpty_uqcp, "Uqcp", "pass_uqcp:Uqcp"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_pass_unique_ptr_disowns(pass_mpty, argm, rtrn):
|
|
|
|
obj = m.mpty(argm)
|
|
|
|
assert pass_mpty(obj) == rtrn
|
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
|
|
m.pass_mpty_uqmp(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-15 06:23:07 +00:00
|
|
|
recycled = m.mpty("passenger")
|
|
|
|
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
|