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