| 1 |
// { dg-additional-options "-lstdc++exp" { target { *-*-mingw* } } } |
| 2 |
// { dg-do run { target c++23 } } |
| 3 |
// { dg-require-fileio "" } |
| 4 |
|
| 5 |
#include <ostream> |
| 6 |
#include <spanstream> |
| 7 |
#include <string_view> |
| 8 |
#include <iostream> |
| 9 |
#include <iomanip> |
| 10 |
#include <testsuite_hooks.h> |
| 11 |
|
| 12 |
void |
| 13 |
test_print_ostream() |
| 14 |
{ |
| 15 |
char buf[64]; |
| 16 |
std::spanstream os(buf); |
| 17 |
std::print(os, "File under '{}' for {}", 'O', "OUT OF FILE"); |
| 18 |
std::string_view txt(os.span()); |
| 19 |
VERIFY( txt == "File under 'O' for OUT OF FILE" ); |
| 20 |
} |
| 21 |
|
| 22 |
void |
| 23 |
test_println_ostream() |
| 24 |
{ |
| 25 |
char buf[64]; |
| 26 |
std::spanstream os(buf); |
| 27 |
std::println(os, "{} Lineman was a song I once heard", "Wichita"); |
| 28 |
std::string_view txt(os.span()); |
| 29 |
VERIFY( txt == "Wichita Lineman was a song I once heard\n" ); |
| 30 |
} |
| 31 |
|
| 32 |
void |
| 33 |
test_print_raw() |
| 34 |
{ |
| 35 |
char buf[64]; |
| 36 |
std::spanstream os(buf); |
| 37 |
std::print(os, "{}", '\xa3'); // Not a valid UTF-8 string. |
| 38 |
std::string_view txt(os.span()); |
| 39 |
// Invalid UTF-8 should be written out unchanged if the ostream is not |
| 40 |
// connected to a tty: |
| 41 |
VERIFY( txt == "\xa3" ); |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
test_print_no_padding() |
| 46 |
{ |
| 47 |
// [ostream.formatted.print] does not say this function "determines padding", |
| 48 |
// see https://gcc.gnu.org/pipermail/gcc-patches/2023-December/640680.html |
| 49 |
char buf[64]; |
| 50 |
std::spanstream os(buf); |
| 51 |
os << std::setw(60) << std::setfill('?') << std::right; // should be ignored |
| 52 |
std::print(os, "{} Luftballons", 99); |
| 53 |
std::string_view txt(os.span()); |
| 54 |
VERIFY( txt == "99 Luftballons" ); |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
test_vprint_nonunicode() |
| 59 |
{ |
| 60 |
std::ostream out(std::cout.rdbuf()); |
| 61 |
std::vprint_nonunicode(out, "{0} in \xc0 {0} out\n", |
| 62 |
std::make_format_args("garbage")); |
| 63 |
// { dg-output "garbage in . garbage out" } |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
test_locale() |
| 68 |
{ |
| 69 |
struct stream_punc : std::numpunct<char> |
| 70 |
{ |
| 71 |
std::string do_grouping() const override { return "\2\2"; } |
| 72 |
char do_thousands_sep() const override { return '~'; } |
| 73 |
}; |
| 74 |
|
| 75 |
// The default C locale. |
| 76 |
std::locale cloc = std::locale::classic(); |
| 77 |
// A custom locale using tilde digit separators. |
| 78 |
std::locale bloc(cloc, new stream_punc); |
| 79 |
|
| 80 |
{ |
| 81 |
std::ostringstream os; |
| 82 |
std::print(os, "{:L} {}", 12345, 6789); |
| 83 |
VERIFY(os.str() == "12345 6789"); |
| 84 |
} |
| 85 |
{ |
| 86 |
std::ostringstream os; |
| 87 |
std::print(os, "{}", 42); |
| 88 |
VERIFY(os.str() == "42"); |
| 89 |
} |
| 90 |
{ |
| 91 |
std::ostringstream os; |
| 92 |
os.imbue(bloc); |
| 93 |
std::print(os, "{:L} {}", 12345, 6789); |
| 94 |
VERIFY(os.str() == "1~23~45 6789"); |
| 95 |
} |
| 96 |
|
| 97 |
{ |
| 98 |
// LWG 4088. println ignores the locale imbued in std::ostream |
| 99 |
std::ostringstream os; |
| 100 |
os.imbue(bloc); |
| 101 |
std::println(os, "{:L} {}", 12345, 6789); |
| 102 |
VERIFY(os.str() == "1~23~45 6789\n"); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
test_errors() |
| 108 |
{ |
| 109 |
// Failure to generate output is reported by setting badbit. |
| 110 |
std::stringstream in(std::ios::in); |
| 111 |
std::print(in, "{}", "nope"); // No exception here. |
| 112 |
VERIFY(in.bad()); |
| 113 |
#ifdef __cpp_exceptions |
| 114 |
in.clear(); |
| 115 |
in.exceptions(std::ios::badbit); |
| 116 |
try |
| 117 |
{ |
| 118 |
std::print(in, "{}", "nope"); // Should throw now. |
| 119 |
VERIFY(false); |
| 120 |
} |
| 121 |
catch (const std::ios::failure&) |
| 122 |
{ |
| 123 |
} |
| 124 |
|
| 125 |
// An exception thrown when formatting the string is propagated |
| 126 |
// without setting badbit. |
| 127 |
std::ostringstream out; |
| 128 |
try |
| 129 |
{ |
| 130 |
std::vprint_nonunicode(out, "{}", std::make_format_args()); |
| 131 |
VERIFY(false); |
| 132 |
} |
| 133 |
catch (const std::format_error&) |
| 134 |
{ |
| 135 |
} |
| 136 |
VERIFY(out.good()); |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
int main() |
| 141 |
{ |
| 142 |
test_print_ostream(); |
| 143 |
test_println_ostream(); |
| 144 |
test_print_raw(); |
| 145 |
test_print_no_padding(); |
| 146 |
test_vprint_nonunicode(); |
| 147 |
test_locale(); |
| 148 |
test_errors(); |
| 149 |
} |