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::map, 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 <format>
28#include <iterator>
29#include <map>
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
39template <class StringViewT>
40void test_format(StringViewT expected, std::map<int, 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::map<int, 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
55template <class CharT>
56void 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::map<parse_call_validator, parse_call_validator> arg{{parse_call_validator{}, 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
73template <class CharT>
74void 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::map<parse_call_validator, parse_call_validator> arg{{parse_call_validator{}, 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("m"));
93 test_assure_parse_is_called(SV("5n"));
94 test_assure_parse_is_called(SV("5m"));
95}
96
97template <class CharT>
98void test_fmt() {
99 test_format(SV("{1: 42}"), std::map<int, int>{{1, 42}});
100 test_format(SV("{0: 99}"), std::map<int, int>{{0, 99}});
101
102 test_assure_parse_is_called<CharT>();
103}
104
105void test() {
106 test_fmt<char>();
107#ifndef TEST_HAS_NO_WIDE_CHARACTERS
108 test_fmt<wchar_t>();
109#endif
110}
111
112int main(int, char**) {
113 test();
114
115 return 0;
116}
117

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