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