| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <flat_map> |
| 4 |
#include <format> |
| 5 |
#include <testsuite_hooks.h> |
| 6 |
#include <vector> |
| 7 |
|
| 8 |
#define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 9 |
#define WIDEN(S) WIDEN_(_CharT, S) |
| 10 |
|
| 11 |
template<typename T, |
| 12 |
template<typename, typename> class Formatter = std::range_formatter> |
| 13 |
struct MyVector : std::vector<T> |
| 14 |
{ |
| 15 |
using std::vector<T>::vector; |
| 16 |
}; |
| 17 |
|
| 18 |
template<typename T, |
| 19 |
template<typename, typename> class Formatter, |
| 20 |
typename CharT> |
| 21 |
struct std::formatter<MyVector<T, Formatter>, CharT> |
| 22 |
{ |
| 23 |
constexpr formatter() noexcept |
| 24 |
{ |
| 25 |
using _CharT = CharT; |
| 26 |
_formatter.set_brackets(WIDEN("<"), WIDEN(">")); |
| 27 |
_formatter.set_separator(WIDEN("; ")); |
| 28 |
} |
| 29 |
|
| 30 |
constexpr std::basic_format_parse_context<CharT>::iterator |
| 31 |
parse(std::basic_format_parse_context<CharT>& pc) |
| 32 |
{ return _formatter.parse(pc); } |
| 33 |
|
| 34 |
template<typename Out> |
| 35 |
typename std::basic_format_context<Out, CharT>::iterator |
| 36 |
format(const MyVector<T, Formatter>& mv, |
| 37 |
std::basic_format_context<Out, CharT>& fc) const |
| 38 |
{ return _formatter.format(mv, fc); } |
| 39 |
|
| 40 |
private: |
| 41 |
Formatter<T, CharT> _formatter; |
| 42 |
}; |
| 43 |
|
| 44 |
template<typename _CharT, template<typename, typename> class Formatter> |
| 45 |
void |
| 46 |
test_default() |
| 47 |
{ |
| 48 |
MyVector<int, Formatter> vec{1, 2, 3}; |
| 49 |
std::basic_string<_CharT> res; |
| 50 |
|
| 51 |
res = std::format(WIDEN("{}"), vec); |
| 52 |
VERIFY( res == WIDEN("<1; 2; 3>") ); |
| 53 |
res = std::format(WIDEN("{:}"), vec); |
| 54 |
VERIFY( res == WIDEN("<1; 2; 3>") ); |
| 55 |
res = std::format(WIDEN("{:n}"), vec); |
| 56 |
VERIFY( res == WIDEN("1; 2; 3") ); |
| 57 |
|
| 58 |
res = std::format(WIDEN("{:3}"), vec); |
| 59 |
VERIFY( res == WIDEN("<1; 2; 3>") ); |
| 60 |
|
| 61 |
res = std::format(WIDEN("{:10}"), vec); |
| 62 |
VERIFY( res == WIDEN("<1; 2; 3> ") ); |
| 63 |
|
| 64 |
res = std::format(WIDEN("{:{}}"), vec, 10); |
| 65 |
VERIFY( res == WIDEN("<1; 2; 3> ") ); |
| 66 |
|
| 67 |
res = std::format(WIDEN("{1:{0}}"), 10, vec); |
| 68 |
VERIFY( res == WIDEN("<1; 2; 3> ") ); |
| 69 |
|
| 70 |
res = std::format(WIDEN("{:10n}"), vec); |
| 71 |
VERIFY( res == WIDEN("1; 2; 3 ") ); |
| 72 |
|
| 73 |
res = std::format(WIDEN("{:*<11}"), vec); |
| 74 |
VERIFY( res == WIDEN("<1; 2; 3>**") ); |
| 75 |
|
| 76 |
res = std::format(WIDEN("{:->12}"), vec); |
| 77 |
VERIFY( res == WIDEN("---<1; 2; 3>") ); |
| 78 |
|
| 79 |
res = std::format(WIDEN("{:=^13}"), vec); |
| 80 |
VERIFY( res == WIDEN("==<1; 2; 3>==") ); |
| 81 |
|
| 82 |
res = std::format(WIDEN("{:=^13n}"), vec); |
| 83 |
VERIFY( res == WIDEN("===1; 2; 3===") ); |
| 84 |
|
| 85 |
res = std::format(WIDEN("{::#x}"), vec); |
| 86 |
VERIFY( res == WIDEN("<0x1; 0x2; 0x3>") ); |
| 87 |
|
| 88 |
res = std::format(WIDEN("{:|^25n:#05x}"), vec); |
| 89 |
VERIFY( res == WIDEN("|||0x001; 0x002; 0x003|||") ); |
| 90 |
|
| 91 |
// ':' is start of the format string for element |
| 92 |
res = std::format(WIDEN("{::^+4}"), vec); |
| 93 |
VERIFY( res == WIDEN("< +1 ; +2 ; +3 >") ); |
| 94 |
} |
| 95 |
|
| 96 |
template<typename _CharT, template<typename, typename> class Formatter> |
| 97 |
void |
| 98 |
test_override() |
| 99 |
{ |
| 100 |
MyVector<_CharT, Formatter> vc{'a', 'b', 'c', 'd'}; |
| 101 |
MyVector<std::pair<int, int>, Formatter> vp{{1, 11}, {2, 21}}; |
| 102 |
std::basic_string<_CharT> res; |
| 103 |
|
| 104 |
res = std::format(WIDEN("{:s}"), vc); |
| 105 |
VERIFY( res == WIDEN("abcd") ); |
| 106 |
res = std::format(WIDEN("{:?s}"), vc); |
| 107 |
VERIFY( res == WIDEN("\"abcd\"") ); |
| 108 |
res = std::format(WIDEN("{:+^6s}"), vc); |
| 109 |
VERIFY( res == WIDEN("+abcd+") ); |
| 110 |
|
| 111 |
res = std::format(WIDEN("{:m}"), vp); |
| 112 |
VERIFY( res == WIDEN("{1: 11, 2: 21}") ); |
| 113 |
res = std::format(WIDEN("{:=^20m}"), vp); |
| 114 |
VERIFY( res == WIDEN("==={1: 11, 2: 21}===") ); |
| 115 |
} |
| 116 |
|
| 117 |
template<template<typename, typename> class Formatter> |
| 118 |
void test_outputs() |
| 119 |
{ |
| 120 |
test_default<char, Formatter>(); |
| 121 |
test_default<wchar_t, Formatter>(); |
| 122 |
test_override<char, Formatter>(); |
| 123 |
test_override<wchar_t, Formatter>(); |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
test_nested() |
| 128 |
{ |
| 129 |
MyVector<MyVector<int>> v |
| 130 |
{ |
| 131 |
{1, 2}, |
| 132 |
{11, 12} |
| 133 |
}; |
| 134 |
|
| 135 |
std::string res = std::format("{}", v); |
| 136 |
VERIFY( res == "<<1; 2>; <11; 12>>" ); |
| 137 |
|
| 138 |
res = std::format("{:+^18:n:02}", v); |
| 139 |
VERIFY( res == "+<01; 02; 11; 12>+" ); |
| 140 |
} |
| 141 |
|
| 142 |
struct MyFlatMap : std::flat_map<int, int> |
| 143 |
{ |
| 144 |
using std::flat_map<int, int>::flat_map; |
| 145 |
}; |
| 146 |
|
| 147 |
template<typename CharT> |
| 148 |
struct std::formatter<MyFlatMap, CharT> |
| 149 |
// This cannot apply format BitVector const&, because formatted type would |
| 150 |
// be std::pair<int const&, int const&>, and formatter for |
| 151 |
// pair<int const&, int> cannot format it. |
| 152 |
: std::range_formatter<MyFlatMap::reference> |
| 153 |
{}; |
| 154 |
|
| 155 |
void test_const_ref_type_mismatch() |
| 156 |
{ |
| 157 |
MyFlatMap m{{1, 11}, {2, 22}}; |
| 158 |
std::string res = std::format("{:m}", m); |
| 159 |
VERIFY( res == "{1: 11, 2: 22}" ); |
| 160 |
} |
| 161 |
|
| 162 |
template<typename T, typename CharT> |
| 163 |
using VectorFormatter = std::formatter<std::vector<T>, CharT>; |
| 164 |
|
| 165 |
int main() |
| 166 |
{ |
| 167 |
test_outputs<std::range_formatter>(); |
| 168 |
test_outputs<VectorFormatter>(); |
| 169 |
test_nested(); |
| 170 |
test_const_ref_type_mismatch(); |
| 171 |
} |