summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
blob: 4db4f017e5ac8e97109e4978c878fec550f3839b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//===----------------------------------------------------------------------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: libcpp-has-no-incomplete-format

// <format>

// template <class _Tp, class _CharT = char>
// struct formatter;

// Tests the enabled and disabled requirements for std::formatter.

#include <array>
#include <bitset>
#include <bitset>
#include <chrono>
#include <complex>
#include <concepts>
#include <deque>
#ifndef _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY
#  include <filesystem>
#endif
#include <format>
#include <forward_list>
#include <list>
#include <memory>
#include <map>
#include <optional>
#include <queue>
#include <set>
#include <stack>
#include <span>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <variant>

#include "test_macros.h"

#ifndef TEST_HAS_NO_LOCALIZATION
#  include <regex>
#endif
#ifndef TEST_HAS_NO_THREADS
#  include <thread>
#endif

// Validate default template argument.
static_assert(std::same_as<std::formatter<int>, std::formatter<int, char>>);

// Concept for an enabled formatter.
//
// Since it's not possible to extract the T and CharT types from the formatter
// they are specified and the proper formatter is always intended to be
// defaulted.
//
// [formatter.requirements]/2
// A type F meets the Formatter requirements if it meets the BasicFormatter
// requirements and the expressions shown in Table 71 are valid and have the
// indicated semantics.
template <class T, class CharT, class F = std::formatter<T, CharT>>
concept enabled =
    // The basic BasicFormatter requirements:
    std::default_initializable<F> && std::copyable<F> && std::destructible<F> && std::swappable<F> &&
    // The expressions shown in Table 71
    requires(F f, std::basic_format_parse_context<CharT> pc, T u, std::basic_format_context<CharT*, CharT> fc) {
  { f.parse(pc) } -> std::same_as<typename decltype(pc)::iterator>;
  { f.format(u, fc) } -> std::same_as<typename decltype(fc)::iterator>;
};

// Concept for a disabled formatter.
//
// This uses the same template arguments as enable. This isn't required since
// the concept doesn't need to inspect T and CharT. This makes it easier for
// future changes. For example P2286 formatting ranges intents to change
// std::formatter<std::vector<int>> from disabled to enabled. The current way
// makes it easy to define a macro like
// #if TEST_STD_VER > 23
//   TEST_ENABLED_AFTER_CXX23(T, CharT) enabled<T, CharT>
// #else
//   TEST_ENABLED_AFTER_CXX23(T, CharT) disabled<T, CharT>
// #endif
template <class T, class CharT, class F = std::formatter<T, CharT>>
// [formatter.requirements]/5
// If F is a disabled specialization of formatter, these values are false:
concept disabled = !std::is_default_constructible_v<F> && !std::is_copy_constructible_v<F> &&
                   !std::is_move_constructible_v<F> && !std::is_copy_assignable_v<F> && !std::is_move_assignable_v<F>;

template <class T, class CharT>
void assert_formatter_is_disabled() {
  static_assert(disabled<T, CharT>);
}

template <class T, class CharT>
void assert_formatter_is_enabled() {
  // Only formatters for CharT == char || CharT == wchar_t are enabled for the
  // standard formatters. When CharT is a different type the formatter should
  // be disabled.
  if constexpr (std::same_as<CharT, char>
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
                || std::same_as<CharT, wchar_t>
#endif
  )
    static_assert(enabled<T, CharT>);
  else
    assert_formatter_is_disabled<T, CharT>();
}

// Tests for P0645 Text Formatting
template <class CharT>
void test_P0645() {
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  // Tests the special formatter that converts a char to a wchar_t.
  assert_formatter_is_enabled<char, wchar_t>();
#endif
  assert_formatter_is_enabled<CharT, CharT>();

  assert_formatter_is_enabled<CharT*, CharT>();
  assert_formatter_is_enabled<const CharT*, CharT>();
  assert_formatter_is_enabled<const CharT[42], CharT>();
  assert_formatter_is_enabled<std::basic_string<CharT>, CharT>();
  assert_formatter_is_enabled<std::basic_string_view<CharT>, CharT>();

  assert_formatter_is_enabled<bool, CharT>();

  assert_formatter_is_enabled<signed char, CharT>();
  assert_formatter_is_enabled<signed short, CharT>();
  assert_formatter_is_enabled<signed int, CharT>();
  assert_formatter_is_enabled<signed long, CharT>();
  assert_formatter_is_enabled<signed long long, CharT>();
#ifndef _LIBCPP_HAS_NO_INT128
  assert_formatter_is_enabled<__int128_t, CharT>();
#endif

  assert_formatter_is_enabled<unsigned char, CharT>();
  assert_formatter_is_enabled<unsigned short, CharT>();
  assert_formatter_is_enabled<unsigned int, CharT>();
  assert_formatter_is_enabled<unsigned long, CharT>();
  assert_formatter_is_enabled<unsigned long long, CharT>();
#ifndef _LIBCPP_HAS_NO_INT128
  assert_formatter_is_enabled<__uint128_t, CharT>();
#endif

  assert_formatter_is_enabled<float, CharT>();
  assert_formatter_is_enabled<double, CharT>();
  assert_formatter_is_enabled<long double, CharT>();

  assert_formatter_is_enabled<std::nullptr_t, CharT>();
  assert_formatter_is_enabled<void*, CharT>();
  assert_formatter_is_enabled<const void*, CharT>();
}

