1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // <charconv> is supported in C++14 as a GNU extension
19 // { dg-do run { target c++14 } }
20 // { dg-add-options ieee }
21 // { dg-additional-options "-DSKIP_LONG_DOUBLE" { target aarch64-*-rtems* aarch64-*-vxworks* x86_64-*-vxworks* } }
28 #include <testsuite_hooks.h>
30 // Test std::from_chars floating-point conversions.
32 #if __cpp_lib_to_chars >= 201611L
38 std::from_chars_result res
;
40 for (auto fmt
: { std::chars_format::fixed
, std::chars_format::scientific
,
41 std::chars_format::general
, std::chars_format::hex
})
44 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
, fmt
);
45 VERIFY( std::isinf(d
) );
46 VERIFY( res
.ptr
== s
.data() + 3 );
47 VERIFY( res
.ec
== std::errc
{} );
50 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
, fmt
);
51 VERIFY( std::isinf(d
) );
53 VERIFY( res
.ptr
== s
.data() + 4 );
54 VERIFY( res
.ec
== std::errc
{} );
56 s
= "InFiNiTy aNd BeYoNd";
57 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
, fmt
);
58 VERIFY( std::isinf(d
) );
59 VERIFY( res
.ptr
== s
.data() + 8 );
60 VERIFY( res
.ec
== std::errc
{} );
63 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
, fmt
);
64 VERIFY( std::isnan(d
) );
65 VERIFY( res
.ptr
== s
.data() + 3 );
66 VERIFY( res
.ec
== std::errc
{} );
69 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
, fmt
);
70 VERIFY( std::isnan(d
) );
71 VERIFY( res
.ptr
== s
.data() + s
.length() );
72 VERIFY( res
.ec
== std::errc
{} );
81 std::from_chars_result res
;
84 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
);
86 VERIFY( res
.ptr
== s
.data() + 1 );
87 VERIFY( res
.ec
== std::errc
{} );
90 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
91 std::chars_format::fixed
);
93 VERIFY( res
.ptr
== s
.data() + 1 );
94 VERIFY( res
.ec
== std::errc
{} );
97 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
98 std::chars_format::scientific
);
100 VERIFY( res
.ptr
== s
.data() );
101 VERIFY( res
.ec
== std::errc::invalid_argument
);
104 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
105 std::chars_format::general
);
107 VERIFY( res
.ptr
== s
.data() + 1 );
108 VERIFY( res
.ec
== std::errc
{} );
111 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
112 std::chars_format::hex
);
114 VERIFY( res
.ptr
== s
.data() + 1 );
115 VERIFY( res
.ec
== std::errc
{} );
123 std::from_chars_result res
;
126 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
);
127 VERIFY( d
== 0.5e+2 );
128 VERIFY( res
.ptr
== s
.data() + s
.length() - 1 - 3 );
129 VERIFY( res
.ec
== std::errc
{} );
131 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
132 std::chars_format::fixed
);
134 VERIFY( res
.ptr
== s
.data() + 3 );
135 VERIFY( res
.ec
== std::errc
{} );
138 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
139 std::chars_format::scientific
);
140 VERIFY( d
== 0.5e+2 );
141 VERIFY( res
.ptr
== s
.data() + s
.length() - 1 - 3 );
142 VERIFY( res
.ec
== std::errc
{} );
145 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
146 std::chars_format::general
);
147 VERIFY( d
== 0.5e+2 );
148 VERIFY( res
.ptr
== s
.data() + s
.length() - 1 - 3 );
149 VERIFY( res
.ec
== std::errc
{} );
152 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
153 std::chars_format::hex
);
154 VERIFY( d
== 0x0.5Ep0
);
155 VERIFY( res
.ptr
== s
.data() + 4 );
156 VERIFY( res
.ec
== std::errc
{} );
159 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
,
160 std::chars_format::hex
);
161 VERIFY( d
== 0.40625 );
162 VERIFY( res
.ptr
== s
.data() + s
.length() - 3 );
163 VERIFY( res
.ec
== std::errc
{} );
169 // Huge input strings
170 std::string
s(1000, '0');
172 std::from_chars_result res
;
173 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
);
174 VERIFY( res
.ptr
== s
.data() + s
.length() );
175 VERIFY( res
.ec
== std::errc
{} );
179 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
);
180 VERIFY( res
.ptr
== s
.data() + s
.length() );
181 VERIFY( res
.ec
== std::errc
{} );
185 auto len
= s
.length();
186 s
+= std::string(1000, 'a');
187 res
= std::from_chars(s
.data(), s
.data() + s
.length(), d
);
188 VERIFY( res
.ptr
== s
.data() + len
);
189 VERIFY( res
.ec
== std::errc
{} );
193 using std::to_string
;
195 #ifdef __GLIBCXX_TYPE_INT_N_0
197 to_string(unsigned __GLIBCXX_TYPE_INT_N_0 val
)
199 using Limits
= std::numeric_limits
<unsigned __GLIBCXX_TYPE_INT_N_0
>;
200 std::string
s(Limits::digits10
+2, '0');
201 for (auto iter
= s
.end(); val
!= 0; val
/= 10)
202 *--iter
= '0' + (val
% 10);
210 std::from_chars_result res
;
215 // Small integer values that are exactly representable
217 for (int i
= 0; i
< 100; ++i
)
219 std::string s
= to_string(i
);
220 int len
= s
.length();
222 const char* s1
= s
.c_str();
223 const char* s1_end
= s1
+ len
;
225 for (auto fmt
: { std::chars_format::fixed
,
226 std::chars_format::general
,
227 std::chars_format::hex
})
229 if (fmt
== std::chars_format::hex
&& i
> 9)
232 res
= std::from_chars(s1
, s1_end
, flt
, fmt
);
233 VERIFY( res
.ec
== std::errc
{} );
234 VERIFY( res
.ptr
== s1_end
);
237 res
= std::from_chars(s1
, s1_end
, dbl
, fmt
);
238 VERIFY( res
.ec
== std::errc
{} );
239 VERIFY( res
.ptr
== s1_end
);
242 res
= std::from_chars(s1
, s1_end
, ldbl
, fmt
);
243 VERIFY( res
.ec
== std::errc
{} );
244 VERIFY( res
.ptr
== s1_end
);
251 // Test single-digit integers with small exponents.
253 const char s2
[] = { '.', *s1
, 'e', '0', '0', '0', '1' };
254 const char* s2_end
= s2
+ sizeof(s2
);
256 const char s3
[] = { *s1
, '0', 'e', '-', '0', '0', '1' };
257 const char* s3_end
= s3
+ sizeof(s3
);
259 for (auto fmt
: { std::chars_format::scientific
,
260 std::chars_format::general
})
262 res
= std::from_chars(s2
, s2_end
, flt
, fmt
);
263 VERIFY( res
.ec
== std::errc
{} );
264 VERIFY( res
.ptr
== s2_end
);
267 res
= std::from_chars(s3
, s3_end
, flt
, fmt
);
268 VERIFY( res
.ec
== std::errc
{} );
269 VERIFY( res
.ptr
== s3_end
);
272 res
= std::from_chars(s2
, s2_end
, dbl
, fmt
);
273 VERIFY( res
.ec
== std::errc
{} );
274 VERIFY( res
.ptr
== s2_end
);
277 res
= std::from_chars(s3
, s3_end
, dbl
, fmt
);
278 VERIFY( res
.ec
== std::errc
{} );
279 VERIFY( res
.ptr
== s3_end
);
282 res
= std::from_chars(s2
, s2_end
, ldbl
, fmt
);
283 VERIFY( res
.ec
== std::errc
{} );
284 VERIFY( res
.ptr
== s2_end
);
287 res
= std::from_chars(s3
, s3_end
, ldbl
, fmt
);
288 VERIFY( res
.ec
== std::errc
{} );
289 VERIFY( res
.ptr
== s3_end
);
295 template<typename FloatT
, typename UIntT
>
299 using Float_limits
= std::numeric_limits
<FloatT
>;
300 using UInt_limits
= std::numeric_limits
<UIntT
>;
302 if (Float_limits::is_iec559
&& Float_limits::digits
< UInt_limits::digits
)
304 #ifdef _GLIBCXX_USE_C99_MATH_FUNCS
305 std::printf("Testing %d-bit float, using %zu-bit integer\n",
306 Float_limits::digits
+ (int)std::log2(Float_limits::max_exponent
) + 1,
307 sizeof(UIntT
) * __CHAR_BIT__
);
310 std::from_chars_result res
;
313 for (int i
= 0; i
< 10; ++i
)
315 // (1 << digits) - 1 is the maximum value of the mantissa
316 const auto val
= ((UIntT
)1 << Float_limits::digits
) - 1 - i
;
317 std::string s
= to_string(val
);
318 auto len
= s
.length();
319 s
+= "000"; // these should be ignored
320 for (auto fmt
: { std::chars_format::fixed
,
321 std::chars_format::general
})
323 res
= std::from_chars(s
.data(), s
.data() + len
, flt
, fmt
);
324 VERIFY( res
.ec
== std::errc
{} );
325 VERIFY( res
.ptr
== s
.data() + len
);
326 VERIFY( flt
== val
);
329 const auto orig_len
= len
;
333 for (auto fmt
: { std::chars_format::scientific
,
334 std::chars_format::general
})
336 res
= std::from_chars(s
.data(), s
.data() + len
, flt
, fmt
);
337 VERIFY( res
.ec
== std::errc
{} );
338 VERIFY( res
.ptr
== s
.data() + len
);
339 VERIFY( flt
== val
);
341 std::string s2
= s
.substr(0, len
- 5);
342 s2
.insert(s2
.begin() + orig_len
- 1, '.');
343 s2
+= "e000000000001";
344 res
= std::from_chars(s
.data(), s
.data() + len
, flt
, fmt
);
345 VERIFY( res
.ec
== std::errc
{} );
346 VERIFY( res
.ptr
== s
.data() + len
);
347 VERIFY( flt
== val
);
356 test_max_mantissa
<float, unsigned long>();
357 test_max_mantissa
<double, unsigned long long>();
358 #if defined __GLIBCXX_TYPE_INT_N_0 && !defined SKIP_LONG_DOUBLE
359 test_max_mantissa
<long double, unsigned __GLIBCXX_TYPE_INT_N_0
>();
367 #if __cpp_lib_to_chars >= 201611L