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: GCC-ALWAYS_INLINE-FIXME |
11 | |
12 | // XFAIL: availability-fp_to_chars-missing |
13 | |
14 | // <format> |
15 | |
16 | // template<class Out> |
17 | // Out vformat_to(Out out, string_view fmt, format_args args); |
18 | // template<class Out> |
19 | // Out vformat_to(Out out, wstring_view fmt, wformat_args_t args); |
20 | |
21 | #include <format> |
22 | #include <algorithm> |
23 | #include <cassert> |
24 | #include <list> |
25 | #include <vector> |
26 | |
27 | #include "assert_macros.h" |
28 | #include "concat_macros.h" |
29 | #include "test_macros.h" |
30 | #include "format_tests.h" |
31 | #include "string_literal.h" |
32 | |
33 | auto test = []<class CharT, class... Args>( |
34 | std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr { |
35 | { |
36 | std::basic_string<CharT> out(expected.size(), CharT(' ')); |
37 | auto it = std::vformat_to(out.begin(), fmt, std::make_format_args<context_t<CharT>>(args...)); |
38 | assert(it == out.end()); |
39 | assert(out == expected); |
40 | } |
41 | { |
42 | std::list<CharT> out; |
43 | std::vformat_to(std::back_inserter(out), fmt, std::make_format_args<context_t<CharT>>(args...)); |
44 | assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); |
45 | } |
46 | { |
47 | std::vector<CharT> out; |
48 | std::vformat_to(std::back_inserter(out), fmt, std::make_format_args<context_t<CharT>>(args...)); |
49 | assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); |
50 | } |
51 | { |
52 | assert(expected.size() < 4096 && "Update the size of the buffer." ); |
53 | CharT out[4096]; |
54 | CharT* it = std::vformat_to(out, fmt, std::make_format_args<context_t<CharT>>(args...)); |
55 | assert(std::distance(out, it) == int(expected.size())); |
56 | // Convert to std::string since output contains '\0' for boolean tests. |
57 | assert(std::basic_string<CharT>(out, it) == expected); |
58 | } |
59 | }; |
60 | |
61 | auto test_exception = |
62 | []<class CharT, class... Args>( |
63 | [[maybe_unused]] std::string_view what, |
64 | [[maybe_unused]] std::basic_string_view<CharT> fmt, |
65 | [[maybe_unused]] Args&&... args) { |
66 | TEST_VALIDATE_EXCEPTION( |
67 | std::format_error, |
68 | [&]([[maybe_unused]] const std::format_error& e) { |
69 | TEST_LIBCPP_REQUIRE( |
70 | e.what() == what, |
71 | TEST_WRITE_CONCATENATED( |
72 | "\nFormat string " , fmt, "\nExpected exception " , what, "\nActual exception " , e.what(), '\n')); |
73 | }, |
74 | [&] { |
75 | std::basic_string<CharT> out; |
76 | std::vformat_to(std::back_inserter(out), fmt, std::make_format_args<context_t<CharT>>(args...)); |
77 | }()); |
78 | }; |
79 | |
80 | int main(int, char**) { |
81 | format_tests<char, execution_modus::partial>(check: test, check_exception: test_exception); |
82 | |
83 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
84 | format_tests_char_to_wchar_t(check: test); |
85 | format_tests<wchar_t, execution_modus::partial>(check: test, check_exception: test_exception); |
86 | #endif |
87 | |
88 | return 0; |
89 | } |
90 | |