Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / format / format.tuple / format.pass.cpp
blobd83139bf2f952b3c3e88aa1d3171246d93a29709
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 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 // <format>
14 // template<class charT, formattable<charT>... Ts>
15 // struct formatter<pair-or-tuple<Ts...>, charT>
17 // template<class FormatContext>
18 // typename FormatContext::iterator
19 // format(see below& elems, FormatContext& ctx) const;
21 // Note this tests the basics of this function. It's tested in more detail in
22 // the format functions tests.
24 #include <cassert>
25 #include <concepts>
26 #include <format>
27 #include <iterator>
28 #include <tuple>
29 #include <utility>
31 #include "assert_macros.h"
32 #include "format.functions.common.h"
33 #include "make_string.h"
34 #include "test_format_context.h"
35 #include "test_macros.h"
37 #define SV(S) MAKE_STRING_VIEW(CharT, S)
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>;
46 const std::formatter<Arg, CharT> formatter;
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);
55 template <class CharT>
56 void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) {
57 using String = std::basic_string<CharT>;
58 using OutIt = std::back_insert_iterator<String>;
59 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
60 std::pair<parse_call_validator, parse_call_validator> arg;
62 String result;
63 OutIt out = std::back_inserter(result);
64 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
66 std::formatter<decltype(arg), CharT> formatter;
67 std::basic_format_parse_context<CharT> ctx{fmt};
69 formatter.parse(ctx);
70 formatter.format(arg, format_ctx);
73 template <class CharT>
74 void test_assure_parse_is_called() {
75 using String = std::basic_string<CharT>;
76 using OutIt = std::back_insert_iterator<String>;
77 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
78 std::pair<parse_call_validator, parse_call_validator> arg;
80 String result;
81 OutIt out = std::back_inserter(result);
82 [[maybe_unused]] FormatCtxT format_ctx =
83 test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
85 { // parse not called
86 [[maybe_unused]] const std::formatter<decltype(arg), CharT> formatter;
87 TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx));
90 test_assure_parse_is_called(SV("5"));
91 test_assure_parse_is_called(SV("n"));
92 test_assure_parse_is_called(SV("m"));
93 test_assure_parse_is_called(SV("5n"));
94 test_assure_parse_is_called(SV("5m"));
97 template <class CharT>
98 void test() {
99 test(SV("(1)"), std::tuple<int>{1});
100 test(SV("(1, 1)"), std::tuple<int, CharT>{1, CharT('1')});
101 test(SV("(1, 1)"), std::pair<int, CharT>{1, CharT('1')});
102 test(SV("(1, 1, true)"), std::tuple<int, CharT, bool>{1, CharT('1'), true});
104 test_assure_parse_is_called<CharT>();
107 void test() {
108 test<char>();
109 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
110 test<wchar_t>();
111 #endif
114 int main(int, char**) {
115 test();
117 return 0;