xref: /llvm-project/libcxx/test/std/strings/string.conversions/stold.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
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 
21 int main(int, char**)
22 {
23     assert(std::stold("0") == 0);
24     assert(std::stold("-0") == 0);
25     assert(std::stold("-10") == -10);
26     assert(std::stold(" 10") == 10);
27     {
28         size_t idx = 0;
29         assert(std::stold("10g", &idx) == 10);
30         assert(idx == 2);
31     }
32     {
33         size_t idx = 0;
34         assert(std::stold("1.e60", &idx) == 1.e60L);
35         assert(idx == 5);
36     }
37     {
38         size_t idx = 0;
39         assert(std::stold("INF", &idx) == INFINITY);
40         assert(idx == 3);
41     }
42     {
43         size_t idx = 0;
44         assert(std::isnan(std::stold("NAN", &idx)));
45         assert(idx == 3);
46     }
47 
48 #ifndef TEST_HAS_NO_EXCEPTIONS
49     {
50         size_t idx = 0;
51         try {
52             std::stold("", &idx);
53             assert(false);
54         } catch (const std::invalid_argument&) {
55             assert(idx == 0);
56         }
57     }
58     {
59         size_t idx = 0;
60         try {
61             std::stold("  - 8", &idx);
62             assert(false);
63         } catch (const std::invalid_argument&) {
64             assert(idx == 0);
65         }
66     }
67     {
68         size_t idx = 0;
69         try {
70             std::stold("a1", &idx);
71             assert(false);
72         } catch (const std::invalid_argument&) {
73             assert(idx == 0);
74         }
75     }
76     {
77         size_t idx = 0;
78         try {
79             assert(std::stold("1.e6000", &idx) == INFINITY);
80             assert(false);
81         } catch (const std::out_of_range&) {
82             assert(idx == 0);
83         }
84     }
85 #endif // TEST_HAS_NO_EXCEPTIONS
86 
87 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
88     assert(std::stold(L"0") == 0);
89     assert(std::stold(L"-0") == 0);
90     assert(std::stold(L"-10.5") == -10.5);
91     assert(std::stold(L" 10") == 10);
92     {
93         size_t idx = 0;
94         assert(std::stold(L"10g", &idx) == 10);
95         assert(idx == 2);
96     }
97     {
98         size_t idx = 0;
99         assert(std::stold(L"1.e60", &idx) == 1.e60L);
100         assert(idx == 5);
101     }
102     {
103         size_t idx = 0;
104         assert(std::stold(L"INF", &idx) == INFINITY);
105         assert(idx == 3);
106     }
107     {
108         size_t idx = 0;
109         assert(std::isnan(std::stold(L"NAN", &idx)));
110         assert(idx == 3);
111     }
112 #ifndef TEST_HAS_NO_EXCEPTIONS
113     {
114         size_t idx = 0;
115         try {
116             std::stold(L"", &idx);
117             assert(false);
118         } catch (const std::invalid_argument&) {
119             assert(idx == 0);
120         }
121     }
122     {
123         size_t idx = 0;
124         try {
125             std::stold(L"  - 8", &idx);
126             assert(false);
127         } catch (const std::invalid_argument&) {
128             assert(idx == 0);
129         }
130     }
131     {
132         size_t idx = 0;
133         try {
134             std::stold(L"a1", &idx);
135             assert(false);
136         } catch (const std::invalid_argument&) {
137             assert(idx == 0);
138         }
139     }
140     {
141         size_t idx = 0;
142         try {
143             assert(std::stold(L"1.e6000", &idx) == INFINITY);
144             assert(false);
145         } catch (const std::out_of_range&) {
146             assert(idx == 0);
147         }
148     }
149 #endif // TEST_HAS_NO_EXCEPTIONS
150 #endif // TEST_HAS_NO_WIDE_CHARACTERS
151 
152   return 0;
153 }
154