[29] | 1 | // Boost ios_state_unit_test.cpp test file ---------------------------------// |
---|
| 2 | |
---|
| 3 | // Copyright 2003 Daryle Walker. Use, modification, and distribution are |
---|
| 4 | // subject to the Boost Software License, Version 1.0. (See accompanying file |
---|
| 5 | // LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) |
---|
| 6 | |
---|
| 7 | // See <http://www.boost.org/libs/io/> for the library's home page. |
---|
| 8 | |
---|
| 9 | // Revision History |
---|
| 10 | // 12 Sep 2003 Initial version (Daryle Walker) |
---|
| 11 | |
---|
| 12 | #include <boost/io/ios_state.hpp> // for boost::io::ios_flags_saver, etc. |
---|
| 13 | #include <boost/test/unit_test.hpp> // for main, BOOST_CHECK, etc. |
---|
| 14 | |
---|
| 15 | #include <cstddef> // for NULL |
---|
| 16 | #include <iomanip> // for std::setiosflags, etc. |
---|
| 17 | #include <ios> // for std::ios_base |
---|
| 18 | #include <iostream> // for std::cout, std::cerr, etc. |
---|
| 19 | #include <istream> // for std::iostream |
---|
| 20 | #include <locale> // for std::locale, std::numpunct |
---|
| 21 | #include <sstream> // for std::stringstream, etc. |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | // Global constants |
---|
| 25 | int const word_index = std::ios_base::xalloc(); |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | // Facet with the (classic) bool names spelled backwards |
---|
| 29 | class backward_bool_names |
---|
| 30 | : public std::numpunct<char> |
---|
| 31 | { |
---|
| 32 | typedef std::numpunct<char> base_type; |
---|
| 33 | |
---|
| 34 | public: |
---|
| 35 | explicit backward_bool_names( std::size_t refs = 0 ) |
---|
| 36 | : base_type( refs ) |
---|
| 37 | {} |
---|
| 38 | |
---|
| 39 | protected: |
---|
| 40 | virtual ~backward_bool_names() {} |
---|
| 41 | |
---|
| 42 | virtual base_type::string_type do_truename() const |
---|
| 43 | { return "eurt"; } |
---|
| 44 | virtual base_type::string_type do_falsename() const |
---|
| 45 | { return "eslaf"; } |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | // Unit test for format-flag saving |
---|
| 50 | void |
---|
| 51 | ios_flags_saver_unit_test |
---|
| 52 | ( |
---|
| 53 | ) |
---|
| 54 | { |
---|
| 55 | using namespace std; |
---|
| 56 | |
---|
| 57 | stringstream ss; |
---|
| 58 | |
---|
| 59 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 60 | |
---|
| 61 | { |
---|
| 62 | boost::io::ios_flags_saver ifs( ss ); |
---|
| 63 | |
---|
| 64 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 65 | |
---|
| 66 | ss << noskipws << fixed << boolalpha; |
---|
| 67 | BOOST_CHECK_EQUAL( (ios_base::boolalpha | ios_base::dec |
---|
| 68 | | ios_base::fixed), ss.flags() ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 72 | |
---|
| 73 | { |
---|
| 74 | boost::io::ios_flags_saver ifs( ss, (ios_base::showbase |
---|
| 75 | | ios_base::internal) ); |
---|
| 76 | |
---|
| 77 | BOOST_CHECK_EQUAL( (ios_base::showbase | ios_base::internal), |
---|
| 78 | ss.flags() ); |
---|
| 79 | |
---|
| 80 | ss << setiosflags( ios_base::unitbuf ); |
---|
| 81 | BOOST_CHECK_EQUAL( (ios_base::showbase | ios_base::internal |
---|
| 82 | | ios_base::unitbuf), ss.flags() ); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // Unit test for precision saving |
---|
| 89 | void |
---|
| 90 | ios_precision_saver_unit_test |
---|
| 91 | ( |
---|
| 92 | ) |
---|
| 93 | { |
---|
| 94 | using namespace std; |
---|
| 95 | |
---|
| 96 | stringstream ss; |
---|
| 97 | |
---|
| 98 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 99 | |
---|
| 100 | { |
---|
| 101 | boost::io::ios_precision_saver ips( ss ); |
---|
| 102 | |
---|
| 103 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 104 | |
---|
| 105 | ss << setprecision( 4 ); |
---|
| 106 | BOOST_CHECK_EQUAL( 4, ss.precision() ); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 110 | |
---|
| 111 | { |
---|
| 112 | boost::io::ios_precision_saver ips( ss, 8 ); |
---|
| 113 | |
---|
| 114 | BOOST_CHECK_EQUAL( 8, ss.precision() ); |
---|
| 115 | |
---|
| 116 | ss << setprecision( 10 ); |
---|
| 117 | BOOST_CHECK_EQUAL( 10, ss.precision() ); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | // Unit test for width saving |
---|
| 124 | void |
---|
| 125 | ios_width_saver_unit_test |
---|
| 126 | ( |
---|
| 127 | ) |
---|
| 128 | { |
---|
| 129 | using namespace std; |
---|
| 130 | |
---|
| 131 | stringstream ss; |
---|
| 132 | |
---|
| 133 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 134 | |
---|
| 135 | { |
---|
| 136 | boost::io::ios_width_saver iws( ss ); |
---|
| 137 | |
---|
| 138 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 139 | |
---|
| 140 | ss << setw( 4 ); |
---|
| 141 | BOOST_CHECK_EQUAL( 4, ss.width() ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 145 | |
---|
| 146 | { |
---|
| 147 | boost::io::ios_width_saver iws( ss, 8 ); |
---|
| 148 | |
---|
| 149 | BOOST_CHECK_EQUAL( 8, ss.width() ); |
---|
| 150 | |
---|
| 151 | ss << setw( 10 ); |
---|
| 152 | BOOST_CHECK_EQUAL( 10, ss.width() ); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // Unit test for I/O-state saving |
---|
| 159 | void |
---|
| 160 | ios_iostate_saver_unit_test |
---|
| 161 | ( |
---|
| 162 | ) |
---|
| 163 | { |
---|
| 164 | using namespace std; |
---|
| 165 | |
---|
| 166 | stringstream ss; |
---|
| 167 | |
---|
| 168 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 169 | BOOST_CHECK( ss.good() ); |
---|
| 170 | |
---|
| 171 | { |
---|
| 172 | boost::io::ios_iostate_saver iis( ss ); |
---|
| 173 | char c; |
---|
| 174 | |
---|
| 175 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 176 | BOOST_CHECK( ss.good() ); |
---|
| 177 | |
---|
| 178 | ss >> c; |
---|
| 179 | BOOST_CHECK_EQUAL( (ios_base::eofbit | ios_base::failbit), |
---|
| 180 | ss.rdstate() ); |
---|
| 181 | BOOST_CHECK( ss.eof() ); |
---|
| 182 | BOOST_CHECK( ss.fail() ); |
---|
| 183 | BOOST_CHECK( !ss.bad() ); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 187 | BOOST_CHECK( ss.good() ); |
---|
| 188 | |
---|
| 189 | { |
---|
| 190 | boost::io::ios_iostate_saver iis( ss, ios_base::eofbit ); |
---|
| 191 | |
---|
| 192 | BOOST_CHECK_EQUAL( ios_base::eofbit, ss.rdstate() ); |
---|
| 193 | BOOST_CHECK( ss.eof() ); |
---|
| 194 | |
---|
| 195 | ss.setstate( ios_base::badbit ); |
---|
| 196 | BOOST_CHECK_EQUAL( (ios_base::eofbit | ios_base::badbit), |
---|
| 197 | ss.rdstate() ); |
---|
| 198 | BOOST_CHECK( ss.eof() ); |
---|
| 199 | BOOST_CHECK( ss.fail() ); |
---|
| 200 | BOOST_CHECK( ss.bad() ); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 204 | BOOST_CHECK( ss.good() ); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | // Unit test for exception-flag saving |
---|
| 208 | void |
---|
| 209 | ios_exception_saver_unit_test |
---|
| 210 | ( |
---|
| 211 | ) |
---|
| 212 | { |
---|
| 213 | using namespace std; |
---|
| 214 | |
---|
| 215 | stringstream ss; |
---|
| 216 | |
---|
| 217 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 218 | |
---|
| 219 | { |
---|
| 220 | boost::io::ios_exception_saver ies( ss ); |
---|
| 221 | |
---|
| 222 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 223 | |
---|
| 224 | ss.exceptions( ios_base::failbit ); |
---|
| 225 | BOOST_CHECK_EQUAL( ios_base::failbit, ss.exceptions() ); |
---|
| 226 | |
---|
| 227 | { |
---|
| 228 | boost::io::ios_iostate_saver iis( ss ); |
---|
| 229 | char c; |
---|
| 230 | |
---|
| 231 | BOOST_CHECK_THROW( ss >> c, std::ios_base::failure ); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 236 | |
---|
| 237 | { |
---|
| 238 | boost::io::ios_exception_saver ies( ss, ios_base::eofbit ); |
---|
| 239 | |
---|
| 240 | BOOST_CHECK_EQUAL( ios_base::eofbit, ss.exceptions() ); |
---|
| 241 | |
---|
| 242 | ss.exceptions( ios_base::badbit ); |
---|
| 243 | BOOST_CHECK_EQUAL( ios_base::badbit, ss.exceptions() ); |
---|
| 244 | |
---|
| 245 | { |
---|
| 246 | boost::io::ios_iostate_saver iis( ss ); |
---|
| 247 | char c; |
---|
| 248 | |
---|
| 249 | BOOST_CHECK_NO_THROW( ss >> c ); |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | // Unit test for tied-stream saving |
---|
| 257 | void |
---|
| 258 | ios_tie_saver_unit_test |
---|
| 259 | ( |
---|
| 260 | ) |
---|
| 261 | { |
---|
| 262 | using namespace std; |
---|
| 263 | |
---|
| 264 | BOOST_CHECK( NULL == cout.tie() ); |
---|
| 265 | |
---|
| 266 | { |
---|
| 267 | boost::io::ios_tie_saver its( cout ); |
---|
| 268 | |
---|
| 269 | BOOST_CHECK( NULL == cout.tie() ); |
---|
| 270 | |
---|
| 271 | cout.tie( &clog ); |
---|
| 272 | BOOST_CHECK_EQUAL( &clog, cout.tie() ); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | BOOST_CHECK( NULL == cout.tie() ); |
---|
| 276 | |
---|
| 277 | { |
---|
| 278 | boost::io::ios_tie_saver its( cout, &clog ); |
---|
| 279 | |
---|
| 280 | BOOST_CHECK_EQUAL( &clog, cout.tie() ); |
---|
| 281 | |
---|
| 282 | cout.tie( &cerr ); |
---|
| 283 | BOOST_CHECK_EQUAL( &cerr, cout.tie() ); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | BOOST_CHECK( NULL == cout.tie() ); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | // Unit test for connected-streambuf saving |
---|
| 290 | void |
---|
| 291 | ios_rdbuf_saver_unit_test |
---|
| 292 | ( |
---|
| 293 | ) |
---|
| 294 | { |
---|
| 295 | using namespace std; |
---|
| 296 | |
---|
| 297 | iostream s( NULL ); |
---|
| 298 | |
---|
| 299 | BOOST_CHECK( NULL == s.rdbuf() ); |
---|
| 300 | |
---|
| 301 | { |
---|
| 302 | stringbuf sb; |
---|
| 303 | boost::io::ios_rdbuf_saver irs( s ); |
---|
| 304 | |
---|
| 305 | BOOST_CHECK( NULL == s.rdbuf() ); |
---|
| 306 | |
---|
| 307 | s.rdbuf( &sb ); |
---|
| 308 | BOOST_CHECK_EQUAL( &sb, s.rdbuf() ); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | BOOST_CHECK( NULL == s.rdbuf() ); |
---|
| 312 | |
---|
| 313 | { |
---|
| 314 | stringbuf sb1, sb2( "Hi there" ); |
---|
| 315 | boost::io::ios_rdbuf_saver irs( s, &sb1 ); |
---|
| 316 | |
---|
| 317 | BOOST_CHECK_EQUAL( &sb1, s.rdbuf() ); |
---|
| 318 | |
---|
| 319 | s.rdbuf( &sb2 ); |
---|
| 320 | BOOST_CHECK_EQUAL( &sb2, s.rdbuf() ); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | BOOST_CHECK( NULL == s.rdbuf() ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | // Unit test for fill-character saving |
---|
| 327 | void |
---|
| 328 | ios_fill_saver_unit_test |
---|
| 329 | ( |
---|
| 330 | ) |
---|
| 331 | { |
---|
| 332 | using namespace std; |
---|
| 333 | |
---|
| 334 | stringstream ss; |
---|
| 335 | |
---|
| 336 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 337 | |
---|
| 338 | { |
---|
| 339 | boost::io::ios_fill_saver ifs( ss ); |
---|
| 340 | |
---|
| 341 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 342 | |
---|
| 343 | ss.fill( 'x' ); |
---|
| 344 | BOOST_CHECK_EQUAL( 'x', ss.fill() ); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 348 | |
---|
| 349 | { |
---|
| 350 | boost::io::ios_fill_saver ifs( ss, '3' ); |
---|
| 351 | |
---|
| 352 | BOOST_CHECK_EQUAL( '3', ss.fill() ); |
---|
| 353 | |
---|
| 354 | ss.fill( '+' ); |
---|
| 355 | BOOST_CHECK_EQUAL( '+', ss.fill() ); |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | // Unit test for locale saving |
---|
| 362 | void |
---|
| 363 | ios_locale_saver_unit_test |
---|
| 364 | ( |
---|
| 365 | ) |
---|
| 366 | { |
---|
| 367 | using namespace std; |
---|
| 368 | |
---|
| 369 | typedef numpunct<char> npc_type; |
---|
| 370 | |
---|
| 371 | stringstream ss; |
---|
| 372 | |
---|
| 373 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 374 | // locales are unprintable, so no BOOST_CHECK_EQUAL |
---|
| 375 | BOOST_CHECK( !has_facet<npc_type>(ss.getloc()) || (NULL |
---|
| 376 | == dynamic_cast<backward_bool_names const *>( |
---|
| 377 | &use_facet<npc_type>(ss.getloc()) )) ); |
---|
| 378 | // my implementation of has_facet just checks IDs, but doesn't do dynamic |
---|
| 379 | // cast (therefore if a specifc facet type is missing, but its base class |
---|
| 380 | // is available, has_facet will mistakenly[?] match), so I have to do it |
---|
| 381 | // here. I wanted: "BOOST_CHECK( ! has_facet< backward_bool_names >( |
---|
| 382 | // ss.getloc() ) )" |
---|
| 383 | { |
---|
| 384 | boost::io::ios_locale_saver ils( ss ); |
---|
| 385 | |
---|
| 386 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 387 | |
---|
| 388 | ss.imbue( locale::classic() ); |
---|
| 389 | BOOST_CHECK( locale::classic() == ss.getloc() ); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 393 | BOOST_CHECK( !has_facet<npc_type>(ss.getloc()) || (NULL |
---|
| 394 | == dynamic_cast<backward_bool_names const *>( |
---|
| 395 | &use_facet<npc_type>(ss.getloc()) )) ); |
---|
| 396 | |
---|
| 397 | { |
---|
| 398 | boost::io::ios_locale_saver ils( ss, locale::classic() ); |
---|
| 399 | |
---|
| 400 | BOOST_CHECK( locale::classic() == ss.getloc() ); |
---|
| 401 | BOOST_CHECK( !has_facet<npc_type>(ss.getloc()) || (NULL |
---|
| 402 | == dynamic_cast<backward_bool_names const *>( |
---|
| 403 | &use_facet<npc_type>(ss.getloc()) )) ); |
---|
| 404 | |
---|
| 405 | ss.imbue( locale(locale::classic(), new backward_bool_names) ); |
---|
| 406 | BOOST_CHECK( locale::classic() != ss.getloc() ); |
---|
| 407 | BOOST_CHECK( has_facet<npc_type>(ss.getloc()) && (NULL |
---|
| 408 | != dynamic_cast<backward_bool_names const *>( |
---|
| 409 | &use_facet<npc_type>(ss.getloc()) )) ); |
---|
| 410 | //BOOST_CHECK( has_facet<backward_bool_names>(ss.getloc()) ); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 414 | BOOST_CHECK( !has_facet<npc_type>(ss.getloc()) || (NULL |
---|
| 415 | == dynamic_cast<backward_bool_names const *>( |
---|
| 416 | &use_facet<npc_type>(ss.getloc()) )) ); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | // Unit test for user-defined integer data saving |
---|
| 420 | void |
---|
| 421 | ios_iword_saver_unit_test |
---|
| 422 | ( |
---|
| 423 | ) |
---|
| 424 | { |
---|
| 425 | using namespace std; |
---|
| 426 | |
---|
| 427 | stringstream ss; |
---|
| 428 | |
---|
| 429 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 430 | |
---|
| 431 | { |
---|
| 432 | boost::io::ios_iword_saver iis( ss, word_index ); |
---|
| 433 | |
---|
| 434 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 435 | |
---|
| 436 | ss.iword( word_index ) = 6; |
---|
| 437 | BOOST_CHECK_EQUAL( 6, ss.iword(word_index) ); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 441 | |
---|
| 442 | { |
---|
| 443 | boost::io::ios_iword_saver iis( ss, word_index, 100 ); |
---|
| 444 | |
---|
| 445 | BOOST_CHECK_EQUAL( 100, ss.iword(word_index) ); |
---|
| 446 | |
---|
| 447 | ss.iword( word_index ) = -2000; |
---|
| 448 | BOOST_CHECK_EQUAL( -2000, ss.iword(word_index) ); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | // Unit test for user-defined pointer data saving |
---|
| 455 | void |
---|
| 456 | ios_pword_saver_unit_test |
---|
| 457 | ( |
---|
| 458 | ) |
---|
| 459 | { |
---|
| 460 | using namespace std; |
---|
| 461 | |
---|
| 462 | stringstream ss; |
---|
| 463 | |
---|
| 464 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 465 | |
---|
| 466 | { |
---|
| 467 | boost::io::ios_pword_saver ips( ss, word_index ); |
---|
| 468 | |
---|
| 469 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 470 | |
---|
| 471 | ss.pword( word_index ) = &ss; |
---|
| 472 | BOOST_CHECK_EQUAL( &ss, ss.pword(word_index) ); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 476 | |
---|
| 477 | { |
---|
| 478 | boost::io::ios_pword_saver ips( ss, word_index, ss.rdbuf() ); |
---|
| 479 | |
---|
| 480 | BOOST_CHECK_EQUAL( ss.rdbuf(), ss.pword(word_index) ); |
---|
| 481 | |
---|
| 482 | ss.pword( word_index ) = &ss; |
---|
| 483 | BOOST_CHECK_EQUAL( &ss, ss.pword(word_index) ); |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | // Unit test for all ios_base data saving |
---|
| 490 | void |
---|
| 491 | ios_base_all_saver_unit_test |
---|
| 492 | ( |
---|
| 493 | ) |
---|
| 494 | { |
---|
| 495 | using namespace std; |
---|
| 496 | |
---|
| 497 | stringstream ss; |
---|
| 498 | |
---|
| 499 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 500 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 501 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 502 | |
---|
| 503 | { |
---|
| 504 | boost::io::ios_base_all_saver ibas( ss ); |
---|
| 505 | |
---|
| 506 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 507 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 508 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 509 | |
---|
| 510 | ss << hex << unitbuf << setprecision( 5 ) << setw( 7 ); |
---|
| 511 | BOOST_CHECK_EQUAL( (ios_base::unitbuf | ios_base::hex |
---|
| 512 | | ios_base::skipws), ss.flags() ); |
---|
| 513 | BOOST_CHECK_EQUAL( 5, ss.precision() ); |
---|
| 514 | BOOST_CHECK_EQUAL( 7, ss.width() ); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 518 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 519 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | // Unit test for all basic_ios data saving |
---|
| 523 | void |
---|
| 524 | ios_all_saver_unit_test |
---|
| 525 | ( |
---|
| 526 | ) |
---|
| 527 | { |
---|
| 528 | using namespace std; |
---|
| 529 | |
---|
| 530 | typedef numpunct<char> npc_type; |
---|
| 531 | |
---|
| 532 | stringbuf sb; |
---|
| 533 | iostream ss( &sb ); |
---|
| 534 | |
---|
| 535 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 536 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 537 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 538 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 539 | BOOST_CHECK( ss.good() ); |
---|
| 540 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 541 | BOOST_CHECK( NULL == ss.tie() ); |
---|
| 542 | BOOST_CHECK( &sb == ss.rdbuf() ); |
---|
| 543 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 544 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 545 | |
---|
| 546 | { |
---|
| 547 | boost::io::ios_all_saver ias( ss ); |
---|
| 548 | |
---|
| 549 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 550 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 551 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 552 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 553 | BOOST_CHECK( ss.good() ); |
---|
| 554 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 555 | BOOST_CHECK( NULL == ss.tie() ); |
---|
| 556 | BOOST_CHECK( &sb == ss.rdbuf() ); |
---|
| 557 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 558 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 559 | |
---|
| 560 | ss << oct << showpos << noskipws; |
---|
| 561 | BOOST_CHECK_EQUAL( (ios_base::showpos | ios_base::oct), ss.flags() ); |
---|
| 562 | |
---|
| 563 | ss << setprecision( 3 ); |
---|
| 564 | BOOST_CHECK_EQUAL( 3, ss.precision() ); |
---|
| 565 | |
---|
| 566 | ss << setw( 9 ); |
---|
| 567 | BOOST_CHECK_EQUAL( 9, ss.width() ); |
---|
| 568 | |
---|
| 569 | ss.setstate( ios_base::eofbit ); |
---|
| 570 | BOOST_CHECK_EQUAL( ios_base::eofbit, ss.rdstate() ); |
---|
| 571 | BOOST_CHECK( ss.eof() ); |
---|
| 572 | |
---|
| 573 | ss.exceptions( ios_base::failbit ); |
---|
| 574 | BOOST_CHECK_EQUAL( ios_base::failbit, ss.exceptions() ); |
---|
| 575 | |
---|
| 576 | { |
---|
| 577 | boost::io::ios_iostate_saver iis( ss ); |
---|
| 578 | char c; |
---|
| 579 | |
---|
| 580 | BOOST_CHECK_THROW( ss >> c, std::ios_base::failure ); |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | ss.tie( &clog ); |
---|
| 584 | BOOST_CHECK_EQUAL( &clog, ss.tie() ); |
---|
| 585 | |
---|
| 586 | ss.rdbuf( cerr.rdbuf() ); |
---|
| 587 | BOOST_CHECK_EQUAL( cerr.rdbuf(), ss.rdbuf() ); |
---|
| 588 | |
---|
| 589 | ss << setfill( 'x' ); |
---|
| 590 | BOOST_CHECK_EQUAL( 'x', ss.fill() ); |
---|
| 591 | |
---|
| 592 | ss.imbue( locale(locale::classic(), new backward_bool_names) ); |
---|
| 593 | BOOST_CHECK( locale() != ss.getloc() ); |
---|
| 594 | BOOST_CHECK( has_facet<npc_type>(ss.getloc()) && (NULL |
---|
| 595 | != dynamic_cast<backward_bool_names const *>( |
---|
| 596 | &use_facet<npc_type>(ss.getloc()) )) ); |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); |
---|
| 600 | BOOST_CHECK_EQUAL( 6, ss.precision() ); |
---|
| 601 | BOOST_CHECK_EQUAL( 0, ss.width() ); |
---|
| 602 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); |
---|
| 603 | BOOST_CHECK( ss.good() ); |
---|
| 604 | BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); |
---|
| 605 | BOOST_CHECK( NULL == ss.tie() ); |
---|
| 606 | BOOST_CHECK( &sb == ss.rdbuf() ); |
---|
| 607 | BOOST_CHECK_EQUAL( ' ', ss.fill() ); |
---|
| 608 | BOOST_CHECK( locale() == ss.getloc() ); |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | // Unit test for user-defined data saving |
---|
| 612 | void |
---|
| 613 | ios_word_saver_unit_test |
---|
| 614 | ( |
---|
| 615 | ) |
---|
| 616 | { |
---|
| 617 | using namespace std; |
---|
| 618 | |
---|
| 619 | stringstream ss; |
---|
| 620 | |
---|
| 621 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 622 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 623 | |
---|
| 624 | { |
---|
| 625 | boost::io::ios_all_word_saver iaws( ss, word_index ); |
---|
| 626 | |
---|
| 627 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 628 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 629 | |
---|
| 630 | ss.iword( word_index ) = -11; |
---|
| 631 | ss.pword( word_index ) = ss.rdbuf(); |
---|
| 632 | BOOST_CHECK_EQUAL( -11, ss.iword(word_index) ); |
---|
| 633 | BOOST_CHECK_EQUAL( ss.rdbuf(), ss.pword(word_index) ); |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); |
---|
| 637 | BOOST_CHECK( NULL == ss.pword(word_index) ); |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | |
---|
| 641 | // Unit test program |
---|
| 642 | boost::unit_test_framework::test_suite * |
---|
| 643 | init_unit_test_suite |
---|
| 644 | ( |
---|
| 645 | int , // "argc" is unused |
---|
| 646 | char * [] // "argv" is unused |
---|
| 647 | ) |
---|
| 648 | { |
---|
| 649 | boost::unit_test_framework::test_suite * test |
---|
| 650 | = BOOST_TEST_SUITE( "I/O state saver test" ); |
---|
| 651 | |
---|
| 652 | test->add( BOOST_TEST_CASE(ios_flags_saver_unit_test) ); |
---|
| 653 | test->add( BOOST_TEST_CASE(ios_precision_saver_unit_test) ); |
---|
| 654 | test->add( BOOST_TEST_CASE(ios_width_saver_unit_test) ); |
---|
| 655 | |
---|
| 656 | test->add( BOOST_TEST_CASE(ios_iostate_saver_unit_test) ); |
---|
| 657 | test->add( BOOST_TEST_CASE(ios_exception_saver_unit_test) ); |
---|
| 658 | test->add( BOOST_TEST_CASE(ios_tie_saver_unit_test) ); |
---|
| 659 | test->add( BOOST_TEST_CASE(ios_rdbuf_saver_unit_test) ); |
---|
| 660 | test->add( BOOST_TEST_CASE(ios_fill_saver_unit_test) ); |
---|
| 661 | test->add( BOOST_TEST_CASE(ios_locale_saver_unit_test) ); |
---|
| 662 | |
---|
| 663 | test->add( BOOST_TEST_CASE(ios_iword_saver_unit_test) ); |
---|
| 664 | test->add( BOOST_TEST_CASE(ios_pword_saver_unit_test) ); |
---|
| 665 | |
---|
| 666 | test->add( BOOST_TEST_CASE(ios_base_all_saver_unit_test) ); |
---|
| 667 | test->add( BOOST_TEST_CASE(ios_all_saver_unit_test) ); |
---|
| 668 | test->add( BOOST_TEST_CASE(ios_word_saver_unit_test) ); |
---|
| 669 | |
---|
| 670 | return test; |
---|
| 671 | } |
---|