xref: /llvm-project/libcxx/test/std/utilities/format/format.tuple/format.pass.cpp (revision eb6e13cb32805ee12d19aaa5823f3e4216a35653)
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 // UNSUPPORTED: libcpp-has-no-incomplete-format
10 
11 // This test requires the dylib support introduced in D92214.
12 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{.+}}
13 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx11.{{.+}}
14 
15 // <format>
16 
17 // template<class charT, formattable<charT>... Ts>
18 //   struct formatter<pair-or-tuple<Ts...>, charT>
19 
20 // template<class FormatContext>
21 //   typename FormatContext::iterator
22 //     format(see below& elems, FormatContext& ctx) const;
23 
24 // Note this tests the basics of this function. It's tested in more detail in
25 // the format functions tests.
26 
27 #include <cassert>
28 #include <concepts>
29 #include <format>
30 #include <tuple>
31 #include <utility>
32 
33 #include "test_format_context.h"
34 #include "test_macros.h"
35 #include "make_string.h"
36 
37 #define SV(S) MAKE_STRING_VIEW(CharT, S)
38 
39 template <class StringViewT, class Arg>
40 void test(StringViewT expected, Arg arg) {
41   using CharT      = typename StringViewT::value_type;
42   using String     = std::basic_string<CharT>;
43   using OutIt      = std::back_insert_iterator<String>;
44   using FormatCtxT = std::basic_format_context<OutIt, CharT>;
45 
46   const std::formatter<Arg, CharT> formatter;
47 
48   String result;
49   OutIt out             = std::back_inserter(result);
50   FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
51   formatter.format(arg, format_ctx);
52   assert(result == expected);
53 }
54 
55 template <class CharT>
56 void test() {
57   test(SV("(1)"), std::tuple<int>{1});
58   test(SV("(1, 1)"), std::tuple<int, CharT>{1, CharT('1')});
59   test(SV("(1, 1)"), std::pair<int, CharT>{1, CharT('1')});
60   test(SV("(1, 1, 1)"), std::tuple<int, CharT, double>{1, CharT('1'), 1.0});
61 }
62 
63 void test() {
64   test<char>();
65 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
66   test<wchar_t>();
67 #endif
68 }
69 
70 int main(int, char**) {
71   test();
72 
73   return 0;
74 }
75