From 47487f6be61be31be05745c6b4c0cbc9b84a42d8 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Thu, 5 Apr 2018 22:32:47 +0200 Subject: [PATCH] Fix bug in patterns.h --- include/deal.II/base/patterns.h | 20 +++++---- tests/parameter_handler/pattern_tools_11.cc | 45 +++++++++++++++++++ .../parameter_handler/pattern_tools_11.output | 2 + 3 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 tests/parameter_handler/pattern_tools_11.cc create mode 100644 tests/parameter_handler/pattern_tools_11.output diff --git a/include/deal.II/base/patterns.h b/include/deal.II/base/patterns.h index f8fea8f4fe..276f06964b 100644 --- a/include/deal.II/base/patterns.h +++ b/include/deal.II/base/patterns.h @@ -1389,15 +1389,17 @@ namespace Patterns const std::unique_ptr &p = Convert::to_pattern()) { - std::string str; - if (std::is_same() || std::is_same()) - str = std::to_string((int)value); + std::stringstream str; + if (std::is_same::value || + std::is_same::value || + std::is_same::value) + str << (int)value; else if (std::is_same::value) - str = value ? "true" : "false"; + str << (value ? "true" : "false"); else - str = std::to_string(value); - AssertThrow(p->match(str), ExcNoMatch(str, *p)); - return str; + str << value; + AssertThrow(p->match(str.str()), ExcNoMatch(str.str(), *p)); + return str.str(); } static T to_value(const std::string &s, @@ -1411,7 +1413,9 @@ namespace Patterns else { std::istringstream is(s); - if (std::is_same::value || std::is_same::value) + if (std::is_same::value || + std::is_same::value || + std::is_same::value) { int i; is >> i; diff --git a/tests/parameter_handler/pattern_tools_11.cc b/tests/parameter_handler/pattern_tools_11.cc new file mode 100644 index 0000000000..a55ec8bb5a --- /dev/null +++ b/tests/parameter_handler/pattern_tools_11.cc @@ -0,0 +1,45 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2005 - 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. +// +// --------------------------------------------------------------------- + +// Check Convert<...>::to_string() for small numbers, and make sure +// we don't break anything for integral types + +#include "../tests.h" +#include +#include +#include +#include + +int main() +{ + initlog(); + + double a = 1e-12; + int i = -1; + unsigned int j = 3; + unsigned char c = 3; + signed char c1 = -3; + char c2 = -3; + bool b = false; + + auto t = std::make_tuple(a,i,j,c,c1,c2,b); + + auto s = Patterns::Tools::Convert::to_string(t); + auto tt = Patterns::Tools::Convert::to_value(s); + auto r = Patterns::Tools::Convert::to_string(tt); + + deallog << "String: " << s + << ", roundtrip: " << r << std::endl; +} diff --git a/tests/parameter_handler/pattern_tools_11.output b/tests/parameter_handler/pattern_tools_11.output new file mode 100644 index 0000000000..e3207c9282 --- /dev/null +++ b/tests/parameter_handler/pattern_tools_11.output @@ -0,0 +1,2 @@ + +DEAL::String: 1e-12 : -1 : 3 : 3 : -3 : -3 : false, roundtrip: 1e-12 : -1 : 3 : 3 : -3 : -3 : false -- 2.39.5