// Tests for P1361 Integration of chrono with text formatting
//
// Some tests are commented out since these types haven't been implemented in
// chrono yet. After P1361 has been implemented these formatters should be all
// enabled.
template <class CharT>
void test_P1361() {
  assert_formatter_is_disabled<std::chrono::microseconds, CharT>();

  assert_formatter_is_disabled<std::chrono::sys_time<std::chrono::microseconds>, CharT>();
  //assert_formatter_is_enabled<std::chrono::utc_time<std::chrono::microseconds>, CharT>();
  //assert_formatter_is_enabled<std::chrono::tai_time<std::chrono::microseconds>, CharT>();
  //assert_formatter_is_enabled<std::chrono::gps_time<std::chrono::microseconds>, CharT>();
  assert_formatter_is_disabled<std::chrono::file_time<std::chrono::microseconds>, CharT>();
  assert_formatter_is_disabled<std::chrono::local_time<std::chrono::microseconds>, CharT>();

  assert_formatter_is_disabled<std::chrono::day, CharT>();
  assert_formatter_is_disabled<std::chrono::month, CharT>();
  assert_formatter_is_disabled<std::chrono::year, CharT>();

  assert_formatter_is_disabled<std::chrono::weekday, CharT>();
  assert_formatter_is_disabled<std::chrono::weekday_indexed, CharT>();
  assert_formatter_is_disabled<std::chrono::weekday_last, CharT>();

  assert_formatter_is_disabled<std::chrono::month_day, CharT>();
  assert_formatter_is_disabled<std::chrono::month_day_last, CharT>();
  assert_formatter_is_disabled<std::chrono::month_weekday, CharT>();
  assert_formatter_is_disabled<std::chrono::month_weekday_last, CharT>();

  assert_formatter_is_disabled<std::chrono::year_month, CharT>();
  assert_formatter_is_disabled<std::chrono::year_month_day, CharT>();
  assert_formatter_is_disabled<std::chrono::year_month_day_last, CharT>();
  assert_formatter_is_disabled<std::chrono::year_month_weekday, CharT>();
  assert_formatter_is_disabled<std::chrono::year_month_weekday_last, CharT>();

  assert_formatter_is_disabled<std::chrono::hh_mm_ss<std::chrono::microseconds>, CharT>();

  //assert_formatter_is_enabled<std::chrono::sys_info, CharT>();
  //assert_formatter_is_enabled<std::chrono::local_info, CharT>();

  //assert_formatter_is_enabled<std::chrono::zoned_time, CharT>();
}

// Tests for P1636 Formatters for library types
//
// The paper hasn't been voted in so currently all formatters are disabled.
// TODO validate whether the test is correct after the paper has been accepted.
template <class CharT>
void test_P1636() {
  assert_formatter_is_disabled<std::basic_streambuf<CharT>, CharT>();
  assert_formatter_is_disabled<std::bitset<42>, CharT>();
  assert_formatter_is_disabled<std::complex<double>, CharT>();
  assert_formatter_is_disabled<std::error_code, CharT>();
#ifndef _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY
  assert_formatter_is_disabled<std::filesystem::path, CharT>();
#endif
  assert_formatter_is_disabled<std::shared_ptr<int>, CharT>();
#ifndef TEST_HAS_NO_LOCALIZATION
  assert_formatter_is_disabled<std::sub_match<CharT*>, CharT>();
#endif
#ifndef TEST_HAS_NO_THREADS
  assert_formatter_is_disabled<std::thread::id, CharT>();
#endif
  assert_formatter_is_disabled<std::unique_ptr<int>, CharT>();
}

// Tests for P2286 Formatting ranges
//
// The paper hasn't been voted in so currently all formatters are disabled.
// TODO validate whether the test is correct after the paper has been accepted.
template <class CharT>
void test_P2286() {
  assert_formatter_is_disabled<std::array<int, 42>, CharT>();
  assert_formatter_is_disabled<std::vector<int>, CharT>();
  assert_formatter_is_disabled<std::deque<int>, CharT>();
  assert_formatter_is_disabled<std::forward_list<int>, CharT>();
  assert_formatter_is_disabled<std::list<int>, CharT>();

  assert_formatter_is_disabled<std::set<int>, CharT>();
  assert_formatter_is_disabled<std::map<int, int>, CharT>();
  assert_formatter_is_disabled<std::multiset<int>, CharT>();
  assert_formatter_is_disabled<std::multimap<int, int>, CharT>();

  assert_formatter_is_disabled<std::unordered_set<int>, CharT>();
  assert_formatter_is_disabled<std::unordered_map<int, int>, CharT>();
  assert_formatter_is_disabled<std::unordered_multiset<int>, CharT>();
  assert_formatter_is_disabled<std::unordered_multimap<int, int>, CharT>();

  assert_formatter_is_disabled<std::stack<int>, CharT>();
  assert_formatter_is_disabled<std::queue<int>, CharT>();
  assert_formatter_is_disabled<std::priority_queue<int>, CharT>();

  assert_formatter_is_disabled<std::span<int>, CharT>();

  assert_formatter_is_disabled<std::valarray<int>, CharT>();

  assert_formatter_is_disabled<std::pair<int, int>, CharT>();
  assert_formatter_is_disabled<std::tuple<int>, CharT>();
}

