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