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