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

source code of libcxx/test/std/utilities/format/format.range/format.range.fmtdef/format.pass.cpp