]> https://gitweb.dealii.org/ - dealii.git/commitdiff
tests: add unit tests for dealii::Lazy<T>
authorMatthias Maier <tamiko@43-1.org>
Fri, 27 Oct 2023 02:28:26 +0000 (21:28 -0500)
committerMatthias Maier <tamiko@43-1.org>
Fri, 27 Oct 2023 07:01:37 +0000 (02:01 -0500)
tests/base/lazy_01.cc [new file with mode: 0644]
tests/base/lazy_01.output [new file with mode: 0644]
tests/base/lazy_02.cc [new file with mode: 0644]
tests/base/lazy_02.output [new file with mode: 0644]

diff --git a/tests/base/lazy_01.cc b/tests/base/lazy_01.cc
new file mode 100644 (file)
index 0000000..879f3c3
--- /dev/null
@@ -0,0 +1,64 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2023 - 2023 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+//
+// Test the ensure_initialized() and value_or_initialize() interface of
+// Lazy<T>.
+//
+
+#include <deal.II/base/lazy.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+using namespace dealii;
+
+int
+main()
+{
+  initlog();
+
+  {
+    Lazy<int> lazy_integer;
+
+    lazy_integer.ensure_initialized([&]() {
+      deallog << "[initializing object]" << std::endl;
+      return 42;
+    });
+    deallog << "lazy_integer.value() = " << lazy_integer.value() << std::endl;
+
+    lazy_integer.ensure_initialized([&]() {
+      deallog << "[initializing object] again... not good." << std::endl;
+      return -1;
+    });
+
+    deallog << "lazy_integer.value() = " << lazy_integer.value() << std::endl;
+  }
+
+  {
+    Lazy<int> lazy_integer;
+
+    deallog << "lazy_integer.value() = "
+            << lazy_integer.value_or_initialize([&]() {
+                 deallog << "... [initializing object] ... ";
+                 return 42;
+               })
+            << std::endl;
+  }
+
+  deallog << "OK!" << std::endl;
+  return 0;
+}
diff --git a/tests/base/lazy_01.output b/tests/base/lazy_01.output
new file mode 100644 (file)
index 0000000..7d33dd9
--- /dev/null
@@ -0,0 +1,6 @@
+
+DEAL::[initializing object]
+DEAL::lazy_integer.value() = 42
+DEAL::lazy_integer.value() = 42
+DEAL::lazy_integer.value() = ... [initializing object] ... 42
+DEAL::OK!
diff --git a/tests/base/lazy_02.cc b/tests/base/lazy_02.cc
new file mode 100644 (file)
index 0000000..2130279
--- /dev/null
@@ -0,0 +1,85 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2021 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+//
+// Test copy, move, and reset semantics of Lazy<T>.
+//
+
+#include <deal.II/base/lazy.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+using namespace dealii;
+
+int
+main()
+{
+  initlog();
+
+  Lazy<int> lazy_integer;
+  lazy_integer.ensure_initialized([&]() {
+    deallog << "... [ initialized ] ... " << std::endl;
+    return 42;
+  });
+  deallog << "Object: " << lazy_integer.value() << std::endl;
+
+  Lazy<int> lazy_integer_2(lazy_integer);
+  deallog << "Copied: " << lazy_integer_2.value() << std::endl;
+
+  Lazy<int> lazy_integer_3;
+  lazy_integer_3 = lazy_integer_2;
+  deallog << "Assigned: " << lazy_integer_3.value() << std::endl;
+
+  Lazy<int> lazy_integer_4 = [&]() {
+    Lazy<int> temp = lazy_integer;
+    return temp;
+  }();
+  deallog << "Moved: " << lazy_integer_4.value() << std::endl;
+
+  lazy_integer_4 = [&]() {
+    Lazy<int> temp = lazy_integer;
+    return temp;
+  }();
+  deallog << "Moved assigned: " << lazy_integer_4.value() << std::endl;
+
+  deallog << "... [ reset ] ... " << std::endl;
+  lazy_integer_4.reset();
+  lazy_integer_4.ensure_initialized([&]() {
+    deallog << "... [ initialized ] ... " << std::endl;
+    return 43;
+  });
+
+  deallog << "... [ reset ] ... " << std::endl;
+  lazy_integer_4.reset();
+  lazy_integer_4.ensure_initialized([&]() {
+    deallog << "... [ initialized ] ... " << std::endl;
+    return 43;
+  });
+  deallog << "Reset: " << lazy_integer_4.value() << std::endl;
+
+  deallog << "... [ reset ] ... " << std::endl;
+  lazy_integer_4.reset();
+  deallog << "value_or_initialize: "
+          << lazy_integer_4.value_or_initialize([&]() {
+               deallog << "... [ initialized ] ... ";
+               return 45;
+             })
+          << std::endl;
+
+  deallog << "OK!" << std::endl;
+  return 0;
+}
diff --git a/tests/base/lazy_02.output b/tests/base/lazy_02.output
new file mode 100644 (file)
index 0000000..54697dc
--- /dev/null
@@ -0,0 +1,15 @@
+
+DEAL::... [ initialized ] ... 
+DEAL::Object: 42
+DEAL::Copied: 42
+DEAL::Assigned: 42
+DEAL::Moved: 42
+DEAL::Moved assigned: 42
+DEAL::... [ reset ] ... 
+DEAL::... [ initialized ] ... 
+DEAL::... [ reset ] ... 
+DEAL::... [ initialized ] ... 
+DEAL::Reset: 43
+DEAL::... [ reset ] ... 
+DEAL::value_or_initialize: ... [ initialized ] ... 45
+DEAL::OK!

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.