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// constexpr void set_separator(basic_string_view<charT> sep) noexcept;
18
19#include <format>
20#include <cassert>
21#include <iterator>
22#include <type_traits>
23#include <vector>
24
25#include "make_string.h"
26#include "test_format_context.h"
27
28#define SV(S) MAKE_STRING_VIEW(CharT, S)
29
30template <class CharT>
31constexpr void test_setter() {
32 std::formatter<std::vector<int>, CharT> formatter;
33 formatter.set_separator(SV("sep"));
34 // Note the SV macro may throw, so can't use it.
35 static_assert(noexcept(formatter.set_separator(std::basic_string_view<CharT>{})));
36
37 // Note there is no direct way to validate this function modified the object.
38 if (!std::is_constant_evaluated()) {
39 using String = std::basic_string<CharT>;
40 using OutIt = std::back_insert_iterator<String>;
41 using FormatCtxT = std::basic_format_context<OutIt, CharT>;
42
43 String result;
44 OutIt out = std::back_inserter(result);
45 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>());
46 formatter.format(std::vector<int>{0, 42, 99}, format_ctx);
47 assert(result == SV("[0sep42sep99]"));
48 }
49}
50
51constexpr bool test() {
52 test_setter<char>();
53#ifndef TEST_HAS_NO_WIDE_CHARACTERS
54 test_setter<wchar_t>();
55#endif
56
57 return true;
58}
59
60int main(int, char**) {
61 test();
62 static_assert(test());
63
64 return 0;
65}
66

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