[llvm][llvm-readobj] Add NT_ARM_GCS Linux core note type (#117545)
[llvm-project.git] / libcxx / test / std / utilities / format / format.functions / vformat_to.locale.pass.cpp
blob34d7c84f087be95b75022d9595475eedfbe714fb
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
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
15 // <format>
17 // template<class Out>
18 // Out vformat_to(Out out, const locale& loc, string_view fmt,
19 // format_args args);
20 // template<class Out>
21 // Out vformat_to(Out out, const locale& loc, wstring_view fmt,
22 // wformat_args args);
24 #include <format>
25 #include <algorithm>
26 #include <cassert>
27 #include <list>
28 #include <vector>
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);
44 std::list<CharT> out;
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.");
55 CharT out[4096];
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(
67 std::format_error,
68 [&]([[maybe_unused]] const std::format_error& e) {
69 TEST_LIBCPP_REQUIRE(
70 e.what() == what,
71 TEST_WRITE_CONCATENATED(
72 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
74 [&] {
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...));
77 }());
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);
86 #endif
88 return 0;