Debian Codegraph @

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

raw
1 // { dg-do run { target c++23 } }
2 // { dg-timeout-factor 2 }
3
4 #include <format>
5 #include <queue>
6 #include <stack>
7 #include <testsuite_hooks.h>
8
9 template<typename... Args>
10 bool
11 is_format_string_for(const char* str, Args&&... args)
12 {
13 try {
14 (void) std::vformat(str, std::make_format_args(args...));
15 return true;
16 } catch (const std::format_error&) {
17 return false;
18 }
19 }
20
21 #define WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
22 #define WIDEN(S) WIDEN_(_CharT, S)
23
24 template<template<typename Tp> class Adaptor>
25 void
26 test_format_string()
27 {
28 Adaptor<int> q;
29 VERIFY( !is_format_string_for("{:?}", q) );
30 VERIFY( !is_format_string_for("{:P}", q) );
31
32 // width needs to be integer type
33 VERIFY( !is_format_string_for("{:{}}", q, 1.0f) );
34 }
35
36 struct NoFormat
37 {
38 friend auto operator<=>(NoFormat, NoFormat) = default;
39 };
40
41 struct MutFormat
42 {
43 MutFormat() = default;
44 MutFormat(int p) : x(p) {}
45
46 int x;
47 friend auto operator<=>(MutFormat, MutFormat) = default;
48 };
49
50 template<typename CharT>
51 struct std::formatter<MutFormat, CharT>
52 : std::formatter<int, CharT>
53 {
54 template<typename Out>
55 Out format(MutFormat& mf, basic_format_context<Out, CharT>& ctx) const
56 { return std::formatter<int, CharT>::format(mf.x, ctx); }
57 };
58
59 template<typename T>
60 struct NotFormattableCont : std::vector<T>
61 {
62 using std::vector<T>::vector;
63 };
64
65 template<typename T>
66 constexpr auto std::format_kind<NotFormattableCont<T>>
67 = std::range_format::disabled;
68
69 template<typename _CharT,
70 template<typename Tp, typename Cont = std::vector<Tp>> class Adaptor>
71 void
72 test_output()
73 {
74 const std::vector<int> v{3, 2, 1};
75 std::basic_string<_CharT> res;
76 Adaptor<int, std::vector<int>> q(std::from_range, v);
77
78 res = std::format(WIDEN("{}"), q);
79 VERIFY( res == WIDEN("[3, 2, 1]") );
80
81 res = std::format(WIDEN("{}"), std::as_const(q));
82 VERIFY( res == WIDEN("[3, 2, 1]") );
83
84 res = std::format(WIDEN("{:n:#x}"), q);
85 VERIFY( res == WIDEN("0x3, 0x2, 0x1") );
86
87 res = std::format(WIDEN("{:=^23:#04x}"), q);
88 VERIFY( res == WIDEN("==[0x03, 0x02, 0x01]===") );
89
90 // Sequence output is always used
91 std::queue<_CharT, std::basic_string<_CharT>> qs(
92 std::from_range,
93 std::basic_string_view<_CharT>(WIDEN("321")));
94
95 res = std::format(WIDEN("{}"), qs);
96 VERIFY( res == WIDEN("['3', '2', '1']") );
97
98 res = std::format(WIDEN("{::}"), std::as_const(qs));
99 VERIFY( res == WIDEN("[3, 2, 1]") );
100
101 res = std::format(WIDEN("{:?s}"), qs);
102 VERIFY( res == WIDEN(R"("321")") );
103
104 Adaptor<int, std::deque<int>> qd(std::from_range, v);
105
106 res = std::format(WIDEN("{}"), qd);
107 VERIFY( res == WIDEN("[3, 2, 1]") );
108
109 res = std::format(WIDEN("{}"), std::as_const(qd));
110 VERIFY( res == WIDEN("[3, 2, 1]") );
111
112 Adaptor<MutFormat> mq(std::from_range, v);
113
114 res = std::format(WIDEN("{}"), mq);
115 VERIFY( res == WIDEN("[3, 2, 1]") );
116
117 static_assert(!std::formattable<const Adaptor<MutFormat>, _CharT>);
118
119 static_assert(!std::formattable<Adaptor<NoFormat>, _CharT>);
120 static_assert(!std::formattable<const Adaptor<NoFormat>, _CharT>);
121
122 // Formatter check if container is formattable, not container elements.
123 static_assert(!std::formattable<Adaptor<int, NotFormattableCont<int>>, _CharT>);
124 }
125
126 template<template<typename Tp, typename Cont = std::vector<Tp>> class Adaptor>
127 void
128 test_adaptor()
129 {
130 test_format_string<Adaptor>();
131 test_output<char, Adaptor>();
132 test_output<wchar_t, Adaptor>();
133
134 static_assert(!std::formattable<Adaptor<int>, int>);
135 static_assert(!std::formattable<Adaptor<int>, char32_t>);
136 }
137
138 template<typename _CharT>
139 void
140 test_compare()
141 {
142 const std::vector<int> v{3, 2, 1};
143 std::basic_string<_CharT> res;
144 std::priority_queue<int, std::vector<int>, std::greater<>> q(
145 std::from_range, v);
146
147 res = std::format(WIDEN("{}"), q);
148 VERIFY( res == WIDEN("[1, 2, 3]") );
149 }
150
151 int main()
152 {
153 test_adaptor<std::queue>();
154 test_adaptor<std::priority_queue>();
155 test_compare<char>();
156 }