From a5e197693d1f1e182e1c329b8bce245c3633cdd8 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Thu, 26 Oct 2023 21:28:26 -0500 Subject: [PATCH] tests: add unit tests for dealii::Lazy --- tests/base/lazy_01.cc | 64 +++++++++++++++++++++++++++++ tests/base/lazy_01.output | 6 +++ tests/base/lazy_02.cc | 85 +++++++++++++++++++++++++++++++++++++++ tests/base/lazy_02.output | 15 +++++++ 4 files changed, 170 insertions(+) create mode 100644 tests/base/lazy_01.cc create mode 100644 tests/base/lazy_01.output create mode 100644 tests/base/lazy_02.cc create mode 100644 tests/base/lazy_02.output diff --git a/tests/base/lazy_01.cc b/tests/base/lazy_01.cc new file mode 100644 index 0000000000..879f3c339c --- /dev/null +++ b/tests/base/lazy_01.cc @@ -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. +// + +#include + +#include + +#include "../tests.h" + +using namespace dealii; + +int +main() +{ + initlog(); + + { + Lazy 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 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 index 0000000000..7d33dd9bad --- /dev/null +++ b/tests/base/lazy_01.output @@ -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 index 0000000000..2130279a27 --- /dev/null +++ b/tests/base/lazy_02.cc @@ -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. +// + +#include + +#include + +#include "../tests.h" + +using namespace dealii; + +int +main() +{ + initlog(); + + Lazy lazy_integer; + lazy_integer.ensure_initialized([&]() { + deallog << "... [ initialized ] ... " << std::endl; + return 42; + }); + deallog << "Object: " << lazy_integer.value() << std::endl; + + Lazy lazy_integer_2(lazy_integer); + deallog << "Copied: " << lazy_integer_2.value() << std::endl; + + Lazy lazy_integer_3; + lazy_integer_3 = lazy_integer_2; + deallog << "Assigned: " << lazy_integer_3.value() << std::endl; + + Lazy lazy_integer_4 = [&]() { + Lazy temp = lazy_integer; + return temp; + }(); + deallog << "Moved: " << lazy_integer_4.value() << std::endl; + + lazy_integer_4 = [&]() { + Lazy 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 index 0000000000..54697dcbb5 --- /dev/null +++ b/tests/base/lazy_02.output @@ -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! -- 2.39.5