Debian Codegraph @

libstdc++-v3/testsuite/std/format/ranges/map.cc

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