forked from boostorg/smart_ptr
Add tests for unique_ptr casts.
This commit is contained in:
@ -7,11 +7,11 @@
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#ifndef BOOST_POINTER_CAST_HPP
|
||||
#define BOOST_POINTER_CAST_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
//static_pointer_cast overload for raw pointers
|
||||
@ -90,8 +90,11 @@ template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std
|
||||
//static_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> static_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
detail::assert_safe_moving_upcast<T, U>();
|
||||
return std::unique_ptr<T>( static_cast<T*>( r.release() ) );
|
||||
|
||||
return std::unique_ptr<T>( static_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
//dynamic_pointer_cast overload for std::unique_ptr
|
||||
@ -107,13 +110,17 @@ template<class T, class U> std::unique_ptr<T> dynamic_pointer_cast( std::unique_
|
||||
//const_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> const_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
return std::unique_ptr<T>( const_cast<T*>( r.release() ) );
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( const_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
//reinterpret_pointer_cast overload for std::unique_ptr
|
||||
template<class T, class U> std::unique_ptr<T> reinterpret_pointer_cast( std::unique_ptr<U> && r ) BOOST_NOEXCEPT
|
||||
{
|
||||
return std::unique_ptr<T>( reinterpret_cast<T*>( r.release() ) );
|
||||
typedef typename std::unique_ptr<T>::element_type E;
|
||||
|
||||
return std::unique_ptr<T>( reinterpret_cast<E*>( r.release() ) );
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
@ -186,5 +186,8 @@ import testing ;
|
||||
|
||||
[ run sp_hash_test2.cpp ]
|
||||
[ run sp_hash_test3.cpp ]
|
||||
|
||||
[ run pointer_cast_test2.cpp ]
|
||||
[ run pointer_cast_test3.cpp ]
|
||||
;
|
||||
}
|
||||
|
214
test/pointer_cast_test2.cpp
Normal file
214
test/pointer_cast_test2.cpp
Normal file
@ -0,0 +1,214 @@
|
||||
//
|
||||
// pointer_cast_test2.cpp - a test for unique_ptr casts
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <memory>
|
||||
|
||||
struct B
|
||||
{
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct D: B
|
||||
{
|
||||
};
|
||||
|
||||
static void test_static_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int> p2 = boost::static_pointer_cast<int>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const> p2 = boost::static_pointer_cast<int const>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int[]> p2 = boost::static_pointer_cast<int[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const[]> p2 = boost::static_pointer_cast<int const[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<D> p1( new D );
|
||||
D * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<B> p2 = boost::static_pointer_cast<B>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<B> p1( new D );
|
||||
B * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<D> p2 = boost::static_pointer_cast<D>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
static void test_const_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int const> p1( new int );
|
||||
int const * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int> p2 = boost::const_pointer_cast<int>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const> p2 = boost::const_pointer_cast<int const>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
int * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int const[]> p2 = boost::const_pointer_cast<int const[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
static void test_dynamic_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<D> p1( new D );
|
||||
D * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<B> p2 = boost::dynamic_pointer_cast<B>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<B> p1( new D );
|
||||
B * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<D> p2 = boost::dynamic_pointer_cast<D>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
static void test_reinterpret_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
void * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<char> p2 = boost::reinterpret_pointer_cast<char>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
p1 = boost::reinterpret_pointer_cast<int>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int> p1( new int );
|
||||
void * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<char[]> p2 = boost::reinterpret_pointer_cast<char[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
p1 = boost::reinterpret_pointer_cast<int>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<int[]> p1( new int[ 1 ] );
|
||||
void * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<char[]> p2 = boost::reinterpret_pointer_cast<char[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
|
||||
p1 = boost::reinterpret_pointer_cast<int[]>( std::move( p2 ) );
|
||||
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
BOOST_TEST_EQ( p1.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_static_cast();
|
||||
test_const_cast();
|
||||
test_dynamic_cast();
|
||||
test_reinterpret_cast();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
49
test/pointer_cast_test3.cpp
Normal file
49
test/pointer_cast_test3.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
//
|
||||
// pointer_cast_test3.cpp - a test for unique_ptr casts
|
||||
//
|
||||
// Copyright 2016 Peter Dimov
|
||||
//
|
||||
// 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
|
||||
//
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <boost/pointer_cast.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
#include <memory>
|
||||
|
||||
// This test fails on msvc-10.0, 11.0, 12.0 because
|
||||
// their unique_ptr implementation can't compile the
|
||||
// initialization of p1
|
||||
|
||||
static void test_const_cast()
|
||||
{
|
||||
{
|
||||
std::unique_ptr<int const[]> p1( new int[ 1 ] );
|
||||
int const * q1 = p1.get();
|
||||
|
||||
std::unique_ptr<int[]> p2 = boost::const_pointer_cast<int[]>( std::move( p1 ) );
|
||||
|
||||
BOOST_TEST( p1.get() == 0 );
|
||||
BOOST_TEST_EQ( p2.get(), q1 );
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_const_cast();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user