1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//===----------------------------------------------------------------------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// UNSUPPORTED: no-filesystem
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
// XFAIL: availability-fp_to_chars-missing
// The error exception has no system error string.
// XFAIL: LIBCXX-ANDROID-FIXME
// <print>
// template<class... Args>
// void println(FILE* stream, format_string<Args...> fmt, Args&&... args);
// In the library when the stdout is redirected to a file it is no
// longer considered a terminal and the special terminal handling is no
// longer executed. There are tests in
// libcxx/test/libcxx/input.output/iostream.format/print.fun/
// to validate that behaviour
#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <fstream>
#include <fstream>
#include <iterator>
#include <print>
#include <string_view>
#include "assert_macros.h"
#include "concat_macros.h"
#include "filesystem_test_helper.h"
#include "print_tests.h"
#include "test_format_string.h"
#include "test_macros.h"
scoped_test_env env;
std::string filename = env.create_file("output.txt");
auto test_file = []<class... Args>(std::string_view e, test_format_string<char, Args...> fmt, Args&&... args) {
std::string expected = std::string{e} + '\n';
FILE* file = fopen(filename.c_str(), "wb");
assert(file);
std::println(file, fmt, std::forward<Args>(args)...);
std::fclose(file);
std::ifstream stream{filename.c_str(), std::ios_base::in | std::ios_base::binary};
std::string out(std::istreambuf_iterator<char>{stream}, {});
TEST_REQUIRE(out == expected,
TEST_WRITE_CONCATENATED(
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n'));
};
auto test_exception = []<class... Args>(std::string_view, std::string_view, Args&&...) {
// After P2216 most exceptions thrown by std::format become ill-formed.
// Therefore this tests does nothing.
// A basic ill-formed test is done in format.verify.cpp
// The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
};
// Glibc fails writing to a wide stream.
#if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
static void test_wide_stream() {
FILE* file = fopen(filename.c_str(), "wb");
assert(file);
int mode = std::fwide(file, 1);
assert(mode > 0);
TEST_VALIDATE_EXCEPTION(
std::system_error,
[&]([[maybe_unused]] const std::system_error& e) {
[[maybe_unused]] std::string_view what{"failed to write formatted output"};
TEST_LIBCPP_REQUIRE(
e.what() == what,
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
},
std::println(file, "hello"));
}
#endif // defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
static void test_read_only() {
FILE* file = fopen(filename.c_str(), "r");
assert(file);
TEST_VALIDATE_EXCEPTION(
std::system_error,
[&]([[maybe_unused]] const std::system_error& e) {
[[maybe_unused]] std::string_view what{
"failed to write formatted output: " TEST_IF_AIX("Broken pipe", "Operation not permitted")};
TEST_LIBCPP_REQUIRE(
e.what() == what,
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
},
std::println(file, "hello"));
}
static void test_new_line() {
// Text does newline translation.
{
FILE* file = fopen(filename.c_str(), "w");
assert(file);
std::println(file, "");
#ifndef _WIN32
assert(std::ftell(file) == 1);
#else
assert(std::ftell(file) == 2);
#endif
}
// Binary no newline translation.
{
FILE* file = fopen(filename.c_str(), "wb");
assert(file);
std::println(file, "");
assert(std::ftell(file) == 1);
}
}
static void test_println_blank_line() {
// Text does newline translation.
{
FILE* file = fopen(filename.c_str(), "w");
assert(file);
std::println(file);
#ifndef _WIN32
assert(std::ftell(file) == 1);
#else
assert(std::ftell(file) == 2);
#endif
}
// Binary no newline translation.
{
FILE* file = fopen(filename.c_str(), "wb");
assert(file);
std::println(file);
assert(std::ftell(file) == 1);
}
}
int main(int, char**) {
print_tests(test_file, test_exception);
#if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
test_wide_stream();
#endif
test_read_only();
test_new_line();
test_println_blank_line();
return 0;
}
|