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

Add tests for Array and Vector containing NaN (remaining of initialization)

parent acb0259f
No related branches found
No related tags found
1 merge request!105Add NaNHelper utility
This commit is part of merge request !105. Comments created here will be created in the context of that merge request.
...@@ -24,17 +24,6 @@ class [[nodiscard]] Array ...@@ -24,17 +24,6 @@ class [[nodiscard]] Array
friend Array<std::add_const_t<DataType>>; friend Array<std::add_const_t<DataType>>;
public: 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 PUGS_INLINE size_t size() const noexcept
{ {
return m_values.extent(0); return m_values.extent(0);
...@@ -121,6 +110,17 @@ class [[nodiscard]] Array ...@@ -121,6 +110,17 @@ class [[nodiscard]] Array
#endif // NDEBUG #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 PUGS_INLINE
Array() = default; Array() = default;
......
...@@ -232,7 +232,44 @@ TEST_CASE("Array", "[utils]") ...@@ -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 #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") SECTION("checking for bounds violation")
{ {
REQUIRE_THROWS_AS(a[10], AssertError); REQUIRE_THROWS_AS(a[10], AssertError);
......
...@@ -362,11 +362,11 @@ TEST_CASE("Vector", "[algebra]") ...@@ -362,11 +362,11 @@ TEST_CASE("Vector", "[algebra]")
SECTION("output") SECTION("output")
{ {
Vector<int> x{5}; Vector<int> x{5};
x[0] = 0; x[0] = 3;
x[1] = 1; x[1] = 7;
x[2] = 2; x[2] = 2;
x[3] = 3; x[3] = 1;
x[4] = 4; x[4] = -4;
std::ostringstream vector_ost; std::ostringstream vector_ost;
vector_ost << x; vector_ost << x;
...@@ -432,6 +432,24 @@ TEST_CASE("Vector", "[algebra]") ...@@ -432,6 +432,24 @@ TEST_CASE("Vector", "[algebra]")
} }
#ifndef NDEBUG #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") SECTION("invalid dot product")
{ {
Vector<int> x{5}; Vector<int> x{5};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment