Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Any.cpp
index 33ab36c..843bc8a 100644 (file)
 #include <iostream>
 #include <stdlib.h>
 
-#include <dali/dali.h>
+#include <dali/public-api/dali-core.h>
 #include <dali-test-suite-utils.h>
 
 // Temp include
 #include <dali/public-api/object/any.h>
 
+namespace
+{
+struct MyStruct
+{
+  MyStruct()
+  : mFloatValue( 0.f ),
+    mIntValue( 0 )
+  {}
+
+  MyStruct( float fValue, int iValue )
+  : mFloatValue( fValue ),
+    mIntValue( iValue )
+  {}
+
+  MyStruct( const MyStruct& myStruct )
+  : mFloatValue( myStruct.mFloatValue ),
+    mIntValue( myStruct.mIntValue )
+  {}
+
+  MyStruct& operator=( const MyStruct& myStruct )
+  {
+    mFloatValue = myStruct.mFloatValue;
+    mIntValue = myStruct.mIntValue;
+
+    return *this;
+  }
+
+  float mFloatValue;
+  int mIntValue;
+};
+}
+
 using namespace Dali;
 
 void utc_dali_any_startup(void)
@@ -85,11 +117,9 @@ int UtcDaliAnyAssignmentOperators(void)
 
   DALI_TEST_EQUALS( fValue, 4.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
 
-  Any value2 = 0.f;
+  value1 = 9.f; // Test operator=( const Type& ).
 
-  value2 = 9.f; // Test operator=( const Type& ).
-
-  value2.Get( fValue );
+  value1.Get( fValue );
 
   DALI_TEST_EQUALS( fValue, 9.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
 
@@ -128,6 +158,13 @@ int UtcDaliAnyAssignmentOperators(void)
 
   value6.Get( fValue );
   DALI_TEST_EQUALS( fValue, 3.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+
+  // test assignment for  non-empty Any = empty Any
+  Any value7;
+  value6 = value7;
+  DALI_TEST_CHECK( value6.Empty() );
+
+
   END_TEST;
 }
 
@@ -148,7 +185,7 @@ int UtcDaliAnyNegativeAssignmentOperators(void)
   }
   catch( Dali::DaliException& e )
   {
-    tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
+    DALI_TEST_PRINT_ASSERT( e );
     assert = true;
   }
 
@@ -194,6 +231,79 @@ int UtcDaliAnyGet(void)
   fValue = 0.f;
   value1.Get( fValue );
   DALI_TEST_EQUALS( fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+
+  class MyClass
+  {
+  public:
+    MyClass( float fValue, int iValue )
+    : mAny( MyStruct( fValue, iValue ) )
+    {
+    }
+
+    const MyStruct& Get() const
+    {
+      return AnyCastReference<MyStruct>( mAny );
+    }
+
+    MyStruct* GetPointer()
+    {
+      return AnyCast<MyStruct>( &mAny );
+    }
+
+    const MyStruct* GetPointer() const
+    {
+      return AnyCast<MyStruct>( &mAny );
+    }
+
+  private:
+    Dali::Any mAny;
+  };
+
+  MyClass myClass( 3.25f, 3 );
+
+  MyStruct myStruct1 = myClass.Get();
+  const MyStruct& myStruct2 = myClass.Get();
+  MyStruct* myStruct3 = myClass.GetPointer();
+  const MyStruct* myStruct4 = myClass.GetPointer();
+
+  if( myStruct3 == NULL )
+  {
+    tet_result( TET_FAIL );
+    END_TEST;
+  }
+
+  if( myStruct4 == NULL )
+  {
+    tet_result( TET_FAIL );
+    END_TEST;
+  }
+
+  DALI_TEST_EQUALS( myStruct1.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct2.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct3->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct4->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct1.mIntValue, 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct2.mIntValue, 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct3->mIntValue, 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( myStruct4->mIntValue, 3, TEST_LOCATION );
+
+  // Test on empty any object
+  Dali::Any myAny;
+  float* f = myAny.GetPointer<float>();
+  DALI_TEST_CHECK( f == NULL );
+
+  // Test on getting wrong type
+  myAny = 1.f;
+  try
+  {
+    myAny.GetPointer<int>();
+    tet_result( TET_FAIL );
+  }
+  catch( Dali::DaliException& e )
+  {
+    DALI_TEST_PRINT_ASSERT( e );
+  }
+
   END_TEST;
 }
 
@@ -217,7 +327,7 @@ int UtcDaliAnyNegativeGet(void)
 
   catch( Dali::DaliException& e )
   {
-    tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
+    DALI_TEST_PRINT_ASSERT( e );
     assert1 = true;
   }
 
@@ -228,7 +338,7 @@ int UtcDaliAnyNegativeGet(void)
 
   catch( Dali::DaliException& e )
   {
-    tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
+    DALI_TEST_PRINT_ASSERT( e );
     assert2 = true;
   }