Debian Codegraph @

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