xref: /llvm-project/libcxx/test/std/utilities/format/format.range/format.range.fmtstr/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 // <format>
12 
13 // template<ranges::input_range R, class charT>
14 //   requires (K == range_format::string || K == range_format::debug_string)
15 // struct range-default-formatter<K, R, charT>
16 
17 // template<class ParseContext>
18 //   constexpr typename ParseContext::iterator
19 //     parse(ParseContext& ctx);
20 
21 // Note this tests the basics of this function. It's tested in more detail in
22 // the format.functions test.
23 
24 #include <cassert>
25 #include <concepts>
26 #include <format>
27 #include <memory>
28 
29 #include "format.functions.tests.h"
30 #include "test_macros.h"
31 
32 template <class FormatterT, class StringViewT>
33 constexpr void test_parse(StringViewT fmt, std::size_t offset) {
34   using CharT    = typename StringViewT::value_type;
35   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
36   FormatterT formatter;
37   static_assert(std::semiregular<decltype(formatter)>);
38 
39   std::same_as<typename StringViewT::iterator> auto it = formatter.parse(parse_ctx);
40   // std::to_address works around LWG3989 and MSVC STL's iterator debugging mechanism.
41   assert(std::to_address(it) == std::to_address(fmt.end()) - offset);
42 }
43 
44 template <class StringViewT>
45 constexpr void test_formatters(StringViewT fmt, std::size_t offset) {
46   using CharT = typename StringViewT::value_type;
47   test_parse<std::formatter<test_range_format_string<std::basic_string<CharT>>, CharT>>(fmt, offset);
48   test_parse<std::formatter<test_range_format_debug_string<std::basic_string<CharT>>, CharT>>(fmt, offset);
49 }
50 
51 template <class CharT>
52 constexpr void test_char_type() {
53   test_formatters(SV(""), 0);
54   test_formatters(SV("}"), 1);
55 }
56 
57 constexpr bool test() {
58   test_char_type<char>();
59 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
60   test_char_type<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