xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/vector.bool.fmt/format.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 // <vector>
14 
15 // template<class T, class charT>
16 //   requires is-vector-bool-reference<T>
17 // struct formatter<T, charT>
18 
19 // template<class FormatContext>
20 //   typename FormatContext::iterator
21 //     format(const T& r, FormatContext& ctx) const;
22 
23 // Note this tests the basics of this function. It's tested in more detail in
24 // the format functions test.
25 
26 #include <cassert>
27 #include <concepts>
28 #include <format>
29 #include <iterator>
30 #include <vector>
31 
32 #include "test_format_context.h"
33 #include "test_macros.h"
34 #include "make_string.h"
35 
36 #define SV(S) MAKE_STRING_VIEW(CharT, S)
37 
38 template <class StringViewT>
39 void test_format(StringViewT expected, std::vector<bool>::reference arg) {
40   using CharT      = typename StringViewT::value_type;
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   const std::formatter<std::vector<bool>::reference, CharT> formatter;
46 
47   String result;
48   OutIt out             = std::back_inserter(result);
49   FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg));
50   formatter.format(arg, format_ctx);
51   assert(result == expected);
52 }
53 
54 template <class CharT>
55 void test_fmt() {
56   test_format(SV("true"), std::vector<bool>{true}[0]);
57   test_format(SV("false"), std::vector<bool>{false}[0]);
58 }
59 
60 void test() {
61   test_fmt<char>();
62 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
63   test_fmt<wchar_t>();
64 #endif
65 }
66 
67 int main(int, char**) {
68   test();
69 
70   return 0;
71 }
72