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
|
//===----------------------------------------------------------------------===//
// 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: libcpp-has-no-unicode
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
// TODO PRINT Enable again
// https://reviews.llvm.org/D150044
// https://lab.llvm.org/buildbot/#/builders/237/builds/3578
// UNSUPPORTED: asan, hwasan, msan
// XFAIL: availability-fp_to_chars-missing
// The error exception has no system error string.
// XFAIL: LIBCXX-ANDROID-FIXME
// <print>
// void vprint_unicode(FILE* stream, string_view fmt, format_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 <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_macros.h"
scoped_test_env env;
std::string filename = env.create_file("output.txt");
auto test_file = []<class... Args>(std::string_view expected, std::string_view fmt, Args&&... args) {
FILE* file = fopen(filename.c_str(), "wb");
assert(file);
std::vprint_unicode(file, fmt, std::make_format_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, "\nExpected output ", expected, "\nActual output ", out, '\n'));
};
auto test_exception = []<class... Args>([[maybe_unused]] std::string_view what,
[[maybe_unused]] std::string_view fmt,
[[maybe_unused]] Args&&... args) {
FILE* file = fopen(filename.c_str(), "wb");
assert(file);
TEST_VALIDATE_EXCEPTION(
std::format_error,
[&]([[maybe_unused]] const std::format_error& e) {
TEST_LIBCPP_REQUIRE(
e.what() == what,
TEST_WRITE_CONCATENATED(
"\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
},
std::vprint_unicode(file, fmt, std::make_format_args(args...)));
fclose(file);
};
// 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::vprint_unicode(file, "hello", std::make_format_args()));
}
#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::vprint_unicode(file, "hello", std::make_format_args()));
}
static void test_new_line() {
// Text does newline translation.
{
FILE* file = fopen(filename.c_str(), "w");
assert(file);
std::vprint_unicode(file, "\n", std::make_format_args());
#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::vprint_unicode(file, "\n", std::make_format_args());
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();
return 0;
}
|