From d5726f98be269dd934599fe3c7fec645329d789c 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 17:19:51 +0200 Subject: [PATCH] Add tests for Array and Vector containing NaN (remaining of initialization) --- src/utils/Array.hpp | 22 +++++++++++----------- tests/test_Array.cpp | 37 +++++++++++++++++++++++++++++++++++++ tests/test_Vector.cpp | 26 ++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/utils/Array.hpp b/src/utils/Array.hpp index bae84f114..54786db88 100644 --- a/src/utils/Array.hpp +++ b/src/utils/Array.hpp @@ -24,17 +24,6 @@ class [[nodiscard]] Array friend Array<std::add_const_t<DataType>>; public: - friend std::ostream& operator<<(std::ostream& os, const Array& x) - { - if (x.size() > 0) { - os << 0 << ':' << NaNHelper(x[0]); - } - for (size_t i = 1; i < x.size(); ++i) { - os << ' ' << i << ':' << NaNHelper(x[i]); - } - return os; - } - PUGS_INLINE size_t size() const noexcept { return m_values.extent(0); @@ -121,6 +110,17 @@ class [[nodiscard]] Array #endif // NDEBUG } + friend std::ostream& operator<<(std::ostream& os, const Array& x) + { + if (x.size() > 0) { + os << 0 << ':' << NaNHelper(x[0]); + } + for (size_t i = 1; i < x.size(); ++i) { + os << ' ' << i << ':' << NaNHelper(x[i]); + } + return os; + } + PUGS_INLINE Array() = default; diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp index dd6f57ea6..5b9889f39 100644 --- a/tests/test_Array.cpp +++ b/tests/test_Array.cpp @@ -232,7 +232,44 @@ TEST_CASE("Array", "[utils]") } } + SECTION("output") + { + Array<int> x{5}; + x[0] = 2; + x[1] = 6; + x[2] = 2; + x[3] = 3; + x[4] = 7; + + std::ostringstream array_ost; + array_ost << x; + std::ostringstream ref_ost; + ref_ost << 0 << ':' << x[0]; + for (size_t i = 1; i < x.size(); ++i) { + ref_ost << ' ' << i << ':' << x[i]; + } + REQUIRE(array_ost.str() == ref_ost.str()); + } + #ifndef NDEBUG + + SECTION("output with signaling NaN") + { + Array<double> x{5}; + x[0] = 2; + x[2] = 3; + + std::ostringstream array_ost; + array_ost << x; + std::ostringstream ref_ost; + ref_ost << 0 << ':' << 2 << ' '; + ref_ost << 1 << ":nan "; + ref_ost << 2 << ':' << 3 << ' '; + ref_ost << 3 << ":nan "; + ref_ost << 4 << ":nan"; + REQUIRE(array_ost.str() == ref_ost.str()); + } + SECTION("checking for bounds violation") { REQUIRE_THROWS_AS(a[10], AssertError); diff --git a/tests/test_Vector.cpp b/tests/test_Vector.cpp index ff6d11854..e31bf1667 100644 --- a/tests/test_Vector.cpp +++ b/tests/test_Vector.cpp @@ -362,11 +362,11 @@ TEST_CASE("Vector", "[algebra]") SECTION("output") { Vector<int> x{5}; - x[0] = 0; - x[1] = 1; + x[0] = 3; + x[1] = 7; x[2] = 2; - x[3] = 3; - x[4] = 4; + x[3] = 1; + x[4] = -4; std::ostringstream vector_ost; vector_ost << x; @@ -432,6 +432,24 @@ TEST_CASE("Vector", "[algebra]") } #ifndef NDEBUG + + SECTION("output with signaling NaN") + { + Vector<double> x{5}; + x[0] = 3; + x[2] = 2; + + std::ostringstream vector_ost; + vector_ost << x; + std::ostringstream ref_ost; + ref_ost << 0 << ':' << 3 << ' '; + ref_ost << 1 << ":nan "; + ref_ost << 2 << ':' << 2 << ' '; + ref_ost << 3 << ":nan "; + ref_ost << 4 << ":nan"; + REQUIRE(vector_ost.str() == ref_ost.str()); + } + SECTION("invalid dot product") { Vector<int> x{5}; -- GitLab