1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  
     | 
    
      // Copyright (C) 2020-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.
// { dg-do run { target c++23 } }
// { dg-add-options ieee }
// { dg-additional-options "-DSKIP_LONG_DOUBLE" { target aarch64-*-vxworks* } }
#include <charconv>
#include <string>
#include <limits>
#include <stdfloat>
#include <cmath>
#include <cstdlib>
#include <testsuite_hooks.h>
// Test std::from_chars floating-point conversions.
#if __cpp_lib_to_chars >= 201611L
#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
void
test01()
{
  std::string s;
  std::float64_t f64;
  std::from_chars_result res;
  for (auto fmt : { std::chars_format::fixed, std::chars_format::scientific,
		    std::chars_format::general, std::chars_format::hex })
  {
    s = "Info";
    res = std::from_chars(s.data(), s.data() + s.length(), f64, fmt);
    VERIFY( std::isinf(f64) );
    VERIFY( res.ptr == s.data() + 3 );
    VERIFY( res.ec == std::errc{} );
    s = "-INFIN";
    res = std::from_chars(s.data(), s.data() + s.length(), f64, fmt);
    VERIFY( std::isinf(f64) );
    VERIFY( f64 < 0 );
    VERIFY( res.ptr == s.data() + 4 );
    VERIFY( res.ec == std::errc{} );
    s = "InFiNiTy aNd BeYoNd";
    res = std::from_chars(s.data(), s.data() + s.length(), f64, fmt);
    VERIFY( std::isinf(f64) );
    VERIFY( res.ptr == s.data() + 8 );
    VERIFY( res.ec == std::errc{} );
    s = "nAn";
    res = std::from_chars(s.data(), s.data() + s.length(), f64, fmt);
    VERIFY( std::isnan(f64) );
    VERIFY( res.ptr == s.data() + 3 );
    VERIFY( res.ec == std::errc{} );
    s = "-NAN()";
    res = std::from_chars(s.data(), s.data() + s.length(), f64, fmt);
    VERIFY( std::isnan(f64) );
    VERIFY( res.ptr == s.data() + s.length() );
    VERIFY( res.ec == std::errc{} );
  }
}
void
test02()
{
  std::string s;
  std::float64_t f64 = 1.0f64;
  std::from_chars_result res;
  s = "0x123";
  res = std::from_chars(s.data(), s.data() + s.length(), f64);
  VERIFY( f64 == 0.0f64 );
  VERIFY( res.ptr == s.data() + 1 );
  VERIFY( res.ec == std::errc{} );
  f64 = 1.0f64;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::fixed);
  VERIFY( f64 == 0.0f64 );
  VERIFY( res.ptr == s.data() + 1 );
  VERIFY( res.ec == std::errc{} );
  f64 = 1.0f64;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::scientific);
  VERIFY( f64 == 1.0f64 );
  VERIFY( res.ptr == s.data() );
  VERIFY( res.ec == std::errc::invalid_argument );
  f64 = 1.0f64;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::general);
  VERIFY( f64 == 0.0f64 );
  VERIFY( res.ptr == s.data() + 1 );
  VERIFY( res.ec == std::errc{} );
  f64 = 1.0f64;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::hex);
  VERIFY( f64 == 0.0f64 );
  VERIFY( res.ptr == s.data() + 1 );
  VERIFY( res.ec == std::errc{} );
}
void
test03()
{
  std::string s;
  std::float64_t f64 = 1.0f64;
  std::from_chars_result res;
  s = "0.5e+2azzz";
  res = std::from_chars(s.data(), s.data() + s.length(), f64);
  VERIFY( f64 == 0.5e+2f64 );
  VERIFY( res.ptr == s.data() + s.length() - 1 - 3 );
  VERIFY( res.ec == std::errc{} );
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::fixed);
  VERIFY( f64 == 0.5f64 );
  VERIFY( res.ptr == s.data() + 3 );
  VERIFY( res.ec == std::errc{} );
  f64 = 1.0f64;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::scientific);
  VERIFY( f64 == 0.5e+2f64 );
  VERIFY( res.ptr == s.data() + s.length() - 1 - 3 );
  VERIFY( res.ec == std::errc{} );
  f64 = 1.0f64;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::general);
  VERIFY( f64 == 0.5e+2f64 );
  VERIFY( res.ptr == s.data() + s.length() - 1 - 3 );
  VERIFY( res.ec == std::errc{} );
  f64 = 1.0;
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::hex);
  VERIFY( f64 == 0x0.5Ep0f64 );
  VERIFY( res.ptr == s.data() + 4 );
  VERIFY( res.ec == std::errc{} );
  s = "1.Ap-2zzz";
  res = std::from_chars(s.data(), s.data() + s.length(), f64,
			std::chars_format::hex);
  VERIFY( f64 == 0.40625f64 );
  VERIFY( res.ptr == s.data() + s.length() - 3 );
  VERIFY( res.ec == std::errc{} );
}
void
test04()
{
  // Huge input strings
  std::string s(1000, '0');
  std::float64_t f64 = 1.0f64;
  std::from_chars_result res;
  res = std::from_chars(s.data(), s.data() + s.length(), f64);
  VERIFY( res.ptr == s.data() + s.length() );
  VERIFY( res.ec == std::errc{} );
  VERIFY( f64 == 0.0f64 );
  s += ".5";
  res = std::from_chars(s.data(), s.data() + s.length(), f64);
  VERIFY( res.ptr == s.data() + s.length() );
  VERIFY( res.ec == std::errc{} );
  VERIFY( f64 == 0.5f64 );
  s += "e2";
  auto len = s.length();
  s += std::string(1000, 'a');
  res = std::from_chars(s.data(), s.data() + s.length(), f64);
  VERIFY( res.ptr == s.data() + len );
  VERIFY( res.ec == std::errc{} );
  VERIFY( f64 == 50.f64 );
}
#endif
using std::to_string;
#ifdef __GLIBCXX_TYPE_INT_N_0
std::string
to_string(unsigned __GLIBCXX_TYPE_INT_N_0 val)
{
  using Limits = std::numeric_limits<unsigned __GLIBCXX_TYPE_INT_N_0>;
  std::string s(Limits::digits10+2, '0');
  for (auto iter = s.end(); val != 0; val /= 10)
    *--iter = '0' + (val % 10);
  return s;
}
#endif
template<typename FloatT>
void
test_small_num()
{
  std::from_chars_result res;
  FloatT flt;
  // Small integer values that are exactly representable
  for (int i = 0; i < 100; ++i)
  {
    std::string s = to_string(i);
    int len = s.length();
    s += "123";
    const char* s1 = s.c_str();
    const char* s1_end = s1 + len;
    for (auto fmt : { std::chars_format::fixed,
		      std::chars_format::general,
		      std::chars_format::hex })
    {
      if (fmt == std::chars_format::hex && i > 9)
	continue;
      res = std::from_chars(s1, s1_end, flt, fmt);
      VERIFY( res.ec == std::errc{} );
      VERIFY( res.ptr == s1_end );
      VERIFY( flt == i );
    }
    if (i > 9)
      continue;
    // Test single-digit integers with small exponents.
    const char s2[] = { '.', *s1, 'e', '0', '0', '0', '1' };
    const char* s2_end = s2 + sizeof(s2);
    const char s3[] = { *s1, '0', 'e', '-', '0', '0', '1' };
    const char* s3_end = s3 + sizeof(s3);
    for (auto fmt : { std::chars_format::scientific,
		      std::chars_format::general })
    {
      res = std::from_chars(s2, s2_end, flt, fmt);
      VERIFY( res.ec == std::errc{} );
      VERIFY( res.ptr == s2_end );
      VERIFY( flt == i );
      res = std::from_chars(s3, s3_end, flt, fmt);
      VERIFY( res.ec == std::errc{} );
      VERIFY( res.ptr == s3_end );
      VERIFY( flt == i );
    }
  }
}
void
test05()
{
#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  test_small_num<std::float32_t>();
#endif
#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  test_small_num<std::float64_t>();
#endif
#if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  test_small_num<std::float128_t>();
#endif
}
template<typename FloatT, typename UIntT>
void
test_max_mantissa()
{
  using Float_limits = std::numeric_limits<FloatT>;
  using UInt_limits = std::numeric_limits<UIntT>;
  if (Float_limits::is_iec559 && Float_limits::digits < UInt_limits::digits)
  {
#ifdef _GLIBCXX_USE_C99_MATH_FUNCS
    std::printf("Testing %d-bit float, using %zu-bit integer\n",
	Float_limits::digits + (int)std::log2(Float_limits::max_exponent) + 1,
	sizeof(UIntT) * __CHAR_BIT__);
#endif
    std::from_chars_result res;
    FloatT flt;
    for (int i = 0; i < 10; ++i)
    {
      // (1 << digits) - 1 is the maximum value of the mantissa
      const auto val = ((UIntT)1 << Float_limits::digits) - 1 - i;
      std::string s = to_string(val);
      auto len = s.length();
      s += "000"; // these should be ignored
      for (auto fmt : { std::chars_format::fixed,
			std::chars_format::general })
      {
	res = std::from_chars(s.data(), s.data() + len, flt, fmt);
	VERIFY( res.ec == std::errc{} );
	VERIFY( res.ptr == s.data() + len );
	VERIFY( flt == val );
      }
      s.resize(len);
      const auto orig_len = len;
      s += "e+000";
      len = s.length();
      s += "111";
      for (auto fmt : { std::chars_format::scientific,
			std::chars_format::general })
      {
	res = std::from_chars(s.data(), s.data() + len, flt, fmt);
	VERIFY( res.ec == std::errc{} );
	VERIFY( res.ptr == s.data() + len );
	VERIFY( flt == val );
	std::string s2 = s.substr(0, len - 5);
	s2.insert(s2.begin() + orig_len - 1, '.');
	s2 += "e000000000001";
	res = std::from_chars(s.data(), s.data() + len, flt, fmt);
	VERIFY( res.ec == std::errc{} );
	VERIFY( res.ptr == s.data() + len );
	VERIFY( flt == val );
      }
    }
  }
}
void
test06()
{
#if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  test_max_mantissa<std::float32_t, unsigned long>();
#endif
#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  test_max_mantissa<std::float64_t, unsigned long long>();
#endif
#if defined(__GLIBCXX_TYPE_INT_N_0) && !defined SKIP_LONG_DOUBLE \
    && defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  test_max_mantissa<std::float128_t, unsigned __GLIBCXX_TYPE_INT_N_0>();
#endif
}
#endif
int
main()
{
#if __cpp_lib_to_chars >= 201611L
#if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  test01();
  test02();
  test03();
  test04();
#endif
  test05();
  test06();
#endif
}
 
     |