Files
smart_ptr/test/smart_ptr_test.cpp

285 lines
8.5 KiB
C++
Raw Permalink Normal View History

// smart pointer test program ----------------------------------------------//
// Copyright Beman Dawes 1998, 1999. 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)
// See http://www.boost.org/libs/smart_ptr for documentation.
// Revision History
// 24 May 01 use Boost test library for error detection, reporting, add tests
// for operations on incomplete types (Beman Dawes)
// 29 Nov 99 added std::swap and associative container tests (Darin Adler)
// 25 Sep 99 added swap tests
// 20 Jul 99 header name changed to .hpp
// 20 Apr 99 additional error tests added.
2018-01-23 21:45:59 +02:00
#define _CRT_SECURE_NO_WARNINGS
#include <boost/smart_ptr.hpp>
2024-09-24 18:25:13 +03:00
#include <boost/config.hpp>
#include <boost/core/lightweight_test.hpp>
#include <iostream>
#include <set>
#include <string.h>
class Incomplete;
Incomplete * get_ptr( boost::shared_ptr<Incomplete>& incomplete )
{
return incomplete.get();
}
template<class T>
void ck( const T* v1, T v2 ) { BOOST_TEST( *v1 == v2 ); }
namespace {
int UDT_use_count; // independent of pointer maintained counts
}
// user defined type -------------------------------------------------------//
class UDT {
long value_;
public:
explicit UDT( long value=0 ) : value_(value) { ++UDT_use_count; }
~UDT() {
--UDT_use_count;
std::cout << "UDT with value " << value_ << " being destroyed\n";
}
long value() const { return value_; }
void value( long v ) { value_ = v;; }
}; // UDT
// tests on incomplete types -----------------------------------------------//
// Certain smart pointer operations are specified to work on incomplete types,
// and some uses depend upon this feature. These tests verify compilation
// only - the functions aren't actually invoked.
class Incomplete;
Incomplete * check_incomplete( boost::scoped_ptr<Incomplete>& incomplete )
{
return incomplete.get();
}
Incomplete * check_incomplete( boost::shared_ptr<Incomplete>& incomplete,
boost::shared_ptr<Incomplete>& i2 )
{
incomplete.swap(i2);
std::cout << incomplete.use_count() << ' ' << incomplete.unique() << '\n';
return incomplete.get();
}
// This isn't a very systematic test; it just hits some of the basics.
void test()
{
BOOST_TEST( UDT_use_count == 0 ); // reality check
// test scoped_ptr with a built-in type
long * lp = new long;
boost::scoped_ptr<long> sp ( lp );
BOOST_TEST( sp.get() == lp );
BOOST_TEST( lp == sp.get() );
BOOST_TEST( &*sp == lp );
*sp = 1234568901L;
BOOST_TEST( *sp == 1234568901L );
BOOST_TEST( *lp == 1234568901L );
ck( static_cast<long*>(sp.get()), 1234568901L );
ck( lp, *sp );
sp.reset();
BOOST_TEST( sp.get() == 0 );
// test scoped_ptr with a user defined type
boost::scoped_ptr<UDT> udt_sp ( new UDT( 999888777 ) );
BOOST_TEST( udt_sp->value() == 999888777 );
udt_sp.reset();
udt_sp.reset( new UDT( 111222333 ) );
BOOST_TEST( udt_sp->value() == 111222333 );
udt_sp.reset( new UDT( 333222111 ) );
BOOST_TEST( udt_sp->value() == 333222111 );
// test scoped_array with a build-in type
char * sap = new char [ 100 ];
boost::scoped_array<char> sa ( sap );
BOOST_TEST( sa.get() == sap );
BOOST_TEST( sap == sa.get() );
strcpy( sa.get(), "Hot Dog with mustard and relish" );
BOOST_TEST( strcmp( sa.get(), "Hot Dog with mustard and relish" ) == 0 );
BOOST_TEST( strcmp( sap, "Hot Dog with mustard and relish" ) == 0 );
BOOST_TEST( sa[0] == 'H' );
BOOST_TEST( sa[30] == 'h' );
sa[0] = 'N';
sa[4] = 'd';
BOOST_TEST( strcmp( sap, "Not dog with mustard and relish" ) == 0 );
sa.reset();
BOOST_TEST( sa.get() == 0 );
// test shared_ptr with a built-in type
int * ip = new int;
boost::shared_ptr<int> cp ( ip );
BOOST_TEST( ip == cp.get() );
BOOST_TEST( cp.use_count() == 1 );
*cp = 54321;
BOOST_TEST( *cp == 54321 );
BOOST_TEST( *ip == 54321 );
ck( static_cast<int*>(cp.get()), 54321 );
ck( static_cast<int*>(ip), *cp );
boost::shared_ptr<int> cp2 ( cp );
BOOST_TEST( ip == cp2.get() );
BOOST_TEST( cp.use_count() == 2 );
BOOST_TEST( cp2.use_count() == 2 );
BOOST_TEST( *cp == 54321 );
BOOST_TEST( *cp2 == 54321 );
ck( static_cast<int*>(cp2.get()), 54321 );
ck( static_cast<int*>(ip), *cp2 );
boost::shared_ptr<int> cp3 ( cp );
BOOST_TEST( cp.use_count() == 3 );
BOOST_TEST( cp2.use_count() == 3 );
BOOST_TEST( cp3.use_count() == 3 );
cp.reset();
BOOST_TEST( cp2.use_count() == 2 );
BOOST_TEST( cp3.use_count() == 2 );
cp.reset( new int );
*cp = 98765;
BOOST_TEST( *cp == 98765 );
*cp3 = 87654;
BOOST_TEST( *cp3 == 87654 );
BOOST_TEST( *cp2 == 87654 );
cp.swap( cp3 );
BOOST_TEST( *cp == 87654 );
BOOST_TEST( *cp2 == 87654 );
BOOST_TEST( *cp3 == 98765 );
cp.swap( cp3 );
BOOST_TEST( *cp == 98765 );
BOOST_TEST( *cp2 == 87654 );
BOOST_TEST( *cp3 == 87654 );
cp2 = cp2;
BOOST_TEST( cp2.use_count() == 2 );
BOOST_TEST( *cp2 == 87654 );
cp = cp2;
BOOST_TEST( cp2.use_count() == 3 );
BOOST_TEST( *cp2 == 87654 );
BOOST_TEST( cp.use_count() == 3 );
BOOST_TEST( *cp == 87654 );
boost::shared_ptr<int> cp4;
swap( cp2, cp4 );
BOOST_TEST( cp4.use_count() == 3 );
BOOST_TEST( *cp4 == 87654 );
BOOST_TEST( cp2.get() == 0 );
std::set< boost::shared_ptr<int> > scp;
scp.insert(cp4);
BOOST_TEST( scp.find(cp4) != scp.end() );
BOOST_TEST( scp.find(cp4) == scp.find( boost::shared_ptr<int>(cp4) ) );
// test shared_array with a built-in type
char * cap = new char [ 100 ];
boost::shared_array<char> ca ( cap );
BOOST_TEST( ca.get() == cap );
BOOST_TEST( cap == ca.get() );
BOOST_TEST( &ca[0] == cap );
strcpy( ca.get(), "Hot Dog with mustard and relish" );
BOOST_TEST( strcmp( ca.get(), "Hot Dog with mustard and relish" ) == 0 );
BOOST_TEST( strcmp( cap, "Hot Dog with mustard and relish" ) == 0 );
BOOST_TEST( ca[0] == 'H' );
BOOST_TEST( ca[30] == 'h' );
boost::shared_array<char> ca2 ( ca );
boost::shared_array<char> ca3 ( ca2 );
ca[0] = 'N';
ca[4] = 'd';
BOOST_TEST( strcmp( ca.get(), "Not dog with mustard and relish" ) == 0 );
BOOST_TEST( strcmp( ca2.get(), "Not dog with mustard and relish" ) == 0 );
BOOST_TEST( strcmp( ca3.get(), "Not dog with mustard and relish" ) == 0 );
BOOST_TEST( ca.use_count() == 3 );
BOOST_TEST( ca2.use_count() == 3 );
BOOST_TEST( ca3.use_count() == 3 );
ca2.reset();
BOOST_TEST( ca.use_count() == 2 );
BOOST_TEST( ca3.use_count() == 2 );
Merged revision(s) 81149, 81159, 81171, 81174, 81219-81220, 81222-81224, 81226, 81229-81239, 81242, 81253, 81257-81262, 81265-81268, 81271-81272, 81275-81277, 81299-81300 from trunk: Implement shared_ptr<X[]>, weak_ptr<X[]>. Refs #1113. ........ Fix shared_ptr<T[]> EDG issues. ........ Disable make_shared<T> overloads when T is Q[]. ........ Add catch(...) clauses to sp_array_test.cpp. ........ Add allocate_shared and make_shared for shared_ptr arrays of runtime size. Fulfills need for allocate_shared_array and make_shared_array. ........ Update Jamfile.v2 to run make_shared array tests and allocate_shared array tests. ........ Fix g++ issues. ........ Add specialization of sp_if_not_array<T[N]>. ........ Rename make_shared.hpp to make_shared_object.hpp, include from make_shared.hpp. ........ Add make_shared_array_args_test.cpp. ........ Add support for make_shared of array of arrays. Correctly destroy elements and construct elements for the variadic template constructor variants. ........ Fix sp_convertible<T const[], T const[]>. ........ Update smart_ptr/detail/array_helper to have create and create_noinit for non-array case. ........ Rename sp_convertible_test.cpp to shared_ptr_convertible_test.cpp. ........ Don't treat array_helper create and create_noinit for array types as a special case. ........ Add sp_convertible_test.cpp. ........ Fix array_helper (create_noinit and use of args...). ........ Update allocate_shared and make_shared to treat multidimensional array as single dimension. Remove detail array_helper. Add detail array traits. Update tests. ........ Simplify array_deleter interface ........ Add missing semicolon. ........ Fix typo. ........ Add tests for variadic template constructors overload of array forms of make_shared and allocate_shared for multidimensional arrays and up to 9 constructor arguments. ........ Add support for shared_ptr<X[N>. ........ Add C++11 initializer list support for make_shared and allocate_shared array forms. ........ Clean up code in allocate_shared_array.hpp and make_shared_array.hpp ........ Change make_shared and allocate_shared array form semantics with initializer lists overload that takes no size. ........ Disable make_shared for arrays when the compiler doesn't support partial specialization or SFINAE. ........ For allocate_shared and make_shared: Separate test case that g++ does support yet. Remove macros testing for no partial specialization in traits. Add additional traits. ........ Actually remove test cases from make_shared_array_create_test.cpp and allocate_shared_array_create_test.cpp that g++ does not handle. ........ Add overloads to support fixed size arrays, T[N], to allocate_shared (variadic) and make_shared (variadic) and make_shared_noinit. ........ Add additional overload for make_shared and allocate_shared for arrays for fixed size arrays and initializer lists. ........ Add assertion to overload of make_shared and allocate_shared for T[N] with initializer lists. Rename detail type to be more intuitive. ........ Add allocate_shared_array_args_test.cpp. ........ Keep old definition of sp_assert_convertible when BOOST_SP_NO_SP_CONVERTIBLE is set. ........ Updated shared_array to match shared_ptr. Refs #1113. ........ Add final overload of make_shared and allocate_shared (array forms) for T[][N] with C++11 initializer lists. ........ Change traits for initializer list for g++ ........ Tidy long line formatting in allocate_shared_array.hpp and make_shared_array.hpp ........ Update tests for make_shared and allocate_shared array forms, for normal case, initializer lists, variadic template arguments, for arrays and fixed size arrays. ........ Update Jamfile.v2 with two new smart_ptr tests for allocate_shared and make_shared ........ [SVN r81339]
2012-11-14 13:52:11 +00:00
BOOST_TEST( ca2.use_count() == 0 );
ca.reset();
BOOST_TEST( ca.get() == 0 );
boost::shared_array<char> ca4;
swap( ca3, ca4 );
BOOST_TEST( ca4.use_count() == 1 );
BOOST_TEST( strcmp( ca4.get(), "Not dog with mustard and relish" ) == 0 );
BOOST_TEST( ca3.get() == 0 );
std::set< boost::shared_array<char> > sca;
sca.insert(ca4);
BOOST_TEST( sca.find(ca4) != sca.end() );
BOOST_TEST( sca.find(ca4) == sca.find( boost::shared_array<char>(ca4) ) );
// test shared_array with user defined type
boost::shared_array<UDT> udta ( new UDT[3] );
udta[0].value( 111 );
udta[1].value( 222 );
udta[2].value( 333 );
boost::shared_array<UDT> udta2 ( udta );
BOOST_TEST( udta[0].value() == 111 );
BOOST_TEST( udta[1].value() == 222 );
BOOST_TEST( udta[2].value() == 333 );
BOOST_TEST( udta2[0].value() == 111 );
BOOST_TEST( udta2[1].value() == 222 );
BOOST_TEST( udta2[2].value() == 333 );
udta2.reset();
BOOST_TEST( udta2.get() == 0 );
BOOST_TEST( udta.use_count() == 1 );
Merged revision(s) 81149, 81159, 81171, 81174, 81219-81220, 81222-81224, 81226, 81229-81239, 81242, 81253, 81257-81262, 81265-81268, 81271-81272, 81275-81277, 81299-81300 from trunk: Implement shared_ptr<X[]>, weak_ptr<X[]>. Refs #1113. ........ Fix shared_ptr<T[]> EDG issues. ........ Disable make_shared<T> overloads when T is Q[]. ........ Add catch(...) clauses to sp_array_test.cpp. ........ Add allocate_shared and make_shared for shared_ptr arrays of runtime size. Fulfills need for allocate_shared_array and make_shared_array. ........ Update Jamfile.v2 to run make_shared array tests and allocate_shared array tests. ........ Fix g++ issues. ........ Add specialization of sp_if_not_array<T[N]>. ........ Rename make_shared.hpp to make_shared_object.hpp, include from make_shared.hpp. ........ Add make_shared_array_args_test.cpp. ........ Add support for make_shared of array of arrays. Correctly destroy elements and construct elements for the variadic template constructor variants. ........ Fix sp_convertible<T const[], T const[]>. ........ Update smart_ptr/detail/array_helper to have create and create_noinit for non-array case. ........ Rename sp_convertible_test.cpp to shared_ptr_convertible_test.cpp. ........ Don't treat array_helper create and create_noinit for array types as a special case. ........ Add sp_convertible_test.cpp. ........ Fix array_helper (create_noinit and use of args...). ........ Update allocate_shared and make_shared to treat multidimensional array as single dimension. Remove detail array_helper. Add detail array traits. Update tests. ........ Simplify array_deleter interface ........ Add missing semicolon. ........ Fix typo. ........ Add tests for variadic template constructors overload of array forms of make_shared and allocate_shared for multidimensional arrays and up to 9 constructor arguments. ........ Add support for shared_ptr<X[N>. ........ Add C++11 initializer list support for make_shared and allocate_shared array forms. ........ Clean up code in allocate_shared_array.hpp and make_shared_array.hpp ........ Change make_shared and allocate_shared array form semantics with initializer lists overload that takes no size. ........ Disable make_shared for arrays when the compiler doesn't support partial specialization or SFINAE. ........ For allocate_shared and make_shared: Separate test case that g++ does support yet. Remove macros testing for no partial specialization in traits. Add additional traits. ........ Actually remove test cases from make_shared_array_create_test.cpp and allocate_shared_array_create_test.cpp that g++ does not handle. ........ Add overloads to support fixed size arrays, T[N], to allocate_shared (variadic) and make_shared (variadic) and make_shared_noinit. ........ Add additional overload for make_shared and allocate_shared for arrays for fixed size arrays and initializer lists. ........ Add assertion to overload of make_shared and allocate_shared for T[N] with initializer lists. Rename detail type to be more intuitive. ........ Add allocate_shared_array_args_test.cpp. ........ Keep old definition of sp_assert_convertible when BOOST_SP_NO_SP_CONVERTIBLE is set. ........ Updated shared_array to match shared_ptr. Refs #1113. ........ Add final overload of make_shared and allocate_shared (array forms) for T[][N] with C++11 initializer lists. ........ Change traits for initializer list for g++ ........ Tidy long line formatting in allocate_shared_array.hpp and make_shared_array.hpp ........ Update tests for make_shared and allocate_shared array forms, for normal case, initializer lists, variadic template arguments, for arrays and fixed size arrays. ........ Update Jamfile.v2 with two new smart_ptr tests for allocate_shared and make_shared ........ [SVN r81339]
2012-11-14 13:52:11 +00:00
BOOST_TEST( udta2.use_count() == 0 );
BOOST_TEST( UDT_use_count == 4 ); // reality check
// test shared_ptr with a user defined type
UDT * up = new UDT;
boost::shared_ptr<UDT> sup ( up );
BOOST_TEST( up == sup.get() );
BOOST_TEST( sup.use_count() == 1 );
sup->value( 54321 ) ;
BOOST_TEST( sup->value() == 54321 );
BOOST_TEST( up->value() == 54321 );
boost::shared_ptr<UDT> sup2;
sup2 = sup;
BOOST_TEST( sup2->value() == 54321 );
BOOST_TEST( sup.use_count() == 2 );
BOOST_TEST( sup2.use_count() == 2 );
sup2 = sup2;
BOOST_TEST( sup2->value() == 54321 );
BOOST_TEST( sup.use_count() == 2 );
BOOST_TEST( sup2.use_count() == 2 );
std::cout << "OK\n";
}
int main()
{
test();
return boost::report_errors();
}