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