Debian Codegraph @

libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.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, c++20
9
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12 // [container.adaptors.format]
13 // For each of queue, priority_queue, and stack, the library provides the
14 // following formatter specialization where adaptor-type is the name of the
15 // template:
16 //
17 // template<class charT, class T, formattable<charT> Container, class... U>
18 // struct formatter<adaptor-type<T, Container, U...>, charT>
19
20 // template<class FormatContext>
21 // typename FormatContext::iterator
22 // format(maybe-const-adaptor& r, FormatContext& ctx) const;
23
24 // Note this tests the basics of this function. It's tested in more detail in
25 // the format functions test.
26
27 #include <array>
28 #include <cassert>
29 #include <concepts>
30 #include <format>
31 #include <queue>
32 #include <stack>
33
34 #include "test_format_context.h"
35 #include "test_macros.h"
36 #include "make_string.h"
37
38 #define SV(S) MAKE_STRING_VIEW(CharT, S)
39
40 template <class StringViewT, class Arg>
41 void test_format(StringViewT expected, Arg arg) {
42 using CharT = typename StringViewT::value_type;
43 using String = std::basic_string<CharT>;
44 using OutIt = std::back_insert_iterator<String>;
45 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
46
47 const std::formatter<Arg, CharT> formatter;
48
49 String result;
50 OutIt out = std::back_inserter(result);
51 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
52 formatter.format(arg, format_ctx);
53 assert(result == expected);
54 }
55
56 template <class CharT>
57 void test_fmt() {
58 std::array input{1, 42, 99, 0};
59 test_format(SV("[1, 42, 99, 0]"), std::queue<int>{input.begin(), input.end()});
60 test_format(SV("[99, 42, 1, 0]"), std::priority_queue<int>{input.begin(), input.end()});
61 test_format(SV("[1, 42, 99, 0]"), std::stack<int>{input.begin(), input.end()});
62 }
63
64 void test() {
65 test_fmt<char>();
66 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
67 test_fmt<wchar_t>();
68 #endif
69 }
70
71 int main(int, char**) {
72 test();
73
74 return 0;
75 }