From 5c0df9bd397df4095483b42a3d2980349d687c90 Mon Sep 17 00:00:00 2001 From: bangerth Date: Mon, 20 Dec 2010 14:45:33 +0000 Subject: [PATCH] Verify that parameters we read in XML format also satisfy their patterns. git-svn-id: https://svn.dealii.org/trunk@23024 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/source/base/parameter_handler.cc | 30 ++++++- .../parameter_handler_read_xml/cmp/generic | 2 +- tests/bits/parameter_handler_read_xml/prm | 2 +- .../parameter_handler_read_xml_error_04.cc | 83 +++++++++++++++++++ .../cmp/generic | 2 + .../parameter_handler_read_xml_error_04/prm | 4 + 6 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 tests/bits/parameter_handler_read_xml_error_04.cc create mode 100644 tests/bits/parameter_handler_read_xml_error_04/cmp/generic create mode 100644 tests/bits/parameter_handler_read_xml_error_04/prm diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index 4958dd9b45..33ce681886 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -1162,6 +1162,8 @@ namespace read_xml_recursively (const boost::property_tree::ptree &source, const std::string ¤t_path, const char path_separator, + const std::vector > & + patterns, boost::property_tree::ptree &destination) { for (boost::property_tree::ptree::const_iterator p = source.begin(); @@ -1186,10 +1188,32 @@ namespace path_separator + "value")) { + // first make sure that the + // new entry actually + // satisfies its constraints + const std::string new_value + = p->second.get("value"); + + const unsigned int pattern_index + = destination.get (full_path + + path_separator + + "pattern"); + if (patterns[pattern_index]->match(new_value) == false) + { + std::cerr << " The entry value" << std::endl + << " " << new_value << std::endl + << " for the entry named" << std::endl + << " " << full_path << std::endl + << " does not match the given pattern" << std::endl + << " " << patterns[pattern_index]->description() + << std::endl; + return false; + } + // set the found parameter in // the destination argument destination.put (full_path + path_separator + "value", - p->second.get_optional("value")); + new_value); // this node might have // sub-nodes in addition to @@ -1221,6 +1245,7 @@ namespace p->first : current_path + path_separator + p->first), path_separator, + patterns, destination); // see if the recursive read @@ -1283,7 +1308,8 @@ bool ParameterHandler::read_input_from_xml (std::istream &in) const boost::property_tree::ptree &my_entries = single_node_tree.get_child("ParameterHandler"); - return read_xml_recursively (my_entries, "", path_separator, *entries); + return read_xml_recursively (my_entries, "", path_separator, patterns, + *entries); } diff --git a/tests/bits/parameter_handler_read_xml/cmp/generic b/tests/bits/parameter_handler_read_xml/cmp/generic index c9f5c7e983..a7ccff8cf6 100644 --- a/tests/bits/parameter_handler_read_xml/cmp/generic +++ b/tests/bits/parameter_handler_read_xml/cmp/generic @@ -1,3 +1,3 @@ - 21doc 10[Integer range -2147483648...2147483647 (inclusive)] 32doc 21[Integer range -2147483648...2147483647 (inclusive)] 2.2341.234doc 32[Double -1.79769e+308...1.79769e+308 (inclusive)] 5.3214.321doc 43[Double -1.79769e+308...1.79769e+308 (inclusive)] __< & > ; /< & > ; /docs 14[Anything] __225[Integer range -2147483648...2147483647 (inclusive)] 7.14159266.1415926docs 36[Double -1.79769e+308...1.79769e+308 (inclusive)] + 21doc 10[Integer range -2147483648...2147483647 (inclusive)] 32doc 21[Integer range -2147483648...2147483647 (inclusive)] 2.2341.234doc 32[Double -1.79769e+308...1.79769e+308 (inclusive)] 5.3214.321doc 43[Double -1.79769e+308...1.79769e+308 (inclusive)] __< & > ; /< & > ; /docs 14[Anything] 225[Integer range -2147483648...2147483647 (inclusive)] 7.14159266.1415926docs 36[Double -1.79769e+308...1.79769e+308 (inclusive)] diff --git a/tests/bits/parameter_handler_read_xml/prm b/tests/bits/parameter_handler_read_xml/prm index cad7566e20..0c4f5a41bd 100644 --- a/tests/bits/parameter_handler_read_xml/prm +++ b/tests/bits/parameter_handler_read_xml/prm @@ -1,4 +1,4 @@ -21doc 10[Integer range -2147483648...2147483647 (inclusive)]32doc 21[Integer range -2147483648...2147483647 (inclusive)]2.2341.234doc 32[Double -1.79769e+308...1.79769e+308 (inclusive)]5.3214.321doc 43[Double -1.79769e+308...1.79769e+308 (inclusive)]__< & > ; /< & > ; /docs 14[Anything]__22 +21doc 10[Integer range -2147483648...2147483647 (inclusive)]32doc 21[Integer range -2147483648...2147483647 (inclusive)]2.2341.234doc 32[Double -1.79769e+308...1.79769e+308 (inclusive)]5.3214.321doc 43[Double -1.79769e+308...1.79769e+308 (inclusive)]__< & > ; /< & > ; /docs 14[Anything]22 5[Integer range -2147483648...2147483647 (inclusive)]7.14159266.1415926docs 36[Double -1.79769e+308...1.79769e+308 (inclusive)] diff --git a/tests/bits/parameter_handler_read_xml_error_04.cc b/tests/bits/parameter_handler_read_xml_error_04.cc new file mode 100644 index 0000000000..21cb95315e --- /dev/null +++ b/tests/bits/parameter_handler_read_xml_error_04.cc @@ -0,0 +1,83 @@ +//---------------------------- parameter_handler_read_xml_error_04.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2002, 2003, 2004, 2005, 2010 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- parameter_handler_read_xml_error_04.cc --------------------------- + + +// check ParameterHandler::read_input_from_xml. try to read a file with a +// parameter that does not satisfy its pattern + +#include "../tests.h" +#include +#include +#include + + +int main () +{ + std::ofstream logfile("parameter_handler_read_xml_error_04/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + ParameterHandler prm; + prm.declare_entry ("int1", + "1", + Patterns::Integer(), + "doc 1"); + prm.declare_entry ("int2", + "2", + Patterns::Integer(), + "doc 2"); + prm.enter_subsection ("ss1"); + { + prm.declare_entry ("double 1", + "1.234", + Patterns::Double(), + "doc 3"); + + prm.enter_subsection ("ss2"); + { + prm.declare_entry ("double 2", + "4.321", + Patterns::Double(), + "doc 4"); + } + prm.leave_subsection (); + } + prm.leave_subsection (); + + // things with strange characters + prm.enter_subsection ("Testing%testing"); + { + prm.declare_entry ("string&list", + "< & > ; /", + Patterns::Anything(), + "docs 1"); + prm.declare_entry ("int*int", + "2", + Patterns::Integer()); + prm.declare_entry ("double+double", + "6.1415926", + Patterns::Double(), + "docs 3"); + } + prm.leave_subsection (); + + // read from XML + std::ifstream in ("parameter_handler_read_xml_error_04/prm"); + bool result = prm.read_input_from_xml (in); + Assert (result == false, ExcInternalError()); + + deallog << "OK" << std::endl; + + return 0; +} diff --git a/tests/bits/parameter_handler_read_xml_error_04/cmp/generic b/tests/bits/parameter_handler_read_xml_error_04/cmp/generic new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/bits/parameter_handler_read_xml_error_04/cmp/generic @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/bits/parameter_handler_read_xml_error_04/prm b/tests/bits/parameter_handler_read_xml_error_04/prm new file mode 100644 index 0000000000..2d17ac6714 --- /dev/null +++ b/tests/bits/parameter_handler_read_xml_error_04/prm @@ -0,0 +1,4 @@ + + +2x1doc 10[Integer range -2147483648...2147483647 (inclusive)]32doc 21[Integer range -2147483648...2147483647 (inclusive)]2.2341.234doc 32[Double -1.79769e+308...1.79769e+308 (inclusive)]5.3214.321doc 43[Double -1.79769e+308...1.79769e+308 (inclusive)]__< & > ; /< & > ; /docs 14[Anything]22 +5[Integer range -2147483648...2147483647 (inclusive)]7.14159266.1415926docs 36[Double -1.79769e+308...1.79769e+308 (inclusive)] -- 2.39.5