]> https://gitweb.dealii.org/ - dealii.git/commitdiff
tests: add more tests for Lazy<T> 16182/head
authorMatthias Maier <tamiko@43-1.org>
Fri, 27 Oct 2023 07:00:17 +0000 (02:00 -0500)
committerMatthias Maier <tamiko@43-1.org>
Fri, 27 Oct 2023 07:12:34 +0000 (02:12 -0500)
 - verify that we can call the interface in const context
 - add another test that checks whether we do the right thing with
   concurrent threads

tests/base/lazy_02.cc
tests/base/lazy_03.cc [new file with mode: 0644]
tests/base/lazy_03.output [new file with mode: 0644]
tests/base/lazy_04.cc [new file with mode: 0644]
tests/base/lazy_04.threads=4.output [new file with mode: 0644]

index 2130279a27101f90fd86eb2196c15c8258e24b88..bb00243cb4c78cb5ae55143ec02b149a44605e0d 100644 (file)
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2005 - 2021 by the deal.II authors
+// Copyright (C) 2023 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
diff --git a/tests/base/lazy_03.cc b/tests/base/lazy_03.cc
new file mode 100644 (file)
index 0000000..48c7734
--- /dev/null
@@ -0,0 +1,58 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 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.
+//
+// ---------------------------------------------------------------------
+
+//
+// Use Lazy<T> in const context:
+//
+
+#include <deal.II/base/lazy.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+using namespace dealii;
+
+class Container
+{
+public:
+  int
+  get() const
+  {
+    lazy_integer.ensure_initialized([]() { return 42; });
+
+    if (lazy_integer.has_value())
+      return lazy_integer.value();
+
+    // unreachable
+    return lazy_integer.value_or_initialize([]() { return 43; });
+  }
+
+private:
+  Lazy<int> lazy_integer;
+};
+
+int
+main()
+{
+  initlog();
+
+  Container foo;
+
+  foo.get();
+
+  deallog << "OK!" << std::endl;
+  return 0;
+}
diff --git a/tests/base/lazy_03.output b/tests/base/lazy_03.output
new file mode 100644 (file)
index 0000000..5cfb783
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK!
diff --git a/tests/base/lazy_04.cc b/tests/base/lazy_04.cc
new file mode 100644 (file)
index 0000000..96db231
--- /dev/null
@@ -0,0 +1,55 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 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.
+//
+// ---------------------------------------------------------------------
+
+//
+// Use Lazy<T> concurrently
+//
+
+#include <deal.II/base/lazy.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+using namespace dealii;
+
+int
+main()
+{
+  initlog();
+
+  Lazy<int> lazy_integer;
+
+  const auto foo = [&]() {
+    lazy_integer.ensure_initialized([&]() {
+      deallog << "... [ initialized ] ... " << std::endl;
+      return 42;
+    });
+  };
+
+  Threads::Task<> t1 = Threads::new_task(foo);
+  Threads::Task<> t2 = Threads::new_task(foo);
+  Threads::Task<> t3 = Threads::new_task(foo);
+  Threads::Task<> t4 = Threads::new_task(foo);
+  t4.join();
+  t3.join();
+  t2.join();
+  t1.join();
+
+  deallog << "Object: " << lazy_integer.value() << std::endl;
+
+  deallog << "OK!" << std::endl;
+  return 0;
+}
diff --git a/tests/base/lazy_04.threads=4.output b/tests/base/lazy_04.threads=4.output
new file mode 100644 (file)
index 0000000..879d648
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::... [ initialized ] ... 
+DEAL::Object: 42
+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.