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.
18 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/dali-core.h>
25 #include <dali/public-api/object/any.h>
39 MyStruct(float fValue, int iValue)
40 : mFloatValue(fValue),
46 MyStruct(const MyStruct& myStruct)
47 : mFloatValue(myStruct.mFloatValue),
48 mIntValue(myStruct.mIntValue)
58 MyStruct& operator=(const MyStruct& myStruct)
60 mFloatValue = myStruct.mFloatValue;
61 mIntValue = myStruct.mIntValue;
73 void utc_dali_any_startup(void)
75 test_return_value = TET_UNDEF;
78 void utc_dali_any_cleanup(void)
80 test_return_value = TET_PASS;
83 int UtcDaliAnyConstructors(void)
85 TestApplication application;
87 tet_infoline("Test Any constructors.");
91 // Test default constructor.
94 DALI_TEST_CHECK(typeid(void) == value.GetType());
96 // Test constructor Any( const Type& )
99 // Test constructor Any( const Any& )
102 // Test constructor Any( const Any& ) with a non initialized Any
105 // Test constructor Any( Any&& ) with a non initialized Any
106 Any value4(Any(MyStruct(1.0f, 2)));
108 DALI_TEST_CHECK(typeid(unsigned int) == value1.GetType());
109 DALI_TEST_CHECK(typeid(unsigned int) == value2.GetType());
110 DALI_TEST_CHECK(typeid(void) == value3.GetType());
111 DALI_TEST_CHECK(typeid(MyStruct) == value4.GetType());
112 DALI_TEST_CHECK(gRefCount == 1);
114 // Test std::move operation result.
115 Any value5(std::move(value4));
117 DALI_TEST_CHECK(value4.Empty());
118 DALI_TEST_CHECK(typeid(MyStruct) == value5.GetType());
119 DALI_TEST_CHECK(gRefCount == 1);
121 unsigned int uiValue1 = 0u;
122 unsigned int uiValue2 = 0u;
123 value1.Get(uiValue1);
124 value2.Get(uiValue2);
126 DALI_TEST_EQUALS(uiValue1, uiValue2, TEST_LOCATION);
131 DALI_TEST_EQUALS(myValue.mFloatValue, 1.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
132 DALI_TEST_EQUALS(myValue.mIntValue, 2, TEST_LOCATION);
137 int UtcDaliAnyAssignmentOperators(void)
139 TestApplication application;
141 tet_infoline("Test assignment operators.");
147 value1 = 4.f; // Test operator=( const Type& ) when current object is not initialized.
151 DALI_TEST_EQUALS(fValue, 4.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
153 value1 = 9.f; // Test operator=( const Type& ).
157 DALI_TEST_EQUALS(fValue, 9.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
161 value1 = value3; // Test operator=( const Any& ).
165 DALI_TEST_EQUALS(fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
169 value4 = value3; // Test operator=( const Any& ) when current object is not initialized.
173 DALI_TEST_EQUALS(fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
175 // Test assign a value to value3 doesn't modify value1.
180 DALI_TEST_EQUALS(fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
184 DALI_TEST_EQUALS(fValue, 3.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
186 // Test the branch when copying the same object.
193 DALI_TEST_EQUALS(fValue, 3.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
195 // test assignment for non-empty Any = empty Any
198 DALI_TEST_CHECK(value6.Empty());
200 // Due to value6 is reference of value5, value5 also become empty
201 DALI_TEST_CHECK(value5.Empty());
205 // Do something to above compiler optimize out
208 DALI_TEST_CHECK(typeid(float) == value8.GetType());
210 // Test operator=( Any&& ).
211 value8 = Any(MyStruct(3.0f, 4));
213 DALI_TEST_CHECK(typeid(MyStruct) == value8.GetType());
214 DALI_TEST_CHECK(gRefCount == 1);
216 // Do something to above compiler optimize out
219 DALI_TEST_CHECK(typeid(float) == value9.GetType());
221 // Test std::move operation result.
222 value9 = std::move(value8);
224 DALI_TEST_CHECK(value8.Empty());
225 DALI_TEST_CHECK(typeid(MyStruct) == value9.GetType());
226 DALI_TEST_CHECK(gRefCount == 1);
231 DALI_TEST_EQUALS(myValue.mFloatValue, 3.0f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
232 DALI_TEST_EQUALS(myValue.mIntValue, 4, TEST_LOCATION);
237 int UtcDaliAnyNegativeAssignmentOperators(void)
239 TestApplication application;
241 tet_infoline("Test assignment operators.");
250 value1 = value2; // Test operator=( const Any& );
252 catch(Dali::DaliException& e)
254 DALI_TEST_PRINT_ASSERT(e);
260 tet_result(TET_PASS);
264 tet_result(TET_FAIL);
269 int UtcDaliAnyGetType(void)
271 TestApplication application;
273 tet_infoline("Test GetType().");
277 DALI_TEST_CHECK(typeid(void) == value.GetType());
281 DALI_TEST_CHECK(typeid(float) == value.GetType());
285 int UtcDaliAnyGet(void)
287 TestApplication application;
289 tet_infoline("Test Get().");
293 float fValue = value1.Get<float>();
295 DALI_TEST_EQUALS(fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
299 DALI_TEST_EQUALS(fValue, 5.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
304 MyClass(float fValue, int iValue)
305 : mAny(MyStruct(fValue, iValue))
309 const MyStruct& Get() const
311 return AnyCastReference<MyStruct>(mAny);
314 MyStruct* GetPointer()
316 return AnyCast<MyStruct>(&mAny);
319 const MyStruct* GetPointer() const
321 return AnyCast<MyStruct>(&mAny);
328 MyClass myClass(3.25f, 3);
330 MyStruct myStruct1 = myClass.Get();
331 const MyStruct& myStruct2 = myClass.Get();
332 MyStruct* myStruct3 = myClass.GetPointer();
333 const MyStruct* myStruct4 = myClass.GetPointer();
335 if(myStruct3 == NULL)
337 tet_result(TET_FAIL);
341 if(myStruct4 == NULL)
343 tet_result(TET_FAIL);
347 DALI_TEST_EQUALS(myStruct1.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
348 DALI_TEST_EQUALS(myStruct2.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
349 DALI_TEST_EQUALS(myStruct3->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
350 DALI_TEST_EQUALS(myStruct4->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
351 DALI_TEST_EQUALS(myStruct1.mIntValue, 3, TEST_LOCATION);
352 DALI_TEST_EQUALS(myStruct2.mIntValue, 3, TEST_LOCATION);
353 DALI_TEST_EQUALS(myStruct3->mIntValue, 3, TEST_LOCATION);
354 DALI_TEST_EQUALS(myStruct4->mIntValue, 3, TEST_LOCATION);
356 // Test on empty any object
358 float* f = myAny.GetPointer<float>();
359 DALI_TEST_CHECK(f == NULL);
361 // Test on getting wrong type
365 myAny.GetPointer<int>();
366 tet_result(TET_FAIL);
368 catch(Dali::DaliException& e)
370 DALI_TEST_PRINT_ASSERT(e);
376 int UtcDaliAnyNegativeGet(void)
378 TestApplication application;
379 tet_infoline("Test Get().");
384 bool assert1 = false;
385 bool assert2 = false;
387 unsigned int uiValue = 0u;
391 uiValue = value1.Get<unsigned int>();
394 catch(Dali::DaliException& e)
396 DALI_TEST_PRINT_ASSERT(e);
402 uiValue = value2.Get<unsigned int>();
405 catch(Dali::DaliException& e)
407 DALI_TEST_PRINT_ASSERT(e);
411 if(assert1 && assert2)
413 tet_result(TET_PASS);
417 tet_result(TET_FAIL);
419 uiValue++; // supresss warning from unused variable
423 int UtcDaliAnyReferenceCheck(void)
428 Dali::Any any[10]; // Create local 10 empty Any
430 DALI_TEST_EQUALS(gRefCount, 0, TEST_LOCATION);
433 for(int i = 0; i < 5; i++)
435 any[i] = MyStruct(1.0f, i);
437 DALI_TEST_EQUALS(gRefCount, 5, TEST_LOCATION);
439 // Move from [0 5) to [5 10)
440 for(int i = 0; i < 5; i++)
442 any[i + 5] = std::move(any[i]);
444 DALI_TEST_EQUALS(gRefCount, 5, TEST_LOCATION);
446 // Copy from [5 10) to [0 5)
447 for(int i = 0; i < 5; i++)
451 DALI_TEST_EQUALS(gRefCount, 10, TEST_LOCATION);
454 // Check whether all Dali::Any are released
455 DALI_TEST_EQUALS(gRefCount, 0, TEST_LOCATION);