xref: /llvm-project/libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.pass.cpp (revision 1cf970db4e5499f6b38d9c6644935a78d758802c)
1 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
2 // UNSUPPORTED: no-filesystem
3 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
4 
5 // XFAIL: msvc, target={{.+}}-windows-gnu
6 // XFAIL: availability-fp_to_chars-missing
7 
8 // <print>
9 
10 // The FILE returned by fmemopen does not have file descriptor.
11 // This means the test could fail when the implementation uses a
12 // function that requires a file descriptor, for example write.
13 //
14 // This tests all print functions which takes a FILE* as argument.
15 
16 // template<class... Args>
17 //   void print(FILE* stream, format_string<Args...> fmt, Args&&... args);
18 // template<class... Args>
19 //   void println(FILE* stream, format_string<Args...> fmt, Args&&... args);
20 // void vprint_unicode(FILE* stream, string_view fmt, format_args args);
21 // void vprint_nonunicode(FILE* stream, string_view fmt, format_args args);
22 
23 #include <array>
24 #include <cstdio>
25 #include <cassert>
26 #include <print>
27 
28 static void test_print() {
29   std::array<char, 100> buffer{0};
30 
31   FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
32   assert(file);
33 
34   std::print(file, "hello world{}", '!');
35   long pos = std::ftell(file);
36   std::fclose(file);
37 
38   assert(pos > 0);
39   assert(std::string_view(buffer.data(), pos) == "hello world!");
40 }
41 
42 static void test_println() {
43   std::array<char, 100> buffer{0};
44 
45   FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
46   assert(file);
47 
48   std::println(file, "hello world{}", '!');
49   long pos = std::ftell(file);
50   std::fclose(file);
51 
52   assert(pos > 0);
53   assert(std::string_view(buffer.data(), pos) == "hello world!\n");
54 }
55 
56 static void test_vprint_unicode() {
57   std::array<char, 100> buffer{0};
58 
59   FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
60   assert(file);
61 
62   std::vprint_unicode(file, "hello world{}", std::make_format_args('!'));
63   long pos = std::ftell(file);
64   std::fclose(file);
65 
66   assert(pos > 0);
67   assert(std::string_view(buffer.data(), pos) == "hello world!");
68 }
69 
70 static void test_vprint_nonunicode() {
71   std::array<char, 100> buffer{0};
72 
73   FILE* file = fmemopen(buffer.data(), buffer.size(), "wb");
74   assert(file);
75 
76   std::vprint_nonunicode(file, "hello world{}", std::make_format_args('!'));
77   long pos = std::ftell(file);
78   std::fclose(file);
79 
80   assert(pos > 0);
81   assert(std::string_view(buffer.data(), pos) == "hello world!");
82 }
83 
84 int main(int, char**) {
85   test_print();
86   test_println();
87   test_vprint_unicode();
88   test_vprint_nonunicode();
89 
90   return 0;
91 }
92