Debian Codegraph @

libcxx/test/std/utilities/format/format.arguments/format.args/get.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
10
11 // <format>
12
13 // basic_format_arg<Context> get(size_t i) const noexcept;
14
15 #include <format>
16 #include <cassert>
17 #include <limits>
18 #include <type_traits>
19
20 #include "test_macros.h"
21 #include "make_string.h"
22
23 template <class Context, class To, class From>
24 void test(From value) {
25 auto store = std::make_format_args<Context>(value);
26 const std::basic_format_args<Context> format_args{store};
27
28 auto visitor = [v = To(value)](auto a) {
29 if constexpr (std::is_same_v<To, decltype(a)>)
30 assert(v == a);
31 else
32 assert(false);
33 };
34 #if TEST_STD_VER >= 26 && defined(TEST_HAS_EXPLICIT_THIS_PARAMETER)
35 format_args.get(0).visit(visitor);
36 #else
37 std::visit_format_arg(visitor, format_args.get(0));
38 #endif
39 }
40
41 // Some types, as an extension, are stored in the variant. The Standard
42 // requires them to be observed as a handle.
43 template <class Context, class T>
44 void test_handle(T value) {
45 auto store = std::make_format_args<Context>(value);
46 std::basic_format_args<Context> format_args{store};
47
48 auto visitor = [](auto a) { assert((std::is_same_v<decltype(a), typename std::basic_format_arg<Context>::handle>)); };
49 #if TEST_STD_VER >= 26 && defined(TEST_HAS_EXPLICIT_THIS_PARAMETER)
50 format_args.get(0).visit(visitor);
51 #else
52 std::visit_format_arg(visitor, format_args.get(0));
53 #endif
54 }
55
56 // Test specific for string and string_view.
57 //
58 // Since both result in a string_view there's no need to pass this as a
59 // template argument.
60 template <class Context, class From>
61 void test_string_view(From value) {
62 auto store = std::make_format_args<Context>(value);
63 const std::basic_format_args<Context> format_args{store};
64
65 using CharT = typename Context::char_type;
66 using To = std::basic_string_view<CharT>;
67 using V = std::basic_string<CharT>;
68
69 auto visitor = [v = V(value.begin(), value.end())](auto a) {
70 if constexpr (std::is_same_v<To, decltype(a)>)
71 assert(v == a);
72 else
73 assert(false);
74 };
75 #if TEST_STD_VER >= 26 && defined(TEST_HAS_EXPLICIT_THIS_PARAMETER)
76 format_args.get(0).visit(visitor);
77 #else
78 std::visit_format_arg(visitor, format_args.get(0));
79 #endif
80 }
81
82 template <class CharT>
83 void test() {
84 using Context = std::basic_format_context<CharT*, CharT>;
85 using char_type = typename Context::char_type;
86 std::basic_string<char_type> empty;
87 std::basic_string<char_type> str = MAKE_STRING(char_type, "abc");
88
89 // Test boolean types.
90
91 test<Context, bool>(true);
92 test<Context, bool>(false);
93
94 // Test char_type types.
95
96 test<Context, char_type, char_type>('a');
97 test<Context, char_type, char_type>('z');
98 test<Context, char_type, char_type>('0');
99 test<Context, char_type, char_type>('9');
100
101 // Test char types.
102
103 if (std::is_same_v<char_type, char>) {
104 // char to char -> char
105 test<Context, char_type, char>('a');
106 test<Context, char_type, char>('z');
107 test<Context, char_type, char>('0');
108 test<Context, char_type, char>('9');
109 } else {
110 if (std::is_same_v<char_type, wchar_t>) {
111 // char to wchar_t -> wchar_t
112 test<Context, wchar_t, char>('a');
113 test<Context, wchar_t, char>('z');
114 test<Context, wchar_t, char>('0');
115 test<Context, wchar_t, char>('9');
116 } else if (std::is_signed_v<char>) {
117 // char to char_type -> int
118 // This happens when Context::char_type is a char8_t, char16_t, or
119 // char32_t and char is a signed type.
120 // Note if sizeof(char_type) > sizeof(int) this test fails. If there are
121 // platforms where that occurs extra tests need to be added for char32_t
122 // testing it against a long long.
123 test<Context, int, char>('a');
124 test<Context, int, char>('z');
125 test<Context, int, char>('0');
126 test<Context, int, char>('9');
127 } else {
128 // char to char_type -> unsigned
129 // This happens when Context::char_type is a char8_t, char16_t, or
130 // char32_t and char is an unsigned type.
131 // Note if sizeof(char_type) > sizeof(unsigned) this test fails. If there
132 // are platforms where that occurs extra tests need to be added for
133 // char32_t testing it against an unsigned long long.
134 test<Context, unsigned, char>('a');
135 test<Context, unsigned, char>('z');
136 test<Context, unsigned, char>('0');
137 test<Context, unsigned, char>('9');
138 }
139 }
140
141 // Test signed integer types.
142
143 test<Context, int, signed char>(std::numeric_limits<signed char>::min());
144 test<Context, int, signed char>(0);
145 test<Context, int, signed char>(std::numeric_limits<signed char>::max());
146
147 test<Context, int, short>(std::numeric_limits<short>::min());
148 test<Context, int, short>(std::numeric_limits<signed char>::min());
149 test<Context, int, short>(0);
150 test<Context, int, short>(std::numeric_limits<signed char>::max());
151 test<Context, int, short>(std::numeric_limits<short>::max());
152
153 test<Context, int, int>(std::numeric_limits<int>::min());
154 test<Context, int, int>(std::numeric_limits<short>::min());
155 test<Context, int, int>(std::numeric_limits<signed char>::min());
156 test<Context, int, int>(0);
157 test<Context, int, int>(std::numeric_limits<signed char>::max());
158 test<Context, int, int>(std::numeric_limits<short>::max());
159 test<Context, int, int>(std::numeric_limits<int>::max());
160
161 using LongToType =
162 std::conditional_t<sizeof(long) == sizeof(int), int, long long>;
163
164 test<Context, LongToType, long>(std::numeric_limits<long>::min());
165 test<Context, LongToType, long>(std::numeric_limits<int>::min());
166 test<Context, LongToType, long>(std::numeric_limits<short>::min());
167 test<Context, LongToType, long>(std::numeric_limits<signed char>::min());
168 test<Context, LongToType, long>(0);
169 test<Context, LongToType, long>(std::numeric_limits<signed char>::max());
170 test<Context, LongToType, long>(std::numeric_limits<short>::max());
171 test<Context, LongToType, long>(std::numeric_limits<int>::max());
172 test<Context, LongToType, long>(std::numeric_limits<long>::max());
173
174 test<Context, long long, long long>(std::numeric_limits<long long>::min());
175 test<Context, long long, long long>(std::numeric_limits<long>::min());
176 test<Context, long long, long long>(std::numeric_limits<int>::min());
177 test<Context, long long, long long>(std::numeric_limits<short>::min());
178 test<Context, long long, long long>(std::numeric_limits<signed char>::min());
179 test<Context, long long, long long>(0);
180 test<Context, long long, long long>(std::numeric_limits<signed char>::max());
181 test<Context, long long, long long>(std::numeric_limits<short>::max());
182 test<Context, long long, long long>(std::numeric_limits<int>::max());
183 test<Context, long long, long long>(std::numeric_limits<long>::max());
184 test<Context, long long, long long>(std::numeric_limits<long long>::max());
185
186 #ifndef TEST_HAS_NO_INT128
187 test_handle<Context, __int128_t>(0);
188 #endif // TEST_HAS_NO_INT128
189
190 // Test unsigned integer types.
191
192 test<Context, unsigned, unsigned char>(0);
193 test<Context, unsigned, unsigned char>(
194 std::numeric_limits<unsigned char>::max());
195
196 test<Context, unsigned, unsigned short>(0);
197 test<Context, unsigned, unsigned short>(
198 std::numeric_limits<unsigned char>::max());
199 test<Context, unsigned, unsigned short>(
200 std::numeric_limits<unsigned short>::max());
201
202 test<Context, unsigned, unsigned>(0);
203 test<Context, unsigned, unsigned>(std::numeric_limits<unsigned char>::max());
204 test<Context, unsigned, unsigned>(std::numeric_limits<unsigned short>::max());
205 test<Context, unsigned, unsigned>(std::numeric_limits<unsigned>::max());
206
207 using UnsignedLongToType =
208 std::conditional_t<sizeof(unsigned long) == sizeof(unsigned), unsigned,
209 unsigned long long>;
210
211 test<Context, UnsignedLongToType, unsigned long>(0);
212 test<Context, UnsignedLongToType, unsigned long>(
213 std::numeric_limits<unsigned char>::max());
214 test<Context, UnsignedLongToType, unsigned long>(
215 std::numeric_limits<unsigned short>::max());
216 test<Context, UnsignedLongToType, unsigned long>(
217 std::numeric_limits<unsigned>::max());
218 test<Context, UnsignedLongToType, unsigned long>(
219 std::numeric_limits<unsigned long>::max());
220
221 test<Context, unsigned long long, unsigned long long>(0);
222 test<Context, unsigned long long, unsigned long long>(
223 std::numeric_limits<unsigned char>::max());
224 test<Context, unsigned long long, unsigned long long>(
225 std::numeric_limits<unsigned short>::max());
226 test<Context, unsigned long long, unsigned long long>(
227 std::numeric_limits<unsigned>::max());
228 test<Context, unsigned long long, unsigned long long>(
229 std::numeric_limits<unsigned long>::max());
230 test<Context, unsigned long long, unsigned long long>(
231 std::numeric_limits<unsigned long long>::max());
232
233 #ifndef TEST_HAS_NO_INT128
234 test_handle<Context, __uint128_t>(0);
235 #endif // TEST_HAS_NO_INT128
236
237 // Test floating point types.
238
239 test<Context, float, float>(-std::numeric_limits<float>::max());
240 test<Context, float, float>(-std::numeric_limits<float>::min());
241 test<Context, float, float>(-0.0);
242 test<Context, float, float>(0.0);
243 test<Context, float, float>(std::numeric_limits<float>::min());
244 test<Context, float, float>(std::numeric_limits<float>::max());
245
246 test<Context, double, double>(-std::numeric_limits<double>::max());
247 test<Context, double, double>(-std::numeric_limits<double>::min());
248 test<Context, double, double>(-0.0);
249 test<Context, double, double>(0.0);
250 test<Context, double, double>(std::numeric_limits<double>::min());
251 test<Context, double, double>(std::numeric_limits<double>::max());
252
253 test<Context, long double, long double>(
254 -std::numeric_limits<long double>::max());
255 test<Context, long double, long double>(
256 -std::numeric_limits<long double>::min());
257 test<Context, long double, long double>(-0.0);
258 test<Context, long double, long double>(0.0);
259 test<Context, long double, long double>(
260 std::numeric_limits<long double>::min());
261 test<Context, long double, long double>(
262 std::numeric_limits<long double>::max());
263
264 // Test const char_type pointer types.
265
266 test<Context, const char_type*, const char_type*>(empty.c_str());
267 test<Context, const char_type*, const char_type*>(str.c_str());
268
269 // Test string_view types.
270
271 test<Context, std::basic_string_view<char_type>>(
272 std::basic_string_view<char_type>());
273 test<Context, std::basic_string_view<char_type>,
274 std::basic_string_view<char_type>>(empty);
275 test<Context, std::basic_string_view<char_type>,
276 std::basic_string_view<char_type>>(str);
277
278 // Test string types.
279
280 test<Context, std::basic_string_view<char_type>>(
281 std::basic_string<char_type>());
282 test<Context, std::basic_string_view<char_type>,
283 std::basic_string<char_type>>(empty);
284 test<Context, std::basic_string_view<char_type>,
285 std::basic_string<char_type>>(str);
286
287 // Test pointer types.
288
289 test<Context, const void*>(nullptr);
290 }
291
292 void test() {
293 test<char>();
294 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
295 test<wchar_t>();
296 #endif
297 }
298
299 int main(int, char**) {
300 test();
301
302 return 0;
303 }