Skip to content

Commit 966c333

Browse files
Add derived class test for unique_ptr arguments.
1 parent a2ac6da commit 966c333

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

tests/test_smart_ptr.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ TEST_SUBMODULE(smart_ptr, m) {
323323
: value_(value) {
324324
print_created(this, value);
325325
}
326-
~UniquePtrHeld() {
326+
virtual ~UniquePtrHeld() {
327327
print_destroyed(this);
328328
}
329329
int value() const { return value_; }
@@ -382,4 +382,22 @@ TEST_SUBMODULE(smart_ptr, m) {
382382
m, "ContainerPlain");
383383
Container<UniquePtrHeld, KeepAliveType::KeepAlive>::def(
384384
m, "ContainerKeepAlive");
385+
386+
class UniquePtrDerived : public UniquePtrHeld {
387+
public:
388+
UniquePtrDerived(int value, std::string name)
389+
: UniquePtrHeld(value), name_(name) {
390+
print_created(this, name);
391+
}
392+
~UniquePtrDerived() {
393+
print_destroyed(this);
394+
}
395+
std::string name() const { return name_; }
396+
private:
397+
std::string name_{};
398+
};
399+
400+
py::class_<UniquePtrDerived, UniquePtrHeld>(m, "UniquePtrDerived")
401+
.def(py::init<int, std::string>())
402+
.def("name", &UniquePtrDerived::name);
385403
}

tests/test_smart_ptr.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,14 @@ def test_unique_ptr_keep_alive():
314314
del c_keep
315315
pytest.gc_collect()
316316
assert c_keep_stats.alive() == 0
317+
318+
319+
def test_unique_ptr_derived():
320+
obj = m.UniquePtrDerived(1, "a")
321+
c_plain = m.ContainerPlain(obj)
322+
del obj
323+
pytest.gc_collect()
324+
obj = c_plain.release()
325+
assert obj.value() == 1
326+
assert obj.name() == "a"
327+
del obj

0 commit comments

Comments
 (0)