xref: /llvm-project/libcxx/test/std/strings/string.conversions/stold.pass.cpp (revision f520c1445f2d9cfb6153e3c125076aa6e3a76fc8)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // XFAIL: libcpp-no-exceptions
11 // <string>
12 
13 // long double stold(const string& str, size_t *idx = 0);
14 // long double stold(const wstring& str, size_t *idx = 0);
15 
16 #include <iostream>
17 
18 #include <string>
19 #include <cmath>
20 #include <cassert>
21 
22 int main()
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     try
39     {
40         assert(std::stold("1.e60", &idx) == 1.e60L);
41         assert(idx == 5);
42     }
43     catch (const std::out_of_range&)
44     {
45         assert(false);
46     }
47     try
48     {
49         assert(std::stold(L"1.e60", &idx) == 1.e60L);
50         assert(idx == 5);
51     }
52     catch (const std::out_of_range&)
53     {
54         assert(false);
55     }
56     idx = 0;
57     try
58     {
59         assert(std::stold("1.e6000", &idx) == INFINITY);
60         assert(false);
61     }
62     catch (const std::out_of_range&)
63     {
64         assert(idx == 0);
65     }
66     try
67     {
68         assert(std::stold(L"1.e6000", &idx) == INFINITY);
69         assert(false);
70     }
71     catch (const std::out_of_range&)
72     {
73         assert(idx == 0);
74     }
75     try
76     {
77         assert(std::stold("INF", &idx) == INFINITY);
78         assert(idx == 3);
79     }
80     catch (const std::out_of_range&)
81     {
82         assert(false);
83     }
84     idx = 0;
85     try
86     {
87         assert(std::stold(L"INF", &idx) == INFINITY);
88         assert(idx == 3);
89     }
90     catch (const std::out_of_range&)
91     {
92         assert(false);
93     }
94     idx = 0;
95     try
96     {
97         assert(std::isnan(std::stold("NAN", &idx)));
98         assert(idx == 3);
99     }
100     catch (const std::out_of_range&)
101     {
102         assert(false);
103     }
104     idx = 0;
105     try
106     {
107         assert(std::isnan(std::stold(L"NAN", &idx)));
108         assert(idx == 3);
109     }
110     catch (const std::out_of_range&)
111     {
112         assert(false);
113     }
114     idx = 0;
115     try
116     {
117         std::stold("", &idx);
118         assert(false);
119     }
120     catch (const std::invalid_argument&)
121     {
122         assert(idx == 0);
123     }
124     try
125     {
126         std::stold(L"", &idx);
127         assert(false);
128     }
129     catch (const std::invalid_argument&)
130     {
131         assert(idx == 0);
132     }
133     try
134     {
135         std::stold("  - 8", &idx);
136         assert(false);
137     }
138     catch (const std::invalid_argument&)
139     {
140         assert(idx == 0);
141     }
142     try
143     {
144         std::stold(L"  - 8", &idx);
145         assert(false);
146     }
147     catch (const std::invalid_argument&)
148     {
149         assert(idx == 0);
150     }
151     try
152     {
153         std::stold("a1", &idx);
154         assert(false);
155     }
156     catch (const std::invalid_argument&)
157     {
158         assert(idx == 0);
159     }
160     try
161     {
162         std::stold(L"a1", &idx);
163         assert(false);
164     }
165     catch (const std::invalid_argument&)
166     {
167         assert(idx == 0);
168     }
169 }
170