Skip to content

Commit 1d2a116

Browse files
authored
Merge pull request #22 from edmBernard/dev/add-test-argument
Dev/add test argument
2 parents c68f1e8 + 4c1c4f9 commit 1d2a116

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

tests/cpp/test.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ cv::Mat cloneimg(cv::Mat image) {
145145
class ClassForReturn {
146146
public:
147147
ClassForReturn() {
148-
m_image = cv::Mat(2, 2, CV_8UC3, cv::Scalar(1,2,3));
148+
m_image = generateMatrix();
149149
}
150150

151151
cv::Mat& returnByRef() { return m_image; };
@@ -160,13 +160,25 @@ class ClassForReturn {
160160
void returnInArgumentByPointer(cv::Mat* image) {};
161161

162162
void changeInternal() {
163-
m_image.at<cv::Vec3b>(0, 0) = cv::Vec3b(4,5,6);
163+
m_image.at<cv::Vec3w>(0, 0) = cv::Vec3w(4,5,6);
164164
}
165165

166166
private:
167167
cv::Mat m_image;
168168
};
169169

170+
void returnByArgumentValue(cv::Mat mat) {
171+
mat.at<cv::Vec3w>(0, 0) = cv::Vec3w(4,5,6);
172+
}
173+
174+
void returnByArgumentRef(cv::Mat & mat) {
175+
mat.at<cv::Vec3w>(0, 0) = cv::Vec3w(4,5,6);
176+
}
177+
178+
void returnByArgumentPointer(cv::Mat *mat) {
179+
mat->at<cv::Vec3w>(0, 0) = cv::Vec3w(4,5,6);
180+
}
181+
170182
PYBIND11_MODULE(test_module, m) {
171183

172184
NDArrayConverter::init_numpy();
@@ -209,4 +221,11 @@ PYBIND11_MODULE(test_module, m) {
209221
.def("returnInArgumentByRef", &ClassForReturn::returnInArgumentByRef)
210222
.def("returnInArgumentByPointer", &ClassForReturn::returnInArgumentByPointer)
211223
;
224+
225+
m.def("returnByArgumentValue", &returnByArgumentValue, py::return_value_policy::copy);
226+
227+
m.def("returnByArgumentRef", &returnByArgumentRef);
228+
229+
m.def("returnByArgumentPointer", &returnByArgumentPointer);
230+
212231
}

tests/test_return.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
11
import numpy as np
22
from tests import test_module as tm
3+
from tests.utils import generate_matrix, check_matrix_content
34

45

56
def test_return_by_value():
67
a = tm.ClassForReturn()
7-
assert(id(a.returnByValue()) != id(a.returnByValue()))
88
mat = a.returnByValue()
99
a.changeInternal()
10-
assert(np.any(mat != a.returnByValue()))
10+
assert(check_matrix_content(mat))
11+
changed_mat = a.returnByValue()
12+
assert(not check_matrix_content(changed_mat))
1113

1214

1315
def test_return_by_ref_but_copy():
1416
a = tm.ClassForReturn()
15-
assert(id(a.returnByRef()) != id(a.returnByRef()))
1617
mat = a.returnByRef()
1718
a.changeInternal()
18-
assert(np.any(mat != a.returnByValue()))
19+
assert(check_matrix_content(mat))
20+
changed_mat = a.returnByValue()
21+
assert(not check_matrix_content(changed_mat))
1922

2023

2124
def test_return_by_ref():
2225
a = tm.ClassForReturn()
23-
assert(id(a.returnByRef()) != id(a.returnByRef()))
2426
mat = a.viewMatrix()
2527
a.changeInternal()
26-
assert(np.all(mat == a.returnByValue()))
28+
# Currently return parameter perform a copy of the data, so the change is not repercuted to mat
29+
assert(check_matrix_content(mat))
30+
changed_mat = a.returnByValue()
31+
assert(not check_matrix_content(changed_mat))
2732

2833

2934
def test_return_by_pointer():
3035
a = tm.ClassForReturn()
31-
assert(id(a.returnByPointer()) != id(a.returnByPointer()))
3236
mat = a.returnByPointer()
3337
a.changeInternal()
34-
assert(np.all(mat == a.returnByValue()))
38+
# Currently return parameter perform a copy of the data, so the change is not repercuted to mat
39+
assert(check_matrix_content(mat))
40+
changed_mat = a.returnByValue()
41+
assert(not check_matrix_content(changed_mat))
42+
43+
44+
def test_return_by_argument_by_value():
45+
mat = generate_matrix()
46+
tm.returnByArgumentValue(mat)
47+
48+
# Not intuitive Argument passed by value can be changed by cpp
49+
# because the cast between from numpy.drarray to cv::Mat avoid copy by passing buffer
50+
assert(not check_matrix_content(mat))
3551

3652

3753
def test_return_by_argument_by_ref():
38-
pass
54+
mat = generate_matrix()
55+
tm.returnByArgumentRef(mat)
56+
assert(np.any(mat != generate_matrix()))
57+
assert(not check_matrix_content(mat))
3958

40-
def test_return_by_argument_by_value():
41-
pass
4259

4360
def test_return_by_argument_by_pointer():
44-
pass
61+
mat = generate_matrix()
62+
tm.returnByArgumentPointer(mat)
63+
assert(not check_matrix_content(mat))

0 commit comments

Comments
 (0)