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

source code of libcxx/test/std/utilities/format/format.functions/vformat_to.pass.cpp