| 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::wostream os(0); |
| 16 |
VERIFY( os.rdstate() == std::ios_base::badbit ); |
| 17 |
|
| 18 |
std::wostream::pos_type pos = std::wostream::pos_type(); |
| 19 |
os.seekp(pos); |
| 20 |
VERIFY( os.rdstate() & std::ios_base::failbit ); |
| 21 |
|
| 22 |
os.clear(); |
| 23 |
std::wostream::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_wstreambuf buf; |
| 68 |
std::wostream os(&buf); |
| 69 |
|
| 70 |
__gnu_test::sync_wstreambuf buf_tie; |
| 71 |
std::wostream os_tie(&buf_tie); |
| 72 |
|
| 73 |
os.tie(&os_tie); |
| 74 |
|
| 75 |
std::wostream::pos_type pos = std::wostream::pos_type(); |
| 76 |
os.seekp(pos); |
| 77 |
|
| 78 |
VERIFY( buf_tie.sync_called() ); |
| 79 |
} |
| 80 |
|
| 81 |
{ |
| 82 |
__gnu_test::sync_wstreambuf buf; |
| 83 |
std::wostream os(&buf); |
| 84 |
|
| 85 |
__gnu_test::sync_wstreambuf buf_tie; |
| 86 |
std::wostream os_tie(&buf_tie); |
| 87 |
|
| 88 |
os.tie(&os_tie); |
| 89 |
|
| 90 |
std::wostream::off_type off(0); |
| 91 |
os.seekp(off, std::ios_base::cur); |
| 92 |
|
| 93 |
VERIFY( buf_tie.sync_called() ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
int main() |
| 98 |
{ |
| 99 |
test01(); |
| 100 |
test02(); |
| 101 |
} |