class c {
  void f();
  void fc() const;
  static void sf();
};
enum e { a };
enum class ec { a };
template <class CharT>
void test_disabled() {
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  assert_formatter_is_disabled<const char*, wchar_t>();
#endif
  assert_formatter_is_disabled<const char*, char8_t>();
  assert_formatter_is_disabled<const char*, char16_t>();
  assert_formatter_is_disabled<const char*, char32_t>();

  assert_formatter_is_disabled<c, CharT>();
  assert_formatter_is_disabled<const c, CharT>();
  assert_formatter_is_disabled<volatile c, CharT>();
  assert_formatter_is_disabled<const volatile c, CharT>();

  assert_formatter_is_disabled<e, CharT>();
  assert_formatter_is_disabled<const e, CharT>();
  assert_formatter_is_disabled<volatile e, CharT>();
  assert_formatter_is_disabled<const volatile e, CharT>();

  assert_formatter_is_disabled<ec, CharT>();
  assert_formatter_is_disabled<const ec, CharT>();
  assert_formatter_is_disabled<volatile ec, CharT>();
  assert_formatter_is_disabled<const volatile ec, CharT>();

  assert_formatter_is_disabled<int*, CharT>();
  assert_formatter_is_disabled<const int*, CharT>();
  assert_formatter_is_disabled<volatile int*, CharT>();
  assert_formatter_is_disabled<const volatile int*, CharT>();

  assert_formatter_is_disabled<c*, CharT>();
  assert_formatter_is_disabled<const c*, CharT>();
  assert_formatter_is_disabled<volatile c*, CharT>();
  assert_formatter_is_disabled<const volatile c*, CharT>();

  assert_formatter_is_disabled<e*, CharT>();
  assert_formatter_is_disabled<const e*, CharT>();
  assert_formatter_is_disabled<volatile e*, CharT>();
  assert_formatter_is_disabled<const volatile e*, CharT>();

  assert_formatter_is_disabled<ec*, CharT>();
  assert_formatter_is_disabled<const ec*, CharT>();
  assert_formatter_is_disabled<volatile ec*, CharT>();
  assert_formatter_is_disabled<const volatile ec*, CharT>();

  assert_formatter_is_disabled<void (*)(), CharT>();
  assert_formatter_is_disabled<void (c::*)(), CharT>();
  assert_formatter_is_disabled<void (c::*)() const, CharT>();

  assert_formatter_is_disabled<std::optional<int>, CharT>();
  assert_formatter_is_disabled<std::variant<int>, CharT>();

  assert_formatter_is_disabled<std::shared_ptr<c>, CharT>();
  assert_formatter_is_disabled<std::unique_ptr<c>, CharT>();

  assert_formatter_is_disabled<std::array<c, 42>, CharT>();
  assert_formatter_is_disabled<std::vector<c>, CharT>();
  assert_formatter_is_disabled<std::deque<c>, CharT>();
  assert_formatter_is_disabled<std::forward_list<c>, CharT>();
  assert_formatter_is_disabled<std::list<c>, CharT>();

  assert_formatter_is_disabled<std::set<c>, CharT>();
  assert_formatter_is_disabled<std::map<c, int>, CharT>();
  assert_formatter_is_disabled<std::multiset<c>, CharT>();
  assert_formatter_is_disabled<std::multimap<c, int>, CharT>();

  assert_formatter_is_disabled<std::unordered_set<c>, CharT>();
  assert_formatter_is_disabled<std::unordered_map<c, int>, CharT>();
  assert_formatter_is_disabled<std::unordered_multiset<c>, CharT>();
  assert_formatter_is_disabled<std::unordered_multimap<c, int>, CharT>();

  assert_formatter_is_disabled<std::stack<c>, CharT>();
  assert_formatter_is_disabled<std::queue<c>, CharT>();
  assert_formatter_is_disabled<std::priority_queue<c>, CharT>();

  assert_formatter_is_disabled<std::span<c>, CharT>();

  assert_formatter_is_disabled<std::valarray<c>, CharT>();

  assert_formatter_is_disabled<std::pair<c, int>, CharT>();
  assert_formatter_is_disabled<std::tuple<c>, CharT>();

  assert_formatter_is_disabled<std::optional<c>, CharT>();
  assert_formatter_is_disabled<std::variant<c>, CharT>();
}

template <class CharT>
void test() {
  test_P0645<CharT>();
  test_P1361<CharT>();
  test_P1636<CharT>();
  test_P2286<CharT>();
  test_disabled<CharT>();
}

void test() {
  test<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  test<wchar_t>();
#endif
  test<char8_t>();
  test<char16_t>();
  test<char32_t>();

  test<int>();
}