Skip to content

Commit 1302103

Browse files
committed
feat: add test for return policy
1 parent 7af4463 commit 1302103

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

tests/cpp/test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class ClassForReturn {
149149
}
150150

151151
cv::Mat& returnByRef() { return m_image; };
152+
const cv::Mat& viewMatrix() { return m_image; };
152153

153154
cv::Mat* returnByPointer() { return &m_image; };
154155

@@ -200,9 +201,11 @@ PYBIND11_MODULE(test_module, m) {
200201
py::class_<ClassForReturn>(m, "ClassForReturn")
201202
.def(py::init<>())
202203
.def("changeInternal", &ClassForReturn::changeInternal)
203-
.def("returnByRef", &ClassForReturn::returnByRef)
204-
.def("returnByPointer", &ClassForReturn::returnByPointer)
205204
.def("returnByValue", &ClassForReturn::returnByValue)
205+
.def("returnByRefButCopy", &ClassForReturn::returnByRef)
206+
.def("returnByRef", &ClassForReturn::returnByRef, py::return_value_policy::reference_internal)
207+
.def("viewMatrix", &ClassForReturn::returnByRef, py::return_value_policy::reference_internal)
208+
.def("returnByPointer", &ClassForReturn::returnByPointer, py::return_value_policy::reference_internal)
206209
.def("returnInArgumentByRef", &ClassForReturn::returnInArgumentByRef)
207210
.def("returnInArgumentByPointer", &ClassForReturn::returnInArgumentByPointer)
208211
;

tests/test_return.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22
from tests import test_module as tm
33

44

5-
# def test_return_by_ref():
6-
# a = tm.ClassForReturn()
7-
# assert(id(a.returnByRef()) == id(a.returnByRef()))
5+
def test_return_by_value():
6+
a = tm.ClassForReturn()
7+
assert(id(a.returnByValue()) != id(a.returnByValue()))
8+
mat = a.returnByValue()
9+
a.changeInternal()
10+
assert(np.any(mat != a.returnByValue()))
811

9-
# def test_return_by_value():
10-
# a = tm.ClassForReturn()
11-
# assert(id(a.returnByValue()) != id(a.returnByValue()))
1212

13-
# def test_return_by_pointer():
14-
# a = tm.ClassForReturn()
15-
# assert(id(a.returnByPointer()) == id(a.returnByPointer()))
13+
def test_return_by_ref_but_copy():
14+
a = tm.ClassForReturn()
15+
assert(id(a.returnByRef()) != id(a.returnByRef()))
16+
mat = a.returnByRef()
17+
a.changeInternal()
18+
assert(np.any(mat != a.returnByValue()))
1619

17-
# def test_return_by_argument_by_ref():
18-
# pass
1920

20-
# def test_return_by_argument_by_value():
21-
# pass
21+
def test_return_by_ref():
22+
a = tm.ClassForReturn()
23+
assert(id(a.returnByRef()) != id(a.returnByRef()))
24+
mat = a.viewMatrix()
25+
a.changeInternal()
26+
assert(np.all(mat == a.returnByValue()))
2227

23-
# def test_return_by_argument_by_pointer():
24-
# pass
28+
29+
def test_return_by_pointer():
30+
a = tm.ClassForReturn()
31+
assert(id(a.returnByPointer()) != id(a.returnByPointer()))
32+
mat = a.returnByPointer()
33+
a.changeInternal()
34+
assert(np.all(mat == a.returnByValue()))
35+
36+
37+
def test_return_by_argument_by_ref():
38+
pass
39+
40+
def test_return_by_argument_by_value():
41+
pass
42+
43+
def test_return_by_argument_by_pointer():
44+
pass

0 commit comments

Comments
 (0)