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>
17 // Out vformat_to(Out out, const locale& loc, string_view fmt,
19 // template<class Out>
20 // Out vformat_to(Out out, const locale& loc, wstring_view fmt,
21 // wformat_args args);
29 #include "assert_macros.h"
30 #include "concat_macros.h"
31 #include "format_tests.h"
32 #include "string_literal.h"
34 auto test
= []<class CharT
, class... Args
>(
35 std::basic_string_view
<CharT
> expected
, std::basic_string_view
<CharT
> fmt
, Args
&&... args
) constexpr {
37 std::basic_string
<CharT
> out(expected
.size(), CharT(' '));
38 auto it
= std::vformat_to(out
.begin(), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
39 assert(it
== out
.end());
40 assert(out
== expected
);
44 std::vformat_to(std::back_inserter(out
), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
45 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
48 std::vector
<CharT
> out
;
49 std::vformat_to(std::back_inserter(out
), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
50 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
53 assert(expected
.size() < 4096 && "Update the size of the buffer.");
55 CharT
* it
= std::vformat_to(out
, std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
56 assert(std::distance(out
, it
) == int(expected
.size()));
57 // Convert to std::string since output contains '\0' for boolean tests.
58 assert(std::basic_string
<CharT
>(out
, it
) == expected
);
62 auto test_exception
= []<class CharT
, class... Args
>([[maybe_unused
]] std::string_view what
,
63 [[maybe_unused
]] std::basic_string_view
<CharT
> fmt
,
64 [[maybe_unused
]] Args
&&... args
) {
65 TEST_VALIDATE_EXCEPTION(
67 [&]([[maybe_unused
]] const std::format_error
& e
) {
70 TEST_WRITE_CONCATENATED(
71 "\nFormat string ", fmt
, "\nExpected exception ", what
, "\nActual exception ", e
.what(), '\n'));
74 std::basic_string
<CharT
> out
;
75 std::vformat_to(std::back_inserter(out
), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
79 int main(int, char**) {
80 format_tests
<char, execution_modus::partial
>(test
, test_exception
);
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
83 format_tests_char_to_wchar_t(test
);
84 format_tests
<wchar_t, execution_modus::partial
>(test
, test_exception
);