mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-13 17:13:53 +00:00
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
This commit is contained in:
parent
3cf4db18ed
commit
a923419e6d
69
tests/test_holder_shared_ptr.cpp
Normal file
69
tests/test_holder_shared_ptr.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// KEEP IN SYNC WITH test_holder_unique_ptr.cpp
|
||||||
|
|
||||||
|
#include "pybind11_tests.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace pybind11_tests {
|
||||||
|
namespace holder_shared_ptr {
|
||||||
|
|
||||||
|
inline void to_cout(std::string text) { std::cout << text << std::endl; }
|
||||||
|
|
||||||
|
class pointee { // NOT copyable.
|
||||||
|
public:
|
||||||
|
pointee() { to_cout("pointee::pointee()"); }
|
||||||
|
|
||||||
|
int get_int() const {
|
||||||
|
to_cout("pointee::get_int()");
|
||||||
|
return 213;
|
||||||
|
}
|
||||||
|
|
||||||
|
~pointee() { to_cout("~pointee()"); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
pointee(const pointee &) = delete;
|
||||||
|
pointee(pointee &&) = delete;
|
||||||
|
pointee &operator=(const pointee &) = delete;
|
||||||
|
pointee &operator=(pointee &&) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline std::unique_ptr<pointee> make_unique_pointee() {
|
||||||
|
return std::unique_ptr<pointee>(new pointee);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::shared_ptr<pointee> make_shared_pointee() {
|
||||||
|
return std::unique_ptr<pointee>(new pointee);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) {
|
||||||
|
return 4000 + ptr->get_int();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
|
||||||
|
return 5000 + ptr->get_int();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline pointee* get_static_pointee() {
|
||||||
|
static pointee cpp_instance;
|
||||||
|
return &cpp_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_SUBMODULE(holder_shared_ptr, m) {
|
||||||
|
m.def("to_cout", to_cout);
|
||||||
|
|
||||||
|
py::class_<pointee, std::shared_ptr<pointee>>(m, "pointee")
|
||||||
|
.def(py::init<>())
|
||||||
|
.def("get_int", &pointee::get_int);
|
||||||
|
|
||||||
|
m.def("make_unique_pointee", make_unique_pointee);
|
||||||
|
m.def("make_shared_pointee", make_shared_pointee);
|
||||||
|
// m.def("pass_unique_pointee", pass_unique_pointee);
|
||||||
|
m.def("pass_shared_pointee", pass_shared_pointee);
|
||||||
|
|
||||||
|
m.def("get_static_pointee",
|
||||||
|
get_static_pointee, py::return_value_policy::reference);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace holder_shared_ptr
|
||||||
|
} // namespace pybind11_tests
|
56
tests/test_holder_shared_ptr.py
Normal file
56
tests/test_holder_shared_ptr.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# KEEP IN SYNC WITH test_holder_unique_ptr.py
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from pybind11_tests import holder_shared_ptr as m
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_unique_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("make_unique_pointee")
|
||||||
|
obj = m.make_unique_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_shared_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("make_shared_pointee")
|
||||||
|
obj = m.make_shared_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_pass_unique_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("pass_unique_pointee")
|
||||||
|
obj = m.make_shared_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
i = m.pass_unique_pointee(obj)
|
||||||
|
assert i == 4213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_pass_shared_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("pass_shared_pointee")
|
||||||
|
obj = m.make_shared_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
i = m.pass_shared_pointee(obj)
|
||||||
|
assert i == 5213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_static_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("get_static_pointee")
|
||||||
|
obj = m.get_static_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
with pytest.raises(RuntimeError) as excinfo:
|
||||||
|
m.pass_shared_pointee(obj)
|
||||||
|
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
|
69
tests/test_holder_unique_ptr.cpp
Normal file
69
tests/test_holder_unique_ptr.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
// KEEP IN SYNC WITH test_holder_shared_ptr.cpp
|
||||||
|
|
||||||
|
#include "pybind11_tests.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace pybind11_tests {
|
||||||
|
namespace holder_unique_ptr {
|
||||||
|
|
||||||
|
inline void to_cout(std::string text) { std::cout << text << std::endl; }
|
||||||
|
|
||||||
|
class pointee { // NOT copyable.
|
||||||
|
public:
|
||||||
|
pointee() { to_cout("pointee::pointee()"); }
|
||||||
|
|
||||||
|
int get_int() const {
|
||||||
|
to_cout("pointee::get_int()");
|
||||||
|
return 213;
|
||||||
|
}
|
||||||
|
|
||||||
|
~pointee() { to_cout("~pointee()"); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
pointee(const pointee &) = delete;
|
||||||
|
pointee(pointee &&) = delete;
|
||||||
|
pointee &operator=(const pointee &) = delete;
|
||||||
|
pointee &operator=(pointee &&) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline std::unique_ptr<pointee> make_unique_pointee() {
|
||||||
|
return std::unique_ptr<pointee>(new pointee);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::shared_ptr<pointee> make_shared_pointee() {
|
||||||
|
return std::unique_ptr<pointee>(new pointee);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) {
|
||||||
|
return 4000 + ptr->get_int();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
|
||||||
|
return 5000 + ptr->get_int();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline pointee* get_static_pointee() {
|
||||||
|
static pointee cpp_instance;
|
||||||
|
return &cpp_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_SUBMODULE(holder_unique_ptr, m) {
|
||||||
|
m.def("to_cout", to_cout);
|
||||||
|
|
||||||
|
py::class_<pointee>(m, "pointee")
|
||||||
|
.def(py::init<>())
|
||||||
|
.def("get_int", &pointee::get_int);
|
||||||
|
|
||||||
|
m.def("make_unique_pointee", make_unique_pointee);
|
||||||
|
m.def("make_shared_pointee", make_shared_pointee);
|
||||||
|
// m.def("pass_unique_pointee", pass_unique_pointee);
|
||||||
|
m.def("pass_shared_pointee", pass_shared_pointee);
|
||||||
|
|
||||||
|
m.def("get_static_pointee",
|
||||||
|
get_static_pointee, py::return_value_policy::reference);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace holder_unique_ptr
|
||||||
|
} // namespace pybind11_tests
|
56
tests/test_holder_unique_ptr.py
Normal file
56
tests/test_holder_unique_ptr.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# KEEP IN SYNC WITH test_holder_shared_ptr.py
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from pybind11_tests import holder_unique_ptr as m
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_unique_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("make_unique_pointee")
|
||||||
|
obj = m.make_unique_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_make_shared_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("make_shared_pointee")
|
||||||
|
obj = m.make_shared_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_pass_unique_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("pass_unique_pointee")
|
||||||
|
obj = m.make_unique_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
i = m.pass_unique_pointee(obj)
|
||||||
|
assert i == 4213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_pass_shared_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("pass_shared_pointee")
|
||||||
|
obj = m.make_unique_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
i = m.pass_shared_pointee(obj)
|
||||||
|
assert i == 5213
|
||||||
|
m.to_cout("")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_static_pointee():
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("")
|
||||||
|
m.to_cout("get_static_pointee")
|
||||||
|
obj = m.get_static_pointee()
|
||||||
|
assert obj.get_int() == 213
|
||||||
|
with pytest.raises(RuntimeError) as excinfo:
|
||||||
|
m.pass_unique_pointee(obj)
|
||||||
|
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
|
Loading…
Reference in New Issue
Block a user