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