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, 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);
22
23#include <format>
24#include <algorithm>
25#include <cassert>
26#include <iterator>
27#include <list>
28#include <vector>
29
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"
35
36auto test =
37 []<class CharT, class... Args>(
38 std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
39 {
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);
44 }
45 {
46 std::list<CharT> out;
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()));
49 }
50 {
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()));
54 }
55 {
56 assert(expected.size() < 4096 && "Update the size of the buffer.");
57 CharT out[4096];
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);
63 }
64 };
65
66auto 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.
71};
72
73int main(int, char**) {
74 format_tests<char, execution_modus::partial>(check: test, check_exception: test_exception);
75
76#ifndef TEST_HAS_NO_WIDE_CHARACTERS
77 format_tests_char_to_wchar_t(check: test);
78 format_tests<wchar_t, execution_modus::partial>(check: test, check_exception: test_exception);
79#endif
80
81 return 0;
82}
83

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