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