xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/P2418.pass.cpp (revision 5629d492df388bf6b5532a2a5c1ef5fd27d65ab0)
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
9 // UNSUPPORTED: libcpp-has-no-incomplete-format
10 // TODO FMT Evaluate gcc-12 status
11 // UNSUPPORTED: gcc-12
12 
13 // Tests whether a move only type can be formatted. This is required by
14 // P2418R2 "Add support for std::generator-like types to std::format"
15 
16 // <format>
17 
18 #include <format>
19 #include <cassert>
20 
21 #include "MoveOnly.h"
22 #include "make_string.h"
23 #include "test_macros.h"
24 
25 #ifndef TEST_HAS_NO_LOCALIZATION
26 #  include <locale>
27 #endif
28 
29 #define SV(S) MAKE_STRING_VIEW(CharT, S)
30 
31 template <class CharT>
32 struct std::formatter<MoveOnly, CharT> : std::formatter<int, CharT> {
33   auto format(const MoveOnly& m, auto& ctx) const -> decltype(ctx.out()) {
34     return std::formatter<int, CharT>::format(m.get(), ctx);
35   }
36 };
37 
38 template <class CharT>
39 static void test() {
40   MoveOnly m{10};
41   CharT buffer[10];
42 #ifndef TEST_HAS_NO_LOCALIZATION
43   std::locale loc;
44 #endif
45 
46   assert(std::format(SV("{}"), MoveOnly{}) == SV("1"));
47 
48   assert(std::format(SV("{}"), m) == SV("10"));
49   assert(m.get() == 10);
50 
51   assert(std::format(SV("{}"), std::move(m)) == SV("10"));
52   assert(m.get() == 10);
53 
54 #ifndef TEST_HAS_NO_LOCALIZATION
55   assert(std::format(loc, SV("{}"), MoveOnly{}) == SV("1"));
56 
57   assert(std::format(loc, SV("{}"), m) == SV("10"));
58   assert(m.get() == 10);
59 
60   assert(std::format(loc, SV("{}"), std::move(m)) == SV("10"));
61   assert(m.get() == 10);
62 #endif
63 
64   assert(std::format_to(buffer, SV("{}"), MoveOnly{}) == &buffer[1]);
65 
66   assert(std::format_to(buffer, SV("{}"), m) == &buffer[2]);
67   assert(m.get() == 10);
68 
69   assert(std::format_to(buffer, SV("{}"), std::move(m)) == &buffer[2]);
70   assert(m.get() == 10);
71 
72 #ifndef TEST_HAS_NO_LOCALIZATION
73   assert(std::format_to(buffer, loc, SV("{}"), MoveOnly{}) == &buffer[1]);
74 
75   assert(std::format_to(buffer, loc, SV("{}"), m) == &buffer[2]);
76   assert(m.get() == 10);
77 
78   assert(std::format_to(buffer, loc, SV("{}"), std::move(m)) == &buffer[2]);
79   assert(m.get() == 10);
80 #endif
81 
82   assert(std::format_to_n(buffer, 5, SV("{}"), MoveOnly{}).out == &buffer[1]);
83 
84   assert(std::format_to_n(buffer, 5, SV("{}"), m).out == &buffer[2]);
85   assert(m.get() == 10);
86 
87   assert(std::format_to_n(buffer, 5, SV("{}"), std::move(m)).out == &buffer[2]);
88   assert(m.get() == 10);
89 
90 #ifndef TEST_HAS_NO_LOCALIZATION
91   assert(std::format_to_n(buffer, 5, loc, SV("{}"), MoveOnly{}).out == &buffer[1]);
92 
93   assert(std::format_to_n(buffer, 5, loc, SV("{}"), m).out == &buffer[2]);
94   assert(m.get() == 10);
95 
96   assert(std::format_to_n(buffer, 5, loc, SV("{}"), std::move(m)).out == &buffer[2]);
97   assert(m.get() == 10);
98 #endif
99 
100   assert(std::formatted_size(SV("{}"), MoveOnly{}) == 1);
101 
102   assert(std::formatted_size(SV("{}"), m) == 2);
103   assert(m.get() == 10);
104 
105   assert(std::formatted_size(SV("{}"), std::move(m)) == 2);
106   assert(m.get() == 10);
107 
108 #ifndef TEST_HAS_NO_LOCALIZATION
109   assert(std::formatted_size(loc, SV("{}"), MoveOnly{}) == 1);
110 
111   assert(std::formatted_size(loc, SV("{}"), m) == 2);
112   assert(m.get() == 10);
113 
114   assert(std::formatted_size(loc, SV("{}"), std::move(m)) == 2);
115   assert(m.get() == 10);
116 #endif
117 }
118 
119 int main(int, char**) {
120   test<char>();
121 
122 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
123   test<wchar_t>();
124 #endif
125 
126   return 0;
127 }
128