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_lib=x86_64-apple-darwin11
11 // XFAIL: with_system_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
main()22 int main()
23 {
24 assert(std::stof("0") == 0);
25 assert(std::stof(L"0") == 0);
26 assert(std::stof("-0") == 0);
27 assert(std::stof(L"-0") == 0);
28 assert(std::stof("-10") == -10);
29 assert(std::stof(L"-10.5") == -10.5);
30 assert(std::stof(" 10") == 10);
31 assert(std::stof(L" 10") == 10);
32 size_t idx = 0;
33 assert(std::stof("10g", &idx) == 10);
34 assert(idx == 2);
35 idx = 0;
36 assert(std::stof(L"10g", &idx) == 10);
37 assert(idx == 2);
38 idx = 0;
39 try
40 {
41 // 21.5p4 and p6: "call strtod [...] Throws out_of_range if strtod
42 // [...] sets errno to ERANGE". 1.e60 fits in a double, so while this
43 // does return INFINITY it shouldn't throw according to the standard.
44 assert(std::stof("1.e60", &idx) == INFINITY);
45 assert(idx == 5);
46 }
47 catch (const std::out_of_range&)
48 {
49 assert(false);
50 }
51 try
52 {
53 // 21.5p11 and p13: "call wcstod [...] Throws out_of_range if wcstod
54 // [...] sets errno to ERANGE". 1.e60 fits in a double, so while this
55 // does return INFINITY it shouldn't throw according to the standard.
56 assert(std::stof(L"1.e60", &idx) == INFINITY);
57 assert(idx == 5);
58 }
59 catch (const std::out_of_range&)
60 {
61 assert(false);
62 }
63 idx = 0;
64 try
65 {
66 assert(std::stof("1.e360", &idx) == INFINITY);
67 assert(false);
68 }
69 catch (const std::out_of_range&)
70 {
71 assert(idx == 0);
72 }
73 try
74 {
75 assert(std::stof(L"1.e360", &idx) == INFINITY);
76 assert(false);
77 }
78 catch (const std::out_of_range&)
79 {
80 assert(idx == 0);
81 }
82 try
83 {
84 assert(std::stof("INF", &idx) == INFINITY);
85 assert(idx == 3);
86 }
87 catch (const std::out_of_range&)
88 {
89 assert(false);
90 }
91 idx = 0;
92 try
93 {
94 assert(std::stof(L"INF", &idx) == INFINITY);
95 assert(idx == 3);
96 }
97 catch (const std::out_of_range&)
98 {
99 assert(false);
100 }
101 idx = 0;
102 try
103 {
104 assert(std::isnan(std::stof("NAN", &idx)));
105 assert(idx == 3);
106 }
107 catch (const std::out_of_range&)
108 {
109 assert(false);
110 }
111 idx = 0;
112 try
113 {
114 assert(std::isnan(std::stof(L"NAN", &idx)));
115 assert(idx == 3);
116 }
117 catch (const std::out_of_range&)
118 {
119 assert(false);
120 }
121 idx = 0;
122 try
123 {
124 std::stof("", &idx);
125 assert(false);
126 }
127 catch (const std::invalid_argument&)
128 {
129 assert(idx == 0);
130 }
131 try
132 {
133 std::stof(L"", &idx);
134 assert(false);
135 }
136 catch (const std::invalid_argument&)
137 {
138 assert(idx == 0);
139 }
140 try
141 {
142 std::stof(" - 8", &idx);
143 assert(false);
144 }
145 catch (const std::invalid_argument&)
146 {
147 assert(idx == 0);
148 }
149 try
150 {
151 std::stof(L" - 8", &idx);
152 assert(false);
153 }
154 catch (const std::invalid_argument&)
155 {
156 assert(idx == 0);
157 }
158 try
159 {
160 std::stof("a1", &idx);
161 assert(false);
162 }
163 catch (const std::invalid_argument&)
164 {
165 assert(idx == 0);
166 }
167 try
168 {
169 std::stof(L"a1", &idx);
170 assert(false);
171 }
172 catch (const std::invalid_argument&)
173 {
174 assert(idx == 0);
175 }
176 }
177