Tests for IntrusivePtr and deleted ScopedPointer 64/39564/2
authorAndrew Cox <andrew.cox@partner.samsung.com>
Mon, 18 May 2015 18:25:45 +0000 (19:25 +0100)
committerAndrew Cox <andrew.cox@partner.samsung.com>
Tue, 19 May 2015 11:46:32 +0000 (12:46 +0100)
One last test still to do.

Change-Id: I10345cf9baf29ae1492f1940cbccdcd0a18712b1
Signed-off-by: Andrew Cox <andrew.cox@partner.samsung.com>
automated-tests/src/dali/CMakeLists.txt
automated-tests/src/dali/utc-Dali-IntrusivePtr.cpp [new file with mode: 0644]
dali/devel-api/common/scoped-pointer.h [deleted file]
dali/devel-api/file.list

index 57d02c2..7caf86a 100644 (file)
@@ -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 (file)
index 0000000..af67735
--- /dev/null
@@ -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 <iostream>
+#include <stdlib.h>
+#include <dali/public-api/dali-core.h>
+#include <dali-test-suite-utils.h>
+
+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> 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<Counted>();
+  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> 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<U> const &)" );
+
+  g_creationCount = g_destructionCount = g_creationCountSubclass = g_destructionCountSubclass = 0;
+
+  IntrusivePtr<CountedSubclass> 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> counted( countedSubclass );
+  DALI_TEST_EQUALS( counted->ReferenceCount(), 2, TEST_LOCATION );
+
+  // Make loads more references:
+  std::vector< IntrusivePtr<Counted> > intrusivePtrs;
+  for( int i = 0; i < REPEAT; ++i )
+  {
+    intrusivePtrs.push_back( IntrusivePtr<Counted>( 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> 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<Counted> 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<Counted> > intrusivePtrs;
+  for( int i = 0; i < REPEAT; ++i )
+  {
+    intrusivePtrs.push_back( IntrusivePtr<Counted>( 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> 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> 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> 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> counted;
+  DALI_TEST_CHECK( (counted.operator->()) == 0 );
+
+  END_TEST;
+}
+
+int UtcDaliIntrusivePtrIndirectionOperatorP(void)
+{
+  tet_infoline( "Positive Test for Dali::IntrusivePtr::operator*()" );
+
+  IntrusivePtr<Counted> 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> counted;
+  DALI_TEST_CHECK( &(counted.operator*()) == 0 );
+
+  END_TEST;
+}
+
+int UtcDaliIntrusivePtrResetP(void)
+{
+  tet_infoline( "Positive Test for Dali::IntrusivePtr::Reset()" );
+
+  IntrusivePtr<Counted> 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;
+  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> counted( new Counted );
+
+  IntrusivePtr<Counted> 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> 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> 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> 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<Counted> counted1( new Counted );
+  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
+  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
+  IntrusivePtr<Counted> 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<Counted> counted1( new Counted );
+  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
+  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
+  IntrusivePtr<Counted> 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<Counted> counted1( new Counted );
+  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
+  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
+  IntrusivePtr<Counted> 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<Counted> counted1( new Counted );
+  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
+  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
+  IntrusivePtr<Counted> 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<Counted> counted1( new Counted );
+  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
+  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
+  IntrusivePtr<Counted> 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<Counted> counted1( new Counted );
+  IntrusivePtr<CountedSubclass> countedSubclass1( new CountedSubclass );
+  IntrusivePtr<CountedSubclass> countedSubclass2( new CountedSubclass );
+  IntrusivePtr<Counted> 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 (file)
index dbd619f..0000000
+++ /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<typename Owned>
-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__ */
index ee2a960..372a31c 100644 (file)
@@ -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 \