From 3a488536270e02e61e0a71356948b33d4a614f91 Mon Sep 17 00:00:00 2001 From: Stefano Date: Fri, 20 Dec 2024 10:55:16 +0100 Subject: [PATCH] Fixed use of const when converting from a const multidimensional array to a eigen matrix. Added test --- include/matioCpp/EigenConversions.h | 2 +- include/matioCpp/impl/EigenConversions.tpp | 2 +- test/ExogenousConversionsUnitTest.cpp | 8 ++++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/matioCpp/EigenConversions.h b/include/matioCpp/EigenConversions.h index 1a02919..3276161 100644 --- a/include/matioCpp/EigenConversions.h +++ b/include/matioCpp/EigenConversions.h @@ -40,7 +40,7 @@ inline Eigen::Map> to_eigen( * @return A const map from the internal data of the MultiDimensionalArray */ template -inline const Eigen::Map> to_eigen(const MultiDimensionalArray& input); +inline Eigen::Map> to_eigen(const MultiDimensionalArray& input); /** * @brief Conversion from a MultiDimensionalArray to an Eigen matrix diff --git a/include/matioCpp/impl/EigenConversions.tpp b/include/matioCpp/impl/EigenConversions.tpp index a82ec20..9c8e3ea 100644 --- a/include/matioCpp/impl/EigenConversions.tpp +++ b/include/matioCpp/impl/EigenConversions.tpp @@ -102,7 +102,7 @@ inline Eigen::Map> matioCpp: } template -inline const Eigen::Map> matioCpp::to_eigen(const matioCpp::MultiDimensionalArray& input) +inline Eigen::Map> matioCpp::to_eigen(const matioCpp::MultiDimensionalArray& input) { assert(input.isValid()); assert(input.dimensions().size() == 2); diff --git a/test/ExogenousConversionsUnitTest.cpp b/test/ExogenousConversionsUnitTest.cpp index 8b3ee28..209af17 100644 --- a/test/ExogenousConversionsUnitTest.cpp +++ b/test/ExogenousConversionsUnitTest.cpp @@ -86,6 +86,10 @@ TEST_CASE("Eigen Conversions") Eigen::MatrixXf toEigenMatrix = matioCpp::to_eigen(matioCppMatrix); checkSameMatrix(toEigenMatrix, matioCppMatrix); + const auto& constMatioCppMatrix = matioCppMatrix; + Eigen::MatrixXf toEigenMatrixConst = matioCpp::to_eigen(constMatioCppMatrix); + checkSameMatrix(toEigenMatrixConst, constMatioCppMatrix); + std::vector tensor(12); for (size_t i = 0; i < 12; ++i) { @@ -125,11 +129,11 @@ TEST_CASE("Eigen Conversions") expectedSlice << 8.0; expectedSlice.isApprox(matioCpp::to_eigen(matioCppMatrix2, { 1, 1, 1 }), 1e-5); - const auto& constMatioCppMatrix = matioCppMatrix2; + const auto& constMatioCppMatrix2 = matioCppMatrix2; expectedSlice.resize(2, 3); expectedSlice << 1.0, 5.0, 9.0, 3.0, 6.0, 11.0; - expectedSlice.isApprox(matioCpp::to_eigen(constMatioCppMatrix, { 0, -1, -1 }), 1e-5); + expectedSlice.isApprox(matioCpp::to_eigen(constMatioCppMatrix2, { 0, -1, -1 }), 1e-5); }