]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add more tests.
authorWolfgang Bangerth <bangerth@colostate.edu>
Fri, 21 Apr 2017 23:30:45 +0000 (17:30 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Mon, 24 Apr 2017 19:25:30 +0000 (13:25 -0600)
tests/parameter_handler/parameter_handler_exceptions_01.cc [new file with mode: 0644]
tests/parameter_handler/parameter_handler_exceptions_01.output [new file with mode: 0644]
tests/parameter_handler/parameter_handler_exceptions_02.cc [new file with mode: 0644]
tests/parameter_handler/parameter_handler_exceptions_02.output [new file with mode: 0644]

diff --git a/tests/parameter_handler/parameter_handler_exceptions_01.cc b/tests/parameter_handler/parameter_handler_exceptions_01.cc
new file mode 100644 (file)
index 0000000..ec68a0e
--- /dev/null
@@ -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 <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+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 (file)
index 0000000..d1d5aaa
--- /dev/null
@@ -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 (file)
index 0000000..7d7c7a4
--- /dev/null
@@ -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 <deal.II/base/logstream.h>
+#include <deal.II/base/parameter_handler.h>
+#include <fstream>
+
+
+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 (file)
index 0000000..d1d5aaa
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL::Trying to read parameters...
+DEAL::Caught an exception -- ignoring...
+DEAL::test_1=1
+DEAL::test_2=0

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.