| 1 | // { dg-do run } |
| 2 | |
| 3 | // 4188. ostream::sentry destructor should handle exceptions |
| 4 | |
| 5 | #include <ostream> |
| 6 | #include <streambuf> |
| 7 | #include <testsuite_hooks.h> |
| 8 | |
| 9 | struct bad_streambuf : std::streambuf |
| 10 | { |
| 11 | int sync() { return -1; } |
| 12 | }; |
| 13 | |
| 14 | void |
| 15 | test_returns_error() |
| 16 | { |
| 17 | bad_streambuf buf; |
| 18 | std::ostream out(&buf); |
| 19 | out.setf(std::ios::unitbuf); |
| 20 | out.exceptions(std::ios::badbit); |
| 21 | out.write("", 0); // constructs sentry |
| 22 | VERIFY( out.bad() ); |
| 23 | } |
| 24 | |
| 25 | struct exceptionally_bad_streambuf : std::streambuf |
| 26 | { |
| 27 | int sync() { throw std::ios::failure("unsyncable"); } |
| 28 | }; |
| 29 | |
| 30 | void |
| 31 | test_throws() |
| 32 | { |
| 33 | exceptionally_bad_streambuf buf; |
| 34 | std::ostream out(&buf); |
| 35 | out.setf(std::ios::unitbuf); |
| 36 | out.write("", 0); // constructs sentry |
| 37 | VERIFY( out.bad() ); |
| 38 | |
| 39 | // Repeat with badbit in exceptions mask |
| 40 | out.clear(); |
| 41 | out.exceptions(std::ios::badbit); |
| 42 | out.write("", 0); // constructs sentry |
| 43 | VERIFY( out.bad() ); |
| 44 | } |
| 45 | |
| 46 | int main() |
| 47 | { |
| 48 | test_returns_error(); |
| 49 | test_throws(); |
| 50 | } |