Skip to content

Commit

Permalink
Add derived class test for unique_ptr arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Jan 7, 2018
1 parent 75aa474 commit d1c1c2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/test_smart_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ TEST_SUBMODULE(smart_ptr, m) {
: value_(value) {
print_created(this, value);
}
~UniquePtrHeld() {
virtual ~UniquePtrHeld() {
print_destroyed(this);
}
int value() const { return value_; }
Expand Down Expand Up @@ -382,4 +382,22 @@ TEST_SUBMODULE(smart_ptr, m) {
m, "ContainerPlain");
Container<UniquePtrHeld, KeepAliveType::KeepAlive>::def(
m, "ContainerKeepAlive");

class UniquePtrDerived : public UniquePtrHeld {
public:
UniquePtrDerived(int value, std::string name)
: UniquePtrHeld(value), name_(name) {
print_created(this, name);
}
~UniquePtrDerived() {
print_destroyed(this);
}
std::string name() const { return name_; }
private:
std::string name_{};
};

py::class_<UniquePtrDerived, UniquePtrHeld>(m, "UniquePtrDerived")
.def(py::init<int, std::string>())
.def("name", &UniquePtrDerived::name);
}
11 changes: 11 additions & 0 deletions tests/test_smart_ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,14 @@ def test_unique_ptr_keep_alive():
del c_keep
pytest.gc_collect()
assert c_keep_stats.alive() == 0


def test_unique_ptr_derived():
obj = m.UniquePtrDerived(1, "a")
c_plain = m.ContainerPlain(obj)
del obj
pytest.gc_collect()
obj = c_plain.release()
assert obj.value() == 1
assert obj.name() == "a"
del obj

0 comments on commit d1c1c2a

Please sign in to comment.