aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2024-02-22 13:06:59 +0000
committerJonathan Wakely <jwakely@redhat.com>2024-02-28 11:27:46 +0000
commit5c22de3fecb6c853fcdf3e39a4cfe594846079a1 (patch)
tree59e74f86888cf245101611af3f2d1804a0435fc2 /libstdc++-v3
parent4da67f63b9e306c43764857a0436d13668e1d3cc (diff)
downloadgcc-5c22de3fecb6c853fcdf3e39a4cfe594846079a1.zip
gcc-5c22de3fecb6c853fcdf3e39a4cfe594846079a1.tar.gz
gcc-5c22de3fecb6c853fcdf3e39a4cfe594846079a1.tar.bz2
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.
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc35
-rw-r--r--libstdc++-v3/testsuite/27_io/print/1.cc16
2 files changed, 51 insertions, 0 deletions
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 71a4daa..cd4b116 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 6a294e0..d570f79 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();
}