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 // PR14919 was fixed in r172447, out_of_range wasn't thrown before.
11 // XFAIL: with_system_cxx_lib=macosx10.7
12 // XFAIL: with_system_cxx_lib=macosx10.8
13
14 // <string>
15
16 // float stof(const string& str, size_t *idx = 0);
17 // float stof(const wstring& str, size_t *idx = 0);
18
19 #include <string>
20 #include <cmath>
21 #include <cassert>
22 #include <stdexcept>
23
24 #include "test_macros.h"
25
main()26 int main()
27 {
28 assert(std::stof("0") == 0);
29 assert(std::stof(L"0") == 0);
30 assert(std::stof("-0") == 0);
31 assert(std::stof(L"-0") == 0);
32 assert(std::stof("-10") == -10);
33 assert(std::stof(L"-10.5") == -10.5);
34 assert(std::stof(" 10") == 10);
35 assert(std::stof(L" 10") == 10);
36 size_t idx = 0;
37 assert(std::stof("10g", &idx) == 10);
38 assert(idx == 2);
39 idx = 0;
40 assert(std::stof(L"10g", &idx) == 10);
41 assert(idx == 2);
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43 idx = 0;
44 try
45 {
46 assert(std::stof("1.e60", &idx) == INFINITY);
47 assert(false);
48 }
49 catch (const std::out_of_range&)
50 {
51 assert(idx == 0);
52 }
53 try
54 {
55 assert(std::stof(L"1.e60", &idx) == INFINITY);
56 assert(false);
57 }
58 catch (const std::out_of_range&)
59 {
60 assert(idx == 0);
61 }
62 idx = 0;
63 try
64 {
65 assert(std::stof("1.e360", &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::stof(L"1.e360", &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::stof("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::stof(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::stof("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::stof(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::stof("", &idx);
138 assert(false);
139 }
140 catch (const std::invalid_argument&)
141 {
142 assert(idx == 0);
143 }
144 try
145 {
146 std::stof(L"", &idx);
147 assert(false);
148 }
149 catch (const std::invalid_argument&)
150 {
151 assert(idx == 0);
152 }
153 try
154 {
155 std::stof(" - 8", &idx);
156 assert(false);
157 }
158 catch (const std::invalid_argument&)
159 {
160 assert(idx == 0);
161 }
162 try
163 {
164 std::stof(L" - 8", &idx);
165 assert(false);
166 }
167 catch (const std::invalid_argument&)
168 {
169 assert(idx == 0);
170 }
171 try
172 {
173 std::stof("a1", &idx);
174 assert(false);
175 }
176 catch (const std::invalid_argument&)
177 {
178 assert(idx == 0);
179 }
180 try
181 {
182 std::stof(L"a1", &idx);
183 assert(false);
184 }
185 catch (const std::invalid_argument&)
186 {
187 assert(idx == 0);
188 }
189 #endif
190 }
191