mirror of
https://github.com/boostorg/core.git
synced 2025-07-31 05:17:22 +02:00
Add sv_stream_insert_test
This commit is contained in:
@ -928,7 +928,38 @@ public:
|
|||||||
|
|
||||||
template<class Ch> std::basic_ostream<Ch>& operator<<( std::basic_ostream<Ch>& os, basic_string_view<Ch> str )
|
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;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,6 +266,7 @@ run sv_find_last_not_of_test.cpp ;
|
|||||||
run sv_contains_test.cpp ;
|
run sv_contains_test.cpp ;
|
||||||
run sv_eq_test.cpp ;
|
run sv_eq_test.cpp ;
|
||||||
run sv_lt_test.cpp ;
|
run sv_lt_test.cpp ;
|
||||||
|
run sv_stream_insert_test.cpp ;
|
||||||
|
|
||||||
use-project /boost/core/swap : ./swap ;
|
use-project /boost/core/swap : ./swap ;
|
||||||
build-project ./swap ;
|
build-project ./swap ;
|
||||||
|
111
test/sv_stream_insert_test.cpp
Normal file
111
test/sv_stream_insert_test.cpp
Normal 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();
|
||||||
|
}
|
Reference in New Issue
Block a user