public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-9212] libstdc++: Test error handling in std::print Date: Wed, 28 Feb 2024 11:29:42 +0000 (GMT) [thread overview] Message-ID: <20240228112942.4D5E3385840F@sourceware.org> (raw) https://gcc.gnu.org/g:5c22de3fecb6c853fcdf3e39a4cfe594846079a1 commit r14-9212-g5c22de3fecb6c853fcdf3e39a4cfe594846079a1 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Feb 22 13:06:59 2024 +0000 libstdc++: Test error handling in std::print The standard requires an exception if std::print fails to write to a FILE*. When writing to a std::ostream, failure to format the arguments doesn't affect the stream state, but failure to write to the streadm sets badbit. libstdc++-v3/ChangeLog: * testsuite/27_io/basic_ostream/print/1.cc: Check error handling. * testsuite/27_io/print/1.cc: Likewise. Diff: --- .../testsuite/27_io/basic_ostream/print/1.cc | 35 ++++++++++++++++++++++ libstdc++-v3/testsuite/27_io/print/1.cc | 16 ++++++++++ 2 files changed, 51 insertions(+) diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc index 71a4daa04c9..cd4b116ac1c 100644 --- a/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc @@ -103,6 +103,40 @@ test_locale() } } +void +test_errors() +{ + // Failure to generate output is reported by setting badbit. + std::stringstream in(std::ios::in); + std::print(in, "{}", "nope"); // No exception here. + VERIFY(in.bad()); +#ifdef __cpp_exceptions + in.clear(); + in.exceptions(std::ios::badbit); + try + { + std::print(in, "{}", "nope"); // Should throw now. + VERIFY(false); + } + catch (const std::ios::failure&) + { + } + + // An exception thrown when formatting the string is propagated + // without setting badbit. + std::ostringstream out; + try + { + std::vprint_nonunicode(out, "{}", std::make_format_args()); + VERIFY(false); + } + catch (const std::format_error&) + { + } + VERIFY(out.good()); +#endif +} + int main() { test_print_ostream(); @@ -111,4 +145,5 @@ int main() test_print_no_padding(); test_vprint_nonunicode(); test_locale(); + test_errors(); } diff --git a/libstdc++-v3/testsuite/27_io/print/1.cc b/libstdc++-v3/testsuite/27_io/print/1.cc index 6a294e0454b..d570f7938be 100644 --- a/libstdc++-v3/testsuite/27_io/print/1.cc +++ b/libstdc++-v3/testsuite/27_io/print/1.cc @@ -74,6 +74,21 @@ test_vprint_nonunicode() // { dg-output "garbage in . garbage out" } } +void +test_errors() +{ +#ifdef __cpp_exceptions + try + { + std::print(stdin, "{}", "nope"); + VERIFY(false); + } + catch (const std::system_error&) + { + } +#endif +} + int main() { test_print_default(); @@ -82,4 +97,5 @@ int main() test_println_file(); test_print_raw(); test_vprint_nonunicode(); + test_errors(); }
reply other threads:[~2024-02-28 11:29 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240228112942.4D5E3385840F@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).