pybind11/tests/test_class_sh_mi_thunks.py
Ralf W. Grosse-Kunstleve 4766065e5c
[smart_holder] test_class_sh_mi_thunks (started from PR #4374) (#4380)
* Content of PR #4374 applied on top of smart_holder branch.

* More tests, with USE_SH switch. [ci skip]

* Use `std::dynamic_pointer_cast<Base0>` [ci skip]

* All tests pass when using `m.make_derived_as_base0_raw_ptr()`, with `USE_SH` defined or not defined. [ci skip]

* WIP

* Debug LOOOK & one-line bug fix:

```diff
-        auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src);
+        auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(std::shared_ptr<void>(src, const_cast<void *>(st.first)));
```

* Remove all print LOOOK and clang-format the fix.

* Resolve clang-tidy errors.

* Systematic test matrix.

* Bug fix in `smart_holder_type_caster<std::unique_ptr<T, D>>::cast()`

* Rename: test_mi_debug -> test_class_sh_mi_thunks

* Add `test_ptrdiff_derived_base0()`

* Miscellaneous polishing (naming, comments). No functional changes.

* Improve test_class_sh_mi_thunks.py implementation. No change in test coverage.

* Resolve clang-tidy error.
2022-12-12 19:55:16 -08:00

57 lines
1.5 KiB
Python

import pytest
from pybind11_tests import class_sh_mi_thunks as m
def test_ptrdiff_drvd_base0():
ptrdiff = m.ptrdiff_drvd_base0()
# A failure here does not (necessarily) mean that there is a bug, but that
# test_class_sh_mi_thunks is not exercising what it is supposed to.
# If this ever fails on some platforms: use pytest.skip()
# If this ever fails on all platforms: don't know, seems extremely unlikely.
assert ptrdiff != 0
@pytest.mark.parametrize(
"vec_size_fn",
[
m.vec_size_base0_raw_ptr,
m.vec_size_base0_shared_ptr,
],
)
@pytest.mark.parametrize(
"get_fn",
[
m.get_drvd_as_base0_raw_ptr,
m.get_drvd_as_base0_shared_ptr,
m.get_drvd_as_base0_unique_ptr,
],
)
def test_get_vec_size_raw_shared(get_fn, vec_size_fn):
obj = get_fn()
assert vec_size_fn(obj) == 5
@pytest.mark.parametrize(
"get_fn", [m.get_drvd_as_base0_raw_ptr, m.get_drvd_as_base0_unique_ptr]
)
def test_get_vec_size_unique(get_fn):
obj = get_fn()
assert m.vec_size_base0_unique_ptr(obj) == 5
with pytest.raises(ValueError) as exc_info:
m.vec_size_base0_unique_ptr(obj)
assert (
str(exc_info.value)
== "Missing value for wrapped C++ type: Python instance was disowned."
)
def test_get_shared_vec_size_unique():
obj = m.get_drvd_as_base0_shared_ptr()
with pytest.raises(ValueError) as exc_info:
m.vec_size_base0_unique_ptr(obj)
assert (
str(exc_info.value)
== "Cannot disown external shared_ptr (loaded_as_unique_ptr)."
)