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, c++20 |
9 | |
10 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
11 | |
12 | // <format> |
13 | |
14 | // template<class T, class charT = char> |
15 | // requires same_as<remove_cvref_t<T>, T> && formattable<T, charT> |
16 | // class range_formatter |
17 | |
18 | // template<class FormatContext> |
19 | // typename FormatContext::iterator |
20 | // format(const T& ref, FormatContext& ctx) const; |
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 <iterator> |
28 | #include <format> |
29 | #include <vector> |
30 | |
31 | #include "assert_macros.h" |
32 | #include "format.functions.common.h" |
33 | #include "make_string.h" |
34 | #include "test_format_context.h" |
35 | #include "test_macros.h" |
36 | |
37 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
38 | |
39 | template <class StringViewT> |
40 | void test_format(StringViewT expected, std::vector<int> arg) { |
41 | using CharT = typename StringViewT::value_type; |
42 | using String = std::basic_string<CharT>; |
43 | using OutIt = std::back_insert_iterator<String>; |
44 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
45 | |
46 | const std::range_formatter<int, CharT> formatter; |
47 | |
48 | String result; |
49 | OutIt out = std::back_inserter(result); |
50 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
51 | formatter.format(arg, format_ctx); |
52 | assert(result == expected); |
53 | } |
54 | |
55 | template <class CharT> |
56 | void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) { |
57 | using String = std::basic_string<CharT>; |
58 | using OutIt = std::back_insert_iterator<String>; |
59 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
60 | std::vector<parse_call_validator> arg{1}; |
61 | |
62 | String result; |
63 | OutIt out = std::back_inserter(result); |
64 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
65 | |
66 | std::range_formatter<parse_call_validator, CharT> formatter; |
67 | std::basic_format_parse_context<CharT> ctx{fmt}; |
68 | |
69 | formatter.parse(ctx); |
70 | formatter.format(arg, format_ctx); |
71 | } |
72 | |
73 | template <class CharT> |
74 | void test_assure_parse_is_called() { |
75 | using String = std::basic_string<CharT>; |
76 | using OutIt = std::back_insert_iterator<String>; |
77 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
78 | std::vector<parse_call_validator> arg{1}; |
79 | |
80 | String result; |
81 | OutIt out = std::back_inserter(result); |
82 | [[maybe_unused]] FormatCtxT format_ctx = |
83 | test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
84 | |
85 | { // parse not called |
86 | [[maybe_unused]] const std::range_formatter<parse_call_validator, CharT> formatter; |
87 | TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx)); |
88 | } |
89 | |
90 | // The range-format-spec has no range-underlying-spec |
91 | // This uses various variants, which have different code paths in libc++ |
92 | test_assure_parse_is_called(SV("5" )); |
93 | test_assure_parse_is_called(SV("n" )); |
94 | test_assure_parse_is_called(SV(":" )); |
95 | test_assure_parse_is_called(SV("5" )); |
96 | test_assure_parse_is_called(SV("5n" )); |
97 | test_assure_parse_is_called(SV("5n:" )); |
98 | } |
99 | |
100 | template <class CharT> |
101 | void test_fmt() { |
102 | test_format(SV("[1]" ), std::vector<int>{1}); |
103 | test_format(SV("[0]" ), std::vector<int>{0}); |
104 | |
105 | test_assure_parse_is_called<CharT>(); |
106 | } |
107 | |
108 | void test() { |
109 | test_fmt<char>(); |
110 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
111 | test_fmt<wchar_t>(); |
112 | #endif |
113 | } |
114 | |
115 | int main(int, char**) { |
116 | test(); |
117 | |
118 | return 0; |
119 | } |
120 | |