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
6 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: no-localization
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, const locale& loc,
18 // format-string<Args...> fmt, const Args&... args);
19 // template<class Out, class... Args>
20 // Out format_to(Out out, const locale& loc,
21 // wformat-string<Args...> fmt, const Args&... args);
30 #include "test_macros.h"
31 #include "format_tests.h"
32 #include "string_literal.h"
33 #include "test_format_string.h"
34 #include "test_iterators.h"
37 []<class CharT
, class... Args
>(
38 std::basic_string_view
<CharT
> expected
, test_format_string
<CharT
, Args
...> fmt
, Args
&&... args
) constexpr {
40 std::basic_string
<CharT
> out(expected
.size(), CharT(' '));
41 auto it
= std::format_to(out
.begin(), std::locale(), fmt
, std::forward
<Args
>(args
)...);
42 assert(it
== out
.end());
43 assert(out
== expected
);
47 std::format_to(std::back_inserter(out
), std::locale(), fmt
, std::forward
<Args
>(args
)...);
48 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
51 std::vector
<CharT
> out
;
52 std::format_to(std::back_inserter(out
), std::locale(), fmt
, std::forward
<Args
>(args
)...);
53 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
56 assert(expected
.size() < 4096 && "Update the size of the buffer.");
58 cpp20_output_iterator
<CharT
*> it
=
59 std::format_to(cpp20_output_iterator
{out
}, std::locale(), fmt
, std::forward
<Args
>(args
)...);
60 assert(std::distance(out
, base(it
)) == int(expected
.size()));
61 // Convert to std::string since output contains '\0' for boolean tests.
62 assert(std::basic_string
<CharT
>(out
, base(it
)) == expected
);
66 auto test_exception
= []<class CharT
, class... Args
>(std::string_view
, std::basic_string_view
<CharT
>, Args
&&...) {
67 // After P2216 most exceptions thrown by std::format_to become ill-formed.
68 // Therefore this tests does nothing.
69 // A basic ill-formed test is done in format_to.locale.verify.cpp
70 // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
73 int main(int, char**) {
74 format_tests
<char, execution_modus::partial
>(test
, test_exception
);
76 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
77 format_tests_char_to_wchar_t(test
);
78 format_tests
<wchar_t, execution_modus::partial
>(test
, test_exception
);