]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Fix bug in patterns.h
authorLuca Heltai <luca.heltai@sissa.it>
Thu, 5 Apr 2018 20:32:47 +0000 (22:32 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Thu, 5 Apr 2018 20:32:47 +0000 (22:32 +0200)
include/deal.II/base/patterns.h
tests/parameter_handler/pattern_tools_11.cc [new file with mode: 0644]
tests/parameter_handler/pattern_tools_11.output [new file with mode: 0644]

index f8fea8f4feb1f3ffbd7d6c5f33ef0cbec08b648b..276f06964bb05e07e121c919a51059eeebf7feb6 100644 (file)
@@ -1389,15 +1389,17 @@ namespace Patterns
                                    const std::unique_ptr<Patterns::PatternBase>
                                    &p = Convert<T>::to_pattern())
       {
-        std::string str;
-        if (std::is_same<T, unsigned char>() || std::is_same<T, signed char>())
-          str = std::to_string((int)value);
+        std::stringstream str;
+        if (std::is_same<T, unsigned char>::value ||
+            std::is_same<T, signed char>::value   ||
+            std::is_same<T, char>::value)
+          str << (int)value;
         else  if (std::is_same<T,bool>::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<T, unsigned char>::value || std::is_same<T, signed char>::value)
+            if (std::is_same<T, unsigned char>::value ||
+                std::is_same<T, signed char>::value   ||
+                std::is_same<T, char>::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 (file)
index 0000000..a55ec8b
--- /dev/null
@@ -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 <deal.II/base/parameter_handler.h>
+#include <deal.II/base/std_cxx14/memory.h>
+#include <memory>
+#include <tuple>
+
+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<decltype(t)>::to_string(t);
+  auto tt = Patterns::Tools::Convert<decltype(t)>::to_value(s);
+  auto r = Patterns::Tools::Convert<decltype(t)>::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 (file)
index 0000000..e3207c9
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::String: 1e-12 : -1 : 3 : 3 : -3 : -3 : false, roundtrip: 1e-12 : -1 : 3 : 3 : -3 : -3 : false

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.