Skip to content

Commit b63f0c6

Browse files
committed
Copying tests as-is from xxx_value_ptr_xxx_holder branch.
https://github.com/rwgk/pybind11/tree/xxx_value_ptr_xxx_holder Systematically exercising returning and passing unique_ptr<T>, shared_ptr<T> with unique_ptr, shared_ptr holder. Observations: test_holder_unique_ptr: make_unique_pointee OK pass_unique_pointee BUILD_FAIL (as documented) make_shared_pointee Abort free(): double free detected pass_shared_pointee RuntimeError: Unable to load a custom holder type from a default-holder instance test_holder_shared_ptr: make_unique_pointee Segmentation fault (#1138) pass_unique_pointee BUILD_FAIL (as documented) make_shared_pointee OK pass_shared_pointee OK
1 parent fbe2e21 commit b63f0c6

4 files changed

+250
-0
lines changed

tests/test_holder_shared_ptr.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// KEEP IN SYNC WITH test_holder_unique_ptr.cpp
2+
3+
#include "pybind11_tests.h"
4+
5+
#include <iostream>
6+
#include <memory>
7+
8+
namespace pybind11_tests {
9+
namespace holder_shared_ptr {
10+
11+
inline void to_cout(std::string text) { std::cout << text << std::endl; }
12+
13+
class pointee { // NOT copyable.
14+
public:
15+
pointee() { to_cout("pointee::pointee()"); }
16+
17+
int get_int() const {
18+
to_cout("pointee::get_int()");
19+
return 213;
20+
}
21+
22+
~pointee() { to_cout("~pointee()"); }
23+
24+
private:
25+
pointee(const pointee &) = delete;
26+
pointee(pointee &&) = delete;
27+
pointee &operator=(const pointee &) = delete;
28+
pointee &operator=(pointee &&) = delete;
29+
};
30+
31+
inline std::unique_ptr<pointee> make_unique_pointee() {
32+
return std::unique_ptr<pointee>(new pointee);
33+
}
34+
35+
inline std::shared_ptr<pointee> make_shared_pointee() {
36+
return std::unique_ptr<pointee>(new pointee);
37+
}
38+
39+
inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) {
40+
return 4000 + ptr->get_int();
41+
}
42+
43+
inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
44+
return 5000 + ptr->get_int();
45+
}
46+
47+
inline pointee* get_static_pointee() {
48+
static pointee cpp_instance;
49+
return &cpp_instance;
50+
}
51+
52+
TEST_SUBMODULE(holder_shared_ptr, m) {
53+
m.def("to_cout", to_cout);
54+
55+
py::class_<pointee, std::shared_ptr<pointee>>(m, "pointee")
56+
.def(py::init<>())
57+
.def("get_int", &pointee::get_int);
58+
59+
m.def("make_unique_pointee", make_unique_pointee);
60+
m.def("make_shared_pointee", make_shared_pointee);
61+
// m.def("pass_unique_pointee", pass_unique_pointee);
62+
m.def("pass_shared_pointee", pass_shared_pointee);
63+
64+
m.def("get_static_pointee",
65+
get_static_pointee, py::return_value_policy::reference);
66+
}
67+
68+
} // namespace holder_shared_ptr
69+
} // namespace pybind11_tests

tests/test_holder_shared_ptr.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
# KEEP IN SYNC WITH test_holder_unique_ptr.py
3+
import pytest
4+
5+
from pybind11_tests import holder_shared_ptr as m
6+
7+
8+
def test_make_unique_pointee():
9+
m.to_cout("")
10+
m.to_cout("")
11+
m.to_cout("make_unique_pointee")
12+
obj = m.make_unique_pointee()
13+
assert obj.get_int() == 213
14+
m.to_cout("")
15+
16+
17+
def test_make_shared_pointee():
18+
m.to_cout("")
19+
m.to_cout("")
20+
m.to_cout("make_shared_pointee")
21+
obj = m.make_shared_pointee()
22+
assert obj.get_int() == 213
23+
m.to_cout("")
24+
25+
26+
def test_pass_unique_pointee():
27+
m.to_cout("")
28+
m.to_cout("")
29+
m.to_cout("pass_unique_pointee")
30+
obj = m.make_shared_pointee()
31+
assert obj.get_int() == 213
32+
i = m.pass_unique_pointee(obj)
33+
assert i == 4213
34+
m.to_cout("")
35+
36+
37+
def test_pass_shared_pointee():
38+
m.to_cout("")
39+
m.to_cout("")
40+
m.to_cout("pass_shared_pointee")
41+
obj = m.make_shared_pointee()
42+
assert obj.get_int() == 213
43+
i = m.pass_shared_pointee(obj)
44+
assert i == 5213
45+
m.to_cout("")
46+
47+
48+
def test_get_static_pointee():
49+
m.to_cout("")
50+
m.to_cout("")
51+
m.to_cout("get_static_pointee")
52+
obj = m.get_static_pointee()
53+
assert obj.get_int() == 213
54+
with pytest.raises(RuntimeError) as excinfo:
55+
m.pass_shared_pointee(obj)
56+
assert "Unable to cast from non-held to held instance" in str(excinfo.value)

