Skip to content

Commit 351af1d

Browse files
committed
Fix numpy test failing due to new vpArray2d constructor
1 parent 80581ff commit 351af1d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

modules/core/include/visp3/core/vpArray2D.h

+12
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,23 @@ template <class Type> class vpArray2D
223223
resize(r, c, false, false);
224224
}
225225
else if (c == 0) {
226+
if (r != vec.size()) {
227+
throw(vpException(vpException::dimensionError,
228+
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
229+
}
226230
resize(static_cast<unsigned int>(vec.size()), 1, false, false);
227231
}
228232
else if (r == 0) {
233+
if (c != vec.size()) {
234+
throw(vpException(vpException::dimensionError,
235+
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
236+
}
229237
resize(1, static_cast<unsigned int>(vec.size()), false, false);
230238
}
239+
else {
240+
throw(vpException(vpException::dimensionError,
241+
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
242+
}
231243
#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
232244
std::copy(vec.begin(), vec.end(), data);
233245
#else

modules/python/test/test_numpy_array.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,17 @@ def test_numpy_constructor():
7676
a = ArrayDouble2D(n_invalid)
7777
n_valid = np.array([[1, 2, 3], [4, 5, 6]])
7878
a = ArrayDouble2D(n_valid)
79-
8079
assert np.all(np.equal(a.numpy(), n_valid))
8180

81+
def test_numpy_constructor_interpreted_as_1d_vector():
82+
n_1d = np.array([1, 2, 3])
83+
with pytest.raises(RuntimeError):
84+
a = ArrayDouble2D(n_1d) # R = 0, c = 0
85+
ar = ArrayDouble2D(n_1d, r=len(n_1d))
86+
ac = ArrayDouble2D(n_1d, c=len(n_1d))
87+
88+
89+
8290
def test_numpy_conversion_and_back():
8391
a = ArrayDouble2D(10, 10, 2.0)
8492
a_np = a.numpy().copy()

0 commit comments

Comments
 (0)