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, class... Args>
18 //   Out format_to(Out out, format-string<Args...> fmt, const Args&... args);
19 // template<class Out, class... Args>
20 //   Out format_to(Out out, wformat-string<Args...> fmt, const Args&... 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::format_to(out.begin(), fmt.template sv<CharT>(), args...);
37     assert(it == out.end());
38     assert(out == expected);
39   }
40   {
41     std::list<CharT> out;
42     std::format_to(std::back_inserter(out), fmt.template sv<CharT>(), args...);
43     assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
44   }
45   {
46     std::vector<CharT> out;
47     std::format_to(std::back_inserter(out), fmt.template sv<CharT>(), args...);
48     assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
49   }
50   {
51     assert(expected.size() < 4096 && "Update the size of the buffer.");
52     CharT out[4096];
53     CharT* it = std::format_to(out, fmt.template sv<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);
57   }
58 };
59 
60 auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
61   // After P2216 most exceptions thrown by std::format become ill-formed.
62   // Therefore this tests does nothing.
63   // A basic ill-formed test is done in format.verify.cpp
64   // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
65 };
66 
main(int,char **)67 int main(int, char**) {
68   format_tests<char>(test, test_exception);
69 
70 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
71   format_tests_char_to_wchar_t(test);
72   format_tests<wchar_t>(test, test_exception);
73 #endif
74 
75   return 0;
76 }
77