| 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 | std::ostream::pos_type pos = std::ostream::pos_type(); |
| 19 | os.seekp(pos); |
| 20 | VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 21 | |
| 22 | os.clear(); |
| 23 | std::ostream::off_type off(5); |
| 24 | os.seekp(off, std::ios_base::cur); |
| 25 | VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 26 | |
| 27 | os.clear(); |
| 28 | os.exceptions(std::ios_base::failbit); |
| 29 | |
| 30 | try |
| 31 | { |
| 32 | os.clear(); |
| 33 | os.seekp(pos); |
| 34 | VERIFY( false ); |
| 35 | } |
| 36 | catch (const std::ios_base::failure&) |
| 37 | { |
| 38 | VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 39 | } |
| 40 | catch (...) |
| 41 | { |
| 42 | VERIFY( false ); |
| 43 | } |
| 44 | |
| 45 | try |
| 46 | { |
| 47 | os.clear(); |
| 48 | os.seekp(off, std::ios_base::cur); |
| 49 | VERIFY( false ); |
| 50 | } |
| 51 | catch (const std::ios_base::failure&) |
| 52 | { |
| 53 | VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 54 | } |
| 55 | catch (...) |
| 56 | { |
| 57 | VERIFY( false ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | test02() |
| 63 | { |
| 64 | // Check that the sentry flushes a tied stream when seeking. |
| 65 | |
| 66 | { |
| 67 | __gnu_test::sync_streambuf buf; |
| 68 | std::ostream os(&buf); |
| 69 | |
| 70 | __gnu_test::sync_streambuf buf_tie; |
| 71 | std::ostream os_tie(&buf_tie); |
| 72 | |
| 73 | os.tie(&os_tie); |
| 74 | |
| 75 | std::ostream::pos_type pos = std::ostream::pos_type(); |
| 76 | os.seekp(pos); |
| 77 | |
| 78 | VERIFY( ! buf.sync_called() ); |
| 79 | VERIFY( buf_tie.sync_called() ); |
| 80 | } |
| 81 | |
| 82 | { |
| 83 | __gnu_test::sync_streambuf buf; |
| 84 | std::ostream os(&buf); |
| 85 | |
| 86 | __gnu_test::sync_streambuf buf_tie; |
| 87 | std::ostream os_tie(&buf_tie); |
| 88 | |
| 89 | os.tie(&os_tie); |
| 90 | |
| 91 | std::ostream::off_type off(0); |
| 92 | os.seekp(off, std::ios_base::cur); |
| 93 | |
| 94 | VERIFY( ! buf.sync_called() ); |
| 95 | VERIFY( buf_tie.sync_called() ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int main() |
| 100 | { |
| 101 | test01(); |
| 102 | test02(); |
| 103 | } |