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 // string vformat(string_view fmt, format_args args);
18 // wstring vformat(wstring_view fmt, wformat_args args);
19
20 #include <format>
21 #include <cassert>
22 #include <vector>
23
24 #include "test_macros.h"
25 #include "format_tests.h"
26 #include "string_literal.h"
27
28 auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
29 const Args&... args) constexpr {
30 std::basic_string<CharT> out =
31 std::vformat(fmt.template sv<CharT>(), std::make_format_args<context_t<CharT>>(args...));
32 assert(out == expected);
33 };
34
35 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt,
36 const Args&... args) {
37 #ifndef TEST_HAS_NO_EXCEPTIONS
38 try {
39 TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
40 assert(false);
41 } catch ([[maybe_unused]] const std::format_error& e) {
42 LIBCPP_ASSERT(e.what() == what);
43 return;
44 }
45 assert(false);
46 #endif
47 (void)what;
48 (void)fmt;
49 (void)sizeof...(args);
50 };
51
main(int,char **)52 int main(int, char**) {
53 format_tests<char>(test, test_exception);
54
55 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
56 format_tests_char_to_wchar_t(test);
57 format_tests<wchar_t>(test, test_exception);
58 #endif
59
60 return 0;
61 }
62