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