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