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 | // Out format_to(Out out, format-string<Args...> fmt, const Args&... args); |
17 | // template<class Out, class... Args> |
18 | // Out format_to(Out out, wformat-string<Args...> fmt, const Args&... args); |
19 | |
20 | #include <format> |
21 | #include <algorithm> |
22 | #include <cassert> |
23 | #include <iterator> |
24 | #include <list> |
25 | #include <vector> |
26 | |
27 | #include "test_macros.h" |
28 | #include "format_tests.h" |
29 | #include "string_literal.h" |
30 | #include "test_format_string.h" |
31 | #include "test_iterators.h" |
32 | |
33 | auto test = |
34 | []<class CharT, class... Args>( |
35 | std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr { |
36 | { |
37 | std::basic_string<CharT> out(expected.size(), CharT(' ')); |
38 | auto it = std::format_to(out.begin(), fmt, std::forward<Args>(args)...); |
39 | assert(it == out.end()); |
40 | assert(out == expected); |
41 | } |
42 | { |
43 | std::list<CharT> out; |
44 | std::format_to(std::back_inserter(out), fmt, std::forward<Args>(args)...); |
45 | assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); |
46 | } |
47 | { |
48 | std::vector<CharT> out; |
49 | std::format_to(std::back_inserter(out), fmt, std::forward<Args>(args)...); |
50 | assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); |
51 | } |
52 | { |
53 | assert(expected.size() < 4096 && "Update the size of the buffer." ); |
54 | CharT out[4096]; |
55 | cpp20_output_iterator<CharT*> it = std::format_to(cpp20_output_iterator{out}, fmt, std::forward<Args>(args)...); |
56 | assert(std::distance(out, base(it)) == int(expected.size())); |
57 | // Convert to std::string since output contains '\0' for boolean tests. |
58 | assert(std::basic_string<CharT>(out, base(it)) == expected); |
59 | } |
60 | }; |
61 | |
62 | auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) { |
63 | // After P2216 most exceptions thrown by std::format become ill-formed. |
64 | // Therefore this tests does nothing. |
65 | // A basic ill-formed test is done in format.verify.cpp |
66 | // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. |
67 | }; |
68 | |
69 | int main(int, char**) { |
70 | format_tests<char, execution_modus::partial>(check: test, check_exception: test_exception); |
71 | |
72 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
73 | format_tests_char_to_wchar_t(check: test); |
74 | format_tests<wchar_t, execution_modus::partial>(check: test, check_exception: test_exception); |
75 | #endif |
76 | |
77 | return 0; |
78 | } |
79 | |