Debian Codegraph @

libcxx/test/std/utilities/format/format.functions/format_to_n.pass.cpp

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