xref: /llvm-project/libcxx/test/std/utilities/format/format.tuple/parse.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 ParseContext>
21 //   constexpr typename ParseContext::iterator
22 //     parse(ParseContext& ctx);
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 Arg, class StringViewT>
40 constexpr void test(StringViewT fmt) {
41   using CharT    = typename StringViewT::value_type;
42   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
43   std::formatter<Arg, CharT> formatter;
44   static_assert(std::semiregular<decltype(formatter)>);
45 
46   std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
47   assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
48 }
49 
50 template <class CharT, class Arg>
51 constexpr void test() {
52   test<Arg>(SV(""));
53   test<Arg>(SV("42"));
54 
55   test<Arg>(SV("}"));
56   test<Arg>(SV("42}"));
57 }
58 
59 template <class CharT>
60 constexpr void test() {
61   test<CharT, std::tuple<int>>();
62   test<CharT, std::tuple<int, CharT>>();
63   test<CharT, std::pair<int, CharT>>();
64   test<CharT, std::tuple<int, CharT, double>>();
65 }
66 
67 constexpr bool test() {
68   test<char>();
69 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
70   test<wchar_t>();
71 #endif
72 
73   return true;
74 }
75 
76 int main(int, char**) {
77   test();
78   static_assert(test());
79 
80   return 0;
81 }
82