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