xref: /llvm-project/libcxx/test/std/utilities/format/format.range/format.range.fmtstr/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 // <format>
12 
13 // template<ranges::input_range R, class charT>
14 //   requires (K == range_format::string || K == range_format::debug_string)
15 // struct range-default-formatter<K, 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 <format>
27 
28 #include "format.functions.tests.h"
29 #include "test_format_context.h"
30 #include "test_macros.h"
31 
32 template <class StringViewT, class ArgT>
33 void test_format(StringViewT expected, ArgT arg) {
34   using CharT      = typename StringViewT::value_type;
35   using String     = std::basic_string<CharT>;
36   using OutIt      = std::back_insert_iterator<String>;
37   using FormatCtxT = std::basic_format_context<OutIt, CharT>;
38 
39   std::formatter<ArgT, CharT> formatter;
40 
41   String result;
42   OutIt out             = std::back_inserter(result);
43   FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
44   formatter.format(arg, format_ctx);
45   assert(result == expected);
46 }
47 
48 template <class CharT>
49 void test_fmt() {
50   test_format(SV("hello"), test_range_format_string<std::basic_string<CharT>>{STR("hello")});
51   test_format(SV("hello"), test_range_format_debug_string<std::basic_string<CharT>>{STR("hello")});
52 }
53 
54 void test() {
55   test_fmt<char>();
56 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
57   test_fmt<wchar_t>();
58 #endif
59 }
60 
61 int main(int, char**) {
62   test();
63 
64   return 0;
65 }
66