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: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
13 // XFAIL: availability-fp_to_chars-missing
17 // template<class Out>
18 // Out vformat_to(Out out, const locale& loc, string_view fmt,
20 // template<class Out>
21 // Out vformat_to(Out out, const locale& loc, wstring_view fmt,
22 // wformat_args args);
30 #include "assert_macros.h"
31 #include "concat_macros.h"
32 #include "format_tests.h"
33 #include "string_literal.h"
35 auto test
= []<class CharT
, class... Args
>(
36 std::basic_string_view
<CharT
> expected
, std::basic_string_view
<CharT
> fmt
, Args
&&... args
) constexpr {
38 std::basic_string
<CharT
> out(expected
.size(), CharT(' '));
39 auto it
= std::vformat_to(out
.begin(), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
40 assert(it
== out
.end());
41 assert(out
== expected
);
45 std::vformat_to(std::back_inserter(out
), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
46 assert(std::equal(out
.begin(), out
.end(), expected
.begin(), expected
.end()));
49 std::vector
<CharT
> out
;
50 std::vformat_to(std::back_inserter(out
), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(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 CharT
* it
= std::vformat_to(out
, std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
57 assert(std::distance(out
, it
) == int(expected
.size()));
58 // Convert to std::string since output contains '\0' for boolean tests.
59 assert(std::basic_string
<CharT
>(out
, it
) == expected
);
63 auto test_exception
= []<class CharT
, class... Args
>([[maybe_unused
]] std::string_view what
,
64 [[maybe_unused
]] std::basic_string_view
<CharT
> fmt
,
65 [[maybe_unused
]] Args
&&... args
) {
66 TEST_VALIDATE_EXCEPTION(
68 [&]([[maybe_unused
]] const std::format_error
& e
) {
71 TEST_WRITE_CONCATENATED(
72 "\nFormat string ", fmt
, "\nExpected exception ", what
, "\nActual exception ", e
.what(), '\n'));
75 std::basic_string
<CharT
> out
;
76 std::vformat_to(std::back_inserter(out
), std::locale(), fmt
, std::make_format_args
<context_t
<CharT
>>(args
...));
80 int main(int, char**) {
81 format_tests
<char, execution_modus::partial
>(test
, test_exception
);
83 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
84 format_tests_char_to_wchar_t(test
);
85 format_tests
<wchar_t, execution_modus::partial
>(test
, test_exception
);