Pure renaming of function and variable names, for better generalization when convoluting with inheritance. NO functional changes.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-23 08:39:26 -08:00
parent 54fa191f20
commit 235fa288b8
2 changed files with 83 additions and 83 deletions

View File

@ -8,48 +8,48 @@
namespace pybind11_tests {
namespace classh_wip {
struct mpty {
struct atyp { // Short for "any type".
std::string mtxt;
};
// clang-format off
mpty rtrn_mpty_valu() { mpty obj{"rtrn_valu"}; return obj; }
mpty&& rtrn_mpty_rref() { static mpty obj; obj.mtxt = "rtrn_rref"; return std::move(obj); }
mpty const& rtrn_mpty_cref() { static mpty obj; obj.mtxt = "rtrn_cref"; return obj; }
mpty& rtrn_mpty_mref() { static mpty obj; obj.mtxt = "rtrn_mref"; return obj; }
mpty const* rtrn_mpty_cptr() { return new mpty{"rtrn_cptr"}; }
mpty* rtrn_mpty_mptr() { return new mpty{"rtrn_mptr"}; }
atyp rtrn_valu_atyp() { atyp obj{"rtrn_valu"}; return obj; }
atyp&& rtrn_rref_atyp() { static atyp obj; obj.mtxt = "rtrn_rref"; return std::move(obj); }
atyp const& rtrn_cref_atyp() { static atyp obj; obj.mtxt = "rtrn_cref"; return obj; }
atyp& rtrn_mref_atyp() { static atyp obj; obj.mtxt = "rtrn_mref"; return obj; }
atyp const* rtrn_cptr_atyp() { return new atyp{"rtrn_cptr"}; }
atyp* rtrn_mptr_atyp() { return new atyp{"rtrn_mptr"}; }
std::string pass_mpty_valu(mpty obj) { return "pass_valu:" + obj.mtxt; }
std::string pass_mpty_rref(mpty&& obj) { return "pass_rref:" + obj.mtxt; }
std::string pass_mpty_cref(mpty const& obj) { return "pass_cref:" + obj.mtxt; }
std::string pass_mpty_mref(mpty& obj) { return "pass_mref:" + obj.mtxt; }
std::string pass_mpty_cptr(mpty const* obj) { return "pass_cptr:" + obj->mtxt; }
std::string pass_mpty_mptr(mpty* obj) { return "pass_mptr:" + obj->mtxt; }
std::string pass_valu_atyp(atyp obj) { return "pass_valu:" + obj.mtxt; }
std::string pass_rref_atyp(atyp&& obj) { return "pass_rref:" + obj.mtxt; }
std::string pass_cref_atyp(atyp const& obj) { return "pass_cref:" + obj.mtxt; }
std::string pass_mref_atyp(atyp& obj) { return "pass_mref:" + obj.mtxt; }
std::string pass_cptr_atyp(atyp const* obj) { return "pass_cptr:" + obj->mtxt; }
std::string pass_mptr_atyp(atyp* obj) { return "pass_mptr:" + obj->mtxt; }
std::shared_ptr<mpty> rtrn_mpty_shmp() { return std::shared_ptr<mpty >(new mpty{"rtrn_shmp"}); }
std::shared_ptr<mpty const> rtrn_mpty_shcp() { return std::shared_ptr<mpty const>(new mpty{"rtrn_shcp"}); }
std::shared_ptr<atyp> rtrn_shmp_atyp() { return std::shared_ptr<atyp >(new atyp{"rtrn_shmp"}); }
std::shared_ptr<atyp const> rtrn_shcp_atyp() { return std::shared_ptr<atyp const>(new atyp{"rtrn_shcp"}); }
std::string pass_mpty_shmp(std::shared_ptr<mpty> obj) { return "pass_shmp:" + obj->mtxt; }
std::string pass_mpty_shcp(std::shared_ptr<mpty const> obj) { return "pass_shcp:" + obj->mtxt; }
std::string pass_shmp_atyp(std::shared_ptr<atyp> obj) { return "pass_shmp:" + obj->mtxt; }
std::string pass_shcp_atyp(std::shared_ptr<atyp const> obj) { return "pass_shcp:" + obj->mtxt; }
std::unique_ptr<mpty> rtrn_mpty_uqmp() { return std::unique_ptr<mpty >(new mpty{"rtrn_uqmp"}); }
std::unique_ptr<mpty const> rtrn_mpty_uqcp() { return std::unique_ptr<mpty const>(new mpty{"rtrn_uqcp"}); }
std::unique_ptr<atyp> rtrn_uqmp_atyp() { return std::unique_ptr<atyp >(new atyp{"rtrn_uqmp"}); }
std::unique_ptr<atyp const> rtrn_uqcp_atyp() { return std::unique_ptr<atyp const>(new atyp{"rtrn_uqcp"}); }
std::string pass_mpty_uqmp(std::unique_ptr<mpty > obj) { return "pass_uqmp:" + obj->mtxt; }
std::string pass_mpty_uqcp(std::unique_ptr<mpty const> obj) { return "pass_uqcp:" + obj->mtxt; }
std::string pass_uqmp_atyp(std::unique_ptr<atyp > obj) { return "pass_uqmp:" + obj->mtxt; }
std::string pass_uqcp_atyp(std::unique_ptr<atyp const> obj) { return "pass_uqcp:" + obj->mtxt; }
// clang-format on
// Helpers for testing.
std::string get_mtxt(mpty const &obj) { return obj.mtxt; }
std::unique_ptr<mpty> unique_ptr_roundtrip(std::unique_ptr<mpty> obj) { return obj; }
std::string get_mtxt(atyp const &obj) { return obj.mtxt; }
std::unique_ptr<atyp> unique_ptr_roundtrip(std::unique_ptr<atyp> obj) { return obj; }
} // namespace classh_wip
} // namespace pybind11_tests
PYBIND11_CLASSH_TYPE_CASTERS(pybind11_tests::classh_wip::mpty)
PYBIND11_CLASSH_TYPE_CASTERS(pybind11_tests::classh_wip::atyp)
namespace pybind11_tests {
namespace classh_wip {
@ -57,42 +57,42 @@ namespace classh_wip {
TEST_SUBMODULE(classh_wip, m) {
namespace py = pybind11;
py::classh<mpty>(m, "mpty").def(py::init<>()).def(py::init([](const std::string &mtxt) {
mpty obj;
py::classh<atyp>(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) {
atyp obj;
obj.mtxt = mtxt;
return obj;
}));
m.def("rtrn_mpty_valu", rtrn_mpty_valu);
m.def("rtrn_mpty_rref", rtrn_mpty_rref);
m.def("rtrn_mpty_cref", rtrn_mpty_cref);
m.def("rtrn_mpty_mref", rtrn_mpty_mref);
m.def("rtrn_mpty_cptr", rtrn_mpty_cptr);
m.def("rtrn_mpty_mptr", rtrn_mpty_mptr);
m.def("rtrn_valu_atyp", rtrn_valu_atyp);
m.def("rtrn_rref_atyp", rtrn_rref_atyp);
m.def("rtrn_cref_atyp", rtrn_cref_atyp);
m.def("rtrn_mref_atyp", rtrn_mref_atyp);
m.def("rtrn_cptr_atyp", rtrn_cptr_atyp);
m.def("rtrn_mptr_atyp", rtrn_mptr_atyp);
m.def("pass_mpty_valu", pass_mpty_valu);
m.def("pass_mpty_rref", pass_mpty_rref);
m.def("pass_mpty_cref", pass_mpty_cref);
m.def("pass_mpty_mref", pass_mpty_mref);
m.def("pass_mpty_cptr", pass_mpty_cptr);
m.def("pass_mpty_mptr", pass_mpty_mptr);
m.def("pass_valu_atyp", pass_valu_atyp);
m.def("pass_rref_atyp", pass_rref_atyp);
m.def("pass_cref_atyp", pass_cref_atyp);
m.def("pass_mref_atyp", pass_mref_atyp);
m.def("pass_cptr_atyp", pass_cptr_atyp);
m.def("pass_mptr_atyp", pass_mptr_atyp);
m.def("rtrn_mpty_shmp", rtrn_mpty_shmp);
m.def("rtrn_mpty_shcp", rtrn_mpty_shcp);
m.def("rtrn_shmp_atyp", rtrn_shmp_atyp);
m.def("rtrn_shcp_atyp", rtrn_shcp_atyp);
m.def("pass_mpty_shmp", pass_mpty_shmp);
m.def("pass_mpty_shcp", pass_mpty_shcp);
m.def("pass_shmp_atyp", pass_shmp_atyp);
m.def("pass_shcp_atyp", pass_shcp_atyp);
m.def("rtrn_mpty_uqmp", rtrn_mpty_uqmp);
m.def("rtrn_mpty_uqcp", rtrn_mpty_uqcp);
m.def("rtrn_uqmp_atyp", rtrn_uqmp_atyp);
m.def("rtrn_uqcp_atyp", rtrn_uqcp_atyp);
m.def("pass_mpty_uqmp", pass_mpty_uqmp);
m.def("pass_mpty_uqcp", pass_mpty_uqcp);
m.def("pass_uqmp_atyp", pass_uqmp_atyp);
m.def("pass_uqcp_atyp", pass_uqcp_atyp);
// Helpers for testing.
// These require selected functions above to work first, as indicated:
m.def("get_mtxt", get_mtxt); // pass_mpty_cref
m.def("unique_ptr_roundtrip", unique_ptr_roundtrip); // pass_mpty_uqmp, rtrn_mpty_uqmp
m.def("get_mtxt", get_mtxt); // pass_cref_atyp
m.def("unique_ptr_roundtrip", unique_ptr_roundtrip); // pass_uqmp_atyp, rtrn_uqmp_atyp
}
} // namespace classh_wip

View File

@ -4,65 +4,65 @@ import pytest
from pybind11_tests import classh_wip as m
def test_mpty_constructors():
e = m.mpty()
assert e.__class__.__name__ == "mpty"
e = m.mpty("")
assert e.__class__.__name__ == "mpty"
e = m.mpty("txtm")
assert e.__class__.__name__ == "mpty"
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"
def test_cast():
assert m.get_mtxt(m.rtrn_mpty_valu()) == "rtrn_valu"
assert m.get_mtxt(m.rtrn_mpty_rref()) == "rtrn_rref"
assert m.get_mtxt(m.rtrn_mpty_cref()) == "rtrn_cref"
assert m.get_mtxt(m.rtrn_mpty_mref()) == "rtrn_mref"
assert m.get_mtxt(m.rtrn_mpty_cptr()) == "rtrn_cptr"
assert m.get_mtxt(m.rtrn_mpty_mptr()) == "rtrn_mptr"
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"
def test_load():
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"
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"
def test_cast_shared_ptr():
assert m.get_mtxt(m.rtrn_mpty_shmp()) == "rtrn_shmp"
assert m.get_mtxt(m.rtrn_mpty_shcp()) == "rtrn_shcp"
assert m.get_mtxt(m.rtrn_shmp_atyp()) == "rtrn_shmp"
assert m.get_mtxt(m.rtrn_shcp_atyp()) == "rtrn_shcp"
def test_load_shared_ptr():
assert m.pass_mpty_shmp(m.mpty("Shmp")) == "pass_shmp:Shmp"
assert m.pass_mpty_shcp(m.mpty("Shcp")) == "pass_shcp:Shcp"
assert m.pass_shmp_atyp(m.atyp("Shmp")) == "pass_shmp:Shmp"
assert m.pass_shcp_atyp(m.atyp("Shcp")) == "pass_shcp:Shcp"
def test_cast_unique_ptr():
assert m.get_mtxt(m.rtrn_mpty_uqmp()) == "rtrn_uqmp"
assert m.get_mtxt(m.rtrn_mpty_uqcp()) == "rtrn_uqcp"
assert m.get_mtxt(m.rtrn_uqmp_atyp()) == "rtrn_uqmp"
assert m.get_mtxt(m.rtrn_uqcp_atyp()) == "rtrn_uqcp"
def test_load_unique_ptr():
assert m.pass_mpty_uqmp(m.mpty("Uqmp")) == "pass_uqmp:Uqmp"
assert m.pass_mpty_uqcp(m.mpty("Uqcp")) == "pass_uqcp:Uqcp"
assert m.pass_uqmp_atyp(m.atyp("Uqmp")) == "pass_uqmp:Uqmp"
assert m.pass_uqcp_atyp(m.atyp("Uqcp")) == "pass_uqcp:Uqcp"
@pytest.mark.parametrize(
"pass_mpty, argm, rtrn",
"pass_atyp, argm, rtrn",
[
(m.pass_mpty_uqmp, "Uqmp", "pass_uqmp:Uqmp"),
(m.pass_mpty_uqcp, "Uqcp", "pass_uqcp:Uqcp"),
(m.pass_uqmp_atyp, "Uqmp", "pass_uqmp:Uqmp"),
(m.pass_uqcp_atyp, "Uqcp", "pass_uqcp:Uqcp"),
],
)
def test_pass_unique_ptr_disowns(pass_mpty, argm, rtrn):
obj = m.mpty(argm)
assert pass_mpty(obj) == rtrn
def test_pass_unique_ptr_disowns(pass_atyp, argm, rtrn):
obj = m.atyp(argm)
assert pass_atyp(obj) == rtrn
with pytest.raises(RuntimeError) as exc_info:
m.pass_mpty_uqmp(obj)
m.pass_uqmp_atyp(obj)
assert str(exc_info.value) == (
"Missing value for wrapped C++ type:"
" Python instance is uninitialized or was disowned."
@ -71,7 +71,7 @@ def test_pass_unique_ptr_disowns(pass_mpty, argm, rtrn):
def test_unique_ptr_roundtrip(num_round_trips=1000):
# Multiple roundtrips to stress-test instance registration/deregistration.
recycled = m.mpty("passenger")
recycled = m.atyp("passenger")
for _ in range(num_round_trips):
id_orig = id(recycled)
recycled = m.unique_ptr_roundtrip(recycled)