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