1 // { dg-do run { target c++11 } }
2 // { dg-require-string-conversions "" }
3 // { dg-xfail-run-if "broken long double IO" { newlib_broken_long_double_io  } }
4 
5 // 2008-06-15  Paolo Carlini  <paolo.carlini@oracle.com>
6 
7 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
8 //
9 // This file is part of the GNU ISO C++ Library.  This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
13 // any later version.
14 
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3.  If not see
22 // <http://www.gnu.org/licenses/>.
23 
24 // 21.4 Numeric Conversions [string.conversions]
25 
26 #include <string>
27 #include <limits>
28 #include <stdexcept>
29 #include <testsuite_hooks.h>
30 
31 void
test01()32 test01()
33 {
34   bool test = false;
35 
36   using namespace std;
37 
38   try
39     {
40       string one;
41       stod(one);
42     }
43   catch(const std::invalid_argument&)
44     {
45       test = true;
46     }
47   catch(...)
48     {
49     }
50   VERIFY( test );
51 
52   test = false;
53   try
54     {
55       string one("a");
56       stod(one);
57     }
58   catch(const std::invalid_argument&)
59     {
60       test = true;
61     }
62   catch(...)
63     {
64     }
65   VERIFY( test );
66 
67   double d1 = 0.0;
68   size_t idx1 = 0;
69   try
70     {
71       string one("2.0a");
72       d1 = stod(one, &idx1);
73     }
74   catch(...)
75     {
76       test = false;
77     }
78   VERIFY( test );
79   VERIFY( d1 == 2.0 );
80   VERIFY( idx1 == 3 );
81 
82   test = false;
83   try
84     {
85       string one("1e");
86       one.append(2 * numeric_limits<double>::max_exponent10, '9');
87       d1 = stod(one);
88     }
89   catch(const std::out_of_range&)
90     {
91       test = true;
92     }
93   catch(...)
94     {
95     }
96   VERIFY( test );
97   VERIFY( d1 == 2.0 );
98 
99   try
100     {
101       long double ld0 = numeric_limits<double>::max() / 100.0;
102       string one(to_string(ld0));
103       stod(one);
104     }
105   catch(...)
106     {
107       test = false;
108     }
109   VERIFY( test );
110 
111   if ((numeric_limits<long double>::max() / 10000.0L)
112       > numeric_limits<double>::max())
113     {
114       test = false;
115       d1 = -1.0;
116       try
117 	{
118 	  long double ld1 = numeric_limits<double>::max();
119 	  ld1 *= 100.0;
120 	  string one(to_string(ld1));
121 	  d1 = stod(one);
122 	}
123       catch(const std::out_of_range&)
124 	{
125 	  test = true;
126 	}
127       catch(...)
128 	{
129 	}
130       VERIFY( test );
131       VERIFY( d1 == -1.0 );
132     }
133 }
134 
main()135 int main()
136 {
137   test01();
138   return 0;
139 }
140