2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
25 #include <dali/public-api/object/any.h>
36 MyStruct( float fValue, int iValue )
37 : mFloatValue( fValue ),
41 MyStruct( const MyStruct& myStruct )
42 : mFloatValue( myStruct.mFloatValue ),
43 mIntValue( myStruct.mIntValue )
46 MyStruct& operator=( const MyStruct& myStruct )
48 mFloatValue = myStruct.mFloatValue;
49 mIntValue = myStruct.mIntValue;
61 void utc_dali_any_startup(void)
63 test_return_value = TET_UNDEF;
66 void utc_dali_any_cleanup(void)
68 test_return_value = TET_PASS;
71 int UtcDaliAnyConstructors(void)
73 TestApplication application;
75 tet_infoline("Test Any constructors.");
77 // Test default constructor.
80 DALI_TEST_CHECK( typeid( void ) == value.GetType() );
82 // Test constructor Any( const Type& )
85 // Test constructor Any( const Any& )
88 // Test constructor Any( const Any& ) with a non initialized Any
91 DALI_TEST_CHECK( typeid( unsigned int ) == value1.GetType() );
92 DALI_TEST_CHECK( typeid( unsigned int ) == value2.GetType() );
93 DALI_TEST_CHECK( typeid( void ) == value3.GetType() );
95 unsigned int uiValue1 = 0u;
96 unsigned int uiValue2 = 0u;
97 value1.Get( uiValue1 );
98 value2.Get( uiValue2 );
100 DALI_TEST_EQUALS( uiValue1, uiValue2, TEST_LOCATION );
104 int UtcDaliAnyAssignmentOperators(void)
106 TestApplication application;
108 tet_infoline("Test assignment operators.");
114 value1 = 4.f; // Test operator=( const Type& ) when current object is not initialized.
116 value1.Get( fValue );
118 DALI_TEST_EQUALS( fValue, 4.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
122 value2 = 9.f; // Test operator=( const Type& ).
124 value2.Get( fValue );
126 DALI_TEST_EQUALS( fValue, 9.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
130 value1 = value3; // Test operator=( const Any& ).
132 value1.Get( fValue );
134 DALI_TEST_EQUALS( fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
138 value4 = value3; // Test operator=( const Any& ) when current object is not initialized.
140 value4.Get( fValue );
142 DALI_TEST_EQUALS( fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
144 // Test assign a value to value3 doesn't modify value1.
147 value1.Get( fValue );
149 DALI_TEST_EQUALS( fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
151 value3.Get( fValue );
153 DALI_TEST_EQUALS( fValue, 3.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
155 // Test the branch when copying the same object.
157 Any& value6( value5 );
161 value6.Get( fValue );
162 DALI_TEST_EQUALS( fValue, 3.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
166 int UtcDaliAnyNegativeAssignmentOperators(void)
168 TestApplication application;
170 tet_infoline("Test assignment operators.");
179 value1 = value2; // Test operator=( const Any& );
181 catch( Dali::DaliException& e )
183 tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
189 tet_result( TET_PASS );
193 tet_result( TET_FAIL );
198 int UtcDaliAnyGetType(void)
200 TestApplication application;
202 tet_infoline("Test GetType().");
206 DALI_TEST_CHECK( typeid( void ) == value.GetType() );
210 DALI_TEST_CHECK( typeid( float ) == value.GetType() );
214 int UtcDaliAnyGet(void)
216 TestApplication application;
218 tet_infoline("Test Get().");
222 float fValue = value1.Get<float>();
224 DALI_TEST_EQUALS( fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
227 value1.Get( fValue );
228 DALI_TEST_EQUALS( fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
233 MyClass( float fValue, int iValue )
234 : mAny( MyStruct( fValue, iValue ) )
238 const MyStruct& Get() const
240 return AnyCastReference<MyStruct>( mAny );
243 MyStruct* GetPointer()
245 return AnyCast<MyStruct>( &mAny );
248 const MyStruct* GetPointer() const
250 return AnyCast<MyStruct>( &mAny );
257 MyClass myClass( 3.25f, 3 );
259 MyStruct myStruct1 = myClass.Get();
260 const MyStruct& myStruct2 = myClass.Get();
261 MyStruct* myStruct3 = myClass.GetPointer();
262 const MyStruct* myStruct4 = myClass.GetPointer();
264 DALI_TEST_EQUALS( myStruct1.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
265 DALI_TEST_EQUALS( myStruct2.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
266 DALI_TEST_EQUALS( myStruct3->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
267 DALI_TEST_EQUALS( myStruct4->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
268 DALI_TEST_EQUALS( myStruct1.mIntValue, 3, TEST_LOCATION );
269 DALI_TEST_EQUALS( myStruct2.mIntValue, 3, TEST_LOCATION );
270 DALI_TEST_EQUALS( myStruct3->mIntValue, 3, TEST_LOCATION );
271 DALI_TEST_EQUALS( myStruct4->mIntValue, 3, TEST_LOCATION );
276 int UtcDaliAnyNegativeGet(void)
278 TestApplication application;
279 tet_infoline("Test Get().");
284 bool assert1 = false;
285 bool assert2 = false;
287 unsigned int uiValue = 0u;
291 uiValue = value1.Get< unsigned int >();
294 catch( Dali::DaliException& e )
296 tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
302 uiValue = value2.Get< unsigned int >();
305 catch( Dali::DaliException& e )
307 tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
311 if( assert1 && assert2 )
313 tet_result( TET_PASS );
317 tet_result( TET_FAIL );
319 uiValue++; // supresss warning from unused variable