| 1 | // Copyright (C) 2017-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 | |
| 21 | #include <charconv> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | // Test std::to_chars error handling. |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | char buf[9] = "********"; |
| 30 | std::to_chars_result r; |
| 31 | |
| 32 | r = std::to_chars(buf, buf, 1); |
| 33 | VERIFY( r.ec == std::errc::value_too_large ); |
| 34 | VERIFY( r.ptr == buf ); |
| 35 | VERIFY( *r.ptr == '*' ); |
| 36 | |
| 37 | r = std::to_chars(buf, buf + 3, 0b1000, 2); |
| 38 | VERIFY( r.ec == std::errc::value_too_large ); |
| 39 | VERIFY( r.ptr == buf + 3 ); |
| 40 | VERIFY( *r.ptr == '*' ); |
| 41 | r = std::to_chars(buf, buf + 4, 0b1000, 2); |
| 42 | VERIFY( r.ec == std::errc{} ); |
| 43 | VERIFY( r.ptr == buf + 4 ); |
| 44 | VERIFY( *r.ptr == '*' ); |
| 45 | |
| 46 | r = std::to_chars(buf, buf + 4, 010000, 8); |
| 47 | VERIFY( r.ec == std::errc::value_too_large ); |
| 48 | VERIFY( r.ptr == buf + 4 ); |
| 49 | VERIFY( *r.ptr == '*' ); |
| 50 | r = std::to_chars(buf, buf + 5, 010000, 8); |
| 51 | VERIFY( r.ec == std::errc{} ); |
| 52 | VERIFY( r.ptr == buf + 5 ); |
| 53 | VERIFY( *r.ptr == '*' ); |
| 54 | |
| 55 | r = std::to_chars(buf, buf + 5, 100000, 10); |
| 56 | VERIFY( r.ec == std::errc::value_too_large ); |
| 57 | VERIFY( r.ptr == buf + 5 ); |
| 58 | VERIFY( *r.ptr == '*' ); |
| 59 | r = std::to_chars(buf, buf + 6, 100000, 10); |
| 60 | VERIFY( r.ec == std::errc{} ); |
| 61 | VERIFY( r.ptr == buf + 6 ); |
| 62 | VERIFY( *r.ptr == '*' ); |
| 63 | |
| 64 | r = std::to_chars(buf, buf + 6, 0x1000000, 16); |
| 65 | VERIFY( r.ec == std::errc::value_too_large ); |
| 66 | VERIFY( r.ptr == buf + 6 ); |
| 67 | VERIFY( *r.ptr == '*' ); |
| 68 | r = std::to_chars(buf, buf + 7, 0x1000000, 16); |
| 69 | VERIFY( r.ec == std::errc{} ); |
| 70 | VERIFY( r.ptr == buf + 7 ); |
| 71 | VERIFY( *r.ptr == '*' ); |
| 72 | } |
| 73 | |
| 74 | int |
| 75 | main() |
| 76 | { |
| 77 | test01(); |
| 78 | } |