| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <array> |
| 4 | #include <format> |
| 5 | #include <list> |
| 6 | #include <ranges> |
| 7 | #include <span> |
| 8 | #include <testsuite_hooks.h> |
| 9 | #include <testsuite_iterators.h> |
| 10 | #include <vector> |
| 11 | |
| 12 | struct NotFormattable |
| 13 | {}; |
| 14 | |
| 15 | static_assert(!std::formattable<std::vector<NotFormattable>, char>); |
| 16 | static_assert(!std::formattable<std::span<NotFormattable>, wchar_t>); |
| 17 | |
| 18 | template<typename... Args> |
| 19 | bool |
| 20 | is_format_string_for(const char* str, Args&&... args) |
| 21 | { |
| 22 | try { |
| 23 | (void) std::vformat(str, std::make_format_args(args...)); |
| 24 | return true; |
| 25 | } catch (const std::format_error&) { |
| 26 | return false; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | template<typename... Args> |
| 31 | bool |
| 32 | is_format_string_for(const wchar_t* str, Args&&... args) |
| 33 | { |
| 34 | try { |
| 35 | (void) std::vformat(str, std::make_wformat_args(args...)); |
| 36 | return true; |
| 37 | } catch (const std::format_error&) { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | template<typename Rg, typename CharT> |
| 43 | bool is_range_formatter_spec_for(CharT const* spec, Rg&& rg) |
| 44 | { |
| 45 | using V = std::remove_cvref_t<std::ranges::range_reference_t<Rg>>; |
| 46 | std::range_formatter<V, CharT> fmt; |
| 47 | std::basic_format_parse_context<CharT> pc(spec); |
| 48 | try { |
| 49 | (void)fmt.parse(pc); |
| 50 | return true; |
| 51 | } catch (const std::format_error&) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | test_format_string() |
| 58 | { |
| 59 | // invalid format spec 'p' |
| 60 | VERIFY( !is_range_formatter_spec_for("p", std::vector<int>()) ); |
| 61 | VERIFY( !is_format_string_for("{:p}", std::vector<int>()) ); |
| 62 | VERIFY( !is_range_formatter_spec_for("np", std::vector<int>()) ); |
| 63 | VERIFY( !is_format_string_for("{:np}", std::vector<int>()) ); |
| 64 | |
| 65 | // width needs to be integer type |
| 66 | VERIFY( !is_format_string_for("{:{}}", std::vector<int>(), 1.0f) ); |
| 67 | |
| 68 | // element format needs to be valid |
| 69 | VERIFY( !is_range_formatter_spec_for(":p", std::vector<int>()) ); |
| 70 | VERIFY( !is_format_string_for("{::p}", std::vector<int>()) ); |
| 71 | VERIFY( !is_range_formatter_spec_for("n:p", std::vector<int>()) ); |
| 72 | VERIFY( !is_format_string_for("{:n:p}", std::vector<int>()) ); |
| 73 | } |
| 74 | |
| 75 | #define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S) |
| 76 | #define WIDEN(S) WIDEN_(_CharT, S) |
| 77 | |
| 78 | template<typename _CharT, typename Range, typename Storage> |
| 79 | void test_output() |
| 80 | { |
| 81 | using Sv = std::basic_string_view<_CharT>; |
| 82 | using T = std::ranges::range_value_t<Range>; |
| 83 | auto makeRange = [](Storage& s) -> Range { |
| 84 | if constexpr (std::is_same_v<std::remove_cvref_t<Range>, Storage>) |
| 85 | return s; |
| 86 | else |
| 87 | return Range(std::ranges::data(s), |
| 88 | std::ranges::data(s) + std::ranges::size(s)); |
| 89 | }; |
| 90 | |
| 91 | std::basic_string<_CharT> res; |
| 92 | size_t size = 0; |
| 93 | |
| 94 | Storage v1{1, 2, 3}; |
| 95 | res = std::format(WIDEN("{}"), makeRange(v1)); |
| 96 | VERIFY( res == WIDEN("[1, 2, 3]") ); |
| 97 | res = std::format(WIDEN("{:}"), makeRange(v1)); |
| 98 | VERIFY( res == WIDEN("[1, 2, 3]") ); |
| 99 | res = std::format(WIDEN("{:n}"), makeRange(v1)); |
| 100 | VERIFY( res == WIDEN("1, 2, 3") ); |
| 101 | |
| 102 | res = std::format(WIDEN("{:3}"), makeRange(v1)); |
| 103 | VERIFY( res == WIDEN("[1, 2, 3]") ); |
| 104 | |
| 105 | res = std::format(WIDEN("{:10}"), makeRange(v1)); |
| 106 | VERIFY( res == WIDEN("[1, 2, 3] ") ); |
| 107 | |
| 108 | res = std::format(WIDEN("{:{}}"), makeRange(v1), 10); |
| 109 | VERIFY( res == WIDEN("[1, 2, 3] ") ); |
| 110 | |
| 111 | res = std::format(WIDEN("{1:{0}}"), 10, makeRange(v1)); |
| 112 | VERIFY( res == WIDEN("[1, 2, 3] ") ); |
| 113 | |
| 114 | res = std::format(WIDEN("{:10n}"), makeRange(v1)); |
| 115 | VERIFY( res == WIDEN("1, 2, 3 ") ); |
| 116 | |
| 117 | res = std::format(WIDEN("{:*<11}"), makeRange(v1)); |
| 118 | VERIFY( res == WIDEN("[1, 2, 3]**") ); |
| 119 | |
| 120 | res = std::format(WIDEN("{:->12}"), makeRange(v1)); |
| 121 | VERIFY( res == WIDEN("---[1, 2, 3]") ); |
| 122 | |
| 123 | res = std::format(WIDEN("{:=^13}"), makeRange(v1)); |
| 124 | VERIFY( res == WIDEN("==[1, 2, 3]==") ); |
| 125 | |
| 126 | res = std::format(WIDEN("{:=^13n}"), makeRange(v1)); |
| 127 | VERIFY( res == WIDEN("===1, 2, 3===") ); |
| 128 | |
| 129 | res = std::format(WIDEN("{::#x}"), makeRange(v1)); |
| 130 | VERIFY( res == WIDEN("[0x1, 0x2, 0x3]") ); |
| 131 | |
| 132 | res = std::format(WIDEN("{:|^25n:#05x}"), makeRange(v1)); |
| 133 | VERIFY( res == WIDEN("|||0x001, 0x002, 0x003|||") ); |
| 134 | |
| 135 | // ':' is start of the format string for element |
| 136 | res = std::format(WIDEN("{::^+04}"), makeRange(v1)); |
| 137 | VERIFY( res == WIDEN("[ +1 , +2 , +3 ]") ); |
| 138 | |
| 139 | size = std::formatted_size(WIDEN("{:}"), makeRange(v1)); |
| 140 | VERIFY( size == Sv(WIDEN("[1, 2, 3]")).size() ); |
| 141 | |
| 142 | size = std::formatted_size(WIDEN("{:3}"), makeRange(v1)); |
| 143 | VERIFY( size == Sv(WIDEN("[1, 2, 3]")).size() ); |
| 144 | |
| 145 | size = std::formatted_size(WIDEN("{:10}"), makeRange(v1)); |
| 146 | VERIFY( size == 10 ); |
| 147 | |
| 148 | size = std::formatted_size(WIDEN("{:|^25n:#05x}"), makeRange(v1)); |
| 149 | VERIFY( size == 25 ); |
| 150 | } |
| 151 | |
| 152 | template<typename Cont> |
| 153 | void test_output_cont() |
| 154 | { |
| 155 | test_output<char, Cont&, Cont>(); |
| 156 | test_output<wchar_t, Cont const&, Cont>(); |
| 157 | } |
| 158 | |
| 159 | template<typename View> |
| 160 | void test_output_view() |
| 161 | { |
| 162 | test_output<char, View, int[3]>(); |
| 163 | test_output<wchar_t, View, int[3]>(); |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | test_outputs() |
| 168 | { |
| 169 | using namespace __gnu_test; |
| 170 | test_output_cont<std::vector<int>>(); |
| 171 | test_output_cont<std::list<int>>(); |
| 172 | test_output_cont<std::array<int, 3>>(); |
| 173 | |
| 174 | test_output_view<std::span<int>>(); |
| 175 | test_output_view<std::ranges::subrange<int*>>(); |
| 176 | test_output_view<test_forward_range<int>>(); |
| 177 | test_output_view<test_input_range<int>>(); |
| 178 | test_output_view<test_input_range_nocopy<int>>(); |
| 179 | |
| 180 | test_output_view<std::span<const int>>(); |
| 181 | test_output_view<std::ranges::subrange<const int*>>(); |
| 182 | test_output_view<test_forward_range<const int>>(); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | test_nested() |
| 187 | { |
| 188 | std::vector<std::vector<int>> v |
| 189 | { |
| 190 | {1, 2}, |
| 191 | {11, 12} |
| 192 | }; |
| 193 | |
| 194 | std::string res = std::format("{}", v); |
| 195 | VERIFY( res == "[[1, 2], [11, 12]]" ); |
| 196 | |
| 197 | res = std::format("{:+^18:n:02}", v); |
| 198 | VERIFY( res == "+[01, 02, 11, 12]+" ); |
| 199 | } |
| 200 | |
| 201 | int main() |
| 202 | { |
| 203 | test_format_string(); |
| 204 | test_outputs(); |
| 205 | test_nested(); |
| 206 | } |