Adding test_pure_cpp_sft_raw_ptr (with 3 TODO: Fix), test_pure_cpp_sft_shd_ptr (works as desired).

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-06-23 16:35:27 -07:00 committed by Ralf W. Grosse-Kunstleve
parent 4a15ed742c
commit ccd16a1a14
2 changed files with 35 additions and 1 deletions

View File

@ -12,7 +12,7 @@ namespace {
struct Sft : std::enable_shared_from_this<Sft> {
std::string history;
explicit Sft(const std::string &history) : history{history} {}
explicit Sft(const std::string &history_seed) : history{history_seed} {}
virtual ~Sft() = default;
#if defined(__clang__)
@ -87,6 +87,12 @@ long pass_shared_ptr(const std::shared_ptr<Sft> &obj) {
void pass_unique_ptr(const std::unique_ptr<Sft> &) {}
Sft *make_pure_cpp_sft_raw_ptr(const std::string &history_seed) { return new Sft{history_seed}; }
std::shared_ptr<Sft> make_pure_cpp_sft_shd_ptr(const std::string &history_seed) {
return std::make_shared<Sft>(history_seed);
}
} // namespace
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Sft)
@ -108,5 +114,7 @@ TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) {
m.def("use_count", use_count);
m.def("pass_shared_ptr", pass_shared_ptr);
m.def("pass_unique_ptr", pass_unique_ptr);
m.def("make_pure_cpp_sft_raw_ptr", make_pure_cpp_sft_raw_ptr);
m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr);
m.def("to_cout", to_cout);
}

View File

@ -128,3 +128,29 @@ def test_pass_released_shared_ptr_as_unique_ptr():
assert str(exc_info.value) == (
"Python instance is currently owned by a std::shared_ptr."
)
def test_pure_cpp_sft_raw_ptr():
obj = m.make_pure_cpp_sft_raw_ptr("PureCppSft")
with pytest.raises(RuntimeError) as exc_info:
assert m.pass_shared_ptr(obj) == 3 # TODO: FIX.
assert str(exc_info.value) == "bad_weak_ptr"
obj = m.make_pure_cpp_sft_raw_ptr("PureCppSft")
stash1 = m.SftSharedPtrStash(1)
with pytest.raises(RuntimeError) as exc_info:
stash1.AddSharedFromThis(obj) # TODO: FIX.
assert str(exc_info.value) == "bad_weak_ptr"
stash1.Add(obj)
with pytest.raises(RuntimeError) as exc_info:
stash1.AddSharedFromThis(obj) # TODO: FIX.
assert str(exc_info.value) == "bad_weak_ptr"
def test_pure_cpp_sft_shd_ptr():
obj = m.make_pure_cpp_sft_shd_ptr("PureCppSft")
assert m.pass_shared_ptr(obj) == 3
assert obj.history == "PureCppSft_PassSharedPtr"
obj = m.make_pure_cpp_sft_shd_ptr("PureCppSft")
stash1 = m.SftSharedPtrStash(1)
stash1.AddSharedFromThis(obj)
assert obj.history == "PureCppSft_Stash1AddSharedFromThis"