|
1 | 1 | import numpy as np
|
2 | 2 | from tests import test_module as tm
|
| 3 | +from tests.utils import generate_matrix, check_matrix_content |
3 | 4 |
|
4 | 5 |
|
5 | 6 | def test_return_by_value():
|
6 | 7 | a = tm.ClassForReturn()
|
7 |
| - assert(id(a.returnByValue()) != id(a.returnByValue())) |
8 | 8 | mat = a.returnByValue()
|
9 | 9 | 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)) |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def test_return_by_ref_but_copy():
|
14 | 16 | a = tm.ClassForReturn()
|
15 |
| - assert(id(a.returnByRef()) != id(a.returnByRef())) |
16 | 17 | mat = a.returnByRef()
|
17 | 18 | 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)) |
19 | 22 |
|
20 | 23 |
|
21 | 24 | def test_return_by_ref():
|
22 | 25 | a = tm.ClassForReturn()
|
23 |
| - assert(id(a.returnByRef()) != id(a.returnByRef())) |
24 | 26 | mat = a.viewMatrix()
|
25 | 27 | 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)) |
27 | 32 |
|
28 | 33 |
|
29 | 34 | def test_return_by_pointer():
|
30 | 35 | a = tm.ClassForReturn()
|
31 |
| - assert(id(a.returnByPointer()) != id(a.returnByPointer())) |
32 | 36 | mat = a.returnByPointer()
|
33 | 37 | 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)) |
35 | 51 |
|
36 | 52 |
|
37 | 53 | 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)) |
39 | 58 |
|
40 |
| -def test_return_by_argument_by_value(): |
41 |
| - pass |
42 | 59 |
|
43 | 60 | 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