Debian Codegraph @

libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.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 // XFAIL: msvc, target={{.+}}-windows-gnu
13 // XFAIL: availability-fp_to_chars-missing
14
15 // fmemopen is available starting in Android M (API 23)
16 // XFAIL: target={{.+}}-android{{(eabi)?(21|22)}}
17
18 // <print>
19
20 // The FILE returned by fmemopen does not have file descriptor.
21 // This means the test could fail when the implementation uses a
22 // function that requires a file descriptor, for example write.
23 //
24 // This tests all print functions which takes a FILE* as argument.
25
26 // template<class... Args>
27 // void print(FILE* stream, format_string<Args...> fmt, Args&&... args);
28 // void println(); // Since C++26
29 // template<class... Args>
30 // void println(FILE* stream, format_string<Args...> fmt, Args&&... args);
31 // void println(FILE* stream); // Since C++26
32 // void vprint_unicode(FILE* stream, string_view fmt, format_args args);
33 // void vprint_nonunicode(FILE* stream, string_view fmt, format_args args);
34
35 #include <array>
36 #include <cstdio>
37 #include <cassert>
38 #include <print>
39
40 static void test_print() {
41 std::array<char, 100> buffer{0};
42
43 FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
44 assert(file);
45
46 std::print(file, "hello world{}", '!');
47 long pos = std::ftell(file);
48 std::fclose(file);
49
50 assert(pos > 0);
51 assert(std::string_view(buffer.data(), pos) == "hello world!");
52 }
53
54 static void test_println() {
55 std::array<char, 100> buffer{0};
56
57 FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
58 assert(file);
59
60 std::println(file, "hello world{}", '!');
61 long pos = std::ftell(file);
62 std::fclose(file);
63
64 assert(pos > 0);
65 assert(std::string_view(buffer.data(), pos) == "hello world!\n");
66 }
67
68 static void test_println_blank_line() {
69 std::array<char, 100> buffer{0};
70
71 FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
72 assert(file);
73
74 std::println(file);
75 long pos = std::ftell(file);
76 std::fclose(file);
77
78 assert(pos > 0);
79 assert(std::string_view(buffer.data(), pos) == "\n");
80 }
81
82 static void test_vprint_unicode() {
83 #ifdef TEST_HAS_NO_UNICODE
84 std::array<char, 100> buffer{0};
85
86 FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
87 assert(file);
88
89 char c = '!';
90 std::vprint_unicode(file, "hello world{}", std::make_format_args(c));
91 long pos = std::ftell(file);
92 std::fclose(file);
93
94 assert(pos > 0);
95 assert(std::string_view(buffer.data(), pos) == "hello world!");
96 #endif // TEST_HAS_NO_UNICODE
97 }
98
99 static void test_vprint_nonunicode() {
100 std::array<char, 100> buffer{0};
101
102 FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
103 assert(file);
104
105 char c = '!';
106 std::vprint_nonunicode(file, "hello world{}", std::make_format_args(c));
107 long pos = std::ftell(file);
108 std::fclose(file);
109
110 assert(pos > 0);
111 assert(std::string_view(buffer.data(), pos) == "hello world!");
112 }
113
114 int main(int, char**) {
115 test_print();
116 test_println();
117 test_println_blank_line();
118 test_vprint_unicode();
119 test_vprint_nonunicode();
120
121 return 0;
122 }