Debian Codegraph @

libstdc++-v3/testsuite/20_util/from_chars/6.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8
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.
13
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/>.
17
18 // <charconv> is supported in C++14 as a GNU extension
19 // { dg-do run { target c++14 } }
20 // { dg-add-options ieee }
21
22 #include <charconv>
23 #include <string>
24 #include <cfenv>
25 #include <testsuite_hooks.h>
26
27 void
28 test01()
29 {
30 #if __cpp_lib_to_chars >= 201611L
31 #if _GLIBCXX_USE_C99_FENV_TR1
32 double d;
33 #ifdef FE_DOWNWARD
34 std::fesetround(FE_DOWNWARD);
35 #endif
36 const std::string s = "0.099999999999999999999999999";
37 auto res = std::from_chars(s.data(), s.data() + s.length(), d);
38 VERIFY( res.ec == std::errc{} );
39 VERIFY( res.ptr == s.data() + s.length() );
40 // std::from_chars should ignore the current rounding mode
41 // and always round to nearest.
42 VERIFY( d == (double) 0.1 );
43 #endif
44 #endif
45 }
46
47 int
48 main()
49 {
50 test01();
51 }