blob: 444a69518c60fa0985f07525209e61f2a55a4104 [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//===----------------------------------------------------------------------===//
9
10// <string>
11
12// float stof(const string& str, size_t *idx = 0);
13// float stof(const wstring& str, size_t *idx = 0);
14
15#include <string>
16#include <cmath>
17#include <cassert>
18
19int main()
20{
21 assert(std::stof("0") == 0);
22 assert(std::stof(L"0") == 0);
23 assert(std::stof("-0") == 0);
24 assert(std::stof(L"-0") == 0);
25 assert(std::stof("-10") == -10);
26 assert(std::stof(L"-10.5") == -10.5);
27 assert(std::stof(" 10") == 10);
28 assert(std::stof(L" 10") == 10);
29 size_t idx = 0;
30 assert(std::stof("10g", &idx) == 10);
31 assert(idx == 2);
32 idx = 0;
33 assert(std::stof(L"10g", &idx) == 10);
34 assert(idx == 2);
Howard Hinnant1afbaba2013-01-14 18:59:43 +000035 idx = 0;
Howard Hinnantcbbf6332010-06-02 18:20:39 +000036 try
37 {
38 assert(std::stof("1.e60", &idx) == INFINITY);
Howard Hinnant1afbaba2013-01-14 18:59:43 +000039 assert(false);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000040 }
41 catch (const std::out_of_range&)
42 {
Howard Hinnant1afbaba2013-01-14 18:59:43 +000043 assert(idx == 0);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000044 }
45 try
46 {
47 assert(std::stof(L"1.e60", &idx) == INFINITY);
Howard Hinnant1afbaba2013-01-14 18:59:43 +000048 assert(false);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000049 }
50 catch (const std::out_of_range&)
51 {
Howard Hinnant1afbaba2013-01-14 18:59:43 +000052 assert(idx == 0);
Howard Hinnantcbbf6332010-06-02 18:20:39 +000053 }
54 idx = 0;
55 try
56 {
57 assert(std::stof("1.e360", &idx) == INFINITY);
58 assert(false);
59 }
60 catch (const std::out_of_range&)
61 {
62 assert(idx == 0);
63 }
64 try
65 {
66 assert(std::stof(L"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("INF", &idx) == INFINITY);
76 assert(idx == 3);
77 }
78 catch (const std::out_of_range&)
79 {
80 assert(false);
81 }
82 idx = 0;
83 try
84 {
85 assert(std::stof(L"INF", &idx) == INFINITY);
86 assert(idx == 3);
87 }
88 catch (const std::out_of_range&)
89 {
90 assert(false);
91 }
92 idx = 0;
93 try
94 {
95 assert(std::isnan(std::stof("NAN", &idx)));
96 assert(idx == 3);
97 }
98 catch (const std::out_of_range&)
99 {
100 assert(false);
101 }
102 idx = 0;
103 try
104 {
105 assert(std::isnan(std::stof(L"NAN", &idx)));
106 assert(idx == 3);
107 }
108 catch (const std::out_of_range&)
109 {
110 assert(false);
111 }
112 idx = 0;
113 try
114 {
115 std::stof("", &idx);
116 assert(false);
117 }
118 catch (const std::invalid_argument&)
119 {
120 assert(idx == 0);
121 }
122 try
123 {
124 std::stof(L"", &idx);
125 assert(false);
126 }
127 catch (const std::invalid_argument&)
128 {
129 assert(idx == 0);
130 }
131 try
132 {
133 std::stof(" - 8", &idx);
134 assert(false);
135 }
136 catch (const std::invalid_argument&)
137 {
138 assert(idx == 0);
139 }
140 try
141 {
142 std::stof(L" - 8", &idx);
143 assert(false);
144 }
145 catch (const std::invalid_argument&)
146 {
147 assert(idx == 0);
148 }
149 try
150 {
151 std::stof("a1", &idx);
152 assert(false);
153 }
154 catch (const std::invalid_argument&)
155 {
156 assert(idx == 0);
157 }
158 try
159 {
160 std::stof(L"a1", &idx);
161 assert(false);
162 }
163 catch (const std::invalid_argument&)
164 {
165 assert(idx == 0);
166 }
167}