From 7e2d56e487ab41c2b3c53d42b5141c7e8a9dc5d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Sat, 31 Jul 2021 18:12:51 +0200
Subject: [PATCH] Use NaNHelper when printing TinyVector and TinyMatrix

---
 src/algebra/TinyMatrix.hpp |  5 +++--
 src/algebra/TinyVector.hpp |  5 +++--
 tests/test_TinyMatrix.cpp  | 16 ++++++++++++++++
 tests/test_TinyVector.cpp  | 15 +++++++++++++++
 4 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/src/algebra/TinyMatrix.hpp b/src/algebra/TinyMatrix.hpp
index 6bf810cde..98030dd44 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 69ecb14e3..382741093 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 31ded1e24..f50d2abc6 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 c4fb06998..cf77a2232 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);
-- 
GitLab