xref: /llvm-project/libcxx/test/std/utilities/format/format.tuple/parse.pass.cpp (revision af5fc4b4d827ab00e20323b6025b458edca46906)
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 // TODO FMT Fix this test using GCC, it currently times out.
12 // UNSUPPORTED: gcc-12
13 
14 // This test requires the dylib support introduced in D92214.
15 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{.+}}
16 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx11.{{.+}}
17 
18 // <format>
19 
20 // template<class charT, formattable<charT>... Ts>
21 //   struct formatter<pair-or-tuple<Ts...>, charT>
22 
23 // template<class ParseContext>
24 //   constexpr typename ParseContext::iterator
25 //     parse(ParseContext& ctx);
26 
27 // Note this tests the basics of this function. It's tested in more detail in
28 // the format functions tests.
29 
30 #include <cassert>
31 #include <concepts>
32 #include <format>
33 #include <tuple>
34 #include <utility>
35 
36 #include "test_format_context.h"
37 #include "test_macros.h"
38 #include "make_string.h"
39 
40 #define SV(S) MAKE_STRING_VIEW(CharT, S)
41 
42 template <class Arg, class StringViewT>
43 constexpr void test(StringViewT fmt) {
44   using CharT    = typename StringViewT::value_type;
45   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
46   std::formatter<Arg, CharT> formatter;
47   static_assert(std::semiregular<decltype(formatter)>);
48 
49   std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
50   assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
51 }
52 
53 template <class CharT, class Arg>
54 constexpr void test() {
55   test<Arg>(SV(""));
56   test<Arg>(SV("42"));
57 
58   test<Arg>(SV("}"));
59   test<Arg>(SV("42}"));
60 }
61 
62 template <class CharT>
63 constexpr void test() {
64   test<CharT, std::tuple<int>>();
65   test<CharT, std::tuple<int, CharT>>();
66   test<CharT, std::pair<int, CharT>>();
67   test<CharT, std::tuple<int, CharT, double>>();
68 }
69 
70 constexpr bool test() {
71   test<char>();
72 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
73   test<wchar_t>();
74 #endif
75 
76   return true;
77 }
78 
79 int main(int, char**) {
80   test();
81   static_assert(test());
82 
83   return 0;
84 }
85