From 3c6fdc98a249ec032466317fef181ba5e469e85e Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Fri, 27 Oct 2023 02:00:17 -0500 Subject: [PATCH] tests: add more tests for Lazy - 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 | 2 +- tests/base/lazy_03.cc | 58 +++++++++++++++++++++++++++++ tests/base/lazy_03.output | 2 + tests/base/lazy_04.cc | 55 +++++++++++++++++++++++++++ tests/base/lazy_04.threads=4.output | 4 ++ 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 tests/base/lazy_03.cc create mode 100644 tests/base/lazy_03.output create mode 100644 tests/base/lazy_04.cc create mode 100644 tests/base/lazy_04.threads=4.output diff --git a/tests/base/lazy_02.cc b/tests/base/lazy_02.cc index 2130279a27..bb00243cb4 100644 --- a/tests/base/lazy_02.cc +++ b/tests/base/lazy_02.cc @@ -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 index 0000000000..48c7734ad1 --- /dev/null +++ b/tests/base/lazy_03.cc @@ -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 in const context: +// + +#include + +#include + +#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 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 index 0000000000..5cfb783b8f --- /dev/null +++ b/tests/base/lazy_03.output @@ -0,0 +1,2 @@ + +DEAL::OK! diff --git a/tests/base/lazy_04.cc b/tests/base/lazy_04.cc new file mode 100644 index 0000000000..96db2318cc --- /dev/null +++ b/tests/base/lazy_04.cc @@ -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 concurrently +// + +#include + +#include + +#include "../tests.h" + +using namespace dealii; + +int +main() +{ + initlog(); + + Lazy 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 index 0000000000..879d648677 --- /dev/null +++ b/tests/base/lazy_04.threads=4.output @@ -0,0 +1,4 @@ + +DEAL::... [ initialized ] ... +DEAL::Object: 42 +DEAL::OK! -- 2.39.5