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<class T, class charT = char>
16// requires same_as<remove_cvref_t<T>, T> && formattable<T, charT>
17// class range_formatter
18
19// template<class FormatContext>
20// typename FormatContext::iterator
21// format(const T& ref, FormatContext& ctx) const;
22
23// Note this tests the basics of this function. It's tested in more detail in
24// the format functions test.
25
26#include <cassert>
27#include <concepts>
28#include <iterator>
29#include <format>
30#include <vector>
31
32#include "assert_macros.h"
33#include "format.functions.common.h"
34#include "make_string.h"
35#include "test_format_context.h"
36#include "test_macros.h"
37
38#define SV(S) MAKE_STRING_VIEW(CharT, S)
39
40template <class StringViewT>
41void test_format(StringViewT expected, std::vector<int> arg) {
42 using CharT = typename StringViewT::value_type;
43 using String = std::basic_string<CharT>;
44 using OutIt = std::back_insert_iterator<String>;
45 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
46
47 const std::range_formatter<int, CharT> formatter;
48
49 String result;
50 OutIt out = std::back_inserter(result);
51 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
52 formatter.format(arg, format_ctx);
53 assert(result == expected);
54}
55
56template <class CharT>
57void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) {
58 using String = std::basic_string<CharT>;
59 using OutIt = std::back_insert_iterator<String>;
60 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
61 std::vector<parse_call_validator> arg{1};
62
63 String result;
64 OutIt out = std::back_inserter(result);
65 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
66
67 std::range_formatter<parse_call_validator, CharT> formatter;
68 std::basic_format_parse_context<CharT> ctx{fmt};
69
70 formatter.parse(ctx);
71 formatter.format(arg, format_ctx);
72}
73
74template <class CharT>
75void test_assure_parse_is_called() {
76 using String = std::basic_string<CharT>;
77 using OutIt = std::back_insert_iterator<String>;
78 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
79 std::vector<parse_call_validator> arg{1};
80
81 String result;
82 OutIt out = std::back_inserter(result);
83 [[maybe_unused]] FormatCtxT format_ctx =
84 test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
85
86 { // parse not called
87 [[maybe_unused]] const std::range_formatter<parse_call_validator, CharT> formatter;
88 TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx));
89 }
90
91 // The range-format-spec has no range-underlying-spec
92 // This uses various variants, which have different code paths in libc++
93 test_assure_parse_is_called(SV("5"));
94 test_assure_parse_is_called(SV("n"));
95 test_assure_parse_is_called(SV(":"));
96 test_assure_parse_is_called(SV("5"));
97 test_assure_parse_is_called(SV("5n"));
98 test_assure_parse_is_called(SV("5n:"));
99}
100
101template <class CharT>
102void test_fmt() {
103 test_format(SV("[1]"), std::vector<int>{1});
104 test_format(SV("[0]"), std::vector<int>{0});
105
106 test_assure_parse_is_called<CharT>();
107}
108
109void test() {
110 test_fmt<char>();
111#ifndef TEST_HAS_NO_WIDE_CHARACTERS
112 test_fmt<wchar_t>();
113#endif
114}
115
116int main(int, char**) {
117 test();
118
119 return 0;
120}
121

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