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