From 6f446ff09ac6f8a544cc675c77753f0d59044038 Mon Sep 17 00:00:00 2001
From: Oliver Sander <sander@dune-project.org>
Date: Wed, 22 Aug 2012 09:15:03 +0000
Subject: [PATCH] Add tests whether all-NaN FieldVector and FieldMatrices have
 NaN norms

Patch by Elias Pipping

FS 1147

[[Imported from SVN: r6921]]
---
 dune/common/test/fmatrixtest.cc | 41 +++++++++++++++++++++++++++++++++
 dune/common/test/fvectortest.cc | 36 +++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)

diff --git a/dune/common/test/fmatrixtest.cc b/dune/common/test/fmatrixtest.cc
index 49e42a024..8940d3708 100644
--- a/dune/common/test/fmatrixtest.cc
+++ b/dune/common/test/fmatrixtest.cc
@@ -498,9 +498,50 @@ void test_invert ()
   A.invert();
 }
 
+// Make sure that a matrix with only NaN entries has norm NaN.
+// Prior to r6819, the infinity_norm would be zero; see also FS #1147.
+void
+test_nan()
+{
+  double mynan = 0.0/0.0;
+
+  Dune::FieldMatrix<double, 2, 2> m2(mynan);
+  assert(std::isnan(m2.infinity_norm()));
+  assert(std::isnan(m2.frobenius_norm()));
+  assert(std::isnan(m2.frobenius_norm2()));
+
+  Dune::FieldMatrix<double, 0, 2> m02(mynan);
+  assert(0.0 == m02.infinity_norm());
+  assert(0.0 == m02.frobenius_norm());
+  assert(0.0 == m02.frobenius_norm2());
+
+  Dune::FieldMatrix<double, 2, 0> m20(mynan);
+  assert(0.0 == m20.infinity_norm());
+  assert(0.0 == m20.frobenius_norm());
+  assert(0.0 == m20.frobenius_norm2());
+}
+
+// The computation of infinity_norm_real() was flawed from r6819 on
+// until r6915.
+void
+test_infinity_norms()
+{
+  std::complex<double> threefour(3.0, -4.0);
+  std::complex<double> eightsix(8.0, -6.0);
+
+  Dune::FieldMatrix<std::complex<double>, 2, 2> m;
+  m[0] = threefour;
+  m[1] = eightsix;
+  assert(std::abs(m.infinity_norm()     -20.0) < 1e-10); // max(5+5, 10+10)
+  assert(std::abs(m.infinity_norm_real()-28.0) < 1e-10); // max(7+7, 14+14)
+}
+
 int main()
 {
   try {
+    test_nan();
+    test_infinity_norms();
+
     // test 1 x 1 matrices
     test_matrix<float, 1, 1>();
     ScalarOperatorTest<float>();
diff --git a/dune/common/test/fvectortest.cc b/dune/common/test/fvectortest.cc
index 4c0cce7d0..81ac1913f 100644
--- a/dune/common/test/fvectortest.cc
+++ b/dune/common/test/fvectortest.cc
@@ -309,12 +309,48 @@ public:
   }
 };
 
+// Make sure that a vector with only NaN entries has norm NaN.
+// Prior to r6914, the infinity_norm would be zero; see also FS #1147.
+void
+test_nan()
+{
+  double mynan = 0.0/0.0;
+
+  Dune::FieldVector<double, 2> v2(mynan);
+  assert(std::isnan(v2.infinity_norm()));
+  assert(std::isnan(v2.one_norm()));
+  assert(std::isnan(v2.two_norm()));
+  assert(std::isnan(v2.two_norm2()));
+
+  Dune::FieldVector<double, 0> v0(mynan);
+  assert(0.0 == v0.infinity_norm());
+  assert(0.0 == v0.one_norm());
+  assert(0.0 == v0.two_norm());
+  assert(0.0 == v0.two_norm2());
+}
+
+void
+test_infinity_norms()
+{
+  std::complex<double> threefour(3.0, -4.0);
+  std::complex<double> eightsix(8.0, -6.0);
+
+  Dune::FieldVector<std::complex<double>, 2> v;
+  v[0] = threefour;
+  v[1] = eightsix;
+  assert(std::abs(v.infinity_norm()     -10.0) < 1e-10); // max(5,10)
+  assert(std::abs(v.infinity_norm_real()-14.0) < 1e-10); // max(7,14)
+}
+
 int main()
 {
   try {
     FieldVectorTest<int, 3>();
     FieldVectorTest<float, 3>();
     FieldVectorTest<double, 3>();
+
+    test_nan();
+    test_infinity_norms();
   } catch (Dune::Exception& e) {
     std::cerr << e << std::endl;
     return 1;
-- 
GitLab