xref: /llvm-project/libcxx/test/std/utilities/format/format.range/format.range.fmtmap/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<ranges::input_range R, class charT>
16 //   struct range-default-formatter<range_format::map, R, 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 test.
24 
25 #include <cassert>
26 #include <concepts>
27 #include <format>
28 #include <map>
29 #include <memory>
30 
31 #include "test_macros.h"
32 #include "make_string.h"
33 
34 #define SV(S) MAKE_STRING_VIEW(CharT, S)
35 
36 template <class StringViewT>
37 constexpr void test_parse(StringViewT fmt, std::size_t offset) {
38   using CharT    = typename StringViewT::value_type;
39   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
40   std::formatter<std::map<int, int>, CharT> formatter;
41   static_assert(std::semiregular<decltype(formatter)>);
42 
43   std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
44   // std::to_address works around LWG3989 and MSVC STL's iterator debugging mechanism.
45   assert(std::to_address(it) == std::to_address(fmt.end()) - offset);
46 }
47 
48 template <class CharT>
49 constexpr void test_fmt() {
50   test_parse(SV(""), 0);
51   test_parse(SV(":5"), 0);
52 
53   test_parse(SV("}"), 1);
54   test_parse(SV(":5}"), 1);
55 }
56 
57 constexpr bool test() {
58   test_fmt<char>();
59 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
60   test_fmt<wchar_t>();
61 #endif
62 
63   return true;
64 }
65 
66 int main(int, char**) {
67   test();
68   static_assert(test());
69 
70   return 0;
71 }
72