1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 // XFAIL: availability-fp_to_chars-missing
16 // template<class Out, class... Args>
17 // Out format_to(Out out, format-string<Args...> fmt, const Args&... args);
18 // template<class Out, class... Args>
19 // Out format_to(Out out, wformat-string<Args...> fmt, const Args&... args);
28 #include "test_macros.h"
29 #include "format_tests.h"
30 #include "string_literal.h"
31 #include "test_format_string.h"
32 #include "test_iterators.h"
35 []<class CharT
, class... Args
>(
36 std::basic_string_view
<CharT
> expected
, test_format_string
<CharT
, Args
...> fmt
, Args
&&... args
) constexpr {
38 std::basic_string
<CharT
> out(expected
.size(), CharT(' '));
39 auto it
= std::format_to(out
.begin(), fmt
, std::forward
<Args
>(args
)...);
40 assert(it
== out
.end());
41 assert(out
== expected
);
45 std::format_to(std::back_inserter(out
), fmt
, std::forward
<Args
>(args
)...);
46 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
49 std::vector
<CharT
> out
;
50 std::format_to(std::back_inserter(out
), fmt
, std::forward
<Args
>(args
)...);
51 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
54 assert(expected
.size() < 4096 && "Update the size of the buffer.");
56 cpp20_output_iterator
<CharT
*> it
= std::format_to(cpp20_output_iterator
{out
}, fmt
, std::forward
<Args
>(args
)...);
57 assert(std::distance(out
, base(it
)) == int(expected
.size()));
58 // Convert to std::string since output contains '\0' for boolean tests.
59 assert(std::basic_string
<CharT
>(out
, base(it
)) == expected
);
63 auto test_exception
= []<class CharT
, class... Args
>(std::string_view
, std::basic_string_view
<CharT
>, Args
&&...) {
64 // After P2216 most exceptions thrown by std::format become ill-formed.
65 // Therefore this tests does nothing.
66 // A basic ill-formed test is done in format.verify.cpp
67 // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
70 int main(int, char**) {
71 format_tests
<char, execution_modus::partial
>(test
, test_exception
);
73 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
74 format_tests_char_to_wchar_t(test
);
75 format_tests
<wchar_t, execution_modus::partial
>(test
, test_exception
);