Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numpy test failing due to new vpArray2d constructor #1382

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/core/include/visp3/core/vpArray2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,25 @@
}
resize(r, c, false, false);
}
else if ((c == 0) && (r == 0)) {
throw(vpException(vpException::dimensionError,
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Using rows = 0 and cols = 0 is ambiguous", r, c, vec.size()));
}
else if (c == 0) {
if (r != vec.size()) {
throw(vpException(vpException::dimensionError,

Check warning on line 231 in modules/core/include/visp3/core/vpArray2D.h

View check run for this annotation

Codecov / codecov/patch

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

Added line #L231 was not covered by tests
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
}
resize(static_cast<unsigned int>(vec.size()), 1, false, false);
}
else if (r == 0) {
if (c != vec.size()) {
throw(vpException(vpException::dimensionError,

Check warning on line 238 in modules/core/include/visp3/core/vpArray2D.h

View check run for this annotation

Codecov / codecov/patch

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

Added line #L238 was not covered by tests
"Cannot initialize vpArray(%d, %d) from std::vector(%d). Wrong dimension", r, c, vec.size()));
}
resize(1, static_cast<unsigned int>(vec.size()), false, false);
}

#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
std::copy(vec.begin(), vec.end(), data);
#else
Expand Down
14 changes: 4 additions & 10 deletions modules/core/test/math/testArray2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,8 @@ TEST_CASE("Test constructors with double", "[constructors]")
}
SECTION("Keep default size (r=0, c=0)")
{
vpArray2D<double> A(bench);
std::cout << "A with default size (r=0, c=0):\n" << A << std::endl;
CHECK(test("A", A, bench));
CHECK(A.getRows() == bench.size());
CHECK(A.getCols() == 1);
std::cout << "A with default size (r=0, c=0):\n" << std::endl;
REQUIRE_THROWS(vpArray2D<double>(bench));
}
SECTION("Keep row size to 0")
{
Expand Down Expand Up @@ -221,11 +218,8 @@ TEST_CASE("Test constructors with float", "[constructors]")
}
SECTION("Keep default size (r=0, c=0)")
{
vpArray2D<float> A(bench);
std::cout << "A with default size (r=0, c=0):\n" << A << std::endl;
CHECK(test("A", A, bench));
CHECK(A.getRows() == bench.size());
CHECK(A.getCols() == 1);
std::cout << "A with default size (r=0, c=0):\n" << std::endl;
REQUIRE_THROWS(vpArray2D<float>(bench));
}
SECTION("Keep row size to 0")
{
Expand Down
9 changes: 8 additions & 1 deletion modules/python/test/test_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ def test_numpy_constructor():
a = ArrayDouble2D(n_invalid)
n_valid = np.array([[1, 2, 3], [4, 5, 6]])
a = ArrayDouble2D(n_valid)

assert np.all(np.equal(a.numpy(), n_valid))

def test_numpy_constructor_interpreted_as_1d_vector():
n_1d = np.array([1, 2, 3])
with pytest.raises(RuntimeError):
a = ArrayDouble2D(n_1d) # R = 0, c = 0
ar = ArrayDouble2D(n_1d, r=len(n_1d))
ac = ArrayDouble2D(n_1d, c=len(n_1d))


def test_numpy_conversion_and_back():
a = ArrayDouble2D(10, 10, 2.0)
a_np = a.numpy().copy()
Expand Down
Loading