From 5fb426f31b39224ffb00882283b5e7e4f32043cb Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 5 May 2023 15:06:00 -0600 Subject: [PATCH] Add a test. --- tests/base/scope_exit_01.cc | 78 +++++++++++++++++++++++++++++++++ tests/base/scope_exit_01.output | 6 +++ 2 files changed, 84 insertions(+) create mode 100644 tests/base/scope_exit_01.cc create mode 100644 tests/base/scope_exit_01.output diff --git a/tests/base/scope_exit_01.cc b/tests/base/scope_exit_01.cc new file mode 100644 index 0000000000..5976a6bf8d --- /dev/null +++ b/tests/base/scope_exit_01.cc @@ -0,0 +1,78 @@ +// --------------------------------------------------------------------- +// +// 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. +// +// --------------------------------------------------------------------- + + +// Check that ScopeExit works in both regular return statements and +// via `throw` statements. + +#include + +#include "../tests.h" + + +void +f1() +{ + ScopeExit s([]() { deallog << "Exiting function regularly." << std::endl; }); + + return; +} + + +void +f2() +{ + ScopeExit s([]() { deallog << "Exiting function via throw." << std::endl; }); + + throw 123; +} + + +void +f3() +{ + ScopeExit s([]() { deallog << "Exiting function regularly." << std::endl; }); + + try + { + throw 123; + } + catch (...) + { + // just eat the exception and return regularly + deallog << "Caught exception. Making it go away." << std::endl; + } +} + + + +int +main() +{ + initlog(); + + f1(); + + try + { + f2(); + } + catch (int i) + { + deallog << "Caught int=" << i << std::endl; + } + + f3(); +} diff --git a/tests/base/scope_exit_01.output b/tests/base/scope_exit_01.output new file mode 100644 index 0000000000..761b801053 --- /dev/null +++ b/tests/base/scope_exit_01.output @@ -0,0 +1,6 @@ + +DEAL::Exiting function regularly. +DEAL::Exiting function via throw. +DEAL::Caught int=123 +DEAL::Caught exception. Making it go away. +DEAL::Exiting function regularly. -- 2.39.5