Compare commits

...

3 Commits

Author SHA1 Message Date
Peter Dimov
e7433ba545 Fix operator<< for shared_ptr and intrusive_ptr. Fixes #115. 2025-01-13 16:13:04 +02:00
Peter Dimov
785a17aaaf Add wide stream tests. Refs #115. 2025-01-13 16:12:31 +02:00
Peter Dimov
576d31f206 Add sp_ostream_test, ip_ostream_test, lsp_ostream_test 2025-01-13 15:24:45 +02:00
6 changed files with 149 additions and 2 deletions

View File

@@ -285,7 +285,7 @@ template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast( intrusive_ptr<
// operator<<
template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
{
os << p.get();
return os;

View File

@@ -775,7 +775,7 @@ template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shar
// operator<<
template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
{
os << p.get();
return os;

View File

@@ -424,3 +424,7 @@ run sp_is_bounded_array_test.cpp ;
run sp_is_unbounded_array_test.cpp ;
run sp_type_identity_test.cpp ;
run sp_type_with_alignment_test.cpp ;
run sp_ostream_test.cpp ;
run ip_ostream_test.cpp ;
run lsp_ostream_test.cpp ;

75
test/ip_ostream_test.cpp Normal file
View File

@@ -0,0 +1,75 @@
// Copyright 2011, 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/intrusive_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
#include <sstream>
class base
{
private:
int use_count_;
base(base const &);
base & operator=(base const &);
protected:
base(): use_count_(0)
{
}
virtual ~base()
{
}
public:
long use_count() const
{
return use_count_;
}
inline friend void intrusive_ptr_add_ref(base * p)
{
++p->use_count_;
}
inline friend void intrusive_ptr_release(base * p)
{
if(--p->use_count_ == 0) delete p;
}
};
struct X: public base
{
};
template<class T> std::string to_string( T const& t )
{
std::ostringstream os;
os << t;
return os.str();
}
template<class T> std::wstring to_wstring( T const& t )
{
std::wostringstream os;
os << t;
return os.str();
}
int main()
{
boost::intrusive_ptr<X> p1, p2( new X );
BOOST_TEST_EQ( to_string( p1 ), to_string( p1.get() ) );
BOOST_TEST_EQ( to_string( p2 ), to_string( p2.get() ) );
BOOST_TEST( to_wstring( p1 ) == to_wstring( p1.get() ) );
BOOST_TEST( to_wstring( p2 ) == to_wstring( p2.get() ) );
return boost::report_errors();
}

34
test/lsp_ostream_test.cpp Normal file
View File

@@ -0,0 +1,34 @@
// Copyright 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/smart_ptr/local_shared_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
#include <sstream>
template<class T> std::string to_string( T const& t )
{
std::ostringstream os;
os << t;
return os.str();
}
template<class T> std::wstring to_wstring( T const& t )
{
std::wostringstream os;
os << t;
return os.str();
}
int main()
{
boost::local_shared_ptr<int> p1, p2( new int );
BOOST_TEST_EQ( to_string( p1 ), to_string( p1.get() ) );
BOOST_TEST_EQ( to_string( p2 ), to_string( p2.get() ) );
BOOST_TEST( to_wstring( p1 ) == to_wstring( p1.get() ) );
BOOST_TEST( to_wstring( p2 ) == to_wstring( p2.get() ) );
return boost::report_errors();
}

34
test/sp_ostream_test.cpp Normal file
View File

@@ -0,0 +1,34 @@
// Copyright 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/shared_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
#include <sstream>
template<class T> std::string to_string( T const& t )
{
std::ostringstream os;
os << t;
return os.str();
}
template<class T> std::wstring to_wstring( T const& t )
{
std::wostringstream os;
os << t;
return os.str();
}
int main()
{
boost::shared_ptr<int> p1, p2( new int );
BOOST_TEST_EQ( to_string( p1 ), to_string( p1.get() ) );
BOOST_TEST_EQ( to_string( p2 ), to_string( p2.get() ) );
BOOST_TEST( to_wstring( p1 ) == to_wstring( p1.get() ) );
BOOST_TEST( to_wstring( p2 ) == to_wstring( p2.get() ) );
return boost::report_errors();
}