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