xref: /llvm-project/libcxx/test/std/utilities/format/format.range/format.range.fmtset/format.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::set, R, charT>
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 <set>
30 
31 #include "assert_macros.h"
32 #include "format.functions.common.h"
33 #include "test_format_context.h"
34 #include "test_macros.h"
35 #include "make_string.h"
36 
37 #define SV(S) MAKE_STRING_VIEW(CharT, S)
38 
39 template <class StringViewT>
40 void test_format(StringViewT expected, std::set<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   std::formatter<std::set<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::set<parse_call_validator> arg{parse_call_validator{}};
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::formatter<decltype(arg), 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::set<parse_call_validator> arg{parse_call_validator{}};
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::formatter<decltype(arg), CharT> formatter;
87     TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx));
88   }
89 
90   test_assure_parse_is_called(SV("5"));
91   test_assure_parse_is_called(SV("n"));
92   test_assure_parse_is_called(SV("5n"));
93 }
94 
95 template <class CharT>
96 void test_fmt() {
97   test_format(SV("{42}"), std::set<int>{42});
98 
99   test_assure_parse_is_called<CharT>();
100 }
101 
102 void test() {
103   test_fmt<char>();
104 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
105   test_fmt<wchar_t>();
106 #endif
107 }
108 
109 int main(int, char**) {
110   test();
111 
112   return 0;
113 }
114