| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *             Copyright Andrey Semashev 2013. | 
					
						
							|  |  |  |  * Distributed under the Boost Software License, Version 1.0. | 
					
						
							|  |  |  |  *    (See accompanying file LICENSE_1_0.txt or copy at | 
					
						
							|  |  |  |  *          http://www.boost.org/LICENSE_1_0.txt)
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \file   string_ref_test_io.cpp | 
					
						
							|  |  |  |  * \author Andrey Semashev | 
					
						
							|  |  |  |  * \date   26.05.2013 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \brief  This header contains tests for stream operations of \c basic_string_ref. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/utility/string_view.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iomanip>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <iterator>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/config.hpp>
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | #include <boost/core/lightweight_test.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | /* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available.
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char* test_strings[] = | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     "begin", | 
					
						
							|  |  |  |     "abcd", | 
					
						
							|  |  |  |     "end" | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //! The context with test data for particular character type
 | 
					
						
							|  |  |  | template< typename CharT > | 
					
						
							|  |  |  | struct context | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     typedef CharT char_type; | 
					
						
							|  |  |  |     typedef std::basic_string< char_type > string_type; | 
					
						
							|  |  |  |     typedef std::basic_ostringstream< char_type > ostream_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     string_type begin, abcd, end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         boost::string_view str = test_strings[0]; | 
					
						
							|  |  |  |         std::copy(str.begin(), str.end(), std::back_inserter(begin)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         str = test_strings[1]; | 
					
						
							|  |  |  |         std::copy(str.begin(), str.end(), std::back_inserter(abcd)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         str = test_strings[2]; | 
					
						
							|  |  |  |         std::copy(str.begin(), str.end(), std::back_inserter(end)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test regular output
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | template<class CharT> | 
					
						
							|  |  |  | void test_string_view_output() | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     typedef CharT char_type; | 
					
						
							|  |  |  |     typedef std::basic_ostringstream< char_type > ostream_type; | 
					
						
							|  |  |  |     typedef boost::basic_string_view< char_type > string_view_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context< char_type > ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ostream_type strm; | 
					
						
							|  |  |  |     strm << string_view_type(ctx.abcd); | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |     BOOST_TEST(strm.str() == ctx.abcd); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test support for padding
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | template<class CharT> | 
					
						
							|  |  |  | void test_padding() | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     typedef CharT char_type; | 
					
						
							|  |  |  |     typedef std::basic_ostringstream< char_type > ostream_type; | 
					
						
							|  |  |  |     typedef boost::basic_string_view< char_type > string_view_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context< char_type > ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test for padding
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ostream_type strm_ref; | 
					
						
							|  |  |  |         strm_ref << ctx.begin << std::setw(8) << string_view_type(ctx.abcd) << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ostream_type strm_correct; | 
					
						
							|  |  |  |         strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |         BOOST_TEST(strm_ref.str() == strm_correct.str()); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test for long padding
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ostream_type strm_ref; | 
					
						
							|  |  |  |         strm_ref << ctx.begin << std::setw(100) << string_view_type(ctx.abcd) << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ostream_type strm_correct; | 
					
						
							|  |  |  |         strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |         BOOST_TEST(strm_ref.str() == strm_correct.str()); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Test that short width does not truncate the string
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ostream_type strm_ref; | 
					
						
							|  |  |  |         strm_ref << ctx.begin << std::setw(1) << string_view_type(ctx.abcd) << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ostream_type strm_correct; | 
					
						
							|  |  |  |         strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |         BOOST_TEST(strm_ref.str() == strm_correct.str()); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test support for padding fill
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | template<class CharT> | 
					
						
							|  |  |  | void test_padding_fill() | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     typedef CharT char_type; | 
					
						
							|  |  |  |     typedef std::basic_ostringstream< char_type > ostream_type; | 
					
						
							|  |  |  |     typedef boost::basic_string_view< char_type > string_view_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context< char_type > ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ostream_type strm_ref; | 
					
						
							|  |  |  |     strm_ref << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << string_view_type(ctx.abcd) << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ostream_type strm_correct; | 
					
						
							|  |  |  |     strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |     BOOST_TEST(strm_ref.str() == strm_correct.str()); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test support for alignment
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | template<class CharT> | 
					
						
							|  |  |  | void test_alignment() | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     typedef CharT char_type; | 
					
						
							|  |  |  |     typedef std::basic_ostringstream< char_type > ostream_type; | 
					
						
							|  |  |  |     typedef boost::basic_string_view< char_type > string_view_type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context< char_type > ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Left alignment
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ostream_type strm_ref; | 
					
						
							|  |  |  |         strm_ref << ctx.begin << std::left << std::setw(8) << string_view_type(ctx.abcd) << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ostream_type strm_correct; | 
					
						
							|  |  |  |         strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |         BOOST_TEST(strm_ref.str() == strm_correct.str()); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Right alignment
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ostream_type strm_ref; | 
					
						
							|  |  |  |         strm_ref << ctx.begin << std::right << std::setw(8) << string_view_type(ctx.abcd) << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ostream_type strm_correct; | 
					
						
							|  |  |  |         strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  |         BOOST_TEST(strm_ref.str() == strm_correct.str()); | 
					
						
							| 
									
										
										
										
											2015-12-21 11:39:59 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-23 12:58:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<class CharT> | 
					
						
							|  |  |  | void test() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     test_string_view_output<CharT>(); | 
					
						
							|  |  |  |     test_padding<CharT>(); | 
					
						
							|  |  |  |     test_padding_fill<CharT>(); | 
					
						
							|  |  |  |     test_alignment<CharT>(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     test<char>(); | 
					
						
							|  |  |  |     test<wchar_t>(); | 
					
						
							|  |  |  |     return boost::report_errors(); | 
					
						
							|  |  |  | } |