-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving tests from pybind11 PR #2672 here as-is.
- Loading branch information
Showing
12 changed files
with
700 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// pybind11 equivalent of Boost.Python test: | ||
// https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/cpp_base_py_derived_ext.cpp | ||
// See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point) | ||
|
||
#include "pybind11_tests.h" | ||
|
||
namespace pybind11_tests { | ||
namespace cpp_base_py_derived { | ||
|
||
struct base { | ||
base() : base_num(100) {} | ||
|
||
virtual int get_num() const { return base_num; } | ||
|
||
virtual std::shared_ptr<base> clone() const { | ||
return std::shared_ptr<base>(new base(150)); | ||
} | ||
|
||
virtual ~base() = default; | ||
|
||
private: | ||
explicit base(int num) : base_num(num) {} | ||
int base_num; | ||
}; | ||
|
||
inline int get_num(std::shared_ptr<base> b) { return b->get_num(); } | ||
|
||
inline int clone_get_num(std::shared_ptr<base> b) { | ||
std::shared_ptr<base> c = b->clone(); | ||
return (b->get_num() + 3) * 1000 + (c->get_num() + 7); | ||
} | ||
|
||
struct base_trampoline : public base { | ||
using base::base; | ||
|
||
int get_num() const override { | ||
PYBIND11_OVERRIDE(int, base, get_num); | ||
} | ||
|
||
std::shared_ptr<base> clone() const override { | ||
PYBIND11_OVERRIDE(std::shared_ptr<base>, base, clone); | ||
} | ||
}; | ||
|
||
TEST_SUBMODULE(cpp_base_py_derived, m) { | ||
py::class_<base, base_trampoline, std::shared_ptr<base>>(m, "base") | ||
.def(py::init<>()) | ||
.def("get_num", &base::get_num) | ||
.def("clone", &base::clone) | ||
; | ||
|
||
m.def("get_num", get_num); | ||
m.def("clone_get_num", clone_get_num); | ||
} | ||
|
||
} // namespace cpp_base_py_derived | ||
} // namespace pybind11_tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
# pybind11 equivalent of Boost.Python test: | ||
# https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/tst_cpp_base_py_derived.py | ||
# See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point) | ||
|
||
from pybind11_tests import cpp_base_py_derived as m | ||
|
||
|
||
class drvd(m.base): # noqa: N801 | ||
def __init__(self, _num=200): | ||
super().__init__() | ||
self._drvd_num = _num | ||
|
||
def get_num(self): | ||
return self._drvd_num | ||
|
||
def clone(self): | ||
return drvd(250) | ||
|
||
|
||
def test_base(): | ||
b = m.base() | ||
assert b.get_num() == 100 | ||
assert m.get_num(b) == 100 | ||
bc = b.clone() | ||
assert bc.get_num() == 150 | ||
assert m.clone_get_num(b) == 103157 | ||
|
||
|
||
def test_drvd(): | ||
d = drvd() | ||
assert d.get_num() == 200 | ||
assert m.get_num(d) == 200 | ||
dc = d.clone() | ||
assert dc.get_num() == 250 | ||
assert m.clone_get_num(d) == 203257 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Oops, something went wrong.