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<ranges::input_range R, class charT> |
15 | // struct range-default-formatter<range_format::set, R, charT> |
16 | |
17 | // template<class FormatContext> |
18 | // typename FormatContext::iterator |
19 | // format(const T& ref, FormatContext& ctx) const; |
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 <iterator> |
27 | #include <format> |
28 | #include <set> |
29 | |
30 | #include "assert_macros.h" |
31 | #include "format.functions.common.h" |
32 | #include "test_format_context.h" |
33 | #include "test_macros.h" |
34 | #include "make_string.h" |
35 | |
36 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
37 | |
38 | template <class StringViewT> |
39 | void test_format(StringViewT expected, std::set<int> arg) { |
40 | using CharT = typename StringViewT::value_type; |
41 | using String = std::basic_string<CharT>; |
42 | using OutIt = std::back_insert_iterator<String>; |
43 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
44 | |
45 | std::formatter<std::set<int>, CharT> formatter; |
46 | |
47 | String result; |
48 | OutIt out = std::back_inserter(result); |
49 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
50 | formatter.format(arg, format_ctx); |
51 | assert(result == expected); |
52 | } |
53 | |
54 | template <class CharT> |
55 | void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) { |
56 | using String = std::basic_string<CharT>; |
57 | using OutIt = std::back_insert_iterator<String>; |
58 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
59 | std::set<parse_call_validator> arg{parse_call_validator{}}; |
60 | |
61 | String result; |
62 | OutIt out = std::back_inserter(result); |
63 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
64 | |
65 | std::formatter<decltype(arg), CharT> formatter; |
66 | std::basic_format_parse_context<CharT> ctx{fmt}; |
67 | |
68 | formatter.parse(ctx); |
69 | formatter.format(arg, format_ctx); |
70 | } |
71 | |
72 | template <class CharT> |
73 | void test_assure_parse_is_called() { |
74 | using String = std::basic_string<CharT>; |
75 | using OutIt = std::back_insert_iterator<String>; |
76 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
77 | std::set<parse_call_validator> arg{parse_call_validator{}}; |
78 | |
79 | String result; |
80 | OutIt out = std::back_inserter(result); |
81 | [[maybe_unused]] FormatCtxT format_ctx = |
82 | test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
83 | |
84 | { // parse not called |
85 | [[maybe_unused]] const std::formatter<decltype(arg), CharT> formatter; |
86 | TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx)); |
87 | } |
88 | |
89 | test_assure_parse_is_called(SV("5" )); |
90 | test_assure_parse_is_called(SV("n" )); |
91 | test_assure_parse_is_called(SV("5n" )); |
92 | } |
93 | |
94 | template <class CharT> |
95 | void test_fmt() { |
96 | test_format(SV("{42}" ), std::set<int>{42}); |
97 | |
98 | test_assure_parse_is_called<CharT>(); |
99 | } |
100 | |
101 | void test() { |
102 | test_fmt<char>(); |
103 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
104 | test_fmt<wchar_t>(); |
105 | #endif |
106 | } |
107 | |
108 | int main(int, char**) { |
109 | test(); |
110 | |
111 | return 0; |
112 | } |
113 | |