| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 12 | |
| 13 | // <format> |
| 14 | |
| 15 | // template<class charT, formattable<charT>... Ts> |
| 16 | // struct formatter<pair-or-tuple<Ts...>, charT> |
| 17 | |
| 18 | // template<class FormatContext> |
| 19 | // typename FormatContext::iterator |
| 20 | // format(see below& elems, FormatContext& ctx) const; |
| 21 | |
| 22 | // Note this tests the basics of this function. It's tested in more detail in |
| 23 | // the format functions tests. |
| 24 | |
| 25 | #include <cassert> |
| 26 | #include <concepts> |
| 27 | #include <format> |
| 28 | #include <iterator> |
| 29 | #include <tuple> |
| 30 | #include <utility> |
| 31 | |
| 32 | #include "assert_macros.h" |
| 33 | #include "format.functions.common.h" |
| 34 | #include "make_string.h" |
| 35 | #include "test_format_context.h" |
| 36 | #include "test_macros.h" |
| 37 | |
| 38 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 39 | |
| 40 | template <class StringViewT, class Arg> |
| 41 | void test(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_assure_parse_is_called(std::basic_string_view<CharT> fmt) { |
| 58 | using String = std::basic_string<CharT>; |
| 59 | using OutIt = std::back_insert_iterator<String>; |
| 60 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
| 61 | std::pair<parse_call_validator, parse_call_validator> arg; |
| 62 | |
| 63 | String result; |
| 64 | OutIt out = std::back_inserter(result); |
| 65 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
| 66 | |
| 67 | std::formatter<decltype(arg), CharT> formatter; |
| 68 | std::basic_format_parse_context<CharT> ctx{fmt}; |
| 69 | |
| 70 | formatter.parse(ctx); |
| 71 | formatter.format(arg, format_ctx); |
| 72 | } |
| 73 | |
| 74 | template <class CharT> |
| 75 | void test_assure_parse_is_called() { |
| 76 | using String = std::basic_string<CharT>; |
| 77 | using OutIt = std::back_insert_iterator<String>; |
| 78 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
| 79 | std::pair<parse_call_validator, parse_call_validator> arg; |
| 80 | |
| 81 | String result; |
| 82 | OutIt out = std::back_inserter(result); |
| 83 | [[maybe_unused]] FormatCtxT format_ctx = |
| 84 | test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
| 85 | |
| 86 | { // parse not called |
| 87 | [[maybe_unused]] const std::formatter<decltype(arg), CharT> formatter; |
| 88 | TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx)); |
| 89 | } |
| 90 | |
| 91 | test_assure_parse_is_called(SV("5" )); |
| 92 | test_assure_parse_is_called(SV("n" )); |
| 93 | test_assure_parse_is_called(SV("m" )); |
| 94 | test_assure_parse_is_called(SV("5n" )); |
| 95 | test_assure_parse_is_called(SV("5m" )); |
| 96 | } |
| 97 | |
| 98 | template <class CharT> |
| 99 | void test() { |
| 100 | test(SV("(1)" ), std::tuple<int>{1}); |
| 101 | test(SV("(1, 1)" ), std::tuple<int, CharT>{1, CharT('1')}); |
| 102 | test(SV("(1, 1)" ), std::pair<int, CharT>{1, CharT('1')}); |
| 103 | test(SV("(1, 1, true)" ), std::tuple<int, CharT, bool>{1, CharT('1'), true}); |
| 104 | |
| 105 | test_assure_parse_is_called<CharT>(); |
| 106 | } |
| 107 | |
| 108 | void test() { |
| 109 | test<char>(); |
| 110 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 111 | test<wchar_t>(); |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | int main(int, char**) { |
| 116 | test(); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |