From a778819db3e1b5e539ed8ad97b92fa06a4c64416 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Thu, 22 Jul 2021 19:58:15 +0200
Subject: [PATCH] Add tests for bad initialization when NDEBUG is not defined

---
 tests/test_Array.cpp               | 18 ++++++++++++++++++
 tests/test_CRSMatrixDescriptor.cpp |  3 +++
 tests/test_Table.cpp               | 22 ++++++++++++++++++++++
 tests/test_TinyMatrix.cpp          | 22 ++++++++++++++++++++++
 tests/test_TinyVector.cpp          | 19 +++++++++++++++++++
 5 files changed, 84 insertions(+)

diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp
index 23d63a4a2..dd6f57ea6 100644
--- a/tests/test_Array.cpp
+++ b/tests/test_Array.cpp
@@ -243,5 +243,23 @@ TEST_CASE("Array", "[utils]")
     Array<int> b{2 * a.size()};
     REQUIRE_THROWS_AS(copy_to(a, b), AssertError);
   }
+
+  SECTION("checking for nan initialization")
+  {
+    Array<double> array(10);
+
+    for (size_t i = 0; i < array.size(); ++i) {
+      REQUIRE(std::isnan(array[i]));
+    }
+  }
+
+  SECTION("checking for bad initialization")
+  {
+    Array<int> array(10);
+
+    for (size_t i = 0; i < array.size(); ++i) {
+      REQUIRE(array[i] == std::numeric_limits<int>::max() / 2);
+    }
+  }
 #endif   // NDEBUG
 }
diff --git a/tests/test_CRSMatrixDescriptor.cpp b/tests/test_CRSMatrixDescriptor.cpp
index 2b311d84b..28276aaad 100644
--- a/tests/test_CRSMatrixDescriptor.cpp
+++ b/tests/test_CRSMatrixDescriptor.cpp
@@ -145,12 +145,14 @@ TEST_CASE("CRSMatrixDescriptor", "[algebra]")
   SECTION("incompatible non zero vector size and number of columns")
   {
     Array<int> non_zeros(3);
+    non_zeros.fill(1);
     REQUIRE_THROWS_AS((CRSMatrixDescriptor<int>{2, 4, non_zeros}), AssertError);
   }
 
   SECTION("bad row number")
   {
     Array<int> non_zeros(2);
+    non_zeros.fill(1);
     CRSMatrixDescriptor<int> S{2, 4, non_zeros};
     REQUIRE_THROWS_AS(S(2, 3) = 1, AssertError);
   }
@@ -158,6 +160,7 @@ TEST_CASE("CRSMatrixDescriptor", "[algebra]")
   SECTION("bad column number")
   {
     Array<int> non_zeros(2);
+    non_zeros.fill(2);
     CRSMatrixDescriptor<int> S{2, 4, non_zeros};
     REQUIRE_THROWS_AS(S(0, 5) = 1, AssertError);
   }
diff --git a/tests/test_Table.cpp b/tests/test_Table.cpp
index 5e738867a..5372c697d 100644
--- a/tests/test_Table.cpp
+++ b/tests/test_Table.cpp
@@ -199,5 +199,27 @@ TEST_CASE("Table", "[utils]")
       REQUIRE_THROWS_AS(copy_to(a, c), AssertError);
     }
   }
+
+  SECTION("checking for nan initialization")
+  {
+    Table<double> B(3, 4);
+
+    for (size_t i = 0; i < B.numberOfRows(); ++i) {
+      for (size_t j = 0; j < B.numberOfColumns(); ++j) {
+        REQUIRE(std::isnan(B(i, j)));
+      }
+    }
+  }
+
+  SECTION("checking for bad initialization")
+  {
+    Table<int> B(4, 2);
+
+    for (size_t i = 0; i < B.numberOfRows(); ++i) {
+      for (size_t j = 0; j < B.numberOfColumns(); ++j) {
+        REQUIRE(B(i, j) == std::numeric_limits<int>::max() / 2);
+      }
+    }
+  }
 #endif   // NDEBUG
 }
diff --git a/tests/test_TinyMatrix.cpp b/tests/test_TinyMatrix.cpp
index 464e78448..3a03b47f7 100644
--- a/tests/test_TinyMatrix.cpp
+++ b/tests/test_TinyMatrix.cpp
@@ -238,5 +238,27 @@ TEST_CASE("TinyMatrix", "[algebra]")
     REQUIRE_THROWS_AS(constA(3, 0), AssertError);
     REQUIRE_THROWS_AS(constA(0, 3), AssertError);
   }
+
+  SECTION("checking for nan initialization")
+  {
+    TinyMatrix<3, double> B;
+
+    for (size_t i = 0; i < B.numberOfRows(); ++i) {
+      for (size_t j = 0; j < B.numberOfColumns(); ++j) {
+        REQUIRE(std::isnan(B(i, j)));
+      }
+    }
+  }
+
+  SECTION("checking for bad initialization")
+  {
+    TinyMatrix<3, int> B;
+
+    for (size_t i = 0; i < B.numberOfRows(); ++i) {
+      for (size_t j = 0; j < B.numberOfColumns(); ++j) {
+        REQUIRE(B(i, j) == std::numeric_limits<int>::max() / 2);
+      }
+    }
+  }
 #endif   // NDEBUG
 }
diff --git a/tests/test_TinyVector.cpp b/tests/test_TinyVector.cpp
index 499536f9c..c4fb06998 100644
--- a/tests/test_TinyVector.cpp
+++ b/tests/test_TinyVector.cpp
@@ -87,5 +87,24 @@ TEST_CASE("TinyVector", "[algebra]")
     const TinyVector<3, int>& const_x = x;
     REQUIRE_THROWS_AS(const_x[-1], AssertError);
   }
+
+  SECTION("checking for nan initialization")
+  {
+    TinyVector<3, double> y;
+
+    for (size_t i = 0; i < y.dimension(); ++i) {
+      REQUIRE(std::isnan(y[i]));
+    }
+  }
+
+  SECTION("checking for bad initialization")
+  {
+    TinyVector<3, int> y;
+
+    for (size_t i = 0; i < y.dimension(); ++i) {
+      REQUIRE(y[i] == std::numeric_limits<int>::max() / 2);
+    }
+  }
+
 #endif   // NDEBUG
 }
-- 
GitLab