| 1 |
// { dg-do run { target c++23 } } |
| 2 |
// { dg-timeout-factor 2 } |
| 3 |
|
| 4 |
#include <format> |
| 5 |
#include <vector> |
| 6 |
#include <testsuite_hooks.h> |
| 7 |
|
| 8 |
static_assert(!std::formattable<std::vector<bool>::reference, int>); |
| 9 |
static_assert(!std::formattable<std::vector<bool>::reference, char32_t>); |
| 10 |
|
| 11 |
template<typename... Args> |
| 12 |
bool |
| 13 |
is_format_string_for(const char* str, Args&&... args) |
| 14 |
{ |
| 15 |
try { |
| 16 |
(void) std::vformat(str, std::make_format_args(args...)); |
| 17 |
return true; |
| 18 |
} catch (const std::format_error&) { |
| 19 |
return false; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 24 |
#define WIDEN(S) WIDEN_(_CharT, S) |
| 25 |
|
| 26 |
void |
| 27 |
test_format_string() |
| 28 |
{ |
| 29 |
std::vector<bool> v(1, true); |
| 30 |
VERIFY( !is_format_string_for("{:?}", v[0]) ); |
| 31 |
VERIFY( !is_format_string_for("{:P}", v[0]) ); |
| 32 |
|
| 33 |
// width needs to be integer type |
| 34 |
VERIFY( !is_format_string_for("{:{}}", v[0], 1.0f) ); |
| 35 |
} |
| 36 |
|
| 37 |
template<typename _CharT> |
| 38 |
void |
| 39 |
test_output() |
| 40 |
{ |
| 41 |
std::basic_string<_CharT> res; |
| 42 |
size_t size = 0; |
| 43 |
std::vector<bool> v{true, false}; |
| 44 |
|
| 45 |
res = std::format(WIDEN("{}"), v[0]); |
| 46 |
VERIFY( res == WIDEN("true") ); |
| 47 |
|
| 48 |
res = std::format(WIDEN("{:s}"), v[1]); |
| 49 |
VERIFY( res == WIDEN("false") ); |
| 50 |
|
| 51 |
res = std::format(WIDEN("{:d} {:#B} {:#o} {:#x}"), v[0], v[1], v[0], v[1]); |
| 52 |
VERIFY( res == WIDEN("1 0B0 01 0x0") ); |
| 53 |
|
| 54 |
res = std::format(WIDEN("{:{}}"), v[0], 6); |
| 55 |
VERIFY( res == WIDEN("true ") ); |
| 56 |
|
| 57 |
res = std::format(WIDEN("{:=^#7X}"), v[1]); |
| 58 |
VERIFY( res == WIDEN("==0X0==") ); |
| 59 |
|
| 60 |
res = std::format(WIDEN("{}"), v); |
| 61 |
VERIFY( res == WIDEN("[true, false]") ); |
| 62 |
|
| 63 |
res = std::format(WIDEN("{::d}"), v); |
| 64 |
VERIFY( res == WIDEN("[1, 0]") ); |
| 65 |
} |
| 66 |
|
| 67 |
int main() |
| 68 |
{ |
| 69 |
test_format_string(); |
| 70 |
test_output<char>(); |
| 71 |
test_output<wchar_t>(); |
| 72 |
} |