| 1 |
// { dg-do compile { target c++23 } } |
| 2 |
|
| 3 |
#include <charconv> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
constexpr bool |
| 7 |
test() |
| 8 |
{ |
| 9 |
const char str[] = "-01234afz###"; |
| 10 |
const char* end = str + sizeof(str); |
| 11 |
|
| 12 |
std::from_chars_result res; |
| 13 |
int ival = 99; |
| 14 |
unsigned uval = 99; |
| 15 |
|
| 16 |
res = std::from_chars(str, str+1, ival, 10); |
| 17 |
VERIFY( res.ptr == str ); |
| 18 |
VERIFY( res.ec == std::errc::invalid_argument ); |
| 19 |
VERIFY( ival == 99 ); |
| 20 |
res = std::from_chars(str, str+4, ival, 10); |
| 21 |
VERIFY( res.ptr == str+4 ); |
| 22 |
VERIFY( res.ec == std::errc{} ); |
| 23 |
VERIFY( ival == -12 ); |
| 24 |
res = std::from_chars(str, end, ival, 10); |
| 25 |
VERIFY( res.ptr == str+6 ); |
| 26 |
VERIFY( res.ec == std::errc{} ); |
| 27 |
VERIFY( ival == -1234 ); |
| 28 |
|
| 29 |
res = std::from_chars(str, end, uval, 10); |
| 30 |
VERIFY( res.ptr == str ); |
| 31 |
VERIFY( res.ec == std::errc::invalid_argument ); |
| 32 |
VERIFY( uval == 99 ); |
| 33 |
res = std::from_chars(str+1, end, uval, 10); |
| 34 |
VERIFY( res.ptr == str+6 ); |
| 35 |
VERIFY( res.ec == std::errc{} ); |
| 36 |
VERIFY( uval == 1234 ); |
| 37 |
|
| 38 |
res = std::from_chars(str, end, ival, 3); |
| 39 |
VERIFY( res.ptr == str+4 ); |
| 40 |
VERIFY( res.ec == std::errc{} ); |
| 41 |
VERIFY( ival == -5 ); |
| 42 |
|
| 43 |
res = std::from_chars(str, end, ival, 16); |
| 44 |
VERIFY( res.ptr == str+8 ); |
| 45 |
VERIFY( res.ec == std::errc{} ); |
| 46 |
VERIFY( ival == -1193135 ); |
| 47 |
|
| 48 |
res = std::from_chars(str+1, end, uval, 36); |
| 49 |
VERIFY( res.ptr == str+1+8 ); |
| 50 |
VERIFY( res.ec == std::errc{} ); |
| 51 |
VERIFY( uval == 2302953695 ); |
| 52 |
|
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
static_assert( test() ); |