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

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