Skip to content

Commit

Permalink
fix(smart_holder): Add unit tests for the default constructible deleter.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanders committed Nov 7, 2023
1 parent 4cdd8bb commit 8d1f30b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
24 changes: 22 additions & 2 deletions tests/test_class_sh_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ struct uconsumer { // unique_ptr consumer
const std::unique_ptr<atyp> &rtrn_cref() const { return held; }
};

/// Custom deleter that is default constructible.
struct custom_deleter {
std::string trace_txt;

custom_deleter() = delete;
custom_deleter() = default;
explicit custom_deleter(const std::string &trace_txt_) : trace_txt(trace_txt_) {}

custom_deleter(const custom_deleter &other) { trace_txt = other.trace_txt + "_CpCtor"; }
Expand All @@ -55,6 +56,14 @@ struct custom_deleter {
void operator()(atyp *p) const { std::default_delete<atyp>()(p); }
void operator()(const atyp *p) const { std::default_delete<const atyp>()(p); }
};
static_assert(std::is_default_constructible<custom_deleter>::value);

/// Custom deleter that is not default constructible.
struct custom_deleter_nd : custom_deleter {
custom_deleter_nd() = delete;
explicit custom_deleter_nd(const std::string &trace_txt_) : custom_deleter(trace_txt_) {}
};
static_assert(!std::is_default_constructible<custom_deleter_nd>::value);

// clang-format off

Expand Down Expand Up @@ -92,13 +101,18 @@ std::unique_ptr<atyp const, sddc> rtrn_udcp() { return std::unique_ptr<atyp cons
std::string pass_udmp(std::unique_ptr<atyp, sddm> obj) { return "pass_udmp:" + obj->mtxt; }
std::string pass_udcp(std::unique_ptr<atyp const, sddc> obj) { return "pass_udcp:" + obj->mtxt; }


std::unique_ptr<atyp, custom_deleter> rtrn_udmp_del() { return std::unique_ptr<atyp, custom_deleter>(new atyp{"rtrn_udmp_del"}, custom_deleter{"udmp_deleter"}); }
std::unique_ptr<atyp const, custom_deleter> rtrn_udcp_del() { return std::unique_ptr<atyp const, custom_deleter>(new atyp{"rtrn_udcp_del"}, custom_deleter{"udcp_deleter"}); }

std::string pass_udmp_del(std::unique_ptr<atyp, custom_deleter> obj) { return "pass_udmp_del:" + obj->mtxt + "," + obj.get_deleter().trace_txt; }
std::string pass_udcp_del(std::unique_ptr<atyp const, custom_deleter> obj) { return "pass_udcp_del:" + obj->mtxt + "," + obj.get_deleter().trace_txt; }

std::unique_ptr<atyp, custom_deleter_nd> rtrn_udmp_del_nd() { return std::unique_ptr<atyp, custom_deleter_nd>(new atyp{"rtrn_udmp_del_nd"}, custom_deleter_nd{"udmp_deleter_nd"}); }
std::unique_ptr<atyp const, custom_deleter_nd> rtrn_udcp_del_nd() { return std::unique_ptr<atyp const, custom_deleter_nd>(new atyp{"rtrn_udcp_del_nd"}, custom_deleter_nd{"udcp_deleter_nd"}); }

std::string pass_udmp_del_nd(std::unique_ptr<atyp, custom_deleter_nd> obj) { return "pass_udmp_del_nd:" + obj->mtxt + "," + obj.get_deleter().trace_txt; }
std::string pass_udcp_del_nd(std::unique_ptr<atyp const, custom_deleter_nd> obj) { return "pass_udcp_del_nd:" + obj->mtxt + "," + obj.get_deleter().trace_txt; }

// clang-format on

// Helpers for testing.
Expand Down Expand Up @@ -171,6 +185,12 @@ TEST_SUBMODULE(class_sh_basic, m) {
m.def("pass_udmp_del", pass_udmp_del);
m.def("pass_udcp_del", pass_udcp_del);

m.def("rtrn_udmp_del_nd", rtrn_udmp_del_nd);
m.def("rtrn_udcp_del_nd", rtrn_udcp_del_nd);

m.def("pass_udmp_del_nd", pass_udmp_del_nd);
m.def("pass_udcp_del_nd", pass_udcp_del_nd);

py::classh<uconsumer>(m, "uconsumer")
.def(py::init<>())
.def("valid", &uconsumer::valid)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_class_sh_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ def test_load_with_rtrn_f(pass_f, rtrn_f, expected):
@pytest.mark.parametrize(
("pass_f", "rtrn_f", "expected"),
[
(m.pass_udmp_del, m.rtrn_udmp_del, "pass_udmp_del:rtrn_udmp_del,udmp_deleter_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo"),
(m.pass_udcp_del, m.rtrn_udcp_del, "pass_udcp_del:rtrn_udcp_del,udcp_deleter_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo_MvCtorTo"),
(m.pass_udmp_del, m.rtrn_udmp_del, "pass_udmp_del:rtrn_udmp_del,udmp_deleter_" + "_".join(["MvCtorTo"] * 6)),
(m.pass_udcp_del, m.rtrn_udcp_del, "pass_udcp_del:rtrn_udcp_del,udcp_deleter_" + "_".join(["MvCtorTo"] * 8)),
(m.pass_udmp_del_nd, m.rtrn_udmp_del_nd,
"pass_udmp_del_nd:rtrn_udmp_del_nd,udmp_deleter_nd_" + "_".join(["MvCtorTo"] * 6)),
(m.pass_udcp_del_nd, m.rtrn_udcp_del_nd,
"pass_udcp_del_nd:rtrn_udcp_del_nd,udcp_deleter_nd_" + "_".join(["MvCtorTo"] * 8)),
],
)
def test_deleter_roundtrip(pass_f, rtrn_f, expected):
Expand Down

0 comments on commit 8d1f30b

Please sign in to comment.