• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
9 
10 // <format>
11 
12 // C++23 the formatter is a debug-enabled specialization.
13 // [format.formatter.spec]:
14 // Each header that declares the template `formatter` provides the following
15 // enabled specializations:
16 // For each `charT`, the string type specializations
17 //   template<class traits, class Allocator>
18 //     struct formatter<basic_string<charT, traits, Allocator>, charT>;
19 //   template<class traits>
20 //     struct formatter<basic_string_view<charT, traits>, charT>;
21 
22 #include <format>
23 #include <cassert>
24 #include <concepts>
25 #include <iterator>
26 #include <type_traits>
27 
28 #include "make_string.h"
29 #include "test_format_context.h"
30 #include "test_macros.h"
31 
32 #define STR(S) MAKE_STRING(CharT, S)
33 #define SV(S) MAKE_STRING_VIEW(CharT, S)
34 #define CSTR(S) MAKE_CSTRING(CharT, S)
35 
36 template <class T, class ArgumentT, class StringT, class StringViewT>
test(StringT expected,StringViewT fmt,StringT a,std::size_t offset)37 void test(StringT expected, StringViewT fmt, StringT a, std::size_t offset) {
38   static_assert(
39       std::same_as<typename T::value_type,
40                    typename std::decay_t<ArgumentT>::value_type> &&
41       std::same_as<typename T::value_type, typename StringT::value_type>);
42   using CharT = typename T::value_type;
43 
44   auto parse_ctx = std::basic_format_parse_context<CharT>(fmt);
45   std::formatter<T, CharT> formatter;
46   static_assert(std::semiregular<decltype(formatter)>);
47 
48   auto it = formatter.parse(parse_ctx);
49   assert(it == fmt.end() - offset);
50 
51   StringT result;
52   auto out = std::back_inserter(result);
53   using FormatCtxT = std::basic_format_context<decltype(out), CharT>;
54 
55   ArgumentT arg = a;
56   FormatCtxT format_ctx = test_format_context_create<decltype(out), CharT>(
57       out, std::make_format_args<FormatCtxT>(std::forward<ArgumentT>(arg)));
58   formatter.format(arg, format_ctx);
59   assert(result == expected);
60 }
61 
62 template <class T, class ArgumentT, class StringT>
test_termination_condition(StringT expected,StringT f,StringT arg)63 void test_termination_condition(StringT expected, StringT f, StringT arg) {
64   // The format-spec is valid if completely consumed or terminates at a '}'.
65   // The valid inputs all end with a '}'. The test is executed twice:
66   // - first with the terminating '}',
67   // - second consuming the entire input.
68   using CharT = typename StringT::value_type;
69   std::basic_string_view<CharT> fmt{f};
70   assert(fmt.back() == CharT('}') && "Pre-condition failure");
71 
72   test<T, ArgumentT>(expected, fmt, arg, 1);
73   fmt.remove_suffix(1);
74   test<T, ArgumentT>(expected, fmt, arg, 0);
75 }
76 
77 #if TEST_STD_VER > 20
78 template <class ArgumentT, class CharT>
test_set_debug_format()79 constexpr bool test_set_debug_format() {
80   std::formatter<ArgumentT, CharT> formatter;
81   LIBCPP_ASSERT(formatter.__parser_.__type_ == std::__format_spec::__type::__default);
82 
83   formatter.set_debug_format();
84   LIBCPP_ASSERT(formatter.__parser_.__type_ == std::__format_spec::__type::__debug);
85 
86   std::basic_string_view fmt = SV("s}");
87   std::basic_format_parse_context<CharT> parse_ctx{fmt};
88   formatter.parse(parse_ctx);
89   LIBCPP_ASSERT(formatter.__parser_.__type_ == std::__format_spec::__type::__string);
90 
91   formatter.set_debug_format();
92   LIBCPP_ASSERT(formatter.__parser_.__type_ == std::__format_spec::__type::__debug);
93 
94   return true;
95 }
96 #endif
97 
98 template <class T, class ArgumentT>
test_string_type()99 void test_string_type() {
100   static_assert(std::same_as<typename T::value_type,
101                              typename std::decay_t<ArgumentT>::value_type>);
102   using CharT = typename T::value_type;
103 
104   test_termination_condition<T, ArgumentT>(STR(" azAZ09,./<>?"), STR("}"),
105                                            STR(" azAZ09,./<>?"));
106 
107   std::basic_string<CharT> s(CSTR("abc\0abc"), 7);
108   test_termination_condition<T, ArgumentT>(s, STR("}"), s);
109 
110   test_termination_condition<T, ArgumentT>(STR("world"), STR("}"),
111                                            STR("world"));
112   test_termination_condition<T, ArgumentT>(STR("world"), STR("_>}"),
113                                            STR("world"));
114 
115   test_termination_condition<T, ArgumentT>(STR("   world"), STR(">8}"),
116                                            STR("world"));
117   test_termination_condition<T, ArgumentT>(STR("___world"), STR("_>8}"),
118                                            STR("world"));
119   test_termination_condition<T, ArgumentT>(STR("_world__"), STR("_^8}"),
120                                            STR("world"));
121   test_termination_condition<T, ArgumentT>(STR("world___"), STR("_<8}"),
122                                            STR("world"));
123 
124   test_termination_condition<T, ArgumentT>(STR("world"), STR(".5}"),
125                                            STR("world"));
126   test_termination_condition<T, ArgumentT>(STR("unive"), STR(".5}"),
127                                            STR("universe"));
128 
129   test_termination_condition<T, ArgumentT>(STR("%world%"), STR("%^7.7}"),
130                                            STR("world"));
131   test_termination_condition<T, ArgumentT>(STR("univers"), STR("%^7.7}"),
132                                            STR("universe"));
133 
134 #if TEST_STD_VER > 20
135   test_set_debug_format<std::remove_cvref_t<ArgumentT>, CharT>();
136   static_assert(test_set_debug_format<std::remove_cvref_t<ArgumentT>, CharT>());
137 #endif
138 }
139 
140 template <class CharT>
test_all_string_types()141 void test_all_string_types() {
142   test_string_type<std::basic_string<CharT>, const std::basic_string<CharT>&>();
143   test_string_type<std::basic_string_view<CharT>,
144                    std::basic_string_view<CharT>>();
145 }
146 
main(int,char **)147 int main(int, char**) {
148   test_all_string_types<char>();
149 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
150   test_all_string_types<wchar_t>();
151 #endif
152   return 0;
153 }
154