| 1 | #include <ostream> |
| 2 | #include <testsuite_hooks.h> |
| 3 | #include <testsuite_io.h> |
| 4 | |
| 5 | // C++11 27.7.3.5 basic_ostream seek members [ostream.seeks] |
| 6 | |
| 7 | // Verify [ostream.seeks] functions use a sentry, as per N3168. |
| 8 | |
| 9 | void |
| 10 | test01() |
| 11 | { |
| 12 | // Check that the sentry sets failbit when seeking on a bad stream. |
| 13 | // The standard doesn't guarantee this, but it is true for libstdc++. |
| 14 | |
| 15 | std::ostream os(0); |
| 16 | VERIFY( os.rdstate() == std::ios_base::badbit ); |
| 17 | |
| 18 | os.tellp(); |
| 19 | VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 20 | |
| 21 | os.clear(); |
| 22 | |
| 23 | os.exceptions(std::ios_base::failbit); |
| 24 | |
| 25 | try |
| 26 | { |
| 27 | os.clear(); |
| 28 | os.tellp(); |
| 29 | VERIFY( false ); |
| 30 | } |
| 31 | catch (const std::ios_base::failure&) |
| 32 | { |
| 33 | VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 34 | } |
| 35 | catch (...) |
| 36 | { |
| 37 | VERIFY( false ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | test02() |
| 43 | { |
| 44 | // Check that the sentry flushes a tied stream when seeking. |
| 45 | |
| 46 | __gnu_test::sync_streambuf buf; |
| 47 | std::ostream os(&buf); |
| 48 | |
| 49 | __gnu_test::sync_streambuf buf_tie; |
| 50 | std::ostream os_tie(&buf_tie); |
| 51 | |
| 52 | os.tie(&os_tie); |
| 53 | |
| 54 | os.tellp(); |
| 55 | |
| 56 | VERIFY( ! buf.sync_called() ); |
| 57 | VERIFY( buf_tie.sync_called() ); |
| 58 | } |
| 59 | |
| 60 | int main() |
| 61 | { |
| 62 | test01(); |
| 63 | test02(); |
| 64 | } |