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
6 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9 // UNSUPPORTED: no-filesystem
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 // XFAIL: availability-fp_to_chars-missing
14 // The error exception has no system error string.
15 // XFAIL: LIBCXX-ANDROID-FIXME
19 // template<class... Args>
20 // void println(FILE* stream, format_string<Args...> fmt, Args&&... args);
22 // In the library when the stdout is redirected to a file it is no
23 // longer considered a terminal and the special terminal handling is no
24 // longer executed. There are tests in
25 // libcxx/test/libcxx/input.output/iostream.format/print.fun/
26 // to validate that behaviour
37 #include <string_view>
39 #include "assert_macros.h"
40 #include "concat_macros.h"
41 #include "filesystem_test_helper.h"
42 #include "print_tests.h"
43 #include "test_format_string.h"
44 #include "test_macros.h"
47 std::string filename
= env
.create_file("output.txt");
49 auto test_file
= []<class... Args
>(std::string_view e
, test_format_string
<char, Args
...> fmt
, Args
&&... args
) {
50 std::string expected
= std::string
{e
} + '\n';
52 FILE* file
= fopen(filename
.c_str(), "wb");
55 std::println(file
, fmt
, std::forward
<Args
>(args
)...);
58 std::ifstream stream
{filename
.c_str(), std::ios_base::in
| std::ios_base::binary
};
59 std::string
out(std::istreambuf_iterator
<char>{stream
}, {});
60 TEST_REQUIRE(out
== expected
,
61 TEST_WRITE_CONCATENATED(
62 "\nFormat string ", fmt
.get(), "\nExpected output ", expected
, "\nActual output ", out
, '\n'));
65 auto test_exception
= []<class... Args
>(std::string_view
, std::string_view
, Args
&&...) {
66 // After P2216 most exceptions thrown by std::format become ill-formed.
67 // Therefore this tests does nothing.
68 // A basic ill-formed test is done in format.verify.cpp
69 // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
72 // Glibc fails writing to a wide stream.
73 #if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
74 static void test_wide_stream() {
75 FILE* file
= fopen(filename
.c_str(), "wb");
78 int mode
= std::fwide(file
, 1);
81 TEST_VALIDATE_EXCEPTION(
83 [&]([[maybe_unused
]] const std::system_error
& e
) {
84 [[maybe_unused
]] std::string_view what
{"failed to write formatted output"};
87 TEST_WRITE_CONCATENATED("\nExpected exception ", what
, "\nActual exception ", e
.what(), '\n'));
89 std::println(file
, "hello"));
91 #endif // defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
93 static void test_read_only() {
94 FILE* file
= fopen(filename
.c_str(), "r");
97 TEST_VALIDATE_EXCEPTION(
99 [&]([[maybe_unused
]] const std::system_error
& e
) {
101 [[maybe_unused
]] std::string_view what
{"failed to write formatted output: Broken pipe"};
103 [[maybe_unused
]] std::string_view what
{"failed to write formatted output: Operation not permitted"};
107 TEST_WRITE_CONCATENATED("\nExpected exception ", what
, "\nActual exception ", e
.what(), '\n'));
109 std::println(file
, "hello"));
112 static void test_new_line() {
113 // Text does newline translation.
115 FILE* file
= fopen(filename
.c_str(), "w");
118 std::println(file
, "");
120 assert(std::ftell(file
) == 1);
122 assert(std::ftell(file
) == 2);
125 // Binary no newline translation.
127 FILE* file
= fopen(filename
.c_str(), "wb");
130 std::println(file
, "");
131 assert(std::ftell(file
) == 1);
135 int main(int, char**) {
136 print_tests(test_file
, test_exception
);
138 #if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)