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: libcpp-has-no-incomplete-format
10 // TODO FMT Evaluate gcc-12 status
11 // UNSUPPORTED: gcc-12
12 // TODO FMT Investigate AppleClang ICE
13 // UNSUPPORTED: apple-clang-13
14 
15 // <format>
16 
17 // template<class Out>
18 //   Out vformat_to(Out out, string_view fmt, format_args args);
19 // template<class Out>
20 //    Out vformat_to(Out out, wstring_view fmt, wformat_args_t args);
21 
22 #include <format>
23 #include <algorithm>
24 #include <cassert>
25 #include <list>
26 #include <vector>
27 
28 #include "test_macros.h"
29 #include "format_tests.h"
30 #include "string_literal.h"
31 
32 auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
33                                                                const Args&... args) constexpr {
34   {
35     std::basic_string<CharT> out(expected.size(), CharT(' '));
36     auto it = std::vformat_to(out.begin(), fmt.template sv<CharT>(), 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.template sv<CharT>(),
43                     std::make_format_args<context_t<CharT>>(args...));
44     assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
45   }
46   {
47     std::vector<CharT> out;
48     std::vformat_to(std::back_inserter(out), fmt.template sv<CharT>(),
49                     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, fmt.template sv<CharT>(), 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 
62 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt,
63                                                      const Args&... args) {
64 #ifndef TEST_HAS_NO_EXCEPTIONS
65   try {
66     std::basic_string<CharT> out;
67     std::vformat_to(std::back_inserter(out), fmt, std::make_format_args<context_t<CharT>>(args...));
68     assert(false);
69   } catch ([[maybe_unused]] const std::format_error& e) {
70     LIBCPP_ASSERT(e.what() == what);
71     return;
72   }
73   assert(false);
74 #endif
75   (void)what;
76   (void)fmt;
77   (void)sizeof...(args);
78 };
79 
main(int,char **)80 int main(int, char**) {
81   format_tests<char>(test, test_exception);
82 
83 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
84   format_tests_char_to_wchar_t(test);
85   format_tests<wchar_t>(test, test_exception);
86 #endif
87 
88   return 0;
89 }
90