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