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 | // <format> |
11 | |
12 | // template<ranges::input_range R, class charT> |
13 | // requires (K == range_format::string || K == range_format::debug_string) |
14 | // struct range-default-formatter<K, R, charT> |
15 | |
16 | // template<class FormatContext> |
17 | // typename FormatContext::iterator |
18 | // format(const T& ref, FormatContext& ctx) const; |
19 | |
20 | // Note this tests the basics of this function. It's tested in more detail in |
21 | // the format.functions test. |
22 | |
23 | #include <cassert> |
24 | #include <concepts> |
25 | #include <format> |
26 | |
27 | #include "format.functions.tests.h" |
28 | #include "test_format_context.h" |
29 | #include "test_macros.h" |
30 | |
31 | template <class StringViewT, class ArgT> |
32 | void test_format(StringViewT expected, ArgT arg) { |
33 | using CharT = typename StringViewT::value_type; |
34 | using String = std::basic_string<CharT>; |
35 | using OutIt = std::back_insert_iterator<String>; |
36 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
37 | |
38 | std::formatter<ArgT, CharT> formatter; |
39 | |
40 | String result; |
41 | OutIt out = std::back_inserter(result); |
42 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
43 | formatter.format(arg, format_ctx); |
44 | assert(result == expected); |
45 | } |
46 | |
47 | template <class CharT> |
48 | void test_fmt() { |
49 | test_format(SV("hello" ), test_range_format_string<std::basic_string<CharT>>{STR("hello" )}); |
50 | test_format(SV("hello" ), test_range_format_debug_string<std::basic_string<CharT>>{STR("hello" )}); |
51 | } |
52 | |
53 | void test() { |
54 | test_fmt<char>(); |
55 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
56 | test_fmt<wchar_t>(); |
57 | #endif |
58 | } |
59 | |
60 | int main(int, char**) { |
61 | test(); |
62 | |
63 | return 0; |
64 | } |
65 | |