diff options
Diffstat (limited to 'src/boost/libs/io')
-rw-r--r-- | src/boost/libs/io/index.html | 14 | ||||
-rw-r--r-- | src/boost/libs/io/meta/libraries.json | 15 | ||||
-rw-r--r-- | src/boost/libs/io/test/Jamfile.v2 | 26 | ||||
-rw-r--r-- | src/boost/libs/io/test/ios_state_test.cpp | 267 | ||||
-rw-r--r-- | src/boost/libs/io/test/ios_state_unit_test.cpp | 685 | ||||
-rw-r--r-- | src/boost/libs/io/test/quoted_manip_test.cpp | 133 |
6 files changed, 1140 insertions, 0 deletions
diff --git a/src/boost/libs/io/index.html b/src/boost/libs/io/index.html new file mode 100644 index 00000000..debe8259 --- /dev/null +++ b/src/boost/libs/io/index.html @@ -0,0 +1,14 @@ +<html> +<head> +<meta http-equiv="refresh" content="0; URL=doc/index.html"> +</head> +<body> +Automatic redirection failed, please go to +<a href="doc/index.html">doc/index.html</a>. <hr> +<p>© Copyright Beman Dawes, 2001</p> +<p>Distributed under the Boost Software License, Version 1.0. (See accompanying +file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy +at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>) +</p> +</body> +</html>
\ No newline at end of file diff --git a/src/boost/libs/io/meta/libraries.json b/src/boost/libs/io/meta/libraries.json new file mode 100644 index 00000000..1e2b3e90 --- /dev/null +++ b/src/boost/libs/io/meta/libraries.json @@ -0,0 +1,15 @@ +{ + "key": "io", + "name": "IO State Savers", + "authors": [ + "Daryle Walker" + ], + "description": "The I/O sub-library of Boost helps segregate the large number of Boost headers. This sub-library should contain various items to use with/for the standard I/O library.", + "documentation": "doc/ios_state.html", + "category": [ + "IO" + ], + "maintainers": [ + "Daryle Walker <darylew -at- hotmail.com>" + ] +} diff --git a/src/boost/libs/io/test/Jamfile.v2 b/src/boost/libs/io/test/Jamfile.v2 new file mode 100644 index 00000000..4b48bce5 --- /dev/null +++ b/src/boost/libs/io/test/Jamfile.v2 @@ -0,0 +1,26 @@ +# Boost.IO Library test Jamfile +# +# Copyright 2003 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. + +test-suite "io" + : [ run ios_state_unit_test.cpp + ../../../libs/test/build//boost_unit_test_framework/<link>static + : # args + : # input files + # : std::locale-support + ] + + [ run ios_state_test.cpp + ../../../libs/test/build//boost_test_exec_monitor/<link>static + : # args + : # input files + # : std::locale-support + ] + + [ run quoted_manip_test.cpp ] + ; diff --git a/src/boost/libs/io/test/ios_state_test.cpp b/src/boost/libs/io/test/ios_state_test.cpp new file mode 100644 index 00000000..79975f02 --- /dev/null +++ b/src/boost/libs/io/test/ios_state_test.cpp @@ -0,0 +1,267 @@ +// Boost ios_state_test.cpp test file --------------------------------------// + +// Copyright 2002, 2003 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. + +// 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/test/minimal.hpp> // main, BOOST_CHECK, etc. + +#include <boost/cstdlib.hpp> // for boost::exit_success +#include <boost/io/ios_state.hpp> // for boost::io::ios_flags_saver, etc. + +#include <cstddef> // for std::size_t +#include <iomanip> // for std::setw +#include <ios> // for std::ios_base, std::streamsize, etc. +#include <iostream> // for std::cout, etc. +#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)) +#include <stdexcept> +#endif + +// Facet with the bool names spelled backwards +class backward_bool_names + : public std::numpunct<char> +{ + typedef std::numpunct<char> base_type; + +public: + explicit backward_bool_names( std::size_t refs = 0 ) + : base_type( refs ) + {} + +protected: + virtual ~backward_bool_names() {} + + virtual base_type::string_type do_truename() const + { return "eurt"; } + virtual base_type::string_type do_falsename() const + { return "eslaf"; } +}; + + +// 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 +test_main +( + int , // "argc" is unused + char * [] // "argv" is unused +) +{ + 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_CHECK( &cin == cout.pword(my_index) ); + BOOST_CHECK( 42L == cout.iword(my_index) ); + BOOST_CHECK( cout_locale == cout.getloc() ); + BOOST_CHECK( cout_fill == cout.fill() ); + BOOST_CHECK( cout_sb == cout.rdbuf() ); + BOOST_CHECK( cin_tie == cin.tie() ); + BOOST_CHECK( cout_exceptions == cout.exceptions() ); + BOOST_CHECK( cout_iostate == cout.rdstate() ); + BOOST_CHECK( cout_width == cout.width() ); + BOOST_CHECK( cout_precision == cout.precision() ); + BOOST_CHECK( 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_CHECK( &cin == cout.pword(my_index) ); + BOOST_CHECK( 42L == cout.iword(my_index) ); + BOOST_CHECK( cout_locale == cout.getloc() ); + BOOST_CHECK( cout_fill == cout.fill() ); + BOOST_CHECK( cout_sb == cout.rdbuf() ); + BOOST_CHECK( cin_tie == cin.tie() ); + BOOST_CHECK( cout_exceptions == cout.exceptions() ); + BOOST_CHECK( cout_iostate == cout.rdstate() ); + BOOST_CHECK( cout_width == cout.width() ); + BOOST_CHECK( cout_precision == cout.precision() ); + BOOST_CHECK( cout_flags == cout.flags() ); + + return boost::exit_success; +} + +// Save, change, and restore stream properties +void +saver_tests_1 +( + std::istream & input, + std::ostream & output, + std::ostream & err +) +{ + using std::locale; + using std::ios_base; + using std::setw; + + boost::io::ios_flags_saver const ifls( output ); + boost::io::ios_precision_saver const iprs( output ); + boost::io::ios_width_saver const iws( output ); + boost::io::ios_tie_saver const its( input ); + boost::io::ios_rdbuf_saver const irs( output ); + boost::io::ios_fill_saver const ifis( output ); + 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 ); + output.rdbuf( err.rdbuf() ); + output.iword( my_index ) = 69L; + output.pword( my_index ) = &err; + + output << "The data is (again):\n"; + output.setf( ios_base::showpos | ios_base::boolalpha ); + output.setf( ios_base::internal, ios_base::adjustfield ); + output.fill( '@' ); + output.precision( 9 ); + output << '\t' << test_string << '\n'; + output << '\t' << setw( 10 ) << test_num1 << '\n'; + output << '\t' << setw( 15 ) << test_num2 << '\n'; + output.imbue( loc ); + output << '\t' << test_bool << '\n'; + + BOOST_CHECK( &err == output.pword(my_index) ); + BOOST_CHECK( 69L == output.iword(my_index) ); + + try + { + boost::io::ios_exception_saver const ies( output ); + 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" ); + } +#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) + catch ( std::exception &f ) +#else + catch ( ios_base::failure &f ) +#endif + { + err << "Got the expected I/O failure: \"" << f.what() << "\".\n"; + BOOST_CHECK( 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 +( + std::istream & input, + std::ostream & output, + std::ostream & err +) +{ + using std::locale; + using std::ios_base; + + boost::io::ios_tie_saver const its( input, &err ); + boost::io::ios_rdbuf_saver const irs( output, err.rdbuf() ); + boost::io::ios_iword_saver const iis( output, my_index, 69L ); + boost::io::ios_pword_saver const ipws( output, my_index, &err ); + output << "The data is (a third time; adding the numbers):\n"; + + boost::io::ios_flags_saver const ifls( output, (output.flags() + & ~ios_base::adjustfield) | ios_base::showpos | ios_base::boolalpha + | (ios_base::internal & ios_base::adjustfield) ); + boost::io::ios_precision_saver const iprs( output, 9 ); + boost::io::ios_fill_saver const ifis( output, '@' ); + output << '\t' << test_string << '\n'; + + boost::io::ios_width_saver const iws( output, 12 ); + output.put( '\t' ); + output << test_num1 + test_num2; + output.put( '\n' ); + + locale loc( locale::classic(), + new backward_bool_names ); + boost::io::ios_locale_saver const ils( output, loc ); + output << '\t' << test_bool << '\n'; + + BOOST_CHECK( &err == output.pword(my_index) ); + BOOST_CHECK( 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" ); + } +#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) + catch ( std::exception &f ) +#else + catch ( ios_base::failure &f ) +#endif + { + err << "Got the expected I/O failure: \"" << f.what() << "\".\n"; + BOOST_CHECK( output.exceptions() == ios_base::goodbit ); + } + catch ( ... ) + { + err << "Got an unknown error when doing exception test!\n"; + throw; + } +} diff --git a/src/boost/libs/io/test/ios_state_unit_test.cpp b/src/boost/libs/io/test/ios_state_unit_test.cpp new file mode 100644 index 00000000..6aa2011f --- /dev/null +++ b/src/boost/libs/io/test/ios_state_unit_test.cpp @@ -0,0 +1,685 @@ +// Boost ios_state_unit_test.cpp test file ---------------------------------// + +// Copyright 2003 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. + +// 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/test/unit_test.hpp> // for main, BOOST_CHECK, etc. + +#include <cstddef> // for NULL +#include <iomanip> // for std::setiosflags, etc. +#include <ios> // for std::ios_base +#include <iostream> // for std::cout, std::cerr, etc. +#include <istream> // for std::iostream +#include <locale> // for std::locale, std::numpunct +#include <sstream> // for std::stringstream, etc. +#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) +#include <stdexcept> +#endif + + +// Global constants +int const word_index = std::ios_base::xalloc(); + + +// Facet with the (classic) bool names spelled backwards +class backward_bool_names + : public std::numpunct<char> +{ + typedef std::numpunct<char> base_type; + +public: + explicit backward_bool_names( std::size_t refs = 0 ) + : base_type( refs ) + {} + +protected: + virtual ~backward_bool_names() {} + + virtual base_type::string_type do_truename() const + { return "eurt"; } + virtual base_type::string_type do_falsename() const + { return "eslaf"; } +}; + + +// Unit test for format-flag saving +void +ios_flags_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + + { + boost::io::ios_flags_saver ifs( ss ); + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + + ss << noskipws << fixed << boolalpha; + BOOST_CHECK_EQUAL( (ios_base::boolalpha | ios_base::dec + | ios_base::fixed), ss.flags() ); + } + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + + { + boost::io::ios_flags_saver ifs( ss, (ios_base::showbase + | ios_base::internal) ); + + BOOST_CHECK_EQUAL( (ios_base::showbase | ios_base::internal), + ss.flags() ); + + ss << setiosflags( ios_base::unitbuf ); + BOOST_CHECK_EQUAL( (ios_base::showbase | ios_base::internal + | ios_base::unitbuf), ss.flags() ); + } + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); +} + +// Unit test for precision saving +void +ios_precision_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( 6, ss.precision() ); + + { + boost::io::ios_precision_saver ips( ss ); + + BOOST_CHECK_EQUAL( 6, ss.precision() ); + + ss << setprecision( 4 ); + BOOST_CHECK_EQUAL( 4, ss.precision() ); + } + + BOOST_CHECK_EQUAL( 6, ss.precision() ); + + { + boost::io::ios_precision_saver ips( ss, 8 ); + + BOOST_CHECK_EQUAL( 8, ss.precision() ); + + ss << setprecision( 10 ); + BOOST_CHECK_EQUAL( 10, ss.precision() ); + } + + BOOST_CHECK_EQUAL( 6, ss.precision() ); +} + +// Unit test for width saving +void +ios_width_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( 0, ss.width() ); + + { + boost::io::ios_width_saver iws( ss ); + + BOOST_CHECK_EQUAL( 0, ss.width() ); + + ss << setw( 4 ); + BOOST_CHECK_EQUAL( 4, ss.width() ); + } + + BOOST_CHECK_EQUAL( 0, ss.width() ); + + { + boost::io::ios_width_saver iws( ss, 8 ); + + BOOST_CHECK_EQUAL( 8, ss.width() ); + + ss << setw( 10 ); + BOOST_CHECK_EQUAL( 10, ss.width() ); + } + + BOOST_CHECK_EQUAL( 0, ss.width() ); +} + +// Unit test for I/O-state saving +void +ios_iostate_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); + + { + boost::io::ios_iostate_saver iis( ss ); + char c; + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); + + ss >> c; + BOOST_CHECK_EQUAL( (ios_base::eofbit | ios_base::failbit), + ss.rdstate() ); + BOOST_CHECK( ss.eof() ); + BOOST_CHECK( ss.fail() ); + BOOST_CHECK( !ss.bad() ); + } + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); + + { + boost::io::ios_iostate_saver iis( ss, ios_base::eofbit ); + + BOOST_CHECK_EQUAL( ios_base::eofbit, ss.rdstate() ); + BOOST_CHECK( ss.eof() ); + + ss.setstate( ios_base::badbit ); + BOOST_CHECK_EQUAL( (ios_base::eofbit | ios_base::badbit), + ss.rdstate() ); + BOOST_CHECK( ss.eof() ); + BOOST_CHECK( ss.fail() ); + BOOST_CHECK( ss.bad() ); + } + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); +} + +// Unit test for exception-flag saving +void +ios_exception_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); + + { + boost::io::ios_exception_saver ies( ss ); + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); + + ss.exceptions( ios_base::failbit ); + BOOST_CHECK_EQUAL( ios_base::failbit, ss.exceptions() ); + + { + boost::io::ios_iostate_saver iis( ss ); + ss.exceptions( ios_base::failbit | ios_base::badbit ); + char c; + +#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) + BOOST_CHECK_THROW( ss >> c, std::exception ); +#else + BOOST_CHECK_THROW( ss >> c, std::ios_base::failure ); +#endif + } + } + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); + + { + boost::io::ios_exception_saver ies( ss, ios_base::eofbit ); + + BOOST_CHECK_EQUAL( ios_base::eofbit, ss.exceptions() ); + + ss.exceptions( ios_base::badbit ); + BOOST_CHECK_EQUAL( ios_base::badbit, ss.exceptions() ); + + { + boost::io::ios_iostate_saver iis( ss ); + char c; + + BOOST_CHECK_NO_THROW( ss >> c ); + } + } + + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); +} + +// Unit test for tied-stream saving +void +ios_tie_saver_unit_test +( +) +{ + using namespace std; + + BOOST_CHECK( NULL == cout.tie() ); + + { + boost::io::ios_tie_saver its( cout ); + + BOOST_CHECK( NULL == cout.tie() ); + + cout.tie( &clog ); + BOOST_CHECK_EQUAL( &clog, cout.tie() ); + } + + BOOST_CHECK( NULL == cout.tie() ); + + { + boost::io::ios_tie_saver its( cout, &clog ); + + BOOST_CHECK_EQUAL( &clog, cout.tie() ); + + cout.tie( &cerr ); + BOOST_CHECK_EQUAL( &cerr, cout.tie() ); + } + + BOOST_CHECK( NULL == cout.tie() ); +} + +// Unit test for connected-streambuf saving +void +ios_rdbuf_saver_unit_test +( +) +{ + using namespace std; + + iostream s( NULL ); + + BOOST_CHECK( NULL == s.rdbuf() ); + + { + stringbuf sb; + boost::io::ios_rdbuf_saver irs( s ); + + BOOST_CHECK( NULL == s.rdbuf() ); + + s.rdbuf( &sb ); + BOOST_CHECK_EQUAL( &sb, s.rdbuf() ); + } + + BOOST_CHECK( NULL == s.rdbuf() ); + + { + stringbuf sb1, sb2( "Hi there" ); + boost::io::ios_rdbuf_saver irs( s, &sb1 ); + + BOOST_CHECK_EQUAL( &sb1, s.rdbuf() ); + + s.rdbuf( &sb2 ); + BOOST_CHECK_EQUAL( &sb2, s.rdbuf() ); + } + + BOOST_CHECK( NULL == s.rdbuf() ); +} + +// Unit test for fill-character saving +void +ios_fill_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( ' ', ss.fill() ); + + { + boost::io::ios_fill_saver ifs( ss ); + + BOOST_CHECK_EQUAL( ' ', ss.fill() ); + + ss.fill( 'x' ); + BOOST_CHECK_EQUAL( 'x', ss.fill() ); + } + + BOOST_CHECK_EQUAL( ' ', ss.fill() ); + + { + boost::io::ios_fill_saver ifs( ss, '3' ); + + BOOST_CHECK_EQUAL( '3', ss.fill() ); + + ss.fill( '+' ); + BOOST_CHECK_EQUAL( '+', ss.fill() ); + } + + BOOST_CHECK_EQUAL( ' ', ss.fill() ); +} + +// Unit test for locale saving +void +ios_locale_saver_unit_test +( +) +{ + using namespace std; + + typedef numpunct<char> npc_type; + + stringstream ss; + + BOOST_CHECK( locale() == ss.getloc() ); + // locales are unprintable, so no BOOST_CHECK_EQUAL + BOOST_CHECK( !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_CHECK( ! has_facet< backward_bool_names >( + // ss.getloc() ) )" + { + boost::io::ios_locale_saver ils( ss ); + + BOOST_CHECK( locale() == ss.getloc() ); + + ss.imbue( locale::classic() ); + BOOST_CHECK( locale::classic() == ss.getloc() ); + } + + BOOST_CHECK( locale() == ss.getloc() ); + BOOST_CHECK( !has_facet<npc_type>(ss.getloc()) || (NULL + == dynamic_cast<backward_bool_names const *>( + &use_facet<npc_type>(ss.getloc()) )) ); + + { + boost::io::ios_locale_saver ils( ss, locale::classic() ); + + BOOST_CHECK( locale::classic() == ss.getloc() ); + BOOST_CHECK( !has_facet<npc_type>(ss.getloc()) || (NULL + == dynamic_cast<backward_bool_names const *>( + &use_facet<npc_type>(ss.getloc()) )) ); + + ss.imbue( locale(locale::classic(), new backward_bool_names) ); + BOOST_CHECK( locale::classic() != ss.getloc() ); + BOOST_CHECK( has_facet<npc_type>(ss.getloc()) && (NULL + != dynamic_cast<backward_bool_names const *>( + &use_facet<npc_type>(ss.getloc()) )) ); + //BOOST_CHECK( has_facet<backward_bool_names>(ss.getloc()) ); + } + + BOOST_CHECK( locale() == ss.getloc() ); + BOOST_CHECK( !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 +ios_iword_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); + + { + boost::io::ios_iword_saver iis( ss, word_index ); + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); + + ss.iword( word_index ) = 6; + BOOST_CHECK_EQUAL( 6, ss.iword(word_index) ); + } + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); + + { + boost::io::ios_iword_saver iis( ss, word_index, 100 ); + + BOOST_CHECK_EQUAL( 100, ss.iword(word_index) ); + + ss.iword( word_index ) = -2000; + BOOST_CHECK_EQUAL( -2000, ss.iword(word_index) ); + } + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); +} + +// Unit test for user-defined pointer data saving +void +ios_pword_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK( NULL == ss.pword(word_index) ); + + { + boost::io::ios_pword_saver ips( ss, word_index ); + + BOOST_CHECK( NULL == ss.pword(word_index) ); + + ss.pword( word_index ) = &ss; + BOOST_CHECK_EQUAL( &ss, ss.pword(word_index) ); + } + + BOOST_CHECK( NULL == ss.pword(word_index) ); + + { + boost::io::ios_pword_saver ips( ss, word_index, ss.rdbuf() ); + + BOOST_CHECK_EQUAL( ss.rdbuf(), ss.pword(word_index) ); + + ss.pword( word_index ) = &ss; + BOOST_CHECK_EQUAL( &ss, ss.pword(word_index) ); + } + + BOOST_CHECK( NULL == ss.pword(word_index) ); +} + +// Unit test for all ios_base data saving +void +ios_base_all_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + BOOST_CHECK_EQUAL( 6, ss.precision() ); + BOOST_CHECK_EQUAL( 0, ss.width() ); + + { + boost::io::ios_base_all_saver ibas( ss ); + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + BOOST_CHECK_EQUAL( 6, ss.precision() ); + BOOST_CHECK_EQUAL( 0, ss.width() ); + + ss << hex << unitbuf << setprecision( 5 ) << setw( 7 ); + BOOST_CHECK_EQUAL( (ios_base::unitbuf | ios_base::hex + | ios_base::skipws), ss.flags() ); + BOOST_CHECK_EQUAL( 5, ss.precision() ); + BOOST_CHECK_EQUAL( 7, ss.width() ); + } + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + BOOST_CHECK_EQUAL( 6, ss.precision() ); + BOOST_CHECK_EQUAL( 0, ss.width() ); +} + +// Unit test for all basic_ios data saving +void +ios_all_saver_unit_test +( +) +{ + using namespace std; + + typedef numpunct<char> npc_type; + + stringbuf sb; + iostream ss( &sb ); + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + BOOST_CHECK_EQUAL( 6, ss.precision() ); + BOOST_CHECK_EQUAL( 0, ss.width() ); + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); + BOOST_CHECK( NULL == ss.tie() ); + BOOST_CHECK( &sb == ss.rdbuf() ); + BOOST_CHECK_EQUAL( ' ', ss.fill() ); + BOOST_CHECK( locale() == ss.getloc() ); + + { + boost::io::ios_all_saver ias( ss ); + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + BOOST_CHECK_EQUAL( 6, ss.precision() ); + BOOST_CHECK_EQUAL( 0, ss.width() ); + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); + BOOST_CHECK( NULL == ss.tie() ); + BOOST_CHECK( &sb == ss.rdbuf() ); + BOOST_CHECK_EQUAL( ' ', ss.fill() ); + BOOST_CHECK( locale() == ss.getloc() ); + + ss << oct << showpos << noskipws; + BOOST_CHECK_EQUAL( (ios_base::showpos | ios_base::oct), ss.flags() ); + + ss << setprecision( 3 ); + BOOST_CHECK_EQUAL( 3, ss.precision() ); + + ss << setw( 9 ); + BOOST_CHECK_EQUAL( 9, ss.width() ); + + ss.setstate( ios_base::eofbit ); + BOOST_CHECK_EQUAL( ios_base::eofbit, ss.rdstate() ); + BOOST_CHECK( ss.eof() ); + + ss.exceptions( ios_base::failbit ); + BOOST_CHECK_EQUAL( ios_base::failbit, ss.exceptions() ); + + { + boost::io::ios_iostate_saver iis( ss ); + ss.exceptions( ios_base::failbit | ios_base::badbit ); + char c; + +#if defined(BOOST_GCC) || (defined(BOOST_CLANG) && defined(BOOST_GNU_STDLIB)) + BOOST_CHECK_THROW( ss >> c, std::exception ); +#else + BOOST_CHECK_THROW( ss >> c, std::ios_base::failure ); +#endif + } + + ss.tie( &clog ); + BOOST_CHECK_EQUAL( &clog, ss.tie() ); + + ss.rdbuf( cerr.rdbuf() ); + BOOST_CHECK_EQUAL( cerr.rdbuf(), ss.rdbuf() ); + + ss << setfill( 'x' ); + BOOST_CHECK_EQUAL( 'x', ss.fill() ); + + ss.imbue( locale(locale::classic(), new backward_bool_names) ); + BOOST_CHECK( locale() != ss.getloc() ); + BOOST_CHECK( has_facet<npc_type>(ss.getloc()) && (NULL + != dynamic_cast<backward_bool_names const *>( + &use_facet<npc_type>(ss.getloc()) )) ); + } + + BOOST_CHECK_EQUAL( (ios_base::skipws | ios_base::dec), ss.flags() ); + BOOST_CHECK_EQUAL( 6, ss.precision() ); + BOOST_CHECK_EQUAL( 0, ss.width() ); + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.rdstate() ); + BOOST_CHECK( ss.good() ); + BOOST_CHECK_EQUAL( ios_base::goodbit, ss.exceptions() ); + BOOST_CHECK( NULL == ss.tie() ); + BOOST_CHECK( &sb == ss.rdbuf() ); + BOOST_CHECK_EQUAL( ' ', ss.fill() ); + BOOST_CHECK( locale() == ss.getloc() ); +} + +// Unit test for user-defined data saving +void +ios_word_saver_unit_test +( +) +{ + using namespace std; + + stringstream ss; + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); + BOOST_CHECK( NULL == ss.pword(word_index) ); + + { + boost::io::ios_all_word_saver iaws( ss, word_index ); + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); + BOOST_CHECK( NULL == ss.pword(word_index) ); + + ss.iword( word_index ) = -11; + ss.pword( word_index ) = ss.rdbuf(); + BOOST_CHECK_EQUAL( -11, ss.iword(word_index) ); + BOOST_CHECK_EQUAL( ss.rdbuf(), ss.pword(word_index) ); + } + + BOOST_CHECK_EQUAL( 0, ss.iword(word_index) ); + BOOST_CHECK( NULL == ss.pword(word_index) ); +} + + +// Unit test program +boost::unit_test::test_suite * +init_unit_test_suite +( + int , // "argc" is unused + char * [] // "argv" is unused +) +{ + boost::unit_test::test_suite * test + = BOOST_TEST_SUITE( "I/O state saver test" ); + + test->add( BOOST_TEST_CASE(ios_flags_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_precision_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_width_saver_unit_test) ); + + test->add( BOOST_TEST_CASE(ios_iostate_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_exception_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_tie_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_rdbuf_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_fill_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_locale_saver_unit_test) ); + + test->add( BOOST_TEST_CASE(ios_iword_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_pword_saver_unit_test) ); + + test->add( BOOST_TEST_CASE(ios_base_all_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_all_saver_unit_test) ); + test->add( BOOST_TEST_CASE(ios_word_saver_unit_test) ); + + return test; +} diff --git a/src/boost/libs/io/test/quoted_manip_test.cpp b/src/boost/libs/io/test/quoted_manip_test.cpp new file mode 100644 index 00000000..31c6f059 --- /dev/null +++ b/src/boost/libs/io/test/quoted_manip_test.cpp @@ -0,0 +1,133 @@ +// libs/io/test/quote_manip_test.cpp ----------------------------------------------- // + +// Copyright Beman Dawes 2010 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// Library home page: http://www.boost.org/libs/io + +// ---------------------------------------------------------------------------------- // + +#include <boost/io/detail/quoted_manip.hpp> +#include <boost/detail/lightweight_test.hpp> +#include <iostream> +#include <sstream> + +using boost::io::quoted; +using std::string; +using std::wstring; + +int main() +{ + + std::wstringstream wss; + + string r; // test results + + const string s0("foo"); + { + std::stringstream ss; + ss << quoted(s0); + ss >> r; + BOOST_TEST(r == "\"foo\""); + } + { + std::stringstream ss; + ss << quoted(s0); + ss >> quoted(r); + BOOST_TEST(r == "foo"); + } + + const string s0s("foo bar"); + { + std::stringstream ss; + ss << quoted(s0s); + ss >> r; + BOOST_TEST(r == "\"foo"); + } + { + std::stringstream ss; + ss << quoted(s0s); + ss >> quoted(r); + BOOST_TEST(r == "foo bar"); + } + + const string s1("foo\\bar, \" *"); + { + std::stringstream ss; + ss << quoted(s1); + ss >> r; + BOOST_TEST(r == "\"foo\\\\bar,"); + } + { + std::stringstream ss; + ss << quoted("foo\\bar, \" *"); + ss >> r; + BOOST_TEST(r == "\"foo\\\\bar,"); + } + { + std::stringstream ss; + ss << quoted(s1); + ss >> quoted(r); + BOOST_TEST(r == s1); + } + { + std::stringstream ss; + ss << quoted(s1.c_str()); + ss >> quoted(r); + BOOST_TEST(r == s1); + } + + string s2("'Jack & Jill'"); + { + std::stringstream ss; + ss << quoted(s2, '&', '\''); + ss >> quoted(r, '&', '\''); + BOOST_TEST(r == s2); + } + + wstring ws1(L"foo$bar, \" *"); + wstring wr; // test results + { + std::wstringstream wss; + wss << quoted(ws1, L'$'); + wss >> quoted(wr, L'$'); + BOOST_TEST(wr == ws1); + } + + const string s3("const string"); + { + std::stringstream ss; + ss << quoted(s3); + ss >> quoted(r); + BOOST_TEST(r == s3); + } + { + // missing end delimiter test + std::stringstream ss; + ss << "\"abc"; // load ss with faulty quoting + ss >> quoted(r); // this loops if istream error/eof not detected + BOOST_TEST(r == "abc"); + } + { + // no initial delmiter test + std::stringstream ss; + ss << "abc"; + ss >> quoted(r); + BOOST_TEST(r == "abc"); + } + { + // no initial delmiter, space in ss + std::stringstream ss; + ss << "abc def"; + ss >> quoted(r); + BOOST_TEST(r == "abc"); + } + + // these should fail to compile because the arguments are const: + // ss >> quoted(s1); + // ss >> quoted("foo"); + + return boost::report_errors(); +} |