blob: 3e9b681ddeb1dc56e42d1c840c840870d1cc01ce [file] [log] [blame]
Howard Hinnanta6a062d2010-06-02 18:20:39 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnanta6a062d2010-06-02 18:20:39 +00007//
8//===----------------------------------------------------------------------===//
Daniel Dunbarc8e18892013-02-05 22:10:27 +00009//
Jonathan Roelofs33459612015-01-14 23:38:12 +000010// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11
11// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12
Howard Hinnanta6a062d2010-06-02 18:20:39 +000012
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
22int 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);
Howard Hinnant3e3ae9e2013-01-14 18:59:43 +000038 idx = 0;
Howard Hinnanta6a062d2010-06-02 18:20:39 +000039 try
40 {
41 assert(std::stof("1.e60", &idx) == INFINITY);
Howard Hinnant3e3ae9e2013-01-14 18:59:43 +000042 assert(false);
Howard Hinnanta6a062d2010-06-02 18:20:39 +000043 }
44 catch (const std::out_of_range&)
45 {
Howard Hinnant3e3ae9e2013-01-14 18:59:43 +000046 assert(idx == 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +000047 }
48 try
49 {
50 assert(std::stof(L"1.e60", &idx) == INFINITY);
Howard Hinnant3e3ae9e2013-01-14 18:59:43 +000051 assert(false);
Howard Hinnanta6a062d2010-06-02 18:20:39 +000052 }
53 catch (const std::out_of_range&)
54 {
Howard Hinnant3e3ae9e2013-01-14 18:59:43 +000055 assert(idx == 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +000056 }
57 idx = 0;
58 try
59 {
60 assert(std::stof("1.e360", &idx) == INFINITY);
61 assert(false);
62 }
63 catch (const std::out_of_range&)
64 {
65 assert(idx == 0);
66 }
67 try
68 {
69 assert(std::stof(L"1.e360", &idx) == INFINITY);
70 assert(false);
71 }
72 catch (const std::out_of_range&)
73 {
74 assert(idx == 0);
75 }
76 try
77 {
78 assert(std::stof("INF", &idx) == INFINITY);
79 assert(idx == 3);
80 }
81 catch (const std::out_of_range&)
82 {
83 assert(false);
84 }
85 idx = 0;
86 try
87 {
88 assert(std::stof(L"INF", &idx) == INFINITY);
89 assert(idx == 3);
90 }
91 catch (const std::out_of_range&)
92 {
93 assert(false);
94 }
95 idx = 0;
96 try
97 {
98 assert(std::isnan(std::stof("NAN", &idx)));
99 assert(idx == 3);
100 }
101 catch (const std::out_of_range&)
102 {
103 assert(false);
104 }
105 idx = 0;
106 try
107 {
108 assert(std::isnan(std::stof(L"NAN", &idx)));
109 assert(idx == 3);
110 }
111 catch (const std::out_of_range&)
112 {
113 assert(false);
114 }
115 idx = 0;
116 try
117 {
118 std::stof("", &idx);
119 assert(false);
120 }
121 catch (const std::invalid_argument&)
122 {
123 assert(idx == 0);
124 }
125 try
126 {
127 std::stof(L"", &idx);
128 assert(false);
129 }
130 catch (const std::invalid_argument&)
131 {
132 assert(idx == 0);
133 }
134 try
135 {
136 std::stof(" - 8", &idx);
137 assert(false);
138 }
139 catch (const std::invalid_argument&)
140 {
141 assert(idx == 0);
142 }
143 try
144 {
145 std::stof(L" - 8", &idx);
146 assert(false);
147 }
148 catch (const std::invalid_argument&)
149 {
150 assert(idx == 0);
151 }
152 try
153 {
154 std::stof("a1", &idx);
155 assert(false);
156 }
157 catch (const std::invalid_argument&)
158 {
159 assert(idx == 0);
160 }
161 try
162 {
163 std::stof(L"a1", &idx);
164 assert(false);
165 }
166 catch (const std::invalid_argument&)
167 {
168 assert(idx == 0);
169 }
170}