Dali 3D User Interface Engine
utc-Dali-Any.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 
18 #include <iostream>
19 #include <stdlib.h>
20 
22 #include <dali-test-suite-utils.h>
23 
24 // Temp include
26 
27 namespace
28 {
29 struct MyStruct
30 {
32  : mFloatValue( 0.f ),
33  mIntValue( 0 )
34  {}
35 
36  MyStruct( float fValue, int iValue )
37  : mFloatValue( fValue ),
38  mIntValue( iValue )
39  {}
40 
41  MyStruct( const MyStruct& myStruct )
42  : mFloatValue( myStruct.mFloatValue ),
43  mIntValue( myStruct.mIntValue )
44  {}
45 
46  MyStruct& operator=( const MyStruct& myStruct )
47  {
48  mFloatValue = myStruct.mFloatValue;
49  mIntValue = myStruct.mIntValue;
50 
51  return *this;
52  }
53 
54  float mFloatValue;
55  int mIntValue;
56 };
57 }
58 
59 using namespace Dali;
60 
62 {
64 }
65 
67 {
69 }
70 
72 {
73  TestApplication application;
74 
75  tet_infoline("Test Any constructors.");
76 
77  // Test default constructor.
78  Any value;
79 
80  DALI_TEST_CHECK( typeid( void ) == value.GetType() );
81 
82  // Test constructor Any( const Type& )
83  Any value1 = 4u;
84 
85  // Test constructor Any( const Any& )
86  Any value2 = value1;
87 
88  // Test constructor Any( const Any& ) with a non initialized Any
89  Any value3 = value;
90 
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() );
94 
95  unsigned int uiValue1 = 0u;
96  unsigned int uiValue2 = 0u;
97  value1.Get( uiValue1 );
98  value2.Get( uiValue2 );
99 
100  DALI_TEST_EQUALS( uiValue1, uiValue2, TEST_LOCATION );
101  END_TEST;
102 }
103 
105 {
106  TestApplication application;
107 
108  tet_infoline("Test assignment operators.");
109 
110  float fValue = 0.f;
111 
112  Any value1;
113 
114  value1 = 4.f; // Test operator=( const Type& ) when current object is not initialized.
115 
116  value1.Get( fValue );
117 
119 
120  Any value2 = 0.f;
121 
122  value2 = 9.f; // Test operator=( const Type& ).
123 
124  value2.Get( fValue );
125 
127 
128  Any value3 = 5.f;
129 
130  value1 = value3; // Test operator=( const Any& ).
131 
132  value1.Get( fValue );
133 
135 
136  Any value4;
137 
138  value4 = value3; // Test operator=( const Any& ) when current object is not initialized.
139 
140  value4.Get( fValue );
141 
143 
144  // Test assign a value to value3 doesn't modify value1.
145  value3 = 3.f;
146 
147  value1.Get( fValue );
148 
150 
151  value3.Get( fValue );
152 
154 
155  // Test the branch when copying the same object.
156  Any value5 = 3.f;
157  Any& value6( value5 );
158 
159  value6 = value5;
160 
161  value6.Get( fValue );
163 
164  // test assignment for non-empty Any = empty Any
165  Any value7;
166  value6 = value7;
167  DALI_TEST_CHECK( value6.Empty() );
168 
169 
170  END_TEST;
171 }
172 
174 {
175  TestApplication application;
176 
177  tet_infoline("Test assignment operators.");
178 
179  Any value1 = 4.f;
180  Any value2 = 5u;
181 
182  bool assert = false;
183 
184  try
185  {
186  value1 = value2; // Test operator=( const Any& );
187  }
188  catch( Dali::DaliException& e )
189  {
191  assert = true;
192  }
193 
194  if( assert )
195  {
196  tet_result( TET_PASS );
197  }
198  else
199  {
200  tet_result( TET_FAIL );
201  }
202  END_TEST;
203 }
204 
206 {
207  TestApplication application;
208 
209  tet_infoline("Test GetType().");
210 
211  Any value;
212 
213  DALI_TEST_CHECK( typeid( void ) == value.GetType() );
214 
215  value = 5.f;
216 
217  DALI_TEST_CHECK( typeid( float ) == value.GetType() );
218  END_TEST;
219 }
220 
221 int UtcDaliAnyGet(void)
222 {
223  TestApplication application;
224 
225  tet_infoline("Test Get().");
226 
227  Any value1( 5.f );
228 
229  float fValue = value1.Get<float>();
230 
232 
233  fValue = 0.f;
234  value1.Get( fValue );
236 
237  class MyClass
238  {
239  public:
240  MyClass( float fValue, int iValue )
241  : mAny( MyStruct( fValue, iValue ) )
242  {
243  }
244 
245  const MyStruct& Get() const
246  {
247  return AnyCastReference<MyStruct>( mAny );
248  }
249 
250  MyStruct* GetPointer()
251  {
252  return AnyCast<MyStruct>( &mAny );
253  }
254 
255  const MyStruct* GetPointer() const
256  {
257  return AnyCast<MyStruct>( &mAny );
258  }
259 
260  private:
261  Dali::Any mAny;
262  };
263 
264  MyClass myClass( 3.25f, 3 );
265 
266  MyStruct myStruct1 = myClass.Get();
267  const MyStruct& myStruct2 = myClass.Get();
268  MyStruct* myStruct3 = myClass.GetPointer();
269  const MyStruct* myStruct4 = myClass.GetPointer();
270 
271  if( myStruct3 == NULL )
272  {
273  tet_result( TET_FAIL );
274  END_TEST;
275  }
276 
277  if( myStruct4 == NULL )
278  {
279  tet_result( TET_FAIL );
280  END_TEST;
281  }
282 
283  DALI_TEST_EQUALS( myStruct1.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
284  DALI_TEST_EQUALS( myStruct2.mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
285  DALI_TEST_EQUALS( myStruct3->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
286  DALI_TEST_EQUALS( myStruct4->mFloatValue, 3.25f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
287  DALI_TEST_EQUALS( myStruct1.mIntValue, 3, TEST_LOCATION );
288  DALI_TEST_EQUALS( myStruct2.mIntValue, 3, TEST_LOCATION );
289  DALI_TEST_EQUALS( myStruct3->mIntValue, 3, TEST_LOCATION );
290  DALI_TEST_EQUALS( myStruct4->mIntValue, 3, TEST_LOCATION );
291 
292  // Test on empty any object
293  Dali::Any myAny;
294  float* f = myAny.GetPointer<float>();
295  DALI_TEST_CHECK( f == NULL );
296 
297  // Test on getting wrong type
298  myAny = 1.f;
299  try
300  {
301  myAny.GetPointer<int>();
302  tet_result( TET_FAIL );
303  }
304  catch( Dali::DaliException& e )
305  {
307  }
308 
309  END_TEST;
310 }
311 
313 {
314  TestApplication application;
315  tet_infoline("Test Get().");
316 
317  Any value1;
318  Any value2( 5.f );
319 
320  bool assert1 = false;
321  bool assert2 = false;
322 
323  unsigned int uiValue = 0u;
324 
325  try
326  {
327  uiValue = value1.Get< unsigned int >();
328  }
329 
330  catch( Dali::DaliException& e )
331  {
333  assert1 = true;
334  }
335 
336  try
337  {
338  uiValue = value2.Get< unsigned int >();
339  }
340 
341  catch( Dali::DaliException& e )
342  {
344  assert2 = true;
345  }
346 
347  if( assert1 && assert2 )
348  {
349  tet_result( TET_PASS );
350  }
351  else
352  {
353  tet_result( TET_FAIL );
354  }
355  uiValue++; // supresss warning from unused variable
356  END_TEST;
357 }
Dali Docs Home
Read more about Dali