| 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 |
// UNSUPPORTED: no-filesystem |
| 10 |
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 11 |
|
| 12 |
// XFAIL: availability-fp_to_chars-missing |
| 13 |
|
| 14 |
// The error exception has no system error string. |
| 15 |
// XFAIL: LIBCXX-ANDROID-FIXME |
| 16 |
|
| 17 |
// <print> |
| 18 |
|
| 19 |
// template<class... Args> |
| 20 |
// void println(FILE* stream, format_string<Args...> fmt, Args&&... args); |
| 21 |
|
| 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 |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <array> |
| 30 |
#include <cassert> |
| 31 |
#include <cstddef> |
| 32 |
#include <cstdio> |
| 33 |
#include <fstream> |
| 34 |
#include <fstream> |
| 35 |
#include <iterator> |
| 36 |
#include <print> |
| 37 |
#include <string_view> |
| 38 |
|
| 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" |
| 45 |
|
| 46 |
scoped_test_env env; |
| 47 |
std::string filename = env.create_file("output.txt"); |
| 48 |
|
| 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'; |
| 51 |
|
| 52 |
FILE* file = fopen(filename.c_str(), "wb"); |
| 53 |
assert(file); |
| 54 |
|
| 55 |
std::println(file, fmt, std::forward<Args>(args)...); |
| 56 |
std::fclose(file); |
| 57 |
|
| 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')); |
| 63 |
}; |
| 64 |
|
| 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. |
| 70 |
}; |
| 71 |
|
| 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"); |
| 76 |
assert(file); |
| 77 |
|
| 78 |
int mode = std::fwide(file, 1); |
| 79 |
assert(mode > 0); |
| 80 |
|
| 81 |
TEST_VALIDATE_EXCEPTION( |
| 82 |
std::system_error, |
| 83 |
[&]([[maybe_unused]] const std::system_error& e) { |
| 84 |
[[maybe_unused]] std::string_view what{"failed to write formatted output"}; |
| 85 |
TEST_LIBCPP_REQUIRE( |
| 86 |
e.what() == what, |
| 87 |
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); |
| 88 |
}, |
| 89 |
std::println(file, "hello")); |
| 90 |
} |
| 91 |
#endif // defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS) |
| 92 |
|
| 93 |
static void test_read_only() { |
| 94 |
FILE* file = fopen(filename.c_str(), "r"); |
| 95 |
assert(file); |
| 96 |
|
| 97 |
TEST_VALIDATE_EXCEPTION( |
| 98 |
std::system_error, |
| 99 |
[&]([[maybe_unused]] const std::system_error& e) { |
| 100 |
[[maybe_unused]] std::string_view what{ |
| 101 |
"failed to write formatted output: " TEST_IF_AIX("Broken pipe", "Operation not permitted")}; |
| 102 |
TEST_LIBCPP_REQUIRE( |
| 103 |
e.what() == what, |
| 104 |
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); |
| 105 |
}, |
| 106 |
std::println(file, "hello")); |
| 107 |
} |
| 108 |
|
| 109 |
static void test_new_line() { |
| 110 |
// Text does newline translation. |
| 111 |
{ |
| 112 |
FILE* file = fopen(filename.c_str(), "w"); |
| 113 |
assert(file); |
| 114 |
|
| 115 |
std::println(file, ""); |
| 116 |
#ifndef _WIN32 |
| 117 |
assert(std::ftell(file) == 1); |
| 118 |
#else |
| 119 |
assert(std::ftell(file) == 2); |
| 120 |
#endif |
| 121 |
} |
| 122 |
// Binary no newline translation. |
| 123 |
{ |
| 124 |
FILE* file = fopen(filename.c_str(), "wb"); |
| 125 |
assert(file); |
| 126 |
|
| 127 |
std::println(file, ""); |
| 128 |
assert(std::ftell(file) == 1); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static void test_println_blank_line() { |
| 133 |
// Text does newline translation. |
| 134 |
{ |
| 135 |
FILE* file = fopen(filename.c_str(), "w"); |
| 136 |
assert(file); |
| 137 |
|
| 138 |
std::println(file); |
| 139 |
#ifndef _WIN32 |
| 140 |
assert(std::ftell(file) == 1); |
| 141 |
#else |
| 142 |
assert(std::ftell(file) == 2); |
| 143 |
#endif |
| 144 |
} |
| 145 |
// Binary no newline translation. |
| 146 |
{ |
| 147 |
FILE* file = fopen(filename.c_str(), "wb"); |
| 148 |
assert(file); |
| 149 |
|
| 150 |
std::println(file); |
| 151 |
assert(std::ftell(file) == 1); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
int main(int, char**) { |
| 156 |
print_tests(test_file, test_exception); |
| 157 |
|
| 158 |
#if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS) |
| 159 |
test_wide_stream(); |
| 160 |
#endif |
| 161 |
test_read_only(); |
| 162 |
test_new_line(); |
| 163 |
test_println_blank_line(); |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |