Clean up ios_state unit tests

This commit is contained in:
Glen Fernandes
2019-12-17 20:07:12 -05:00
parent 00b2270c66
commit c761bbc285
3 changed files with 518 additions and 834 deletions

View File

@ -1,43 +1,41 @@
// Boost io_fwd.hpp header file --------------------------------------------// /*
Copyright 2002 Daryle Walker
// Copyright 2002 Daryle Walker. Use, modification, and distribution are subject
// to the Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// See <http://www.boost.org/libs/io/> for the library's home page.
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_IO_FWD_HPP #ifndef BOOST_IO_FWD_HPP
#define BOOST_IO_FWD_HPP #define BOOST_IO_FWD_HPP
#include <iosfwd> // for std::char_traits (declaration) #include <iosfwd>
namespace boost {
namespace boost namespace io {
{
namespace io
{
// From <boost/io/ios_state.hpp> -------------------------------------------//
class ios_flags_saver; class ios_flags_saver;
class ios_precision_saver; class ios_precision_saver;
class ios_width_saver; class ios_width_saver;
class ios_base_all_saver; class ios_base_all_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> > template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_iostate_saver; class basic_ios_iostate_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> >
template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_exception_saver; class basic_ios_exception_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> >
template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_tie_saver; class basic_ios_tie_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> >
template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_rdbuf_saver; class basic_ios_rdbuf_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> >
template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_fill_saver; class basic_ios_fill_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> >
template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_locale_saver; class basic_ios_locale_saver;
template < typename Ch, class Tr = ::std::char_traits<Ch> >
template<class Ch, class Tr = std::char_traits<Ch> >
class basic_ios_all_saver; class basic_ios_all_saver;
typedef basic_ios_iostate_saver<char> ios_iostate_saver; typedef basic_ios_iostate_saver<char> ios_iostate_saver;
@ -59,9 +57,7 @@ class ios_iword_saver;
class ios_pword_saver; class ios_pword_saver;
class ios_all_word_saver; class ios_all_word_saver;
} /* io */
} /* boost */
} // namespace io #endif
} // namespace boost
#endif // BOOST_IO_FWD_HPP

View File

@ -1,264 +1,171 @@
// Boost ios_state_test.cpp test file --------------------------------------// /*
Copyright 2002, 2003 Daryle Walker
// Copyright 2002, 2003 Daryle Walker. Use, modification, and distribution are Copyright 2019 Glen Joseph Fernandes
// subject to the Boost Software License, Version 1.0. (See accompanying file (glenjofe@gmail.com)
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// Copyright 2019 Glen Joseph Fernandes Distributed under the Boost Software License, Version 1.0.
// (glenjofe@gmail.com) (http://www.boost.org/LICENSE_1_0.txt)
*/
// See <http://www.boost.org/libs/io/> for the library's home page. #include <boost/io/ios_state.hpp>
// Revision History
// 15 Jun 2003 Adjust to changes in Boost.Test (Daryle Walker)
// 26 Feb 2002 Initial version (Daryle Walker)
#include <boost/config.hpp>
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/io/ios_state.hpp> // for boost::io::ios_flags_saver, etc. #include <iomanip>
#include <ios>
#include <cstddef> // for std::size_t #include <istream>
#include <iomanip> // for std::setw #include <locale>
#include <ios> // for std::ios_base, std::streamsize, etc. #include <ostream>
#include <iostream> // for std::cout, etc. #include <sstream>
#include <istream> // for std::istream
#include <locale> // for std::numpunct, std::locale
#include <ostream> // for std::endl, std::ostream
#include <streambuf> // for std::streambuf
#include <string> // for std::string
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
#include <stdexcept> #include <stdexcept>
#endif #endif
#include <streambuf>
#include <string>
#include <cstddef>
// Facet with the bool names spelled backwards
class backward_bool_names class backward_bool_names
: public std::numpunct<char> : public std::numpunct<char> {
{
typedef std::numpunct<char> base_type; typedef std::numpunct<char> base_type;
public: public:
explicit backward_bool_names(std::size_t refs = 0) explicit backward_bool_names(std::size_t refs = 0)
: base_type( refs ) : base_type( refs ) { }
{}
protected: protected:
virtual ~backward_bool_names() {} ~backward_bool_names() { }
virtual base_type::string_type do_truename() const base_type::string_type do_truename() const {
{ return "eurt"; } return "eurt";
virtual base_type::string_type do_falsename() const }
{ return "eslaf"; }
base_type::string_type do_falsename() const {
return "eslaf";
}
}; };
void saver_tests_1(int index,
// Index to test custom storage
int const my_index = std::ios_base::xalloc();
// Test data
char const test_string[] = "Hello world";
int const test_num1 = -16;
double const test_num2 = 34.5678901234;
bool const test_bool = true;
// Function prototypes
void saver_tests_1( std::istream &input, std::ostream &output,
std::ostream &err );
void saver_tests_2( std::istream &input, std::ostream &output,
std::ostream &err );
// Test program
int main()
{
using std::cout;
using std::ios_base;
using std::streamsize;
using std::cin;
cout << "The original data is:\n";
cout << '\t' << test_string << '\n';
cout << '\t' << test_num1 << '\n';
cout << '\t' << test_num2 << '\n';
cout << '\t' << std::boolalpha << test_bool << std::endl;
// Save states for comparison later
ios_base::fmtflags const cout_flags = cout.flags();
streamsize const cout_precision = cout.precision();
streamsize const cout_width = cout.width();
ios_base::iostate const cout_iostate = cout.rdstate();
ios_base::iostate const cout_exceptions = cout.exceptions();
std::ostream * const cin_tie = cin.tie();
std::streambuf * const cout_sb = cout.rdbuf();
char const cout_fill = cout.fill();
std::locale const cout_locale = cout.getloc();
cout.iword( my_index ) = 42L;
cout.pword( my_index ) = &cin;
// Run saver tests with changing separate from saving
saver_tests_1( cin, cout, std::cerr );
// Check if states are back to normal
BOOST_TEST( &cin == cout.pword(my_index) );
BOOST_TEST( 42L == cout.iword(my_index) );
BOOST_TEST( cout_locale == cout.getloc() );
BOOST_TEST( cout_fill == cout.fill() );
BOOST_TEST( cout_sb == cout.rdbuf() );
BOOST_TEST( cin_tie == cin.tie() );
BOOST_TEST( cout_exceptions == cout.exceptions() );
BOOST_TEST( cout_iostate == cout.rdstate() );
BOOST_TEST( cout_width == cout.width() );
BOOST_TEST( cout_precision == cout.precision() );
BOOST_TEST( cout_flags == cout.flags() );
// Run saver tests with combined saving and changing
saver_tests_2( cin, cout, std::cerr );
// Check if states are back to normal
BOOST_TEST( &cin == cout.pword(my_index) );
BOOST_TEST( 42L == cout.iword(my_index) );
BOOST_TEST( cout_locale == cout.getloc() );
BOOST_TEST( cout_fill == cout.fill() );
BOOST_TEST( cout_sb == cout.rdbuf() );
BOOST_TEST( cin_tie == cin.tie() );
BOOST_TEST( cout_exceptions == cout.exceptions() );
BOOST_TEST( cout_iostate == cout.rdstate() );
BOOST_TEST( cout_width == cout.width() );
BOOST_TEST( cout_precision == cout.precision() );
BOOST_TEST( cout_flags == cout.flags() );
return boost::report_errors();
}
// Save, change, and restore stream properties
void
saver_tests_1
(
std::istream& input, std::istream& input,
std::ostream& output, std::ostream& output,
std::ostream & err std::ostream& err)
)
{ {
using std::locale; boost::io::ios_flags_saver ifls(output);
using std::ios_base; boost::io::ios_precision_saver iprs(output);
using std::setw; boost::io::ios_width_saver iws(output);
boost::io::ios_tie_saver its(input);
boost::io::ios_flags_saver const ifls( output ); boost::io::ios_rdbuf_saver irs(output);
boost::io::ios_precision_saver const iprs( output ); boost::io::ios_fill_saver ifis(output);
boost::io::ios_width_saver const iws( output ); boost::io::ios_locale_saver ils(output);
boost::io::ios_tie_saver const its( input ); boost::io::ios_iword_saver iis(output, index);
boost::io::ios_rdbuf_saver const irs( output ); boost::io::ios_pword_saver ipws(output, index);
boost::io::ios_fill_saver const ifis( output ); std::locale loc(std::locale::classic(), new backward_bool_names);
boost::io::ios_locale_saver const ils( output );
boost::io::ios_iword_saver const iis( output, my_index );
boost::io::ios_pword_saver const ipws( output, my_index );
locale loc( locale::classic(), new backward_bool_names );
input.tie(&err); input.tie(&err);
output.rdbuf(err.rdbuf()); output.rdbuf(err.rdbuf());
output.iword( my_index ) = 69L; output.iword(index) = 69L;
output.pword( my_index ) = &err; output.pword(index) = &err;
output.setf(std::ios_base::showpos | std::ios_base::boolalpha);
output << "The data is (again):\n"; output.setf(std::ios_base::internal, std::ios_base::adjustfield);
output.setf( ios_base::showpos | ios_base::boolalpha );
output.setf( ios_base::internal, ios_base::adjustfield );
output.fill('@'); output.fill('@');
output.precision( 9 ); output.precision( 9 );
output << '\t' << test_string << '\n'; output << "Hello world";
output << '\t' << setw( 10 ) << test_num1 << '\n'; output << std::setw(10) << -16;
output << '\t' << setw( 15 ) << test_num2 << '\n'; output << std::setw(15) << 34.5678901234;
output.imbue(loc); output.imbue(loc);
output << '\t' << test_bool << '\n'; output << true;
BOOST_TEST(&err == output.pword(index));
BOOST_TEST( &err == output.pword(my_index) ); BOOST_TEST(69L == output.iword(index));
BOOST_TEST( 69L == output.iword(my_index) ); try {
boost::io::ios_exception_saver ies(output);
try boost::io::ios_iostate_saver iis(output);
{ output.exceptions(std::ios_base::eofbit | std::ios_base::badbit);
boost::io::ios_exception_saver const ies( output ); output.setstate(std::ios_base::eofbit);
boost::io::ios_iostate_saver const iis( output );
output.exceptions( ios_base::eofbit | ios_base::badbit );
output.setstate( ios_base::eofbit );
BOOST_ERROR("previous line should have thrown"); BOOST_ERROR("previous line should have thrown");
}
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
catch ( std::exception &f ) } catch (std::exception&) {
#else #else
catch ( ios_base::failure &f ) } catch (std::ios_base::failure&) {
#endif #endif
{ BOOST_TEST(output.exceptions() == std::ios_base::goodbit );
err << "Got the expected I/O failure: \"" << f.what() << "\".\n";
BOOST_TEST( output.exceptions() == ios_base::goodbit );
}
catch ( ... )
{
err << "Got an unknown error when doing exception test!\n";
throw;
} }
} }
// Save & change and restore stream properties void saver_tests_2(int index,
void
saver_tests_2
(
std::istream& input, std::istream& input,
std::ostream& output, std::ostream& output,
std::ostream & err std::ostream& err)
)
{ {
using std::locale; boost::io::ios_tie_saver its(input, &err);
using std::ios_base; boost::io::ios_rdbuf_saver irs(output, err.rdbuf());
boost::io::ios_iword_saver iis(output, index, 69L);
boost::io::ios_tie_saver const its( input, &err ); boost::io::ios_pword_saver ipws(output, index, &err);
boost::io::ios_rdbuf_saver const irs( output, err.rdbuf() ); boost::io::ios_flags_saver ifls(output,
boost::io::ios_iword_saver const iis( output, my_index, 69L ); (output.flags() & ~std::ios_base::adjustfield) |
boost::io::ios_pword_saver const ipws( output, my_index, &err ); std::ios_base::showpos |
output << "The data is (a third time; adding the numbers):\n"; std::ios_base::boolalpha |
(std::ios_base::internal & std::ios_base::adjustfield));
boost::io::ios_flags_saver const ifls( output, (output.flags() boost::io::ios_precision_saver iprs(output, 9);
& ~ios_base::adjustfield) | ios_base::showpos | ios_base::boolalpha boost::io::ios_fill_saver ifis(output, '@');
| (ios_base::internal & ios_base::adjustfield) ); output << "Hello world";
boost::io::ios_precision_saver const iprs( output, 9 ); boost::io::ios_width_saver iws(output, 12);
boost::io::ios_fill_saver const ifis( output, '@' ); output << -16 + 34.5678901234;
output << '\t' << test_string << '\n'; std::locale loc(std::locale::classic(), new backward_bool_names);
boost::io::ios_locale_saver ils(output, loc);
boost::io::ios_width_saver const iws( output, 12 ); output << true;
output.put( '\t' ); BOOST_TEST(&err == output.pword(index));
output << test_num1 + test_num2; BOOST_TEST(69L == output.iword(index));
output.put( '\n' ); try {
boost::io::ios_exception_saver ies(output, std::ios_base::eofbit);
locale loc( locale::classic(), boost::io::ios_iostate_saver iis(output,
new backward_bool_names ); output.rdstate() | std::ios_base::eofbit );
boost::io::ios_locale_saver const ils( output, loc );
output << '\t' << test_bool << '\n';
BOOST_TEST( &err == output.pword(my_index) );
BOOST_TEST( 69L == output.iword(my_index) );
try
{
boost::io::ios_exception_saver const ies( output, ios_base::eofbit );
boost::io::ios_iostate_saver const iis( output, output.rdstate()
| ios_base::eofbit );
BOOST_ERROR("previous line should have thrown"); BOOST_ERROR("previous line should have thrown");
}
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
catch ( std::exception &f ) } catch (std::exception&) {
#else #else
catch ( ios_base::failure &f ) } catch (std::ios_base::failure&) {
#endif #endif
BOOST_TEST(output.exceptions() == std::ios_base::goodbit);
}
}
int main()
{ {
err << "Got the expected I/O failure: \"" << f.what() << "\".\n"; int index = std::ios_base::xalloc();
BOOST_TEST( output.exceptions() == ios_base::goodbit ); std::ostringstream out;
} std::ostringstream err;
catch ( ... ) std::istringstream in;
{ std::ios_base::fmtflags out_flags = out.flags();
err << "Got an unknown error when doing exception test!\n"; std::streamsize out_precision = out.precision();
throw; std::streamsize out_width = out.width();
} std::ios_base::iostate out_iostate = out.rdstate();
std::ios_base::iostate out_exceptions = out.exceptions();
std::ostream* in_tie = in.tie();
std::streambuf* out_sb = out.rdbuf();
char out_fill = out.fill();
std::locale out_locale = out.getloc();
out.iword(index) = 42L;
out.pword(index) = &in;
saver_tests_1(index, in, out, err);
BOOST_TEST(&in == out.pword(index));
BOOST_TEST(42L == out.iword(index));
BOOST_TEST(out_locale == out.getloc());
BOOST_TEST(out_fill == out.fill());
BOOST_TEST(out_sb == out.rdbuf());
BOOST_TEST(in_tie == in.tie());
BOOST_TEST(out_exceptions == out.exceptions());
BOOST_TEST(out_iostate == out.rdstate());
BOOST_TEST(out_width == out.width());
BOOST_TEST(out_precision == out.precision());
BOOST_TEST(out_flags == out.flags());
saver_tests_2(index, in, out, err);
BOOST_TEST(&in == out.pword(index));
BOOST_TEST(42L == out.iword(index));
BOOST_TEST(out_locale == out.getloc());
BOOST_TEST(out_fill == out.fill());
BOOST_TEST(out_sb == out.rdbuf());
BOOST_TEST(in_tie == in.tie());
BOOST_TEST(out_exceptions == out.exceptions());
BOOST_TEST(out_iostate == out.rdstate());
BOOST_TEST(out_width == out.width());
BOOST_TEST(out_precision == out.precision());
BOOST_TEST(out_flags == out.flags());
return boost::report_errors();
} }

