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: no-localization
10// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12// XFAIL: availability-fp_to_chars-missing
13
14// <format>
15
16// template<class Out>
17// Out vformat_to(Out out, const locale& loc, string_view fmt,
18// format_args args);
19// template<class Out>
20// Out vformat_to(Out out, const locale& loc, wstring_view fmt,
21// wformat_args args);
22
23#include <format>
24#include <algorithm>
25#include <cassert>
26#include <list>
27#include <vector>
28
29#include "assert_macros.h"
30#include "concat_macros.h"
31#include "format_tests.h"
32#include "string_literal.h"
33
34auto test = []<class CharT, class... Args>(
35 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr {
36 {
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);
41 }
42 {
43 std::list<CharT> out;
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()));
46 }
47 {
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()));
51 }
52 {
53 assert(expected.size() < 4096 && "Update the size of the buffer.");
54 CharT out[4096];
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);
59 }
60};
61
62auto 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(
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), std::locale(), 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.locale.pass.cpp