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