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: 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 | // const locale& loc, format-string<Args...> fmt, |
19 | // const Args&... args); |
20 | // template<class Out, class... Args> |
21 | // format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n, |
22 | // const locale& loc, wformat-string<Args...> fmt, |
23 | // const Args&... args); |
24 | |
25 | #include <format> |
26 | #include <algorithm> |
27 | #include <cassert> |
28 | #include <list> |
29 | #include <vector> |
30 | |
31 | #include "test_macros.h" |
32 | #include "format_tests.h" |
33 | #include "string_literal.h" |
34 | #include "test_format_string.h" |
35 | |
36 | auto test = |
37 | []<class CharT, class... Args>( |
38 | std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, 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, std::forward<Args>(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, std::forward<Args>(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, std::forward<Args>(args)...); |
55 | using diff_type = decltype(result.size); |
56 | diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(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, std::forward<Args>(args)...); |
66 | using diff_type = decltype(result.size); |
67 | diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(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, std::forward<Args>(args)...); |
78 | using diff_type = decltype(result.size); |
79 | diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(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, std::forward<Args>(args)...); |
92 | using diff_type = decltype(result.size); |
93 | diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(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>, 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 | |
108 | int main(int, char**) { |
109 | format_tests<char, execution_modus::partial>(check: test, check_exception: test_exception); |
110 | |
111 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
112 | format_tests_char_to_wchar_t(check: test); |
113 | format_tests<wchar_t, execution_modus::partial>(check: test, check_exception: test_exception); |
114 | #endif |
115 | |
116 | return 0; |
117 | } |
118 | |