Skip to content

Commit 9e60650

Browse files
committed
test: add test for Eigen::Ref
1 parent 1fe6a61 commit 9e60650

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ADD_PYTHON_UNIT_TEST("py-matrix" "unittest/python/test_matrix.py" "unittest")
4242
ADD_PYTHON_UNIT_TEST("py-geometry" "unittest/python/test_geometry.py" "unittest")
4343
ADD_PYTHON_UNIT_TEST("py-complex" "unittest/python/test_complex.py" "unittest")
4444
ADD_PYTHON_UNIT_TEST("py-return-by-ref" "unittest/python/test_return_by_ref.py" "unittest")
45+
ADD_PYTHON_UNIT_TEST("py-eigen-ref" "unittest/python/test_eigen_ref.py" "unittest")
4546

4647
ADD_PYTHON_UNIT_TEST("py-switch" "unittest/python/test_switch.py" "python/eigenpy")
4748
SET_TESTS_PROPERTIES("py-switch" PROPERTIES DEPENDS ${PYWRAP})

unittest/eigen_ref.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace Eigen;
1010
using namespace eigenpy;
1111

1212
template<typename MatType>
13-
void printMatrix(const Eigen::Ref<MatType> & mat)
13+
void printMatrix(const Eigen::Ref<const MatType> mat)
1414
{
1515
if(MatType::IsVectorAtCompileTime)
1616
std::cout << "isVector" << std::endl;
@@ -19,7 +19,7 @@ void printMatrix(const Eigen::Ref<MatType> & mat)
1919
}
2020

2121
template<typename VecType>
22-
void printVector(const Eigen::Ref<VecType> & vec)
22+
void printVector(const Eigen::Ref<const VecType> & vec)
2323
{
2424
EIGEN_STATIC_ASSERT_VECTOR_ONLY(VecType);
2525
printMatrix(vec);
@@ -28,9 +28,13 @@ void printVector(const Eigen::Ref<VecType> & vec)
2828
template<typename MatType>
2929
void setOnes(Eigen::Ref<MatType> mat)
3030
{
31-
printMatrix(mat);
3231
mat.setOnes();
33-
printMatrix(mat);
32+
}
33+
34+
template<typename MatType>
35+
void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar & value)
36+
{
37+
mat.fill(value);
3438
}
3539

3640
BOOST_PYTHON_MODULE(eigen_ref)
@@ -48,4 +52,8 @@ BOOST_PYTHON_MODULE(eigen_ref)
4852
bp::def("setOnes", setOnes<Vector3d>);
4953
bp::def("setOnes", setOnes<VectorXd>);
5054
bp::def("setOnes", setOnes<MatrixXd>);
55+
56+
bp::def("fillVec3", fill<Vector3d>);
57+
bp::def("fillVec", fill<VectorXd>);
58+
bp::def("fill", fill<MatrixXd>);
5159
}

unittest/python/test_eigen_ref.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
from eigen_ref import *
3+
4+
def test(mat):
5+
6+
printMatrix(mat)
7+
fill(mat,1.)
8+
printMatrix(mat)
9+
assert np.array_equal(mat,np.full(mat.shape,1.))
10+
11+
rows = 10
12+
cols = 30
13+
14+
mat = np.array(np.zeros((rows,cols)))
15+
16+
test(mat)

0 commit comments

Comments
 (0)