corelib/tests/ptr-owned.cc

280 lines
6.4 KiB
C++
Raw Normal View History

#include <lw/lib/Pointers.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
class OwnPointerTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( OwnPointerTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testNewOwned );
CPPUNIT_TEST( testClear );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testMakeShared );
CPPUNIT_TEST( testOwnRaw );
CPPUNIT_TEST( testOwnRawC2P );
CPPUNIT_TEST( testOwnRawP2C );
CPPUNIT_TEST( testOwnRawBad );
CPPUNIT_TEST( testMoveConsCastC2P );
CPPUNIT_TEST( testMoveConsCastP2C );
CPPUNIT_TEST( testMoveConsCastBad );
CPPUNIT_TEST( testMoveAssCastC2P );
CPPUNIT_TEST( testMoveAssCastP2C );
CPPUNIT_TEST( testMoveAssCastBad );
CPPUNIT_TEST_SUITE_END( );
public:
void tearDown( );
void testEmpty( );
void testNewOwned( );
void testClear( );
void testSwap( );
void testMoveCons( );
void testMoveAss( );
void testMakeShared( );
void testOwnRaw( );
void testOwnRawC2P( );
void testOwnRawP2C( );
void testOwnRawBad( );
void testMoveConsCastC2P( );
void testMoveConsCastP2C( );
void testMoveConsCastBad( );
void testMoveAssCastC2P( );
void testMoveAssCastP2C( );
void testMoveAssCastBad( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( OwnPointerTest );
/*----------------------------------------------------------------------------*/
#include "ptr-common.hh"
using Ptr = T_OwnPtr< T_Test >;
void OwnPointerTest::tearDown( )
{
T_Test::dCalled = 0;
}
/*----------------------------------------------------------------------------*/
void OwnPointerTest::testEmpty( )
{
{
Ptr p;
CPPUNIT_ASSERT( !p );
CPPUNIT_ASSERT( p.get( ) == nullptr );
}
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
}
void OwnPointerTest::testNewOwned( )
{
{
Ptr p( NewOwned< T_Test >( ) );
CPPUNIT_ASSERT( bool( p ) );
CPPUNIT_ASSERT( p.get( ) != nullptr );
CPPUNIT_ASSERT( p->field == -1 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testClear( )
{
Ptr p( NewOwned< T_Test >( ) );
p.clear( );
CPPUNIT_ASSERT( !p );
CPPUNIT_ASSERT( p.get( ) == nullptr );
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testSwap( )
{
{
Ptr p1( NewOwned< T_Test >( 123 ) );
Ptr p2( NewOwned< T_Test >( 456 ) );
swap( p1 , p2 );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( bool( p1 ) );
CPPUNIT_ASSERT( p1->field == 456 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 123 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 2 );
}
void OwnPointerTest::testMoveCons( )
{
{
Ptr p1( NewOwned< T_Test >( 123 ) );
Ptr p2( std::move( p1 ) );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( !p1 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 123 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testMoveAss( )
{
{
Ptr p1( NewOwned< T_Test >( 123 ) );
Ptr p2;
p2 = std::move( p1 );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( !p1 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 123 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testMakeShared( )
{
Ptr op( NewOwned< T_Test >( 123 ) );
auto sp( op.makeShared( ) );
CPPUNIT_ASSERT( !op );
CPPUNIT_ASSERT( bool( sp ) );
CPPUNIT_ASSERT( sp->field == 123 );
}
/*----------------------------------------------------------------------------*/
void OwnPointerTest::testOwnRaw( )
{
{
T_Test* obj( new T_Test( 123 ) );
Ptr p( OwnRawPointer( obj ) );
CPPUNIT_ASSERT( obj == nullptr );
CPPUNIT_ASSERT( bool( p ) );
CPPUNIT_ASSERT( p.get( ) != nullptr );
CPPUNIT_ASSERT( p->field == 123 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testOwnRawC2P( )
{
{
T_TestChild* obj( new T_TestChild( ) );
Ptr p( OwnRawPointer( obj ) );
CPPUNIT_ASSERT( obj == nullptr );
CPPUNIT_ASSERT( bool( p ) );
CPPUNIT_ASSERT( p.get( ) != nullptr );
CPPUNIT_ASSERT( p->field == 256 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testOwnRawP2C( )
{
T_Test* obj( new T_TestChild( ) );
T_OwnPtr< T_TestChild > p( OwnRawPointer< T_TestChild , T_Test >( obj ) );
CPPUNIT_ASSERT( obj == nullptr );
CPPUNIT_ASSERT( bool( p ) );
}
void OwnPointerTest::testOwnRawBad( )
{
T_Test* obj( new T_OtherChild( 123 , 456 ) );
try {
T_OwnPtr< T_TestChild > p( OwnRawPointer< T_TestChild , T_Test >( obj ) );
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
CPPUNIT_ASSERT( obj != nullptr );
delete obj;
}
/*----------------------------------------------------------------------------*/
void OwnPointerTest::testMoveConsCastC2P( )
{
{
T_OwnPtr< T_TestChild > p1( NewOwned< T_TestChild >( ) );
Ptr p2( std::move( p1 ) );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( !p1 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 256 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testMoveConsCastP2C( )
{
{
Ptr p1( NewOwned< T_TestChild >( ) );
T_OwnPtr< T_TestChild > p2( std::move( p1 ) );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( !p1 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 256 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testMoveConsCastBad( )
{
Ptr p1( NewOwned< T_OtherChild >( 123 , 456 ) );
try {
T_OwnPtr< T_TestChild > p2( std::move( p1 ) );
CPPUNIT_FAIL( "exception not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
}
/*----------------------------------------------------------------------------*/
void OwnPointerTest::testMoveAssCastC2P( )
{
{
T_OwnPtr< T_TestChild > p1( NewOwned< T_TestChild >( ) );
Ptr p2;
p2 = std::move( p1 );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( !p1 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 256 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testMoveAssCastP2C( )
{
{
Ptr p1( NewOwned< T_TestChild >( ) );
T_OwnPtr< T_TestChild > p2;
p2 = std::move( p1 );
CPPUNIT_ASSERT( T_Test::dCalled == 0 );
CPPUNIT_ASSERT( !p1 );
CPPUNIT_ASSERT( bool( p2 ) );
CPPUNIT_ASSERT( p2->field == 256 );
}
CPPUNIT_ASSERT( T_Test::dCalled == 1 );
}
void OwnPointerTest::testMoveAssCastBad( )
{
try {
Ptr p1( NewOwned< T_OtherChild >( 123 , 456 ) );
T_OwnPtr< T_TestChild > p2;
p2 = std::move( p1 );
CPPUNIT_FAIL( "exception not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
}