xref: /llvm-project/libcxx/test/std/utilities/format/format.tuple/parse.pass.cpp (revision 6a54dfbfe534276d644d7f9c027f0deeb748dd53)
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 ParseContext>
19 //   constexpr typename ParseContext::iterator
20 //     parse(ParseContext& ctx);
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 <memory>
29 #include <tuple>
30 #include <utility>
31 
32 #include "test_macros.h"
33 #include "make_string.h"
34 
35 #define SV(S) MAKE_STRING_VIEW(CharT, S)
36 
37 template <class Arg, class StringViewT>
38 constexpr void test(StringViewT fmt, std::size_t offset) {
39   using CharT    = typename StringViewT::value_type;
40   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
41   std::formatter<Arg, CharT> formatter;
42   static_assert(std::semiregular<decltype(formatter)>);
43 
44   std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
45   // std::to_address works around LWG3989 and MSVC STL's iterator debugging mechanism.
46   assert(std::to_address(it) == std::to_address(fmt.end()) - offset);
47 }
48 
49 template <class CharT, class Arg>
50 constexpr void test() {
51   test<Arg>(SV(""), 0);
52   test<Arg>(SV("42"), 0);
53 
54   test<Arg>(SV("}"), 1);
55   test<Arg>(SV("42}"), 1);
56 }
57 
58 template <class CharT>
59 constexpr void test() {
60   test<CharT, std::tuple<int>>();
61   test<CharT, std::tuple<int, CharT>>();
62   test<CharT, std::pair<int, CharT>>();
63   test<CharT, std::tuple<int, CharT, bool>>();
64 }
65 
66 constexpr bool test() {
67   test<char>();
68 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69   test<wchar_t>();
70 #endif
71 
72   return true;
73 }
74 
75 int main(int, char**) {
76   test();
77   static_assert(test());
78 
79   return 0;
80 }
81