| 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 print(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 <iterator> |
| 35 |
#include <print> |
| 36 |
#include <string_view> |
| 37 |
|
| 38 |
#include "assert_macros.h" |
| 39 |
#include "concat_macros.h" |
| 40 |
#include "filesystem_test_helper.h" |
| 41 |
#include "print_tests.h" |
| 42 |
#include "test_format_string.h" |
| 43 |
#include "test_macros.h" |
| 44 |
|
| 45 |
scoped_test_env env; |
| 46 |
std::string filename = env.create_file("output.txt"); |
| 47 |
|
| 48 |
auto test_file = []<class... Args>(std::string_view expected, test_format_string<char, Args...> fmt, Args&&... args) { |
| 49 |
FILE* file = fopen(filename.c_str(), "wb"); |
| 50 |
assert(file); |
| 51 |
|
| 52 |
std::print(file, fmt, std::forward<Args>(args)...); |
| 53 |
std::fclose(file); |
| 54 |
|
| 55 |
std::ifstream stream{filename.c_str(), std::ios_base::in | std::ios_base::binary}; |
| 56 |
std::string out(std::istreambuf_iterator<char>{stream}, {}); |
| 57 |
TEST_REQUIRE(out == expected, |
| 58 |
TEST_WRITE_CONCATENATED( |
| 59 |
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); |
| 60 |
}; |
| 61 |
|
| 62 |
auto test_exception = []<class... Args>(std::string_view, std::string_view, Args&&...) { |
| 63 |
// After P2216 most exceptions thrown by std::format become ill-formed. |
| 64 |
// Therefore this tests does nothing. |
| 65 |
// A basic ill-formed test is done in format.verify.cpp |
| 66 |
// The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. |
| 67 |
}; |
| 68 |
|
| 69 |
// Glibc fails writing to a wide stream. |
| 70 |
#if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS) |
| 71 |
static void test_wide_stream() { |
| 72 |
FILE* file = fopen(filename.c_str(), "wb"); |
| 73 |
assert(file); |
| 74 |
|
| 75 |
int mode = std::fwide(file, 1); |
| 76 |
assert(mode > 0); |
| 77 |
|
| 78 |
TEST_VALIDATE_EXCEPTION( |
| 79 |
std::system_error, |
| 80 |
[&]([[maybe_unused]] const std::system_error& e) { |
| 81 |
[[maybe_unused]] std::string_view what{"failed to write formatted output"}; |
| 82 |
TEST_LIBCPP_REQUIRE( |
| 83 |
e.what() == what, |
| 84 |
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); |
| 85 |
}, |
| 86 |
std::print(file, "hello")); |
| 87 |
} |
| 88 |
#endif // defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS) |
| 89 |
|
| 90 |
static void test_read_only() { |
| 91 |
FILE* file = fopen(filename.c_str(), "r"); |
| 92 |
assert(file); |
| 93 |
|
| 94 |
TEST_VALIDATE_EXCEPTION( |
| 95 |
std::system_error, |
| 96 |
[&]([[maybe_unused]] const std::system_error& e) { |
| 97 |
[[maybe_unused]] std::string_view what{ |
| 98 |
"failed to write formatted output: " TEST_IF_AIX("Broken pipe", "Operation not permitted")}; |
| 99 |
TEST_LIBCPP_REQUIRE( |
| 100 |
e.what() == what, |
| 101 |
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); |
| 102 |
}, |
| 103 |
std::print(file, "hello")); |
| 104 |
} |
| 105 |
|
| 106 |
static void test_new_line() { |
| 107 |
// Text does newline translation. |
| 108 |
{ |
| 109 |
FILE* file = fopen(filename.c_str(), "w"); |
| 110 |
assert(file); |
| 111 |
|
| 112 |
std::print(file, "\n"); |
| 113 |
#ifndef _WIN32 |
| 114 |
assert(std::ftell(file) == 1); |
| 115 |
#else |
| 116 |
assert(std::ftell(file) == 2); |
| 117 |
#endif |
| 118 |
} |
| 119 |
// Binary no newline translation. |
| 120 |
{ |
| 121 |
FILE* file = fopen(filename.c_str(), "wb"); |
| 122 |
assert(file); |
| 123 |
|
| 124 |
std::print(file, "\n"); |
| 125 |
assert(std::ftell(file) == 1); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
int main(int, char**) { |
| 130 |
print_tests(test_file, test_exception); |
| 131 |
|
| 132 |
#if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS) |
| 133 |
test_wide_stream(); |
| 134 |
#endif |
| 135 |
test_read_only(); |
| 136 |
test_new_line(); |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |