Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / input.output / iostream.format / print.fun / println.file.pass.cpp
blobebdddd074faf51c3d5fa1078466f913da3ea799a
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 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
9 // UNSUPPORTED: no-filesystem
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 // XFAIL: availability-fp_to_chars-missing
14 // The error exception has no system error string.
15 // XFAIL: LIBCXX-ANDROID-FIXME
17 // <print>
19 // template<class... Args>
20 // void println(FILE* stream, format_string<Args...> fmt, Args&&... args);
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
28 #include <algorithm>
29 #include <array>
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdio>
33 #include <fstream>
34 #include <fstream>
35 #include <iterator>
36 #include <print>
37 #include <string_view>
39 #include "assert_macros.h"
40 #include "concat_macros.h"
41 #include "filesystem_test_helper.h"
42 #include "print_tests.h"
43 #include "test_format_string.h"
44 #include "test_macros.h"
46 scoped_test_env env;
47 std::string filename = env.create_file("output.txt");
49 auto test_file = []<class... Args>(std::string_view e, test_format_string<char, Args...> fmt, Args&&... args) {
50 std::string expected = std::string{e} + '\n';
52 FILE* file = fopen(filename.c_str(), "wb");
53 assert(file);
55 std::println(file, fmt, std::forward<Args>(args)...);
56 std::fclose(file);
58 std::ifstream stream{filename.c_str(), std::ios_base::in | std::ios_base::binary};
59 std::string out(std::istreambuf_iterator<char>{stream}, {});
60 TEST_REQUIRE(out == expected,
61 TEST_WRITE_CONCATENATED(
62 "\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n'));
65 auto test_exception = []<class... Args>(std::string_view, std::string_view, Args&&...) {
66 // After P2216 most exceptions thrown by std::format become ill-formed.
67 // Therefore this tests does nothing.
68 // A basic ill-formed test is done in format.verify.cpp
69 // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument.
72 // Glibc fails writing to a wide stream.
73 #if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
74 static void test_wide_stream() {
75 FILE* file = fopen(filename.c_str(), "wb");
76 assert(file);
78 int mode = std::fwide(file, 1);
79 assert(mode > 0);
81 TEST_VALIDATE_EXCEPTION(
82 std::system_error,
83 [&]([[maybe_unused]] const std::system_error& e) {
84 [[maybe_unused]] std::string_view what{"failed to write formatted output"};
85 TEST_LIBCPP_REQUIRE(
86 e.what() == what,
87 TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
89 std::println(file, "hello"));
91 #endif // defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
93 static void test_read_only() {
94 FILE* file = fopen(filename.c_str(), "r");
95 assert(file);
97 TEST_VALIDATE_EXCEPTION(
98 std::system_error,
99 [&]([[maybe_unused]] const std::system_error& e) {
100 #ifdef _AIX
101 [[maybe_unused]] std::string_view what{"failed to write formatted output: Broken pipe"};
102 #else
103 [[maybe_unused]] std::string_view what{"failed to write formatted output: Operation not permitted"};
104 #endif
105 TEST_LIBCPP_REQUIRE(
106 e.what() == what,
107 TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
109 std::println(file, "hello"));
112 static void test_new_line() {
113 // Text does newline translation.
115 FILE* file = fopen(filename.c_str(), "w");
116 assert(file);
118 std::println(file, "");
119 #ifndef _WIN32
120 assert(std::ftell(file) == 1);
121 #else
122 assert(std::ftell(file) == 2);
123 #endif
125 // Binary no newline translation.
127 FILE* file = fopen(filename.c_str(), "wb");
128 assert(file);
130 std::println(file, "");
131 assert(std::ftell(file) == 1);
135 int main(int, char**) {
136 print_tests(test_file, test_exception);
138 #if defined(TEST_HAS_GLIBC) && !defined(TEST_HAS_NO_WIDE_CHARACTERS)
139 test_wide_stream();
140 #endif
141 test_read_only();
142 test_new_line();
144 return 0;