Debian Codegraph @

libcxx/test/std/input.output/iostream.format/print.fun/vprint_unicode.file.pass.cpp

raw
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: libcpp-has-no-unicode
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12
13 // TODO PRINT Enable again
14 // https://reviews.llvm.org/D150044
15 // https://lab.llvm.org/buildbot/#/builders/237/builds/3578
16 // UNSUPPORTED: asan, hwasan, msan
17
18 // XFAIL: availability-fp_to_chars-missing
19
20 // The error exception has no system error string.
21 // XFAIL: LIBCXX-ANDROID-FIXME
22
23 // <print>
24
25 // void vprint_unicode(FILE* stream, string_view fmt, format_args args);
26
27 // In the library when the stdout is redirected to a file it is no
28 // longer considered a terminal and the special terminal handling is no
29 // longer executed. There are tests in
30 // libcxx/test/libcxx/input.output/iostream.format/print.fun/
31 // to validate that behaviour
32
33 #include <algorithm>
34 #include <array>
35 #include <cassert>
36 #include <cstddef>
37 #include <cstdio>
38 #include <fstream>
39 #include <iterator>
40 #include <print>
41 #include <string_view>
42
43 #include "assert_macros.h"
44 #include "concat_macros.h"
45 #include "filesystem_test_helper.h"
46 #include "print_tests.h"
47 #include "test_macros.h"
48
49 scoped_test_env env;
50 std::string filename = env.create_file("output.txt");
51
52 auto test_file = []<class... Args>(std::string_view expected, std::string_view fmt, Args&&... args) {
53 FILE* file = fopen(filename.c_str(), "wb");
54 assert(file);
55
56 std::vprint_unicode(file, fmt, std::make_format_args(args...));
57 std::fclose(file);
58
59 std::ifstream stream{filename.c_str(), std::ios_base::in | std::ios_base::binary};
60 std::string out(std::istreambuf_iterator<char>{stream}, {});
61 TEST_REQUIRE(out == expected,
62 TEST_WRITE_CONCATENATED(
63 "\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
64 };
65
66 auto test_exception = []<class... Args>([[maybe_unused]] std::string_view what,
67 [[maybe_unused]] std::string_view fmt,
68 [[maybe_unused]] Args&&... args) {
69 FILE* file = fopen(filename.c_str(), "wb");
70 assert(file);
71
72 TEST_VALIDATE_EXCEPTION(
73 std::format_error,
74 [&]([[maybe_unused]] const std::format_error& e) {
75 TEST_LIBCPP_REQUIRE(
76 e.what() == what,
77 TEST_WRITE_CONCATENATED(
78 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
79 },
80 std::vprint_unicode(file, fmt, std::make_format_args(args...)));
81
82 fclose(file);
83 };
84
85 // Glibc fails writing to a wide stream.
86 #if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
87 static void test_wide_stream() {
88 FILE* file = fopen(filename.c_str(), "wb");
89 assert(file);
90
91 int mode = std::fwide(file, 1);
92 assert(mode > 0);
93
94 TEST_VALIDATE_EXCEPTION(
95 std::system_error,
96 [&]([[maybe_unused]] const std::system_error& e) {
97 [[maybe_unused]] std::string_view what{"failed to write formatted output"};
98 TEST_LIBCPP_REQUIRE(
99 e.what() == what,
100 TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
101 },
102 std::vprint_unicode(file, "hello", std::make_format_args()));
103 }
104 #endif // defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
105
106 static void test_read_only() {
107 FILE* file = fopen(filename.c_str(), "r");
108 assert(file);
109
110 TEST_VALIDATE_EXCEPTION(
111 std::system_error,
112 [&]([[maybe_unused]] const std::system_error& e) {
113 [[maybe_unused]] std::string_view what{
114 "failed to write formatted output: " TEST_IF_AIX("Broken pipe", "Operation not permitted")};
115 TEST_LIBCPP_REQUIRE(
116 e.what() == what,
117 TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
118 },
119 std::vprint_unicode(file, "hello", std::make_format_args()));
120 }
121
122 static void test_new_line() {
123 // Text does newline translation.
124 {
125 FILE* file = fopen(filename.c_str(), "w");
126 assert(file);
127
128 std::vprint_unicode(file, "\n", std::make_format_args());
129 #ifndef _WIN32
130 assert(std::ftell(file) == 1);
131 #else
132 assert(std::ftell(file) == 2);
133 #endif
134 }
135 // Binary no newline translation.
136 {
137 FILE* file = fopen(filename.c_str(), "wb");
138 assert(file);
139
140 std::vprint_unicode(file, "\n", std::make_format_args());
141 assert(std::ftell(file) == 1);
142 }
143 }
144
145 int main(int, char**) {
146 print_tests(test_file, test_exception);
147
148 #if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
149 test_wide_stream();
150 #endif
151 test_read_only();
152 test_new_line();
153
154 return 0;
155 }