diff --git a/src/algebra/TinyMatrix.hpp b/src/algebra/TinyMatrix.hpp index 6bf810cde020681da88afedb6a815e54a563504d..98030dd447ee6cac14b6de0f3c45d55b68cdf7ba 100644 --- a/src/algebra/TinyMatrix.hpp +++ b/src/algebra/TinyMatrix.hpp @@ -3,6 +3,7 @@ #include <algebra/TinyVector.hpp> #include <utils/InvalidData.hpp> +#include <utils/NaNHelper.hpp> #include <utils/PugsAssert.hpp> #include <utils/PugsMacros.hpp> #include <utils/Types.hpp> @@ -134,9 +135,9 @@ class [[nodiscard]] TinyMatrix { os << '['; for (size_t i = 0; i < N; ++i) { - os << '(' << A(i, 0); + os << '(' << NaNHelper(A(i, 0)); for (size_t j = 1; j < N; ++j) { - os << ',' << A(i, j); + os << ',' << NaNHelper(A(i, j)); } os << ')'; } diff --git a/src/algebra/TinyVector.hpp b/src/algebra/TinyVector.hpp index 69ecb14e39d626b1ed0e2709b6fd018af92714da..382741093f351cea80ff1e261399df9583dc4d0c 100644 --- a/src/algebra/TinyVector.hpp +++ b/src/algebra/TinyVector.hpp @@ -2,6 +2,7 @@ #define TINY_VECTOR_HPP #include <utils/InvalidData.hpp> +#include <utils/NaNHelper.hpp> #include <utils/PugsAssert.hpp> #include <utils/PugsMacros.hpp> #include <utils/Types.hpp> @@ -98,9 +99,9 @@ class [[nodiscard]] TinyVector PUGS_INLINE constexpr friend std::ostream& operator<<(std::ostream& os, const TinyVector& v) { - os << '(' << v.m_values[0]; + os << '(' << NaNHelper(v.m_values[0]); for (size_t i = 1; i < N; ++i) { - os << ',' << v.m_values[i]; + os << ',' << NaNHelper(v.m_values[i]); } os << ')'; return os; diff --git a/tests/test_TinyMatrix.cpp b/tests/test_TinyMatrix.cpp index 31ded1e245bc026a0a31405ddccca0413717c402..f50d2abc61f26bdbebbac4ba14ce884b352ec0aa 100644 --- a/tests/test_TinyMatrix.cpp +++ b/tests/test_TinyMatrix.cpp @@ -9,6 +9,8 @@ #include <algebra/TinyMatrix.hpp> +#include <sstream> + // Instantiate to ensure full coverage is performed template class TinyMatrix<1, int>; template class TinyMatrix<2, int>; @@ -238,6 +240,20 @@ TEST_CASE("TinyMatrix", "[algebra]") } #ifndef NDEBUG + SECTION("output with signaling NaN") + { + TinyMatrix<2> A; + A(0, 0) = 1; + A(1, 0) = 2; + std::ostringstream A_ost; + A_ost << A; + + std::ostringstream ref_ost; + ref_ost << "[(1,nan)(2,nan)]"; + + REQUIRE(A_ost.str() == ref_ost.str()); + } + SECTION("checking for bounds violation") { REQUIRE_THROWS_AS(A(3, 0), AssertError); diff --git a/tests/test_TinyVector.cpp b/tests/test_TinyVector.cpp index c4fb06998505d0eb5e5697835958d9a970d5e01a..cf77a22325ee19137367ab68eff4f2f5a817ef6a 100644 --- a/tests/test_TinyVector.cpp +++ b/tests/test_TinyVector.cpp @@ -5,6 +5,8 @@ #include <algebra/TinyVector.hpp> +#include <sstream> + // Instantiate to ensure full coverage is performed template class TinyVector<1, int>; template class TinyVector<3, int>; @@ -81,6 +83,19 @@ TEST_CASE("TinyVector", "[algebra]") } #ifndef NDEBUG + SECTION("output with signaling NaN") + { + TinyVector<3> x; + x[1] = 1; + std::ostringstream x_ost; + x_ost << x; + + std::ostringstream ref_ost; + ref_ost << "(nan,1,nan)"; + + REQUIRE(x_ost.str() == ref_ost.str()); + } + SECTION("checking for bounds validation") { REQUIRE_THROWS_AS(x[4] = 0, AssertError);