From: Andrew Cox Date: Mon, 18 May 2015 18:25:45 +0000 (+0100) Subject: Tests for IntrusivePtr and deleted ScopedPointer X-Git-Tag: accepted/tizen/common/20150529.134107~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7427dc41cf97bc75fb9c3e7816ae2ae7f9603f99;p=platform%2Fcore%2Fuifw%2Fdali-core.git Tests for IntrusivePtr and deleted ScopedPointer One last test still to do. Change-Id: I10345cf9baf29ae1492f1940cbccdcd0a18712b1 Signed-off-by: Andrew Cox --- diff --git a/automated-tests/src/dali/CMakeLists.txt b/automated-tests/src/dali/CMakeLists.txt index 57d02c2..7caf86a 100644 --- a/automated-tests/src/dali/CMakeLists.txt +++ b/automated-tests/src/dali/CMakeLists.txt @@ -36,6 +36,7 @@ SET(TC_SOURCES utc-Dali-HoverProcessing.cpp utc-Dali-Image.cpp utc-Dali-ImageActor.cpp + utc-Dali-IntrusivePtr.cpp utc-Dali-KeyEvent.cpp utc-Dali-Layer.cpp utc-Dali-LocklessBuffer.cpp diff --git a/automated-tests/src/dali/utc-Dali-IntrusivePtr.cpp b/automated-tests/src/dali/utc-Dali-IntrusivePtr.cpp new file mode 100644 index 0000000..af67735 --- /dev/null +++ b/automated-tests/src/dali/utc-Dali-IntrusivePtr.cpp @@ -0,0 +1,480 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +#include +#include +#include +#include + +using namespace Dali; + +namespace +{ + +const int REPEAT = 1000; + +size_t g_creationCount = 0; +size_t g_destructionCount = 0; +size_t g_creationCountSubclass = 0; +size_t g_destructionCountSubclass = 0; +size_t g_creationCountUnrelated = 0; +size_t g_destructionCountUnrelated = 0; + +class Counted : public RefObject +{ +public: + Counted() + { + ++g_creationCount; + } + ~Counted() + { + ++g_destructionCount; + } +}; + +class CountedSubclass : public Counted +{ +public: + CountedSubclass() + { + ++g_creationCountSubclass; + } + ~CountedSubclass() + { + ++g_destructionCountSubclass; + } +}; + +class UnrelatedCounted : public RefObject +{ +public: + UnrelatedCounted() + { + ++g_creationCountUnrelated; + } + ~UnrelatedCounted() + { + ++g_destructionCountUnrelated; + } +}; +} + +/** + * Test that a default constructed pointer is null and harmless. + */ +int UtcDaliIntrusivePtrIntrusivePtr(void) +{ + tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr()" ); + + g_creationCount = g_destructionCount = 0; + + IntrusivePtr counted; + DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + // Test the pointer is null + DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION ); + DALI_TEST_EQUALS( &(*counted), (Counted*) 0, TEST_LOCATION ); + // Check destruction of the null smart pointer does nothing: + counted = IntrusivePtr(); + DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrIntrusivePtrTP(void) +{ + tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(T*)" ); + + g_creationCount = g_destructionCount = 0; + + IntrusivePtr counted( new Counted ); + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + counted = 0; + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION ); + + END_TEST; +} + +// Class is too simple for a negative case to be created: int UtcDaliIntrusiveIntrusivePtrTN(void) + +int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUP(void) +{ + tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" ); + + g_creationCount = g_destructionCount = g_creationCountSubclass = g_destructionCountSubclass = 0; + + IntrusivePtr countedSubclass( new CountedSubclass ); + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_creationCountSubclass, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCountSubclass, 0u, TEST_LOCATION ); + + IntrusivePtr counted( countedSubclass ); + DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION ); + + // Make loads more references: + std::vector< IntrusivePtr > intrusivePtrs; + for( int i = 0; i < REPEAT; ++i ) + { + intrusivePtrs.push_back( IntrusivePtr( countedSubclass ) ); + } + DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION ); + + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_creationCountSubclass, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + + END_TEST; +} + +// The negative version of this test would fail at compile time: +// int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrUN(void) + +int UtcDaliIntrusivePtrIntrusivePtrIntrusivePtrP(void) +{ + tet_infoline( "Testing Dali::IntrusivePtr::IntrusivePtr(IntrusivePtr const &)" ); + + // Pass a pointer to a constructed second object: + // Pass a pointer to null: + + g_creationCount = g_destructionCount = 0; + + IntrusivePtr counted( new Counted ); + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION ); + + IntrusivePtr counted2( counted ); + DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION ); + DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION ); + + // Make loads more references: + std::vector< IntrusivePtr > intrusivePtrs; + for( int i = 0; i < REPEAT; ++i ) + { + intrusivePtrs.push_back( IntrusivePtr( counted ) ); + } + DALI_TEST_EQUALS( counted->ReferenceCount(), 2 + REPEAT, TEST_LOCATION ); + + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + + intrusivePtrs.clear(); + + DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION ); + + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 0u, TEST_LOCATION ); + + counted.Reset(); + DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION ); + counted2.Reset(); + + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrGetP(void) +{ + tet_infoline( "Testing Dali::IntrusivePtr::Get()" ); + + IntrusivePtr counted( new Counted ); + DALI_TEST_CHECK( counted.Get() != 0 ); + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrGetN(void) +{ + tet_infoline( "Testing Dali::IntrusivePtr::Get()" ); + + g_creationCount = 0; + + IntrusivePtr counted( 0 ); + DALI_TEST_CHECK( counted.Get() == 0 ); + DALI_TEST_EQUALS( g_creationCount, 0u, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrArrowOperatorP(void) +{ + tet_infoline( "Positive Test for Dali::IntrusivePtr::operator->()" ); + + IntrusivePtr counted( new Counted ); + DALI_TEST_CHECK( (counted.operator->()) != 0 ); + DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrArrowOperatorN(void) +{ + tet_infoline( "Negative Test for Dali::IntrusivePtr::operator->()" ); + + IntrusivePtr counted; + DALI_TEST_CHECK( (counted.operator->()) == 0 ); + + END_TEST; +} + +int UtcDaliIntrusivePtrIndirectionOperatorP(void) +{ + tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" ); + + IntrusivePtr counted( new Counted ); + DALI_TEST_CHECK( &(counted.operator*()) != 0 ); + DALI_TEST_EQUALS( (*counted).ReferenceCount(), 1, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrIndirectionOperatorN(void) +{ + tet_infoline( "Negative Test for Dali::IntrusivePtr::operator*()" ); + + IntrusivePtr counted; + DALI_TEST_CHECK( &(counted.operator*()) == 0 ); + + END_TEST; +} + +int UtcDaliIntrusivePtrResetP(void) +{ + tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" ); + + IntrusivePtr counted( new Counted ); + DALI_TEST_CHECK( counted.Get() != 0 ); + counted.Reset(); + DALI_TEST_CHECK( counted.Get() == 0 ); + + END_TEST; +} + +int UtcDaliIntrusivePtrResetN(void) +{ + tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset()" ); + + IntrusivePtr counted; + Counted* firstGet = counted.Get(); + counted.Reset(); + DALI_TEST_EQUALS( counted.Get(), firstGet, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrResetTP(void) +{ + tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset(T*)" ); + + g_creationCount = g_destructionCount = 0; + + IntrusivePtr counted( new Counted ); + + IntrusivePtr counted2( new Counted ); + + DALI_TEST_EQUALS( counted->ReferenceCount(), 1, TEST_LOCATION ); + DALI_TEST_EQUALS( counted2->ReferenceCount(), 1, TEST_LOCATION ); + + counted.Reset( counted2.Get() ); + + DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION ); + DALI_TEST_EQUALS( counted2->ReferenceCount(), 2, TEST_LOCATION ); + + DALI_TEST_EQUALS( counted.Get(), counted2.Get(), TEST_LOCATION ); + + DALI_TEST_EQUALS( g_creationCount, 2u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION ); + + counted2.Reset( (Counted*) 0 ); + counted.Reset( counted2.Get() ); + DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION ); + + // Check that reseting nulls is harmless: + counted2.Reset( counted.Get() ); + counted.Reset( counted2.Get() ); + + DALI_TEST_EQUALS( g_destructionCount, 2u, TEST_LOCATION ); + + END_TEST; +} + + +int UtcDaliIntrusivePtrResetTN(void) +{ + tet_infoline( "Negative Test for Dali::IntrusivePtr::Reset(T*)" ); + + g_creationCount = g_destructionCount = 0; + + IntrusivePtr counted( new Counted ); + + counted.Reset( (Counted*) 0 ); + + DALI_TEST_EQUALS( counted.Get(), (Counted*) 0, TEST_LOCATION ); + DALI_TEST_EQUALS( g_creationCount, 1u, TEST_LOCATION ); + DALI_TEST_EQUALS( g_destructionCount, 1u, TEST_LOCATION ); + + END_TEST; +} + +int UtcDaliIntrusivePtrOperatorBooleanTypeP(void) +{ + tet_infoline( "Positive Test for Dali::IntrusivePtr::operator Booleantype()" ); + + IntrusivePtr counted( new Counted ); + DALI_TEST_CHECK( counted.operator BooleanType() != 0 ); + DALI_TEST_CHECK( counted ); + counted.Reset(); + DALI_TEST_CHECK( counted.operator BooleanType() == 0 ); + + END_TEST; +} + +int UtcDaliIntrusivePtrOperatorBooleanTypeN(void) +{ + tet_infoline( "Negative Test for Dali::IntrusivePtr::operator Booleantype()" ); + + IntrusivePtr counted; + DALI_TEST_CHECK( counted.operator BooleanType() == 0 ); + DALI_TEST_CHECK( !counted ); + END_TEST; +} + +/** Equality of two different types*/ +int UtcDaliIntrusivePtrOperatorEqualTU(void) +{ + tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U)" ); + + IntrusivePtr counted1( new Counted ); + IntrusivePtr countedSubclass1( new CountedSubclass ); + IntrusivePtr countedSubclass2( new CountedSubclass ); + IntrusivePtr counted2( countedSubclass2 ); + + DALI_TEST_EQUALS( operator==( counted1, countedSubclass1 ), false, TEST_LOCATION ); + DALI_TEST_EQUALS( operator==( counted2, countedSubclass2 ), true, TEST_LOCATION ); + END_TEST; +} + +/** Inequality of two different types*/ +int UtcDaliIntrusivePtrOperatorNotEqualTU(void) +{ + tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U)" ); + + IntrusivePtr counted1( new Counted ); + IntrusivePtr countedSubclass1( new CountedSubclass ); + IntrusivePtr countedSubclass2( new CountedSubclass ); + IntrusivePtr counted2( countedSubclass2 ); + + DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1 ), true, TEST_LOCATION ); + DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2 ), false, TEST_LOCATION ); + END_TEST; +} + +/** Equality of two different types where right hand side is a raw pointer */ +int UtcDaliIntrusivePtrOperatorEqualRightPointerTU(void) +{ + tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T, U*)" ); + + IntrusivePtr counted1( new Counted ); + IntrusivePtr countedSubclass1( new CountedSubclass ); + IntrusivePtr countedSubclass2( new CountedSubclass ); + IntrusivePtr counted2( countedSubclass2 ); + + DALI_TEST_EQUALS( operator==( counted1, countedSubclass1.Get() ), false, TEST_LOCATION ); + DALI_TEST_EQUALS( operator==( counted2, countedSubclass2.Get() ), true, TEST_LOCATION ); + END_TEST; +} + +/** Inequality of two different types where the right hand side is a raw pointer */ +int UtcDaliIntrusivePtrOperatorNotEqualRightPointerTU(void) +{ + tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T, U*)" ); + + IntrusivePtr counted1( new Counted ); + IntrusivePtr countedSubclass1( new CountedSubclass ); + IntrusivePtr countedSubclass2( new CountedSubclass ); + IntrusivePtr counted2( countedSubclass2 ); + + DALI_TEST_EQUALS( operator!=( counted1, countedSubclass1.Get() ), true, TEST_LOCATION ); + DALI_TEST_EQUALS( operator!=( counted2, countedSubclass2.Get() ), false, TEST_LOCATION ); + END_TEST; +} + +/** Equality of two different types where left hand side is a raw pointer */ +int UtcDaliIntrusivePtrOperatorEqualLeftPointerTU(void) +{ + tet_infoline( "Test for Dali::IntrusivePtr::operator ==(T*, U)" ); + + IntrusivePtr counted1( new Counted ); + IntrusivePtr countedSubclass1( new CountedSubclass ); + IntrusivePtr countedSubclass2( new CountedSubclass ); + IntrusivePtr counted2( countedSubclass2 ); + + DALI_TEST_EQUALS( operator==( counted1.Get(), countedSubclass1 ), false, TEST_LOCATION ); + DALI_TEST_EQUALS( operator==( counted2.Get(), countedSubclass2 ), true, TEST_LOCATION ); + END_TEST; +} + +/** Inequality of two different types where the left hand side is a raw pointer */ +int UtcDaliIntrusivePtrOperatorNotEqualLeftPointerTU(void) +{ + tet_infoline( "Test for Dali::IntrusivePtr::operator !=(T*, U)" ); + + IntrusivePtr counted1( new Counted ); + IntrusivePtr countedSubclass1( new CountedSubclass ); + IntrusivePtr countedSubclass2( new CountedSubclass ); + IntrusivePtr counted2( countedSubclass2 ); + + DALI_TEST_EQUALS( operator!=( counted1.Get(), countedSubclass1 ), true, TEST_LOCATION ); + DALI_TEST_EQUALS( operator!=( counted2.Get(), countedSubclass2 ), false, TEST_LOCATION ); + END_TEST; +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dali/devel-api/common/scoped-pointer.h b/dali/devel-api/common/scoped-pointer.h deleted file mode 100644 index dbd619f..0000000 --- a/dali/devel-api/common/scoped-pointer.h +++ /dev/null @@ -1,101 +0,0 @@ -#ifndef __DALI_SCOPED_POINTER_H__ -#define __DALI_SCOPED_POINTER_H__ -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -namespace Dali -{ - -/** - * @brief Deletes the object pointed-to when it goes out of scope. - * - * A simple template class to call delete on an owned pointer when it goes - * out of scope, whether that be by ordinary return or stack unwind for - * exception throw. - */ -template -class ScopedPointer -{ -public: - - /** - * @brief Construct a ScopedPointer guarding a new Owned*. - */ - ScopedPointer( Owned * const owned ) : - mOwned( owned ) - {} - - /** - * @brief Destroy the ScopedPointer and clean up its Owned*. - */ - ~ScopedPointer() - { - if( mOwned != 0 ) - { - delete mOwned; - mOwned = 0; - } - } - - /** - * @brief Getter for the underlying pointer. - * @return The Owned* guarded by this object. - */ - Owned* Get() const - { - return mOwned; - } - - /** - * @brief Give up ownership of the object guarded by this pointer. - * @return The Owned* previously guarded by this object. - */ - Owned* Release() - { - Owned* const owned = mOwned; - mOwned = 0; - return owned; - } - - /** - * @brief Dereference this pointer. - */ - Owned& operator*() const - { - return *mOwned; - } - - /** - * @brief Allow member access through arrow notation. - */ - Owned * operator->() const - { - return mOwned; - } - -private: - - // Non-copyable: - ScopedPointer( const ScopedPointer& rhs ); - ScopedPointer& operator = ( const ScopedPointer& rhs ); - - Owned* mOwned; -}; - -} /* namespace Dali */ - -#endif /* __DALI_SCOPED_POINTER_H__ */ diff --git a/dali/devel-api/file.list b/dali/devel-api/file.list index ee2a960..372a31c 100644 --- a/dali/devel-api/file.list +++ b/dali/devel-api/file.list @@ -38,8 +38,7 @@ devel_api_core_common_header_files = \ $(devel_api_src_dir)/common/map-wrapper.h \ $(devel_api_src_dir)/common/mutex.h \ $(devel_api_src_dir)/common/ref-counted-dali-vector.h \ - $(devel_api_src_dir)/common/scoped-pointer.h \ - $(devel_api_src_dir)/common/set-wrapper.h + $(devel_api_src_dir)/common/set-wrapper.h devel_api_core_dynamics_header_files = \ $(devel_api_src_dir)/dynamics/dynamics.h \