tests/test_holder_unique_ptr.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// KEEP IN SYNC WITH test_holder_shared_ptr.cpp
2+
3+
#include "pybind11_tests.h"
4+
5+
#include <iostream>
6+
#include <memory>
7+
8+
namespace pybind11_tests {
9+
namespace holder_unique_ptr {
10+
11+
inline void to_cout(std::string text) { std::cout << text << std::endl; }
12+
13+
class pointee { // NOT copyable.
14+
public:
15+
pointee() { to_cout("pointee::pointee()"); }
16+
17+
int get_int() const {
18+
to_cout("pointee::get_int()");
19+
return 213;
20+
}
21+
22+
~pointee() { to_cout("~pointee()"); }
23+
24+
private:
25+
pointee(const pointee &) = delete;
26+
pointee(pointee &&) = delete;
27+
pointee &operator=(const pointee &) = delete;
28+
pointee &operator=(pointee &&) = delete;
29+
};
30+
31+
inline std::unique_ptr<pointee> make_unique_pointee() {
32+
return std::unique_ptr<pointee>(new pointee);
33+
}
34+
35+
inline std::shared_ptr<pointee> make_shared_pointee() {
36+
return std::unique_ptr<pointee>(new pointee);
37+
}
38+
39+
inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) {
40+
return 4000 + ptr->get_int();
41+
}
42+
43+
inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
44+
return 5000 + ptr->get_int();
45+
}
46+
47+
inline pointee* get_static_pointee() {
48+
static pointee cpp_instance;
49+
return &cpp_instance;
50+
}
51+
52+
TEST_SUBMODULE(holder_unique_ptr, m) {
53+
m.def("to_cout", to_cout);
54+
55+
py::class_<pointee>(m, "pointee")
56+
.def(py::init<>())
57+
.def("get_int", &pointee::get_int);
58+
59+
m.def("make_unique_pointee", make_unique_pointee);
60+
m.def("make_shared_pointee", make_shared_pointee);
61+
// m.def("pass_unique_pointee", pass_unique_pointee);
62+
m.def("pass_shared_pointee", pass_shared_pointee);
63+
64+
m.def("get_static_pointee",
65+
get_static_pointee, py::return_value_policy::reference);
66+
}
67+
68+
} // namespace holder_unique_ptr
69+
} // namespace pybind11_tests

tests/test_holder_unique_ptr.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
# KEEP IN SYNC WITH test_holder_shared_ptr.py
3+
import pytest
4+
5+
from pybind11_tests import holder_unique_ptr as m
6+
7+
8+
def test_make_unique_pointee():
9+
m.to_cout("")
10+
m.to_cout("")
11+
m.to_cout("make_unique_pointee")
12+
obj = m.make_unique_pointee()
13+
assert obj.get_int() == 213
14+
m.to_cout("")
15+
16+
17+
def test_make_shared_pointee():
18+
m.to_cout("")
19+
m.to_cout("")
20+
m.to_cout("make_shared_pointee")
21+
obj = m.make_shared_pointee()
22+
assert obj.get_int() == 213
23+
m.to_cout("")
24+
25+
26+
def test_pass_unique_pointee():
27+
m.to_cout("")
28+
m.to_cout("")
29+
m.to_cout("pass_unique_pointee")
30+
obj = m.make_unique_pointee()
31+
assert obj.get_int() == 213
32+
i = m.pass_unique_pointee(obj)
33+
assert i == 4213
34+
m.to_cout("")
35+
36+
37+
def test_pass_shared_pointee():
38+
m.to_cout("")
39+
m.to_cout("")
40+
m.to_cout("pass_shared_pointee")
41+
obj = m.make_unique_pointee()
42+
assert obj.get_int() == 213
43+
i = m.pass_shared_pointee(obj)
44+
assert i == 5213
45+
m.to_cout("")
46+
47+
48+
def test_get_static_pointee():
49+
m.to_cout("")
50+
m.to_cout("")
51+
m.to_cout("get_static_pointee")
52+
obj = m.get_static_pointee()
53+
assert obj.get_int() == 213
54+
with pytest.raises(RuntimeError) as excinfo:
55+
m.pass_unique_pointee(obj)
56+
assert "Unable to cast from non-held to held instance" in str(excinfo.value)

0 commit comments

Comments
 (0)