xref: /aosp_15_r20/external/libcxx/test/std/strings/string.conversions/stof.pass.cpp (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker //
5*58b9f456SAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker //
8*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*58b9f456SAndroid Build Coastguard Worker //
10*58b9f456SAndroid Build Coastguard Worker // PR14919 was fixed in r172447, out_of_range wasn't thrown before.
11*58b9f456SAndroid Build Coastguard Worker // XFAIL: with_system_cxx_lib=macosx10.7
12*58b9f456SAndroid Build Coastguard Worker // XFAIL: with_system_cxx_lib=macosx10.8
13*58b9f456SAndroid Build Coastguard Worker 
14*58b9f456SAndroid Build Coastguard Worker // <string>
15*58b9f456SAndroid Build Coastguard Worker 
16*58b9f456SAndroid Build Coastguard Worker // float stof(const string& str, size_t *idx = 0);
17*58b9f456SAndroid Build Coastguard Worker // float stof(const wstring& str, size_t *idx = 0);
18*58b9f456SAndroid Build Coastguard Worker 
19*58b9f456SAndroid Build Coastguard Worker #include <string>
20*58b9f456SAndroid Build Coastguard Worker #include <cmath>
21*58b9f456SAndroid Build Coastguard Worker #include <cassert>
22*58b9f456SAndroid Build Coastguard Worker #include <stdexcept>
23*58b9f456SAndroid Build Coastguard Worker 
24*58b9f456SAndroid Build Coastguard Worker #include "test_macros.h"
25*58b9f456SAndroid Build Coastguard Worker 
main()26*58b9f456SAndroid Build Coastguard Worker int main()
27*58b9f456SAndroid Build Coastguard Worker {
28*58b9f456SAndroid Build Coastguard Worker     assert(std::stof("0") == 0);
29*58b9f456SAndroid Build Coastguard Worker     assert(std::stof(L"0") == 0);
30*58b9f456SAndroid Build Coastguard Worker     assert(std::stof("-0") == 0);
31*58b9f456SAndroid Build Coastguard Worker     assert(std::stof(L"-0") == 0);
32*58b9f456SAndroid Build Coastguard Worker     assert(std::stof("-10") == -10);
33*58b9f456SAndroid Build Coastguard Worker     assert(std::stof(L"-10.5") == -10.5);
34*58b9f456SAndroid Build Coastguard Worker     assert(std::stof(" 10") == 10);
35*58b9f456SAndroid Build Coastguard Worker     assert(std::stof(L" 10") == 10);
36*58b9f456SAndroid Build Coastguard Worker     size_t idx = 0;
37*58b9f456SAndroid Build Coastguard Worker     assert(std::stof("10g", &idx) == 10);
38*58b9f456SAndroid Build Coastguard Worker     assert(idx == 2);
39*58b9f456SAndroid Build Coastguard Worker     idx = 0;
40*58b9f456SAndroid Build Coastguard Worker     assert(std::stof(L"10g", &idx) == 10);
41*58b9f456SAndroid Build Coastguard Worker     assert(idx == 2);
42*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
43*58b9f456SAndroid Build Coastguard Worker     idx = 0;
44*58b9f456SAndroid Build Coastguard Worker     try
45*58b9f456SAndroid Build Coastguard Worker     {
46*58b9f456SAndroid Build Coastguard Worker         assert(std::stof("1.e60", &idx) == INFINITY);
47*58b9f456SAndroid Build Coastguard Worker         assert(false);
48*58b9f456SAndroid Build Coastguard Worker     }
49*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
50*58b9f456SAndroid Build Coastguard Worker     {
51*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
52*58b9f456SAndroid Build Coastguard Worker     }
53*58b9f456SAndroid Build Coastguard Worker     try
54*58b9f456SAndroid Build Coastguard Worker     {
55*58b9f456SAndroid Build Coastguard Worker         assert(std::stof(L"1.e60", &idx) == INFINITY);
56*58b9f456SAndroid Build Coastguard Worker         assert(false);
57*58b9f456SAndroid Build Coastguard Worker     }
58*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
59*58b9f456SAndroid Build Coastguard Worker     {
60*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
61*58b9f456SAndroid Build Coastguard Worker     }
62*58b9f456SAndroid Build Coastguard Worker     idx = 0;
63*58b9f456SAndroid Build Coastguard Worker     try
64*58b9f456SAndroid Build Coastguard Worker     {
65*58b9f456SAndroid Build Coastguard Worker         assert(std::stof("1.e360", &idx) == INFINITY);
66*58b9f456SAndroid Build Coastguard Worker         assert(false);
67*58b9f456SAndroid Build Coastguard Worker     }
68*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
69*58b9f456SAndroid Build Coastguard Worker     {
70*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
71*58b9f456SAndroid Build Coastguard Worker     }
72*58b9f456SAndroid Build Coastguard Worker     try
73*58b9f456SAndroid Build Coastguard Worker     {
74*58b9f456SAndroid Build Coastguard Worker         assert(std::stof(L"1.e360", &idx) == INFINITY);
75*58b9f456SAndroid Build Coastguard Worker         assert(false);
76*58b9f456SAndroid Build Coastguard Worker     }
77*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
78*58b9f456SAndroid Build Coastguard Worker     {
79*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
80*58b9f456SAndroid Build Coastguard Worker     }
81*58b9f456SAndroid Build Coastguard Worker     try
82*58b9f456SAndroid Build Coastguard Worker #endif
83*58b9f456SAndroid Build Coastguard Worker     {
84*58b9f456SAndroid Build Coastguard Worker         assert(std::stof("INF", &idx) == INFINITY);
85*58b9f456SAndroid Build Coastguard Worker         assert(idx == 3);
86*58b9f456SAndroid Build Coastguard Worker     }
87*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
88*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
89*58b9f456SAndroid Build Coastguard Worker     {
90*58b9f456SAndroid Build Coastguard Worker         assert(false);
91*58b9f456SAndroid Build Coastguard Worker     }
92*58b9f456SAndroid Build Coastguard Worker #endif
93*58b9f456SAndroid Build Coastguard Worker     idx = 0;
94*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
95*58b9f456SAndroid Build Coastguard Worker     try
96*58b9f456SAndroid Build Coastguard Worker #endif
97*58b9f456SAndroid Build Coastguard Worker     {
98*58b9f456SAndroid Build Coastguard Worker         assert(std::stof(L"INF", &idx) == INFINITY);
99*58b9f456SAndroid Build Coastguard Worker         assert(idx == 3);
100*58b9f456SAndroid Build Coastguard Worker     }
101*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
102*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
103*58b9f456SAndroid Build Coastguard Worker     {
104*58b9f456SAndroid Build Coastguard Worker         assert(false);
105*58b9f456SAndroid Build Coastguard Worker     }
106*58b9f456SAndroid Build Coastguard Worker #endif
107*58b9f456SAndroid Build Coastguard Worker     idx = 0;
108*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
109*58b9f456SAndroid Build Coastguard Worker     try
110*58b9f456SAndroid Build Coastguard Worker #endif
111*58b9f456SAndroid Build Coastguard Worker     {
112*58b9f456SAndroid Build Coastguard Worker         assert(std::isnan(std::stof("NAN", &idx)));
113*58b9f456SAndroid Build Coastguard Worker         assert(idx == 3);
114*58b9f456SAndroid Build Coastguard Worker     }
115*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
116*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
117*58b9f456SAndroid Build Coastguard Worker     {
118*58b9f456SAndroid Build Coastguard Worker         assert(false);
119*58b9f456SAndroid Build Coastguard Worker     }
120*58b9f456SAndroid Build Coastguard Worker #endif
121*58b9f456SAndroid Build Coastguard Worker     idx = 0;
122*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
123*58b9f456SAndroid Build Coastguard Worker     try
124*58b9f456SAndroid Build Coastguard Worker #endif
125*58b9f456SAndroid Build Coastguard Worker     {
126*58b9f456SAndroid Build Coastguard Worker         assert(std::isnan(std::stof(L"NAN", &idx)));
127*58b9f456SAndroid Build Coastguard Worker         assert(idx == 3);
128*58b9f456SAndroid Build Coastguard Worker     }
129*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_HAS_NO_EXCEPTIONS
130*58b9f456SAndroid Build Coastguard Worker     catch (const std::out_of_range&)
131*58b9f456SAndroid Build Coastguard Worker     {
132*58b9f456SAndroid Build Coastguard Worker         assert(false);
133*58b9f456SAndroid Build Coastguard Worker     }
134*58b9f456SAndroid Build Coastguard Worker     idx = 0;
135*58b9f456SAndroid Build Coastguard Worker     try
136*58b9f456SAndroid Build Coastguard Worker     {
137*58b9f456SAndroid Build Coastguard Worker         std::stof("", &idx);
138*58b9f456SAndroid Build Coastguard Worker         assert(false);
139*58b9f456SAndroid Build Coastguard Worker     }
140*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
141*58b9f456SAndroid Build Coastguard Worker     {
142*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
143*58b9f456SAndroid Build Coastguard Worker     }
144*58b9f456SAndroid Build Coastguard Worker     try
145*58b9f456SAndroid Build Coastguard Worker     {
146*58b9f456SAndroid Build Coastguard Worker         std::stof(L"", &idx);
147*58b9f456SAndroid Build Coastguard Worker         assert(false);
148*58b9f456SAndroid Build Coastguard Worker     }
149*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
150*58b9f456SAndroid Build Coastguard Worker     {
151*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
152*58b9f456SAndroid Build Coastguard Worker     }
153*58b9f456SAndroid Build Coastguard Worker     try
154*58b9f456SAndroid Build Coastguard Worker     {
155*58b9f456SAndroid Build Coastguard Worker         std::stof("  - 8", &idx);
156*58b9f456SAndroid Build Coastguard Worker         assert(false);
157*58b9f456SAndroid Build Coastguard Worker     }
158*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
159*58b9f456SAndroid Build Coastguard Worker     {
160*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
161*58b9f456SAndroid Build Coastguard Worker     }
162*58b9f456SAndroid Build Coastguard Worker     try
163*58b9f456SAndroid Build Coastguard Worker     {
164*58b9f456SAndroid Build Coastguard Worker         std::stof(L"  - 8", &idx);
165*58b9f456SAndroid Build Coastguard Worker         assert(false);
166*58b9f456SAndroid Build Coastguard Worker     }
167*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
168*58b9f456SAndroid Build Coastguard Worker     {
169*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
170*58b9f456SAndroid Build Coastguard Worker     }
171*58b9f456SAndroid Build Coastguard Worker     try
172*58b9f456SAndroid Build Coastguard Worker     {
173*58b9f456SAndroid Build Coastguard Worker         std::stof("a1", &idx);
174*58b9f456SAndroid Build Coastguard Worker         assert(false);
175*58b9f456SAndroid Build Coastguard Worker     }
176*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
177*58b9f456SAndroid Build Coastguard Worker     {
178*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
179*58b9f456SAndroid Build Coastguard Worker     }
180*58b9f456SAndroid Build Coastguard Worker     try
181*58b9f456SAndroid Build Coastguard Worker     {
182*58b9f456SAndroid Build Coastguard Worker         std::stof(L"a1", &idx);
183*58b9f456SAndroid Build Coastguard Worker         assert(false);
184*58b9f456SAndroid Build Coastguard Worker     }
185*58b9f456SAndroid Build Coastguard Worker     catch (const std::invalid_argument&)
186*58b9f456SAndroid Build Coastguard Worker     {
187*58b9f456SAndroid Build Coastguard Worker         assert(idx == 0);
188*58b9f456SAndroid Build Coastguard Worker     }
189*58b9f456SAndroid Build Coastguard Worker #endif
190*58b9f456SAndroid Build Coastguard Worker }
191