From c05a33cb6f2eabaadb04ade26d4de33e2448b03f Mon Sep 17 00:00:00 2001 From: bangerth Date: Thu, 19 Dec 2013 19:02:44 +0000 Subject: [PATCH] Detect in ParameterHandler::get_double/integer whenever the text of a parameter is not a number. git-svn-id: https://svn.dealii.org/trunk@32065 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 8 +++ deal.II/source/base/parameter_handler.cc | 21 +++++-- tests/bits/parameter_handler_17.cc | 74 ++++++++++++++++++++++++ tests/bits/parameter_handler_17.output | 4 ++ 4 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 tests/bits/parameter_handler_17.cc create mode 100644 tests/bits/parameter_handler_17.output diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 63dd4ad503..bdde5bfdd4 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -59,6 +59,14 @@ inconvenience this causes.

Specific improvements

    +
  1. Fixed: ParameterHandler::get_double() and ParameterHandler::get_integer() + had bugs in that they didn't detect if they were asked to return a number + for a parameter whose value was in fact not a number but some general + text. This is now fixed. +
    + (Wolfgang Bangerth, 2013/12/19) +
  2. +
  3. Fixed: VectorTools::project_boundary_values could not deal with function values close to (but not exactly equal to) zero. This is now fixed.
    diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index 58f27f55fb..8742ec4c08 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -1643,9 +1643,14 @@ long int ParameterHandler::get_integer (const std::string &entry_string) const { std::string s = get (entry_string); char *endptr; - long int i = std::strtol (s.c_str(), &endptr, 10); - // assert there was no error - AssertThrow (*endptr == '\0', ExcConversionError(s)); + const long int i = std::strtol (s.c_str(), &endptr, 10); + + // assert that there was no error. an error would be if + // either there was no string to begin with, or if + // strtol set the endptr to anything but the end of + // the string + AssertThrow ((s.size()>0) && (*endptr == '\0'), + ExcConversionError(s)); return i; } @@ -1657,9 +1662,13 @@ double ParameterHandler::get_double (const std::string &entry_string) const std::string s = get (entry_string); char *endptr; double d = std::strtod (s.c_str(), &endptr); - // assert there was no error - AssertThrow ((*s.c_str() != '\0') || (*endptr == '\0'), - ExcConversionError(s)); + + // assert that there was no error. an error would be if + // either there was no string to begin with, or if + // strtol set the endptr to anything but the end of + // the string + AssertThrow ((s.size()>0) && (*endptr == '\0'), + ExcConversionError(s)); return d; } diff --git a/tests/bits/parameter_handler_17.cc b/tests/bits/parameter_handler_17.cc new file mode 100644 index 0000000000..b03e3662be --- /dev/null +++ b/tests/bits/parameter_handler_17.cc @@ -0,0 +1,74 @@ +// --------------------------------------------------------------------- +// $Id$ +// +// Copyright (C) 2002 - 2013 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. +// +// --------------------------------------------------------------------- + + + +// One can (accidentally) call ParameterHandler::get_int/double on parameters +// that are really strings. There used to be a bug in these functions in that +// they didn't throw an error when the string wasn't actually convertible to a +// number + +#include "../tests.h" +#include +#include +#include + +void check () +{ + ParameterHandler prm; + prm.declare_entry ("a", "this that and the other", Patterns::Anything(), + ""); + try + { + prm.get_double ("a"); + } + catch (const ParameterHandler::ExcConversionError &) + { + deallog << "get_double() detected the mistake" << std::endl; + } + + try + { + prm.get_integer ("a"); + } + catch (const ParameterHandler::ExcConversionError &) + { + deallog << "get_integer() detected the mistake" << std::endl; + } + + try + { + prm.get_bool ("a"); + } + catch (const ParameterHandler::ExcConversionError &) + { + deallog << "get_bool() detected the mistake" << std::endl; + } + +} + + +int main () +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + check (); + + return 0; +} diff --git a/tests/bits/parameter_handler_17.output b/tests/bits/parameter_handler_17.output new file mode 100644 index 0000000000..58d69e3503 --- /dev/null +++ b/tests/bits/parameter_handler_17.output @@ -0,0 +1,4 @@ + +DEAL::get_double() detected the mistake +DEAL::get_integer() detected the mistake +DEAL::get_bool() detected the mistake -- 2.39.5