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