Add sv_stream_insert_test

This commit is contained in:
Peter Dimov
2021-10-09 17:36:06 +03:00
parent e9ffc4336c
commit c8b860704e
3 changed files with 144 additions and 1 deletions

View File

@ -928,7 +928,38 @@ public:
template<class Ch> std::basic_ostream<Ch>& operator<<( std::basic_ostream<Ch>& os, basic_string_view<Ch> str )
{
os.write( str.data(), str.size() );
Ch const* p = str.data();
std::size_t n = str.size();
if( n == 0 )
{
os << "";
}
else
{
std::size_t m = os.width();
if( n >= m )
{
os.write( p, n );
}
else if( ( os.flags() & std::ios_base::adjustfield ) == std::ios_base::left )
{
os.write( p, n - 1 );
os.width( m - n + 1 );
os << p[ n-1 ];
}
else
{
os.width( m - n + 1 );
os << p[0];
os.write( p + 1, n - 1 );
}
}
os.width( 0 );
return os;
}

View File

@ -266,6 +266,7 @@ run sv_find_last_not_of_test.cpp ;
run sv_contains_test.cpp ;
run sv_eq_test.cpp ;
run sv_lt_test.cpp ;
run sv_stream_insert_test.cpp ;
use-project /boost/core/swap : ./swap ;
build-project ./swap ;

View File

@ -0,0 +1,111 @@
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/core/string_view.hpp>
#include <boost/core/lightweight_test.hpp>
#include <sstream>
#include <iomanip>
int main()
{
using boost::core::string_view;
{
std::ostringstream os;
os << string_view( "" );
BOOST_TEST_EQ( os.str(), std::string( "" ) );
}
{
std::ostringstream os;
os << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( "123" ) );
}
{
std::ostringstream os;
os << std::setw( 5 ) << string_view( "" );
BOOST_TEST_EQ( os.str(), std::string( " " ) );
}
{
std::ostringstream os;
os << std::setfill( '-' ) << std::setw( 5 ) << string_view( "" );
BOOST_TEST_EQ( os.str(), std::string( "-----" ) );
}
{
std::ostringstream os;
os << std::setw( 5 ) << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( " 123" ) );
}
{
std::ostringstream os;
os << std::left << std::setw( 5 ) << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( "123 " ) );
}
{
std::ostringstream os;
os << std::right << std::setw( 5 ) << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( " 123" ) );
}
{
std::ostringstream os;
os << std::setfill( '-' ) << std::setw( 5 ) << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( "--123" ) );
}
{
std::ostringstream os;
os << std::setfill( '-' ) << std::left << std::setw( 5 ) << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( "123--" ) );
}
{
std::ostringstream os;
os << std::setfill( '-' ) << std::right << std::setw( 5 ) << string_view( "123" );
BOOST_TEST_EQ( os.str(), std::string( "--123" ) );
}
{
std::ostringstream os;
os << std::setw( 5 ) << string_view( "12345" );
BOOST_TEST_EQ( os.str(), std::string( "12345" ) );
}
{
std::ostringstream os;
os << std::setw( 5 ) << string_view( "1234567" );
BOOST_TEST_EQ( os.str(), std::string( "1234567" ) );
}
return boost::report_errors();
}