From 675351f4a46b157ee532fe593d962caf0a5f657a Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 21 Apr 2017 17:30:45 -0600 Subject: [PATCH] Add more tests. --- .../parameter_handler_exceptions_01.cc | 79 +++++++++++++++++ .../parameter_handler_exceptions_01.output | 5 ++ .../parameter_handler_exceptions_02.cc | 88 +++++++++++++++++++ .../parameter_handler_exceptions_02.output | 5 ++ 4 files changed, 177 insertions(+) create mode 100644 tests/parameter_handler/parameter_handler_exceptions_01.cc create mode 100644 tests/parameter_handler/parameter_handler_exceptions_01.output create mode 100644 tests/parameter_handler/parameter_handler_exceptions_02.cc create mode 100644 tests/parameter_handler/parameter_handler_exceptions_02.output diff --git a/tests/parameter_handler/parameter_handler_exceptions_01.cc b/tests/parameter_handler/parameter_handler_exceptions_01.cc new file mode 100644 index 0000000000..ec68a0e6a4 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_exceptions_01.cc @@ -0,0 +1,79 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 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 at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +// ensure that we end up in a defined state after a pattern is not matched + +#include "../tests.h" +#include +#include +#include + + +std::string input = "set test_1 = 1\n" + "subsection subsec\n" + " set test_2 = 42\n" // forbidden + "end\n"; + + + +void check (const char *p) +{ + ParameterHandler prm; + prm.declare_entry ("test_1", "0", + Patterns::Integer(-1,1)); + prm.enter_subsection ("subsec"); + prm.declare_entry ("test_2", "0", + Patterns::Integer(-1,1)); + prm.leave_subsection (); + + std::istringstream in(input); + try + { + deallog << "Trying to read parameters..." << std::endl; + prm.parse_input (in); + deallog << "Done reading parameters..." << std::endl; + } + catch (...) + { + deallog << "Caught an exception -- ignoring..." << std::endl; + } + + + // make sure the prm object was reset to a state where we are in the + // subsection we were in before attempting the `read_input` call + // (namely, in the top-level section of the prm tree) + deallog << "test_1=" + << prm.get ("test_1") // should =1, because we read that value + << std::endl; + prm.enter_subsection ("subsec"); + deallog << "test_2=" + << prm.get ("test_2") // should =default, because reading the value failed + << std::endl; + prm.leave_subsection (); +} + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + check (SOURCE_DIR "/prm/parameter_handler_1.prm"); + + return 0; +} diff --git a/tests/parameter_handler/parameter_handler_exceptions_01.output b/tests/parameter_handler/parameter_handler_exceptions_01.output new file mode 100644 index 0000000000..d1d5aaa008 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_exceptions_01.output @@ -0,0 +1,5 @@ + +DEAL::Trying to read parameters... +DEAL::Caught an exception -- ignoring... +DEAL::test_1=1 +DEAL::test_2=0 diff --git a/tests/parameter_handler/parameter_handler_exceptions_02.cc b/tests/parameter_handler/parameter_handler_exceptions_02.cc new file mode 100644 index 0000000000..7d7c7a40b0 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_exceptions_02.cc @@ -0,0 +1,88 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2017 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 at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +// ensure that we end up in a defined state if an action throws an +// exception + +#include "../tests.h" +#include +#include +#include + + +std::string input = "set test_1 = 1\n" + "subsection subsec\n" + " set test_2 = -1\n" + "end\n"; + + + +void check (const char *p) +{ + ParameterHandler prm; + prm.declare_entry ("test_1", "0", + Patterns::Integer(-1,1)); + prm.enter_subsection ("subsec"); + prm.declare_entry ("test_2", "0", + Patterns::Integer(-1,1)); + prm.add_action ("test_2", + [](const std::string &s) + { + // throw an exception from the action for + // everything but the default value + if (s != "0") + throw 1; + }); + prm.leave_subsection (); + + std::istringstream in(input); + try + { + deallog << "Trying to read parameters..." << std::endl; + prm.parse_input (in); + deallog << "Done reading parameters..." << std::endl; + } + catch (...) + { + deallog << "Caught an exception -- ignoring..." << std::endl; + } + + + // make sure the prm object was reset to a state where we are in the + // subsection we were in before attempting the `read_input` call + // (namely, in the top-level section of the prm tree) + deallog << "test_1=" + << prm.get ("test_1") // should =1, because we read that value + << std::endl; + prm.enter_subsection ("subsec"); + deallog << "test_2=" + << prm.get ("test_2") // should =default, because the action failed + << std::endl; + prm.leave_subsection (); +} + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + check (SOURCE_DIR "/prm/parameter_handler_1.prm"); + + return 0; +} diff --git a/tests/parameter_handler/parameter_handler_exceptions_02.output b/tests/parameter_handler/parameter_handler_exceptions_02.output new file mode 100644 index 0000000000..d1d5aaa008 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_exceptions_02.output @@ -0,0 +1,5 @@ + +DEAL::Trying to read parameters... +DEAL::Caught an exception -- ignoring... +DEAL::test_1=1 +DEAL::test_2=0 -- 2.39.5