Debian Codegraph @

libcxx/test/std/containers/container.adaptors/container.adaptors.format/parse.pass.cpp

raw
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
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12 // [container.adaptors.format]
13 // For each of queue, priority_queue, and stack, the library provides the
14 // following formatter specialization where adaptor-type is the name of the
15 // template:
16 //
17 // template<class charT, class T, formattable<charT> Container, class... U>
18 // struct formatter<adaptor-type<T, Container, U...>, 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 test.
26
27 #include <cassert>
28 #include <concepts>
29 #include <format>
30 #include <memory>
31 #include <queue>
32 #include <stack>
33
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_parse(StringViewT fmt, std::size_t offset) {
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 // std::to_address works around LWG3989 and MSVC STL's iterator debugging mechanism.
48 assert(std::to_address(it) == std::to_address(fmt.end()) - offset);
49 }
50
51 template <class StringViewT>
52 constexpr void test_parse(StringViewT fmt, std::size_t offset) {
53 test_parse<std::queue<int>>(fmt, offset);
54 test_parse<std::priority_queue<int>>(fmt, offset);
55 test_parse<std::stack<int>>(fmt, offset);
56 }
57
58 template <class CharT>
59 constexpr void test_fmt() {
60 test_parse(SV(""), 0);
61 test_parse(SV(":d"), 0);
62
63 test_parse(SV("}"), 1);
64 test_parse(SV(":d}"), 1);
65 }
66
67 constexpr bool test() {
68 test_fmt<char>();
69 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
70 test_fmt<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 }