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

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