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