corelib/tests/ptr-weak.cc

487 lines
12 KiB
C++
Raw Permalink Normal View History

#include <ebcl/Pointers.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace ebcl;
class WeakPointerTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( WeakPointerTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testSharedCons );
CPPUNIT_TEST( testSharedConsEmpty );
CPPUNIT_TEST( testSharedConsCastC2P );
CPPUNIT_TEST( testSharedConsCastP2C );
CPPUNIT_TEST( testSharedConsCastBad );
CPPUNIT_TEST( testSharedAss );
CPPUNIT_TEST( testSharedAssEmpty );
CPPUNIT_TEST( testSharedAssCastC2P );
CPPUNIT_TEST( testSharedAssCastP2C );
CPPUNIT_TEST( testSharedAssCastBad );
CPPUNIT_TEST( testCopyCons );
CPPUNIT_TEST( testCopyConsCastC2P );
CPPUNIT_TEST( testCopyConsCastP2C );
CPPUNIT_TEST( testCopyConsCastBad );
CPPUNIT_TEST( testCopyAss );
CPPUNIT_TEST( testCopyAssCastC2P );
CPPUNIT_TEST( testCopyAssCastP2C );
CPPUNIT_TEST( testCopyAssCastBad );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveConsCastC2P );
CPPUNIT_TEST( testMoveConsCastP2C );
CPPUNIT_TEST( testMoveConsCastBad );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testMoveAssCastC2P );
CPPUNIT_TEST( testMoveAssCastP2C );
CPPUNIT_TEST( testMoveAssCastBad );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST( testClear );
CPPUNIT_TEST( testBecomesNull );
CPPUNIT_TEST( testMakeOwned );
CPPUNIT_TEST_SUITE_END( );
public:
void tearDown( );
void testEmpty( );
void testSharedCons( );
void testSharedConsEmpty( );
void testSharedConsCastC2P( );
void testSharedConsCastP2C( );
void testSharedConsCastBad( );
void testSharedAss( );
void testSharedAssEmpty( );
void testSharedAssCastC2P( );
void testSharedAssCastP2C( );
void testSharedAssCastBad( );
void testCopyCons( );
void testCopyConsCastC2P( );
void testCopyConsCastP2C( );
void testCopyConsCastBad( );
void testCopyAss( );
void testCopyAssCastC2P( );
void testCopyAssCastP2C( );
void testCopyAssCastBad( );
void testMoveCons( );
void testMoveConsCastC2P( );
void testMoveConsCastP2C( );
void testMoveConsCastBad( );
void testMoveAss( );
void testMoveAssCastC2P( );
void testMoveAssCastP2C( );
void testMoveAssCastBad( );
void testSwap( );
void testClear( );
void testBecomesNull( );
void testMakeOwned( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( WeakPointerTest );
/*----------------------------------------------------------------------------*/
#include "ptr-common.hh"
using Ptr = T_SharedPtr< T_Test >;
using WPtr = T_WeakPtr< T_Test >;
void WeakPointerTest::tearDown( )
{
T_Test::dCalled = 0;
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testEmpty( )
{
{
WPtr test;
CPPUNIT_ASSERT( !test );
CPPUNIT_ASSERT_EQUAL( false , bool( test ) );
CPPUNIT_ASSERT_EQUAL( (T_Test *) nullptr , (T_Test *) test );
}
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testSharedCons( )
{
Ptr shared( NewShared< T_Test >( ) );
{
WPtr test( shared );
CPPUNIT_ASSERT( bool( test ) );
CPPUNIT_ASSERT_EQUAL( false , !test );
CPPUNIT_ASSERT( (T_Test *) test != nullptr );
CPPUNIT_ASSERT( test == shared );
CPPUNIT_ASSERT( shared == test );
CPPUNIT_ASSERT_EQUAL( -1 , test->field );
}
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testSharedConsEmpty( )
{
{
Ptr shared;
WPtr test( shared );
CPPUNIT_ASSERT( !test );
CPPUNIT_ASSERT_EQUAL( false , bool( test ) );
CPPUNIT_ASSERT_EQUAL( (T_Test *) nullptr , (T_Test *) test );
CPPUNIT_ASSERT( test == shared );
CPPUNIT_ASSERT( shared == test );
}
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testSharedConsCastC2P( )
{
T_SharedPtr< T_TestChild > shared( NewShared< T_TestChild >( ) );
WPtr test( shared );
CPPUNIT_ASSERT( bool( test ) );
CPPUNIT_ASSERT( (T_Test *) test == (T_TestChild *) shared );
}
void WeakPointerTest::testSharedConsCastP2C( )
{
Ptr shared( NewShared< T_TestChild >( ) );
T_WeakPtr< T_TestChild > test( shared );
CPPUNIT_ASSERT( bool( test ) );
CPPUNIT_ASSERT( (T_TestChild *) test == (T_Test *) shared );
}
void WeakPointerTest::testSharedConsCastBad( )
{
Ptr shared( NewShared< T_OtherChild >( 12 , 34 ) );
try {
T_WeakPtr< T_TestChild > test( shared );
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testSharedAss( )
{
Ptr shared( NewShared< T_Test >( ) );
{
WPtr test;
test = shared;
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( test == shared );
CPPUNIT_ASSERT( shared == test );
CPPUNIT_ASSERT_EQUAL( -1 , test->field );
}
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testSharedAssEmpty( )
{
Ptr shared;
WPtr test;
test = shared;
CPPUNIT_ASSERT( !test );
shared = NewShared< T_Test >( );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testSharedAssCastC2P( )
{
T_SharedPtr< T_TestChild > shared( NewShared< T_TestChild >( ) );
WPtr test;
test = shared;
CPPUNIT_ASSERT( bool( test ) );
CPPUNIT_ASSERT( (T_Test *) test == (T_TestChild *) shared );
}
void WeakPointerTest::testSharedAssCastP2C( )
{
Ptr shared( NewShared< T_TestChild >( ) );
T_WeakPtr< T_TestChild > test;
test = shared;
CPPUNIT_ASSERT( bool( test ) );
CPPUNIT_ASSERT( (T_TestChild *) test == (T_Test *) shared );
}
void WeakPointerTest::testSharedAssCastBad( )
{
Ptr shared( NewShared< T_OtherChild >( 12 , 34 ) );
T_WeakPtr< T_TestChild > test;
try {
test = shared;
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
CPPUNIT_ASSERT( !test );
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testCopyCons( )
{
Ptr shared( NewShared< T_Test >( ) );
WPtr wtest( shared );
{
WPtr test( wtest );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( test == shared );
CPPUNIT_ASSERT( shared == test );
CPPUNIT_ASSERT( test == wtest );
CPPUNIT_ASSERT( wtest == test );
CPPUNIT_ASSERT_EQUAL( -1 , test->field );
}
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testCopyConsCastC2P( )
{
auto shared( NewShared< T_TestChild >( ) );
T_WeakPtr< T_TestChild > weak( shared );
WPtr test( weak );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( (T_TestChild*) weak == (T_Test*) test );
shared.clear( );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testCopyConsCastP2C( )
{
Ptr shared( NewShared< T_TestChild >( ) );
WPtr weak( shared );
T_WeakPtr< T_TestChild > test( weak );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( (T_Test*) weak == (T_TestChild*) test );
shared.clear( );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testCopyConsCastBad( )
{
Ptr shared( NewShared< T_OtherChild >( 12 , 34 ) );
WPtr weak( shared );
try {
T_WeakPtr< T_TestChild > test( weak );
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testCopyAss( )
{
Ptr shared( NewShared< T_Test >( ) );
WPtr wtest( shared );
{
WPtr test;
test = wtest;
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( test == shared );
CPPUNIT_ASSERT( shared == test );
CPPUNIT_ASSERT( test == wtest );
CPPUNIT_ASSERT( wtest == test );
CPPUNIT_ASSERT_EQUAL( -1 , test->field );
}
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testCopyAssCastC2P( )
{
auto shared( NewShared< T_TestChild >( ) );
T_WeakPtr< T_TestChild > weak( shared );
WPtr test;
test = weak;
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( (T_TestChild*) weak == (T_Test*) test );
shared.clear( );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testCopyAssCastP2C( )
{
Ptr shared( NewShared< T_TestChild >( ) );
WPtr weak( shared );
T_WeakPtr< T_TestChild > test;
test = weak;
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( (T_Test*) weak == (T_TestChild*) test );
shared.clear( );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testCopyAssCastBad( )
{
Ptr shared( NewShared< T_OtherChild >( 12 , 34 ) );
WPtr weak( shared );
T_WeakPtr< T_TestChild > test;
try {
test = weak;
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
CPPUNIT_ASSERT( !test );
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testMoveCons( )
{
Ptr shared( NewShared< T_Test >( ) );
WPtr weak( shared );
WPtr test( std::move( weak ) );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( !weak );
CPPUNIT_ASSERT( shared == test );
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testMoveConsCastC2P( )
{
auto shared( NewShared< T_TestChild >( ) );
T_WeakPtr< T_TestChild > weak( shared );
WPtr test( std::move( weak ) );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( !weak );
CPPUNIT_ASSERT( (T_TestChild*) shared == (T_Test*) test );
}
void WeakPointerTest::testMoveConsCastP2C( )
{
Ptr shared( NewShared< T_TestChild >( ) );
WPtr weak( shared );
T_WeakPtr< T_TestChild > test( std::move( weak ) );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( !weak );
CPPUNIT_ASSERT( (T_Test*) shared == (T_TestChild*) test );
}
void WeakPointerTest::testMoveConsCastBad( )
{
Ptr shared( NewShared< T_OtherChild >( 12 , 34 ) );
WPtr weak( shared );
try {
T_WeakPtr< T_TestChild > test( std::move( weak ) );
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
CPPUNIT_ASSERT( weak );
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testMoveAss( )
{
Ptr shared( NewShared< T_Test >( ) );
WPtr weak( shared );
WPtr test;
test = std::move( weak );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( !weak );
CPPUNIT_ASSERT( shared == test );
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
}
void WeakPointerTest::testMoveAssCastC2P( )
{
auto shared( NewShared< T_TestChild >( ) );
T_WeakPtr< T_TestChild > weak( shared );
WPtr test;
test = std::move( weak );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( !weak );
CPPUNIT_ASSERT( (T_TestChild*) shared == (T_Test*) test );
}
void WeakPointerTest::testMoveAssCastP2C( )
{
Ptr shared( NewShared< T_TestChild >( ) );
WPtr weak( shared );
T_WeakPtr< T_TestChild > test;
test = std::move( weak );
CPPUNIT_ASSERT( test );
CPPUNIT_ASSERT( !weak );
CPPUNIT_ASSERT( (T_Test*) shared == (T_TestChild*) test );
}
void WeakPointerTest::testMoveAssCastBad( )
{
Ptr shared( NewShared< T_OtherChild >( 12 , 34 ) );
WPtr weak( shared );
T_WeakPtr< T_TestChild > test;
try {
test = std::move( weak );
CPPUNIT_FAIL( "std::bad_cast not thrown" );
} catch ( std::bad_cast const& ) {
// OK
}
CPPUNIT_ASSERT( !test );
CPPUNIT_ASSERT( weak );
}
/*----------------------------------------------------------------------------*/
void WeakPointerTest::testSwap( )
{
Ptr stest1( NewShared< T_Test >( 1 ) );
Ptr stest2( NewShared< T_Test >( 2 ) );
WPtr test1( stest1 ) , test2( stest2 );
swap( stest1 , stest2 );
CPPUNIT_ASSERT_EQUAL( 1 , stest2->field );
CPPUNIT_ASSERT_EQUAL( 2 , stest1->field );
}
void WeakPointerTest::testClear( )
{
Ptr shared( NewShared< T_Test >( ) );
WPtr test( shared );
test.clear( );
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testBecomesNull( )
{
WPtr test;
{
Ptr shared( NewShared< T_Test >( ) );
test = shared;
}
CPPUNIT_ASSERT_EQUAL( 1 , T_Test::dCalled );
CPPUNIT_ASSERT( !test );
}
void WeakPointerTest::testMakeOwned( )
{
Ptr shared( NewShared< T_Test >( ) );
WPtr test( shared );
T_OwnPtr< T_Test > otest( shared.makeOwned( ) );
CPPUNIT_ASSERT_EQUAL( 0 , T_Test::dCalled );
CPPUNIT_ASSERT( !test );
}