Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / format / format.functions / vformat_to.pass.cpp
blobc247a9bc8cef26eec527cf66ad723d4e1fc6c328
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 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11 // XFAIL: availability-fp_to_chars-missing
13 // <format>
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);
20 #include <format>
21 #include <algorithm>
22 #include <cassert>
23 #include <list>
24 #include <vector>
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"
32 auto test = []<class CharT, class... Args>(
33 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr {
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);
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()));
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()));
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);
60 auto 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'));
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 }());
79 int main(int, char**) {
80 format_tests<char, execution_modus::partial>(test, test_exception);
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
83 format_tests_char_to_wchar_t(test);
84 format_tests<wchar_t, execution_modus::partial>(test, test_exception);
85 #endif
87 return 0;