View File

@ -1,241 +1,164 @@
// Boost ios_state_unit_test.cpp test file ---------------------------------// /*
Copyright 2003 Daryle Walker
// Copyright 2003 Daryle Walker. Use, modification, and distribution are Copyright 2019 Glen Joseph Fernandes
// subject to the Boost Software License, Version 1.0. (See accompanying file (glenjofe@gmail.com)
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// Copyright 2019 Glen Joseph Fernandes Distributed under the Boost Software License, Version 1.0.
// (glenjofe@gmail.com) (http://www.boost.org/LICENSE_1_0.txt)
*/
// See <http://www.boost.org/libs/io/> for the library's home page. #include <boost/io/ios_state.hpp>
// Revision History
// 12 Sep 2003 Initial version (Daryle Walker)
#include <boost/config.hpp>
#include <boost/io/ios_state.hpp> // for boost::io::ios_flags_saver, etc.
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <cstddef> // for NULL #include <iomanip>
#include <iomanip> // for std::setiosflags, etc. #include <ios>
#include <ios> // for std::ios_base #include <iostream>
#include <iostream> // for std::cout, std::cerr, etc. #include <istream>
#include <istream> // for std::iostream #include <locale>
#include <locale> // for std::locale, std::numpunct
#include <sstream> // for std::stringstream, etc.
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
#include <stdexcept> #include <stdexcept>
#endif #endif
#include <sstream>
#include <cstddef>
// Global constants
int const word_index = std::ios_base::xalloc();
// Facet with the (classic) bool names spelled backwards
class backward_bool_names class backward_bool_names
: public std::numpunct<char> : public std::numpunct<char> {
{
typedef std::numpunct<char> base_type; typedef std::numpunct<char> base_type;
public: public:
explicit backward_bool_names(std::size_t refs = 0) explicit backward_bool_names(std::size_t refs = 0)
: base_type( refs ) : base_type(refs) { }
{}
protected: protected:
virtual ~backward_bool_names() {} ~backward_bool_names() { }
virtual base_type::string_type do_truename() const base_type::string_type do_truename() const {
{ return "eurt"; } return "eurt";
virtual base_type::string_type do_falsename() const }
{ return "eslaf"; }
base_type::string_type do_falsename() const {
return "eslaf";
}
}; };
// Unit test for format-flag saving
void void
ios_flags_saver_unit_test ios_flags_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
stringstream ss;
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
{ {
boost::io::ios_flags_saver ifs(ss); boost::io::ios_flags_saver ifs(ss);
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() ); ss << std::noskipws << std::fixed << std::boolalpha;
BOOST_TEST_EQ(std::ios_base::boolalpha |
ss << noskipws << fixed << boolalpha; std::ios_base::dec |
BOOST_TEST_EQ( (ios_base::boolalpha | ios_base::dec std::ios_base::fixed, ss.flags());
| ios_base::fixed), ss.flags() );
} }
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
{ {
boost::io::ios_flags_saver ifs( ss, (ios_base::showbase boost::io::ios_flags_saver ifs(ss,
| ios_base::internal) ); std::ios_base::showbase | std::ios_base::internal);
BOOST_TEST_EQ(std::ios_base::showbase |
BOOST_TEST_EQ( (ios_base::showbase | ios_base::internal), std::ios_base::internal, ss.flags());
ss.flags() ); ss << std::setiosflags(std::ios_base::unitbuf);
BOOST_TEST_EQ(std::ios_base::showbase |
ss << setiosflags( ios_base::unitbuf ); std::ios_base::internal |
BOOST_TEST_EQ( (ios_base::showbase | ios_base::internal std::ios_base::unitbuf, ss.flags());
| ios_base::unitbuf), ss.flags() ); }
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
} }
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
}
// Unit test for precision saving
void void
ios_precision_saver_unit_test ios_precision_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
stringstream ss;
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
{ {
boost::io::ios_precision_saver ips(ss); boost::io::ios_precision_saver ips(ss);
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
ss << std::setprecision(4);
ss << setprecision( 4 );
BOOST_TEST_EQ(4, ss.precision()); BOOST_TEST_EQ(4, ss.precision());
} }
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
{ {
boost::io::ios_precision_saver ips(ss, 8); boost::io::ios_precision_saver ips(ss, 8);
BOOST_TEST_EQ(8, ss.precision()); BOOST_TEST_EQ(8, ss.precision());
ss << std::setprecision(10);
ss << setprecision( 10 );
BOOST_TEST_EQ(10, ss.precision()); BOOST_TEST_EQ(10, ss.precision());
} }
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
} }
// Unit test for width saving
void void
ios_width_saver_unit_test ios_width_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
stringstream ss;
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
{ {
boost::io::ios_width_saver iws(ss); boost::io::ios_width_saver iws(ss);
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
ss << std::setw(4);
ss << setw( 4 );
BOOST_TEST_EQ(4, ss.width()); BOOST_TEST_EQ(4, ss.width());
} }
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
{ {
boost::io::ios_width_saver iws(ss, 8); boost::io::ios_width_saver iws(ss, 8);
BOOST_TEST_EQ(8, ss.width()); BOOST_TEST_EQ(8, ss.width());
ss << std::setw(10);
ss << setw( 10 );
BOOST_TEST_EQ(10, ss.width()); BOOST_TEST_EQ(10, ss.width());
} }
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
} }
// Unit test for I/O-state saving
void void
ios_iostate_saver_unit_test ios_iostate_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
stringstream ss;
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() );
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
{ {
boost::io::ios_iostate_saver iis(ss); boost::io::ios_iostate_saver iis(ss);
char c; char c;
BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() );
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
ss >> c; ss >> c;
BOOST_TEST_EQ( (ios_base::eofbit | ios_base::failbit), BOOST_TEST_EQ(std::ios_base::eofbit | std::ios_base::failbit,
ss.rdstate()); ss.rdstate());
BOOST_TEST(ss.eof()); BOOST_TEST(ss.eof());
BOOST_TEST(ss.fail()); BOOST_TEST(ss.fail());
BOOST_TEST(!ss.bad()); BOOST_TEST(!ss.bad());
} }
BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() );
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
{ {
boost::io::ios_iostate_saver iis( ss, ios_base::eofbit ); boost::io::ios_iostate_saver iis(ss, std::ios_base::eofbit);
BOOST_TEST_EQ(std::ios_base::eofbit, ss.rdstate());
BOOST_TEST_EQ( ios_base::eofbit, ss.rdstate() );
BOOST_TEST(ss.eof()); BOOST_TEST(ss.eof());
ss.setstate(std::ios_base::badbit);
ss.setstate( ios_base::badbit ); BOOST_TEST_EQ(std::ios_base::eofbit | std::ios_base::badbit,
BOOST_TEST_EQ( (ios_base::eofbit | ios_base::badbit),
ss.rdstate()); ss.rdstate());
BOOST_TEST(ss.eof()); BOOST_TEST(ss.eof());
BOOST_TEST(ss.fail()); BOOST_TEST(ss.fail());
BOOST_TEST(ss.bad()); BOOST_TEST(ss.bad());
} }
BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() );
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
} }
// Unit test for exception-flag saving
void void
ios_exception_saver_unit_test ios_exception_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
stringstream ss;
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() );
{ {
boost::io::ios_exception_saver ies(ss); boost::io::ios_exception_saver ies(ss);
BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() ); ss.exceptions(std::ios_base::failbit);
BOOST_TEST_EQ(std::ios_base::failbit, ss.exceptions());
ss.exceptions( ios_base::failbit );
BOOST_TEST_EQ( ios_base::failbit, ss.exceptions() );
{ {
boost::io::ios_iostate_saver iis(ss); boost::io::ios_iostate_saver iis(ss);
ss.exceptions( ios_base::failbit | ios_base::badbit ); ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
char c; char c;
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
BOOST_TEST_THROWS(ss >> c, std::exception); BOOST_TEST_THROWS(ss >> c, std::exception);
#else #else
@ -243,438 +166,296 @@ ios_exception_saver_unit_test
#endif #endif
} }
} }
BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() );
{ {
boost::io::ios_exception_saver ies( ss, ios_base::eofbit ); boost::io::ios_exception_saver ies(ss, std::ios_base::eofbit);
BOOST_TEST_EQ(std::ios_base::eofbit, ss.exceptions());
BOOST_TEST_EQ( ios_base::eofbit, ss.exceptions() ); ss.exceptions(std::ios_base::badbit);
BOOST_TEST_EQ(std::ios_base::badbit, ss.exceptions());
ss.exceptions( ios_base::badbit );
BOOST_TEST_EQ( ios_base::badbit, ss.exceptions() );
{ {
boost::io::ios_iostate_saver iis(ss); boost::io::ios_iostate_saver iis(ss);
char c; char c;
BOOST_TEST_NOT(ss >> c); BOOST_TEST_NOT(ss >> c);
} }
} }
BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() );
} }
// Unit test for tied-stream saving
void void
ios_tie_saver_unit_test ios_tie_saver_unit_test(int index)
(
)
{ {
using namespace std; BOOST_TEST(NULL == std::cout.tie());
BOOST_TEST( NULL == cout.tie() );
{ {
boost::io::ios_tie_saver its( cout ); boost::io::ios_tie_saver its(std::cout);
BOOST_TEST(NULL == std::cout.tie());
BOOST_TEST( NULL == cout.tie() ); std::cout.tie(&std::clog);
BOOST_TEST_EQ(&std::clog, std::cout.tie());
cout.tie( &clog ); }
BOOST_TEST_EQ( &clog, cout.tie() ); BOOST_TEST(NULL == std::cout.tie());
{
boost::io::ios_tie_saver its(std::cout, &std::clog);
BOOST_TEST_EQ(&std::clog, std::cout.tie());
std::cout.tie(&std::cerr);
BOOST_TEST_EQ(&std::cerr, std::cout.tie());
}
BOOST_TEST(NULL == std::cout.tie());
} }
BOOST_TEST( NULL == cout.tie() );
{
boost::io::ios_tie_saver its( cout, &clog );
BOOST_TEST_EQ( &clog, cout.tie() );
cout.tie( &cerr );
BOOST_TEST_EQ( &cerr, cout.tie() );
}
BOOST_TEST( NULL == cout.tie() );
}
// Unit test for connected-streambuf saving
void void
ios_rdbuf_saver_unit_test ios_rdbuf_saver_unit_test(int index)
(
)
{ {
using namespace std; std::iostream s(NULL);
iostream s( NULL );
BOOST_TEST(NULL == s.rdbuf()); BOOST_TEST(NULL == s.rdbuf());
{ {
stringbuf sb; std::stringbuf sb;
boost::io::ios_rdbuf_saver irs(s); boost::io::ios_rdbuf_saver irs(s);
BOOST_TEST(NULL == s.rdbuf()); BOOST_TEST(NULL == s.rdbuf());
s.rdbuf(&sb); s.rdbuf(&sb);
BOOST_TEST_EQ(&sb, s.rdbuf()); BOOST_TEST_EQ(&sb, s.rdbuf());
} }
BOOST_TEST(NULL == s.rdbuf()); BOOST_TEST(NULL == s.rdbuf());
{ {
stringbuf sb1, sb2( "Hi there" ); std::stringbuf sb1, sb2("Hi there");
boost::io::ios_rdbuf_saver irs(s, &sb1); boost::io::ios_rdbuf_saver irs(s, &sb1);
BOOST_TEST_EQ(&sb1, s.rdbuf()); BOOST_TEST_EQ(&sb1, s.rdbuf());
s.rdbuf(&sb2); s.rdbuf(&sb2);
BOOST_TEST_EQ(&sb2, s.rdbuf()); BOOST_TEST_EQ(&sb2, s.rdbuf());
} }
BOOST_TEST(NULL == s.rdbuf()); BOOST_TEST(NULL == s.rdbuf());
} }
// Unit test for fill-character saving
void void
ios_fill_saver_unit_test ios_fill_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
stringstream ss;
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
{ {
boost::io::ios_fill_saver ifs(ss); boost::io::ios_fill_saver ifs(ss);
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
ss.fill('x'); ss.fill('x');
BOOST_TEST_EQ('x', ss.fill()); BOOST_TEST_EQ('x', ss.fill());
} }
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
{ {
boost::io::ios_fill_saver ifs(ss, '3'); boost::io::ios_fill_saver ifs(ss, '3');
BOOST_TEST_EQ('3', ss.fill()); BOOST_TEST_EQ('3', ss.fill());
ss.fill('+'); ss.fill('+');
BOOST_TEST_EQ('+', ss.fill()); BOOST_TEST_EQ('+', ss.fill());
} }
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
} }
// Unit test for locale saving
void void
ios_locale_saver_unit_test ios_locale_saver_unit_test(int index)
(
)
{ {
using namespace std; typedef std::numpunct<char> npc_type;
std::stringstream ss;
typedef numpunct<char> npc_type; BOOST_TEST(std::locale() == ss.getloc());
BOOST_TEST(!std::has_facet<npc_type>(ss.getloc()) ||
stringstream ss; !dynamic_cast<const backward_bool_names*>(&
std::use_facet<npc_type>(ss.getloc())));
BOOST_TEST( locale() == ss.getloc() );
// locales are unprintable, so no BOOST_TEST_EQ
BOOST_TEST( !has_facet<npc_type>(ss.getloc()) || (NULL
== dynamic_cast<backward_bool_names const *>(
&use_facet<npc_type>(ss.getloc()) )) );
// my implementation of has_facet just checks IDs, but doesn't do dynamic
// cast (therefore if a specifc facet type is missing, but its base class
// is available, has_facet will mistakenly[?] match), so I have to do it
// here. I wanted: "BOOST_TEST( ! has_facet< backward_bool_names >(
// ss.getloc() ) )"
{ {
boost::io::ios_locale_saver ils(ss); boost::io::ios_locale_saver ils(ss);
BOOST_TEST(std::locale() == ss.getloc());
BOOST_TEST( locale() == ss.getloc() ); ss.imbue(std::locale::classic());
BOOST_TEST(std::locale::classic() == ss.getloc());
ss.imbue( locale::classic() );
BOOST_TEST( locale::classic() == ss.getloc() );
} }
BOOST_TEST(std::locale() == ss.getloc());
BOOST_TEST( locale() == ss.getloc() ); BOOST_TEST(!std::has_facet<npc_type>(ss.getloc()) ||
BOOST_TEST( !has_facet<npc_type>(ss.getloc()) || (NULL !dynamic_cast<const backward_bool_names*>(&
== dynamic_cast<backward_bool_names const *>( std::use_facet<npc_type>(ss.getloc())));
&use_facet<npc_type>(ss.getloc()) )) );
{ {
boost::io::ios_locale_saver ils( ss, locale::classic() ); boost::io::ios_locale_saver ils(ss, std::locale::classic());
BOOST_TEST(std::locale::classic() == ss.getloc());
BOOST_TEST( locale::classic() == ss.getloc() ); BOOST_TEST(!std::has_facet<npc_type>(ss.getloc()) ||
BOOST_TEST( !has_facet<npc_type>(ss.getloc()) || (NULL !dynamic_cast<const backward_bool_names*>(&
== dynamic_cast<backward_bool_names const *>( std::use_facet<npc_type>(ss.getloc())));
&use_facet<npc_type>(ss.getloc()) )) ); ss.imbue(std::locale(std::locale::classic(), new backward_bool_names));
BOOST_TEST(std::locale::classic() != ss.getloc());
ss.imbue( locale(locale::classic(), new backward_bool_names) ); BOOST_TEST(std::has_facet<npc_type>(ss.getloc()) &&
BOOST_TEST( locale::classic() != ss.getloc() ); dynamic_cast<const backward_bool_names*>(&
BOOST_TEST( has_facet<npc_type>(ss.getloc()) && (NULL std::use_facet<npc_type>(ss.getloc())));
!= dynamic_cast<backward_bool_names const *>( }
&use_facet<npc_type>(ss.getloc()) )) ); BOOST_TEST(std::locale() == ss.getloc());
//BOOST_TEST( has_facet<backward_bool_names>(ss.getloc()) ); BOOST_TEST(!std::has_facet<npc_type>(ss.getloc()) ||
!dynamic_cast<const backward_bool_names*>(&
std::use_facet<npc_type>(ss.getloc())));
} }
BOOST_TEST( locale() == ss.getloc() );
BOOST_TEST( !has_facet<npc_type>(ss.getloc()) || (NULL
== dynamic_cast<backward_bool_names const *>(
&use_facet<npc_type>(ss.getloc()) )) );
}
// Unit test for user-defined integer data saving
void void
ios_iword_saver_unit_test ios_iword_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST_EQ(0, ss.iword(index));
stringstream ss;
BOOST_TEST_EQ( 0, ss.iword(word_index) );
{ {
boost::io::ios_iword_saver iis( ss, word_index ); boost::io::ios_iword_saver iis(ss, index);
BOOST_TEST_EQ(0, ss.iword(index));
BOOST_TEST_EQ( 0, ss.iword(word_index) ); ss.iword(index) = 6;
BOOST_TEST_EQ(6, ss.iword(index));
ss.iword( word_index ) = 6; }
BOOST_TEST_EQ( 6, ss.iword(word_index) ); BOOST_TEST_EQ(0, ss.iword(index));
{
boost::io::ios_iword_saver iis(ss, index, 100);
BOOST_TEST_EQ(100, ss.iword(index));
ss.iword(index) = -2000;
BOOST_TEST_EQ(-2000, ss.iword(index));
}
BOOST_TEST_EQ(0, ss.iword(index));
} }
BOOST_TEST_EQ( 0, ss.iword(word_index) );
{
boost::io::ios_iword_saver iis( ss, word_index, 100 );
BOOST_TEST_EQ( 100, ss.iword(word_index) );
ss.iword( word_index ) = -2000;
BOOST_TEST_EQ( -2000, ss.iword(word_index) );
}
BOOST_TEST_EQ( 0, ss.iword(word_index) );
}
// Unit test for user-defined pointer data saving
void void
ios_pword_saver_unit_test ios_pword_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST(NULL == ss.pword(index));
stringstream ss;
BOOST_TEST( NULL == ss.pword(word_index) );
{ {
boost::io::ios_pword_saver ips( ss, word_index ); boost::io::ios_pword_saver ips(ss, index);
BOOST_TEST(NULL == ss.pword(index));
BOOST_TEST( NULL == ss.pword(word_index) ); ss.pword(index) = &ss;
BOOST_TEST_EQ(&ss, ss.pword(index));
ss.pword( word_index ) = &ss; }
BOOST_TEST_EQ( &ss, ss.pword(word_index) ); BOOST_TEST(NULL == ss.pword(index));
{
boost::io::ios_pword_saver ips(ss, index, ss.rdbuf());
BOOST_TEST_EQ(ss.rdbuf(), ss.pword(index));
ss.pword(index) = &ss;
BOOST_TEST_EQ(&ss, ss.pword(index));
}
BOOST_TEST(NULL == ss.pword(index));
} }
BOOST_TEST( NULL == ss.pword(word_index) );
{
boost::io::ios_pword_saver ips( ss, word_index, ss.rdbuf() );
BOOST_TEST_EQ( ss.rdbuf(), ss.pword(word_index) );
ss.pword( word_index ) = &ss;
BOOST_TEST_EQ( &ss, ss.pword(word_index) );
}
BOOST_TEST( NULL == ss.pword(word_index) );
}
// Unit test for all ios_base data saving
void void
ios_base_all_saver_unit_test ios_base_all_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
stringstream ss;
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
{ {
boost::io::ios_base_all_saver ibas(ss); boost::io::ios_base_all_saver ibas(ss);
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
ss << std::hex << std::unitbuf << std::setprecision(5) << std::setw(7);
ss << hex << unitbuf << setprecision( 5 ) << setw( 7 ); BOOST_TEST_EQ(std::ios_base::unitbuf |
BOOST_TEST_EQ( (ios_base::unitbuf | ios_base::hex std::ios_base::hex |
| ios_base::skipws), ss.flags() ); std::ios_base::skipws, ss.flags());
BOOST_TEST_EQ(5, ss.precision()); BOOST_TEST_EQ(5, ss.precision());
BOOST_TEST_EQ(7, ss.width()); BOOST_TEST_EQ(7, ss.width());
} }
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
} }
// Unit test for all basic_ios data saving
void void
ios_all_saver_unit_test ios_all_saver_unit_test(int index)
(
)
{ {
using namespace std; typedef std::numpunct<char> npc_type;
std::stringbuf sb;
typedef numpunct<char> npc_type; std::iostream ss(&sb);
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
stringbuf sb;
iostream ss( &sb );
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() ); BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() ); BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
BOOST_TEST(NULL == ss.tie()); BOOST_TEST(NULL == ss.tie());
BOOST_TEST(&sb == ss.rdbuf()); BOOST_TEST(&sb == ss.rdbuf());
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
BOOST_TEST( locale() == ss.getloc() ); BOOST_TEST(std::locale() == ss.getloc());
{ {
boost::io::ios_all_saver ias(ss); boost::io::ios_all_saver ias(ss);
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() ); BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() ); BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
BOOST_TEST(NULL == ss.tie()); BOOST_TEST(NULL == ss.tie());
BOOST_TEST(&sb == ss.rdbuf()); BOOST_TEST(&sb == ss.rdbuf());
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
BOOST_TEST( locale() == ss.getloc() ); BOOST_TEST(std::locale() == ss.getloc());
ss << std::oct << std::showpos << std::noskipws;
ss << oct << showpos << noskipws; BOOST_TEST_EQ(std::ios_base::showpos | std::ios_base::oct, ss.flags());
BOOST_TEST_EQ( (ios_base::showpos | ios_base::oct), ss.flags() ); ss << std::setprecision(3);
ss << setprecision( 3 );
BOOST_TEST_EQ(3, ss.precision()); BOOST_TEST_EQ(3, ss.precision());
ss << std::setw(9);
ss << setw( 9 );
BOOST_TEST_EQ(9, ss.width()); BOOST_TEST_EQ(9, ss.width());
ss.setstate(std::ios_base::eofbit);
ss.setstate( ios_base::eofbit ); BOOST_TEST_EQ(std::ios_base::eofbit, ss.rdstate());
BOOST_TEST_EQ( ios_base::eofbit, ss.rdstate() );
BOOST_TEST(ss.eof()); BOOST_TEST(ss.eof());
ss.exceptions(std::ios_base::failbit);
ss.exceptions( ios_base::failbit ); BOOST_TEST_EQ(std::ios_base::failbit, ss.exceptions());
BOOST_TEST_EQ( ios_base::failbit, ss.exceptions() );
{ {
boost::io::ios_iostate_saver iis(ss); boost::io::ios_iostate_saver iis(ss);
ss.exceptions( ios_base::failbit | ios_base::badbit ); ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
char c; char c;
#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) #if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB))
BOOST_TEST_THROWS(ss >> c, std::exception); BOOST_TEST_THROWS(ss >> c, std::exception);
#else #else
BOOST_TEST_THROWS(ss >> c, std::ios_base::failure); BOOST_TEST_THROWS(ss >> c, std::ios_base::failure);
#endif #endif
} }
ss.tie(&std::clog);
ss.tie( &clog ); BOOST_TEST_EQ(&std::clog, ss.tie());
BOOST_TEST_EQ( &clog, ss.tie() ); ss.rdbuf(std::cerr.rdbuf());
BOOST_TEST_EQ(std::cerr.rdbuf(), ss.rdbuf());
ss.rdbuf( cerr.rdbuf() ); ss << std::setfill('x');
BOOST_TEST_EQ( cerr.rdbuf(), ss.rdbuf() );
ss << setfill( 'x' );
BOOST_TEST_EQ('x', ss.fill()); BOOST_TEST_EQ('x', ss.fill());
ss.imbue(std::locale(std::locale::classic(), new backward_bool_names));
ss.imbue( locale(locale::classic(), new backward_bool_names) ); BOOST_TEST(std::locale() != ss.getloc());
BOOST_TEST( locale() != ss.getloc() ); BOOST_TEST(std::has_facet<npc_type>(ss.getloc()) &&
BOOST_TEST( has_facet<npc_type>(ss.getloc()) && (NULL dynamic_cast<const backward_bool_names*>(&
!= dynamic_cast<backward_bool_names const *>( std::use_facet<npc_type>(ss.getloc())));
&use_facet<npc_type>(ss.getloc()) )) );
} }
BOOST_TEST_EQ(std::ios_base::skipws | std::ios_base::dec, ss.flags());
BOOST_TEST_EQ( (ios_base::skipws | ios_base::dec), ss.flags() );
BOOST_TEST_EQ(6, ss.precision()); BOOST_TEST_EQ(6, ss.precision());
BOOST_TEST_EQ(0, ss.width()); BOOST_TEST_EQ(0, ss.width());
BOOST_TEST_EQ( ios_base::goodbit, ss.rdstate() ); BOOST_TEST_EQ(std::ios_base::goodbit, ss.rdstate());
BOOST_TEST(ss.good()); BOOST_TEST(ss.good());
BOOST_TEST_EQ( ios_base::goodbit, ss.exceptions() ); BOOST_TEST_EQ(std::ios_base::goodbit, ss.exceptions());
BOOST_TEST(NULL == ss.tie()); BOOST_TEST(NULL == ss.tie());
BOOST_TEST(&sb == ss.rdbuf()); BOOST_TEST(&sb == ss.rdbuf());
BOOST_TEST_EQ(' ', ss.fill()); BOOST_TEST_EQ(' ', ss.fill());
BOOST_TEST( locale() == ss.getloc() ); BOOST_TEST(std::locale() == ss.getloc());
} }
// Unit test for user-defined data saving
void void
ios_word_saver_unit_test ios_word_saver_unit_test(int index)
(
)
{ {
using namespace std; std::stringstream ss;
BOOST_TEST_EQ(0, ss.iword(index));
stringstream ss; BOOST_TEST(NULL == ss.pword(index));
BOOST_TEST_EQ( 0, ss.iword(word_index) );
BOOST_TEST( NULL == ss.pword(word_index) );
{ {
boost::io::ios_all_word_saver iaws( ss, word_index ); boost::io::ios_all_word_saver iaws(ss, index);
BOOST_TEST_EQ(0, ss.iword(index));
BOOST_TEST_EQ( 0, ss.iword(word_index) ); BOOST_TEST(NULL == ss.pword(index));
BOOST_TEST( NULL == ss.pword(word_index) ); ss.iword(index) = -11;
ss.pword(index) = ss.rdbuf();
ss.iword( word_index ) = -11; BOOST_TEST_EQ(-11, ss.iword(index));
ss.pword( word_index ) = ss.rdbuf(); BOOST_TEST_EQ(ss.rdbuf(), ss.pword(index));
BOOST_TEST_EQ( -11, ss.iword(word_index) ); }
BOOST_TEST_EQ( ss.rdbuf(), ss.pword(word_index) ); BOOST_TEST_EQ(0, ss.iword(index));
BOOST_TEST(NULL == ss.pword(index));
} }
BOOST_TEST_EQ( 0, ss.iword(word_index) );
BOOST_TEST( NULL == ss.pword(word_index) );
}
// Unit test program
int main() int main()
{ {
ios_flags_saver_unit_test(); int index = std::ios_base::xalloc();
ios_precision_saver_unit_test(); ios_flags_saver_unit_test(index);
ios_width_saver_unit_test(); ios_precision_saver_unit_test(index);
ios_width_saver_unit_test(index);
ios_iostate_saver_unit_test(); ios_iostate_saver_unit_test(index);
ios_exception_saver_unit_test(); ios_exception_saver_unit_test(index);
ios_tie_saver_unit_test(); ios_tie_saver_unit_test(index);
ios_rdbuf_saver_unit_test(); ios_rdbuf_saver_unit_test(index);
ios_fill_saver_unit_test(); ios_fill_saver_unit_test(index);
ios_locale_saver_unit_test(); ios_locale_saver_unit_test(index);
ios_iword_saver_unit_test(index);
ios_iword_saver_unit_test(); ios_pword_saver_unit_test(index);
ios_pword_saver_unit_test(); ios_base_all_saver_unit_test(index);
ios_all_saver_unit_test(index);
ios_base_all_saver_unit_test(); ios_word_saver_unit_test(index);
ios_all_saver_unit_test();
ios_word_saver_unit_test();
return boost::report_errors(); return boost::report_errors();
} }