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 10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME 11 12 // XFAIL: availability-fp_to_chars-missing 13 14 // <format> 15 16 // template<class Out, class... Args> 17 // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 18 // format-string<Args...> fmt, const Args&... args); 19 // template<class Out, class... Args> 20 // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, 21 // wformat-string<Args...> fmt, const Args&... args); 22 23 #include <format> 24 #include <algorithm> 25 #include <cassert> 26 #include <iterator> 27 #include <list> 28 #include <vector> 29 30 #include "test_macros.h" 31 #include "format_tests.h" 32 #include "string_literal.h" 33 #include "test_format_string.h" 34 35 auto test = []<class CharT, class... Args>( 36 std::basic_string_view<CharT> expected, 37 test_format_string<CharT, Args...> fmt, 38 Args&&... args) constexpr { 39 { 40 std::list<CharT> out; 41 std::format_to_n_result result = std::format_to_n(std::back_inserter(out), 0, fmt, std::forward<Args>(args)...); 42 // To avoid signedness warnings make sure formatted_size uses the same type 43 // as result.size. 44 using diff_type = decltype(result.size); 45 diff_type formatted_size = std::formatted_size(fmt, std::forward<Args>(args)...); 46 47 assert(result.size == formatted_size); 48 assert(out.empty()); 49 } 50 { 51 std::vector<CharT> out; 52 std::format_to_n_result result = std::format_to_n(std::back_inserter(out), 5, fmt, std::forward<Args>(args)...); 53 using diff_type = decltype(result.size); 54 diff_type formatted_size = std::formatted_size(fmt, std::forward<Args>(args)...); 55 diff_type size = std::min<diff_type>(5, formatted_size); 56 57 assert(result.size == formatted_size); 58 assert(std::equal(out.begin(), out.end(), expected.begin(), expected.begin() + size)); 59 } 60 { 61 std::basic_string<CharT> out; 62 std::format_to_n_result result = std::format_to_n(std::back_inserter(out), 1000, fmt, std::forward<Args>(args)...); 63 using diff_type = decltype(result.size); 64 diff_type formatted_size = std::formatted_size(fmt, std::forward<Args>(args)...); 65 diff_type size = std::min<diff_type>(1000, formatted_size); 66 67 assert(result.size == formatted_size); 68 assert(out == expected.substr(0, size)); 69 } 70 { 71 // Test the returned iterator. 72 std::basic_string<CharT> out(10, CharT(' ')); 73 std::format_to_n_result result = std::format_to_n(out.begin(), 10, fmt, std::forward<Args>(args)...); 74 using diff_type = decltype(result.size); 75 diff_type formatted_size = std::formatted_size(fmt, std::forward<Args>(args)...); 76 diff_type size = std::min<diff_type>(10, formatted_size); 77 78 assert(result.size == formatted_size); 79 assert(result.out == out.begin() + size); 80 assert(out.substr(0, size) == expected.substr(0, size)); 81 } 82 { 83 static_assert(std::is_signed_v<std::iter_difference_t<CharT*>>, 84 "If the difference type isn't negative the test will fail " 85 "due to using a large positive value."); 86 CharT buffer[1] = {CharT(0)}; 87 std::format_to_n_result result = std::format_to_n(buffer, -1, fmt, std::forward<Args>(args)...); 88 using diff_type = decltype(result.size); 89 diff_type formatted_size = std::formatted_size(fmt, std::forward<Args>(args)...); 90 91 assert(result.size == formatted_size); 92 assert(result.out == buffer); 93 assert(buffer[0] == CharT(0)); 94 } 95 }; 96 97 auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) { 98 // After P2216 most exceptions thrown by std::format_to_n become ill-formed. 99 // Therefore this tests does nothing. 100 // A basic ill-formed test is done in format_to_n.verify.cpp 101 // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. 102 }; 103 104 int main(int, char**) { 105 format_tests<char, execution_modus::partial>(test, test_exception); 106 107 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 108 format_tests_char_to_wchar_t(test); 109 format_tests<wchar_t, execution_modus::partial>(test, test_exception); 110 #endif 111 112 return 0; 113 } 114