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// <format>
15
16// template<ranges::input_range R, class charT>
17// struct range-default-formatter<range_format::map, R, charT>
18//
19// string vformat(string_view fmt, format_args args);
20// wstring vformat(wstring_view fmt, wformat_args args);
21
22#include <format>
23#include <cassert>
24
25#include "format.functions.tests.h"
26#include "test_macros.h"
27#include "assert_macros.h"
28#include "concat_macros.h"
29
30auto test = []<class CharT, class... Args>(
31 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) {
32 std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
33 TEST_REQUIRE(out == expected,
34 TEST_WRITE_CONCATENATED(
35 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
36};
37
38auto test_exception =
39 []<class CharT, class... Args>(
40 [[maybe_unused]] std::string_view what,
41 [[maybe_unused]] std::basic_string_view<CharT> fmt,
42 [[maybe_unused]] Args&&... args) {
43 TEST_VALIDATE_EXCEPTION(
44 std::format_error,
45 [&]([[maybe_unused]] const std::format_error& e) {
46 TEST_LIBCPP_REQUIRE(
47 e.what() == what,
48 TEST_WRITE_CONCATENATED(
49 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
50 },
51 TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...)));
52 };
53
54int main(int, char**) {
55 format_tests<char>(check: test, check_exception: test_exception);
56
57#ifndef TEST_HAS_NO_WIDE_CHARACTERS
58 format_tests<wchar_t>(check: test, check_exception: test_exception);
59#endif
60
61 return 0;
62}
63

source code of libcxx/test/std/utilities/format/format.range/format.range.fmtmap/format.functions.vformat.pass.cpp