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

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