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: libcpp-has-no-incomplete-format
11 // TODO FMT Evaluate gcc-12 status
12 // UNSUPPORTED: gcc-12
13 // TODO FMT Investigate AppleClang ICE
14 // UNSUPPORTED: apple-clang-13
15 
16 // <format>
17 
18 // template<class Out, class... Args>
19 //   Out format_to(Out out, const locale& loc,
20 //                 format-string<Args...> fmt, const Args&... args);
21 // template<class Out, class... Args>
22 //   Out format_to(Out out, const locale& loc,
23 //                 wformat-string<Args...> fmt, const Args&... args);
24 
25 #include <format>
26 #include <algorithm>
27 #include <cassert>
28 #include <list>
29 #include <vector>
30 
31 #include "test_macros.h"
32 #include "format_tests.h"
33 #include "string_literal.h"
34 
35 auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
36                                                                const Args&... args) constexpr {
37   {
38     std::basic_string<CharT> out(expected.size(), CharT(' '));
39     auto it = std::format_to(out.begin(), std::locale(), fmt.template sv<CharT>(), args...);
40     assert(it == out.end());
41     assert(out == expected);
42   }
43   {
44     std::list<CharT> out;
45     std::format_to(std::back_inserter(out), std::locale(), fmt.template sv<CharT>(), args...);
46     assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
47   }
48   {
49     std::vector<CharT> out;
50     std::format_to(std::back_inserter(out), std::locale(), fmt.template sv<CharT>(), args...);
51     assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
52   }
53   {
54     assert(expected.size() < 4096 && "Update the size of the buffer.");
55     CharT out[4096];
56     CharT* it = std::format_to(out, std::locale(), fmt.template sv<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);
60   }
61 };
62 
63 auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
64   // After P2216 most exceptions thrown by std::format_to become ill-formed.
65   // Therefore this tests does nothing.
66   // A basic ill-formed test is done in format_to.locale.verify.cpp
67   // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
68 };
69 
main(int,char **)70 int main(int, char**) {
71   format_tests<char>(test, test_exception);
72 
73 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
74   format_tests_char_to_wchar_t(test);
75   format_tests<wchar_t>(test, test_exception);
76 #endif
77 
78   return 0;
79 }
80