1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
10// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
11
12// XFAIL: availability-fp_to_chars-missing
13
14// <format>
15
16// Tests the behavior of
17//
18// runtime-format-string<char> runtime_format(string_view fmt) noexcept;
19// runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept;
20//
21// and
22//
23// template<class charT, class... Args>
24// struct basic_format_string {
25// ...
26// basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {}
27// ...
28// }
29//
30// This is done by testing it in the top-level functions:
31//
32// template<class... Args>
33// string format(format_string<Args...> fmt, Args&&... args);
34// template<class... Args>
35// wstring format(wformat_string<Args...> fmt, Args&&... args);
36//
37// The basics of runtime_format and basic_format_string's constructor are tested in
38// - libcxx/test/std/utilities/format/format.syn/runtime_format_string.pass.cpp
39// - libcxx/test/std/utilities/format/format.fmt.string/ctor.runtime-format-string.pass.cpp
40
41#include <format>
42#include <cassert>
43#include <vector>
44
45#include "test_macros.h"
46#include "format_tests.h"
47#include "string_literal.h"
48#include "assert_macros.h"
49#include "concat_macros.h"
50
51auto test = []<class CharT, class... Args>(
52 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr {
53 std::basic_string<CharT> out = std::format(std::runtime_format(fmt), std::forward<Args>(args)...);
54 TEST_REQUIRE(out == expected,
55 TEST_WRITE_CONCATENATED(
56 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
57};
58
59auto test_exception =
60 []<class CharT, class... Args>(
61 [[maybe_unused]] std::string_view what,
62 [[maybe_unused]] std::basic_string_view<CharT> fmt,
63 [[maybe_unused]] Args&&... args) {
64 TEST_VALIDATE_EXCEPTION(
65 std::format_error,
66 [&]([[maybe_unused]] const std::format_error& e) {
67 TEST_LIBCPP_REQUIRE(
68 e.what() == what,
69 TEST_WRITE_CONCATENATED(
70 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
71 },
72 TEST_IGNORE_NODISCARD std::format(std::runtime_format(fmt), std::forward<Args>(args)...));
73 };
74
75int main(int, char**) {
76 format_tests<char, execution_modus::partial>(check: test, check_exception: test_exception);
77
78#ifndef TEST_HAS_NO_WIDE_CHARACTERS
79 format_tests_char_to_wchar_t(check: test);
80 format_tests<wchar_t, execution_modus::partial>(check: test, check_exception: test_exception);
81#endif
82
83 return 0;
84}
85

source code of libcxx/test/std/utilities/format/format.functions/format.runtime_format.pass.cpp