blob: 38519a2feba0fa6c8e482e99fec778922d61cbda [file] [log] [blame]
Howard Hinnantcbbf6332010-06-02 18:20:39 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-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 Hinnantcbbf6332010-06-02 18:20:39 +00007//
8//===----------------------------------------------------------------------===//
Daniel Dunbar5ce9a532013-02-05 22:10:27 +00009//
Asiri Rathnayakef520c142015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Jonathan Roelofseb7b5e72015-01-14 23:38:12 +000011// XFAIL: with_system_cxx_lib=x86_64-apple-darwin11
12// XFAIL: with_system_cxx_lib=x86_64-apple-darwin12
Howard Hinnantcbbf6332010-06-02 18:20:39 +000013
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
23int main()
24{
25 assert(std::stof("0") == 0);
26 assert(std::stof(L"0") == 0);
27 assert(std::stof("-0") == 0);
28 assert(std::stof(L"-0") == 0);
29 assert(std::stof("-10") == -10);
30 assert(std::stof(L"-10.5") == -10.5);
31 assert(std::stof(" 10") == 10);
32 assert(std::stof(L" 10") == 10);
33 size_t idx = 0;
34 assert(std::stof("10g", &idx) == 10);
35 assert(idx == 2);
36 idx = 0;
37 assert(std::stof(L"10g", &idx) == 10);
38 assert(idx == 2);
Howard Hinnant1afbaba2013-01-14 18:59:43 +000039 idx = 0;
Howard Hinnantcbbf6332010-06-02 18:20:39 +000040 try
41 {
42 assert(std::stof("1.e60", &idx) == INFINITY);
Howard Hinnant1afbaba2013-01-14 18:59:43 +000043 assert(false);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000044 }
45 catch (const std::out_of_range&)
46 {
Howard Hinnant1afbaba2013-01-14 18:59:43 +000047 assert(idx == 0);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000048 }
49 try
50 {
51 assert(std::stof(L"1.e60", &idx) == INFINITY);
Howard Hinnant1afbaba2013-01-14 18:59:43 +000052 assert(false);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000053 }
54 catch (const std::out_of_range&)
55 {
Howard Hinnant1afbaba2013-01-14 18:59:43 +000056 assert(idx == 0);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000057 }
58 idx = 0;
59 try
60 {
61 assert(std::stof("1.e360", &idx) == INFINITY);
62 assert(false);
63 }
64 catch (const std::out_of_range&)
65 {
66 assert(idx == 0);
67 }
68 try
69 {
70 assert(std::stof(L"1.e360", &idx) == INFINITY);
71 assert(false);
72 }
73 catch (const std::out_of_range&)
74 {
75 assert(idx == 0);
76 }
77 try
78 {
79 assert(std::stof("INF", &idx) == INFINITY);
80 assert(idx == 3);
81 }
82 catch (const std::out_of_range&)
83 {
84 assert(false);
85 }
86 idx = 0;
87 try
88 {
89 assert(std::stof(L"INF", &idx) == INFINITY);
90 assert(idx == 3);
91 }
92 catch (const std::out_of_range&)
93 {
94 assert(false);
95 }
96 idx = 0;
97 try
98 {
99 assert(std::isnan(std::stof("NAN", &idx)));
100 assert(idx == 3);
101 }
102 catch (const std::out_of_range&)
103 {
104 assert(false);
105 }
106 idx = 0;
107 try
108 {
109 assert(std::isnan(std::stof(L"NAN", &idx)));
110 assert(idx == 3);
111 }
112 catch (const std::out_of_range&)
113 {
114 assert(false);
115 }
116 idx = 0;
117 try
118 {
119 std::stof("", &idx);
120 assert(false);
121 }
122 catch (const std::invalid_argument&)
123 {
124 assert(idx == 0);
125 }
126 try
127 {
128 std::stof(L"", &idx);
129 assert(false);
130 }
131 catch (const std::invalid_argument&)
132 {
133 assert(idx == 0);
134 }
135 try
136 {
137 std::stof(" - 8", &idx);
138 assert(false);
139 }
140 catch (const std::invalid_argument&)
141 {
142 assert(idx == 0);
143 }
144 try
145 {
146 std::stof(L" - 8", &idx);
147 assert(false);
148 }
149 catch (const std::invalid_argument&)
150 {
151 assert(idx == 0);
152 }
153 try
154 {
155 std::stof("a1", &idx);
156 assert(false);
157 }
158 catch (const std::invalid_argument&)
159 {
160 assert(idx == 0);
161 }
162 try
163 {
164 std::stof(L"a1", &idx);
165 assert(false);
166 }
167 catch (const std::invalid_argument&)
168 {
169 assert(idx == 0);
170 }
171}