Debian Codegraph @

libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.functions.vformat.pass.cpp

raw
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, c++20
9
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12 // XFAIL: availability-fp_to_chars-missing
13
14 // [container.adaptors.format]
15 // For each of queue, priority_queue, and stack, the library provides the
16 // following formatter specialization where adaptor-type is the name of the
17 // template:
18 //
19 // template<class charT, class T, formattable<charT> Container, class... U>
20 // struct formatter<adaptor-type<T, Container, U...>, charT>
21
22 // string vformat(string_view fmt, format_args args);
23 // wstring vformat(wstring_view fmt, wformat_args args);
24
25 #include <format>
26 #include <cassert>
27
28 #include "format.functions.tests.h"
29 #include "test_macros.h"
30 #include "assert_macros.h"
31 #include "concat_macros.h"
32
33 auto test = []<class CharT, class... Args>(
34 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) {
35 std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
36 TEST_REQUIRE(out == expected,
37 TEST_WRITE_CONCATENATED(
38 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
39 };
40
41 auto test_exception =
42 []<class CharT, class... Args>(
43 [[maybe_unused]] std::string_view what,
44 [[maybe_unused]] std::basic_string_view<CharT> fmt,
45 [[maybe_unused]] Args&&... args) {
46 TEST_VALIDATE_EXCEPTION(
47 std::format_error,
48 [&]([[maybe_unused]] const std::format_error& e) {
49 TEST_LIBCPP_REQUIRE(
50 e.what() == what,
51 TEST_WRITE_CONCATENATED(
52 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
53 },
54 TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...)));
55 };
56
57 int main(int, char**) {
58 format_tests<char>(test, test_exception);
59
60 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
61 format_tests<wchar_t>(test, test_exception);
62 #endif
63
64 return 0;
65 }