Debian Codegraph @

libstdc++-v3/testsuite/27_io/print/1.cc

raw
1 // { dg-additional-options "-lstdc++exp" { target { *-*-mingw* } } }
2 // { dg-do run { target c++23 } }
3 // { dg-require-fileio "" }
4
5 #include <print>
6 #include <cstdio>
7 #include <testsuite_hooks.h>
8 #include <testsuite_fs.h>
9
10 void
11 test_print_default()
12 {
13 std::print("H{}ll{}, {}!", 3, 0, "world");
14 // { dg-output "H3ll0, world!" }
15 }
16
17 void
18 test_println_default()
19 {
20 std::println("I walk the line");
21 // { dg-output "I walk the line\r?\n" }
22 }
23
24 void
25 test_print_file()
26 {
27 __gnu_test::scoped_file f;
28 FILE* strm = std::fopen(f.path.string().c_str(), "w");
29 VERIFY( strm );
30 std::print(strm, "File under '{}' for {}", 'O', "OUT OF FILE");
31 std::fclose(strm);
32
33 std::ifstream in(f.path);
34 std::string txt(std::istreambuf_iterator<char>(in), {});
35 VERIFY( txt == "File under 'O' for OUT OF FILE" );
36 }
37
38 void
39 test_println_file()
40 {
41 __gnu_test::scoped_file f;
42 FILE* strm = std::fopen(f.path.string().c_str(), "w");
43 VERIFY( strm );
44 std::println(strm, "{} Lineman was a song I once heard", "Wichita");
45 std::fclose(strm);
46
47 std::ifstream in(f.path);
48 std::string txt(std::istreambuf_iterator<char>(in), {});
49 VERIFY( txt == "Wichita Lineman was a song I once heard\n" );
50 }
51
52 void
53 test_print_raw()
54 {
55 __gnu_test::scoped_file f;
56 FILE* strm = std::fopen(f.path.string().c_str(), "w");
57 VERIFY( strm );
58 std::print(strm, "{}", '\xa3'); // Not a valid UTF-8 string.
59 std::fclose(strm);
60
61 std::ifstream in(f.path);
62 std::string txt(std::istreambuf_iterator<char>(in), {});
63 // Invalid UTF-8 should be written out unchanged if the stream is not
64 // connected to a tty:
65 VERIFY( txt == "\xa3" );
66 }
67
68 void
69 test_vprint_nonunicode()
70 {
71 std::vprint_nonunicode("{0} in \xc0 {0} out\n",
72 std::make_format_args("garbage"));
73 // { dg-output "garbage in . garbage out" }
74 }
75
76 void
77 test_errors()
78 {
79 #ifdef __cpp_exceptions
80 try
81 {
82 std::print(stdin, "{}", "nope");
83 VERIFY(false);
84 }
85 catch (const std::system_error&)
86 {
87 }
88 #endif
89 }
90
91 int main()
92 {
93 test_print_default();
94 test_println_default();
95 test_print_file();
96 test_println_file();
97 test_print_raw();
98 test_vprint_nonunicode();
99 test_errors();
100 }