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
6 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11 // TODO FMT This test should not require std::to_chars(floating-point)
12 // XFAIL: availability-fp_to_chars-missing
14 // Tests whether a move only type can be formatted. This is required by
15 // P2418R2 "Add support for std::generator-like types to std::format"
23 #include "make_string.h"
24 #include "test_macros.h"
26 #ifndef TEST_HAS_NO_LOCALIZATION
30 #define SV(S) MAKE_STRING_VIEW(CharT, S)
32 template <class CharT
>
33 struct std::formatter
<MoveOnly
, CharT
> : std::formatter
<int, CharT
> {
34 auto format(const MoveOnly
& m
, auto& ctx
) const -> decltype(ctx
.out()) {
35 return std::formatter
<int, CharT
>::format(m
.get(), ctx
);
39 template <class CharT
>
43 #ifndef TEST_HAS_NO_LOCALIZATION
47 assert(std::format(SV("{}"), MoveOnly
{}) == SV("1"));
49 assert(std::format(SV("{}"), m
) == SV("10"));
50 assert(m
.get() == 10);
52 assert(std::format(SV("{}"), std::move(m
)) == SV("10"));
53 assert(m
.get() == 10);
55 #ifndef TEST_HAS_NO_LOCALIZATION
56 assert(std::format(loc
, SV("{}"), MoveOnly
{}) == SV("1"));
58 assert(std::format(loc
, SV("{}"), m
) == SV("10"));
59 assert(m
.get() == 10);
61 assert(std::format(loc
, SV("{}"), std::move(m
)) == SV("10"));
62 assert(m
.get() == 10);
65 assert(std::format_to(buffer
, SV("{}"), MoveOnly
{}) == &buffer
[1]);
67 assert(std::format_to(buffer
, SV("{}"), m
) == &buffer
[2]);
68 assert(m
.get() == 10);
70 assert(std::format_to(buffer
, SV("{}"), std::move(m
)) == &buffer
[2]);
71 assert(m
.get() == 10);
73 #ifndef TEST_HAS_NO_LOCALIZATION
74 assert(std::format_to(buffer
, loc
, SV("{}"), MoveOnly
{}) == &buffer
[1]);
76 assert(std::format_to(buffer
, loc
, SV("{}"), m
) == &buffer
[2]);
77 assert(m
.get() == 10);
79 assert(std::format_to(buffer
, loc
, SV("{}"), std::move(m
)) == &buffer
[2]);
80 assert(m
.get() == 10);
83 assert(std::format_to_n(buffer
, 5, SV("{}"), MoveOnly
{}).out
== &buffer
[1]);
85 assert(std::format_to_n(buffer
, 5, SV("{}"), m
).out
== &buffer
[2]);
86 assert(m
.get() == 10);
88 assert(std::format_to_n(buffer
, 5, SV("{}"), std::move(m
)).out
== &buffer
[2]);
89 assert(m
.get() == 10);
91 #ifndef TEST_HAS_NO_LOCALIZATION
92 assert(std::format_to_n(buffer
, 5, loc
, SV("{}"), MoveOnly
{}).out
== &buffer
[1]);
94 assert(std::format_to_n(buffer
, 5, loc
, SV("{}"), m
).out
== &buffer
[2]);
95 assert(m
.get() == 10);
97 assert(std::format_to_n(buffer
, 5, loc
, SV("{}"), std::move(m
)).out
== &buffer
[2]);
98 assert(m
.get() == 10);
101 assert(std::formatted_size(SV("{}"), MoveOnly
{}) == 1);
103 assert(std::formatted_size(SV("{}"), m
) == 2);
104 assert(m
.get() == 10);
106 assert(std::formatted_size(SV("{}"), std::move(m
)) == 2);
107 assert(m
.get() == 10);
109 #ifndef TEST_HAS_NO_LOCALIZATION
110 assert(std::formatted_size(loc
, SV("{}"), MoveOnly
{}) == 1);
112 assert(std::formatted_size(loc
, SV("{}"), m
) == 2);
113 assert(m
.get() == 10);
115 assert(std::formatted_size(loc
, SV("{}"), std::move(m
)) == 2);
116 assert(m
.get() == 10);
120 int main(int, char**) {
123 #ifndef TEST_HAS_NO_WIDE_CHARACTERS