Skip to content

Commit 73bf2aa

Browse files
author
Trevor Laughlin
committed
Add test for unique_ptr<T, D> change
1 parent 6c9e06a commit 73bf2aa

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

tests/test_smart_ptr.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ TEST_SUBMODULE(smart_ptr, m) {
157157
.def(py::init<int>())
158158
.def_readwrite("value", &MyObject4::value);
159159

160+
// test_unique_deleter
161+
// Object with std::unique_ptr<T, D> where D is not matching the base class
162+
// Object with a protected destructor
163+
class MyObject4a {
164+
public:
165+
MyObject4a(int i) {
166+
value = i;
167+
print_created(this);
168+
};
169+
int value;
170+
protected:
171+
virtual ~MyObject4a() { print_destroyed(this); }
172+
};
173+
py::class_<MyObject4a, std::unique_ptr<MyObject4a, py::nodelete>>(m, "MyObject4a")
174+
.def(py::init<int>())
175+
.def_readwrite("value", &MyObject4a::value);
176+
177+
// Object derived but with public destructor and no Deleter in default holder
178+
class MyObject4b : public MyObject4a {
179+
public:
180+
MyObject4b(int i) : MyObject4a(i) { print_created(this); }
181+
~MyObject4b() { print_destroyed(this); }
182+
};
183+
py::class_<MyObject4b, MyObject4a>(m, "MyObject4b")
184+
.def(py::init<int>());
185+
160186
// test_large_holder
161187
class MyObject5 { // managed by huge_unique_ptr
162188
public:

tests/test_smart_ptr.py

+21
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ def test_unique_nodelete():
115115
assert cstats.alive() == 1 # Leak, but that's intentional
116116

117117

118+
def test_unique_nodelete4a():
119+
o = m.MyObject4a(23)
120+
assert o.value == 23
121+
cstats = ConstructorStats.get(m.MyObject4a)
122+
assert cstats.alive() == 1
123+
del o
124+
assert cstats.alive() == 1 # Leak, but that's intentional
125+
126+
127+
def test_unique_deleter():
128+
o = m.MyObject4b(23)
129+
assert o.value == 23
130+
cstats4a = ConstructorStats.get(m.MyObject4a)
131+
assert cstats4a.alive() == 2 # Two becaue of previous test
132+
cstats4b = ConstructorStats.get(m.MyObject4b)
133+
assert cstats4b.alive() == 1
134+
del o
135+
assert cstats4a.alive() == 1 # Should now only be one leftover from previous test
136+
assert cstats4b.alive() == 0 # Should be deleted
137+
138+
118139
def test_large_holder():
119140
o = m.MyObject5(5)
120141
assert o.value == 5

0 commit comments

Comments
 (0)