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