Skip to content

Commit dddbf6d

Browse files
committed
test/ref: enforce testing of vector blocks
1 parent 7235c40 commit dddbf6d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

unittest/eigen_ref.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ void setOnes(Eigen::Ref<MatType> mat) {
3030
mat.setOnes();
3131
}
3232

33+
template <typename VecType>
34+
VecType copyVectorFromConstRef(const Eigen::Ref<const VecType> vec) {
35+
std::cout << "copyVectorFromConstRef::vec: " << vec.transpose() << std::endl;
36+
return VecType(vec);
37+
}
38+
3339
template <typename MatType>
3440
Eigen::Ref<MatType> getBlock(Eigen::Ref<MatType> mat, Eigen::DenseIndex i,
3541
Eigen::DenseIndex j, Eigen::DenseIndex n,
@@ -120,6 +126,9 @@ BOOST_PYTHON_MODULE(eigen_ref) {
120126
bp::def("getBlock", &getBlock<MatrixXd>);
121127
bp::def("editBlock", &editBlock<MatrixXd>);
122128

129+
bp::def("copyVectorFromConstRef", &copyVectorFromConstRef<VectorXd>);
130+
bp::def("copyRowVectorFromConstRef", &copyVectorFromConstRef<RowVectorXd>);
131+
123132
bp::class_<modify_block_wrap, boost::noncopyable>("modify_block",
124133
bp::init<>())
125134
.def_readonly("J", &modify_block::J)

unittest/python/test_eigen_ref.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
editBlock,
1010
modify_block,
1111
has_ref_member,
12+
copyVectorFromConstRef,
13+
copyRowVectorFromConstRef,
1214
)
1315

1416

@@ -44,6 +46,18 @@ def test_create_ref_to_static(mat):
4446
assert np.array_equal(A_ref, A_ref2)
4547

4648

49+
def test_read_block():
50+
data = np.array([[0, 0.2, 0.3, 0.4], [0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0]])
51+
52+
data_strided = data[:, 0]
53+
54+
data_strided_copy = copyVectorFromConstRef(data_strided)
55+
assert np.all(data_strided == data_strided_copy)
56+
57+
data_strided_copy = copyRowVectorFromConstRef(data_strided)
58+
assert np.all(data_strided == data_strided_copy)
59+
60+
4761
def test_create_ref(mat):
4862
print("[asRef(mat)]")
4963
ref = asRef(mat)
@@ -116,6 +130,7 @@ def do_test(mat):
116130
test_create_const_ref(mat)
117131
test_create_ref(mat)
118132
test_edit_block(rows, cols)
133+
test_read_block()
119134
print("=" * 10)
120135

121136

0 commit comments

Comments
 (0)