xref: /llvm-project/libcxx/test/std/containers/container.adaptors/container.adaptors.format/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 // [container.adaptors.format]
14 // For each of queue, priority_queue, and stack, the library provides the
15 // following formatter specialization where adaptor-type is the name of the
16 // template:
17 //
18 // template<class charT, class T, formattable<charT> Container, class... U>
19 //   struct formatter<adaptor-type<T, Container, U...>, charT>
20 
21 // template<class ParseContext>
22 //   constexpr typename ParseContext::iterator
23 //     parse(ParseContext& ctx);
24 
25 // Note this tests the basics of this function. It's tested in more detail in
26 // the format functions test.
27 
28 #include <cassert>
29 #include <concepts>
30 #include <format>
31 #include <memory>
32 #include <queue>
33 #include <stack>
34 
35 #include "test_macros.h"
36 #include "make_string.h"
37 
38 #define SV(S) MAKE_STRING_VIEW(CharT, S)
39 
40 template <class Arg, class StringViewT>
41 constexpr void test_parse(StringViewT fmt, std::size_t offset) {
42   using CharT    = typename StringViewT::value_type;
43   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
44   std::formatter<Arg, CharT> formatter;
45   static_assert(std::semiregular<decltype(formatter)>);
46 
47   std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
48   // std::to_address works around LWG3989 and MSVC STL's iterator debugging mechanism.
49   assert(std::to_address(it) == std::to_address(fmt.end()) - offset);
50 }
51 
52 template <class StringViewT>
53 constexpr void test_parse(StringViewT fmt, std::size_t offset) {
54   test_parse<std::queue<int>>(fmt, offset);
55   test_parse<std::priority_queue<int>>(fmt, offset);
56   test_parse<std::stack<int>>(fmt, offset);
57 }
58 
59 template <class CharT>
60 constexpr void test_fmt() {
61   test_parse(SV(""), 0);
62   test_parse(SV(":d"), 0);
63 
64   test_parse(SV("}"), 1);
65   test_parse(SV(":d}"), 1);
66 }
67 
68 constexpr bool test() {
69   test_fmt<char>();
70 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
71   test_fmt<wchar_t>();
72 #endif
73 
74   return true;
75 }
76 
77 int main(int, char**) {
78   test();
79   static_assert(test());
80 
81   return 0;
82 }
83