Skip to content
Snippets Groups Projects
Commit 7e2d56e4 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Use NaNHelper when printing TinyVector and TinyMatrix

parent d1e784f7
No related branches found
No related tags found
1 merge request!105Add NaNHelper utility
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <algebra/TinyVector.hpp> #include <algebra/TinyVector.hpp>
#include <utils/InvalidData.hpp> #include <utils/InvalidData.hpp>
#include <utils/NaNHelper.hpp>
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <utils/PugsMacros.hpp> #include <utils/PugsMacros.hpp>
#include <utils/Types.hpp> #include <utils/Types.hpp>
...@@ -134,9 +135,9 @@ class [[nodiscard]] TinyMatrix ...@@ -134,9 +135,9 @@ class [[nodiscard]] TinyMatrix
{ {
os << '['; os << '[';
for (size_t i = 0; i < N; ++i) { 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) { for (size_t j = 1; j < N; ++j) {
os << ',' << A(i, j); os << ',' << NaNHelper(A(i, j));
} }
os << ')'; os << ')';
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define TINY_VECTOR_HPP #define TINY_VECTOR_HPP
#include <utils/InvalidData.hpp> #include <utils/InvalidData.hpp>
#include <utils/NaNHelper.hpp>
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <utils/PugsMacros.hpp> #include <utils/PugsMacros.hpp>
#include <utils/Types.hpp> #include <utils/Types.hpp>
...@@ -98,9 +99,9 @@ class [[nodiscard]] TinyVector ...@@ -98,9 +99,9 @@ class [[nodiscard]] TinyVector
PUGS_INLINE PUGS_INLINE
constexpr friend std::ostream& operator<<(std::ostream& os, const TinyVector& v) 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) { for (size_t i = 1; i < N; ++i) {
os << ',' << v.m_values[i]; os << ',' << NaNHelper(v.m_values[i]);
} }
os << ')'; os << ')';
return os; return os;
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include <algebra/TinyMatrix.hpp> #include <algebra/TinyMatrix.hpp>
#include <sstream>
// Instantiate to ensure full coverage is performed // Instantiate to ensure full coverage is performed
template class TinyMatrix<1, int>; template class TinyMatrix<1, int>;
template class TinyMatrix<2, int>; template class TinyMatrix<2, int>;
...@@ -238,6 +240,20 @@ TEST_CASE("TinyMatrix", "[algebra]") ...@@ -238,6 +240,20 @@ TEST_CASE("TinyMatrix", "[algebra]")
} }
#ifndef NDEBUG #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") SECTION("checking for bounds violation")
{ {
REQUIRE_THROWS_AS(A(3, 0), AssertError); REQUIRE_THROWS_AS(A(3, 0), AssertError);
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <algebra/TinyVector.hpp> #include <algebra/TinyVector.hpp>
#include <sstream>
// Instantiate to ensure full coverage is performed // Instantiate to ensure full coverage is performed
template class TinyVector<1, int>; template class TinyVector<1, int>;
template class TinyVector<3, int>; template class TinyVector<3, int>;
...@@ -81,6 +83,19 @@ TEST_CASE("TinyVector", "[algebra]") ...@@ -81,6 +83,19 @@ TEST_CASE("TinyVector", "[algebra]")
} }
#ifndef NDEBUG #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") SECTION("checking for bounds validation")
{ {
REQUIRE_THROWS_AS(x[4] = 0, AssertError); REQUIRE_THROWS_AS(x[4] = 0, AssertError);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment