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>
26 void utc_dali_property_map_startup(void)
28 test_return_value = TET_UNDEF;
31 void utc_dali_property_map_cleanup(void)
33 test_return_value = TET_PASS;
36 int UtcDaliPropertyMapPopulate(void)
39 DALI_TEST_CHECK( map.Empty() );
43 map[ "world" ] = "world";
45 map[ "world" ] = 3; // same item as line above
46 DALI_TEST_CHECK( !map.Empty() ); // Should no longer be empty
47 DALI_TEST_CHECK( map.Count() == 4 ); // Should only have four items, not five!!
48 DALI_TEST_CHECK( map["hello"].Get<int>() == 1 );
49 DALI_TEST_CHECK( map["world"].Get<int>() == 3 );
50 DALI_TEST_EQUALS( "DALi", map[ 10 ].Get<std::string>(), TEST_LOCATION );
51 DALI_TEST_CHECK( map[100].Get<int>() == 9 );
54 DALI_TEST_CHECK( map.Empty() );
58 int UtcDaliPropertyMapCopyAndAssignment(void)
65 Property::Map assignedMap;
66 assignedMap[ "foo" ] = 3;
67 assignedMap[ 100 ] = 9;
68 DALI_TEST_CHECK( assignedMap.Count() == 2 );
70 DALI_TEST_CHECK( assignedMap.Count() == 3 );
72 Property::Map copiedMap( map );
73 DALI_TEST_CHECK( copiedMap.Count() == 3 );
76 DALI_TEST_CHECK( map.Count() == 3 );
78 DALI_TEST_CHECK( map.Count() == 3 );
83 int UtcDaliPropertyMapConstOperator(void)
89 DALI_TEST_CHECK( map.Count() == 3 );
91 const Property::Map& constMap( map );
92 DALI_TEST_CHECK( constMap[ "world" ].Get<int>() == 2 );
93 DALI_TEST_CHECK( constMap.Count() == 3 ); // Ensure count hasn't gone up
95 DALI_TEST_EQUALS( "DALi", map[ 10 ].Get<std::string>(), TEST_LOCATION );
96 DALI_TEST_CHECK( constMap.Count() == 3 ); // Ensure count hasn't gone up
101 constMap[ "invalidKey" ];
102 tet_result( TET_FAIL );
104 catch ( DaliException& e )
106 DALI_TEST_ASSERT( e, "! \"Invalid Key\"", TEST_LOCATION );
112 // deprecated API, only retrieve the value from string-value pairs
113 int UtcDaliPropertyMapGetValue(void)
119 Property::Value& value = map.GetValue( 0 );
120 DALI_TEST_CHECK( value.Get<int>() == 1 );
121 value = 10; // Allows the actual changing of the value as we have a ref
122 DALI_TEST_CHECK( map[ "hello" ].Get<int>() == 10 );
128 tet_result( TET_FAIL );
130 catch ( DaliException& e )
132 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
138 // deprecated API, only retrieve the key from the string-value pairs
139 int UtcDaliPropertyMapGetKey(void)
145 DALI_TEST_CHECK( map.GetKey( 0 ) == "hello" );
146 DALI_TEST_CHECK( map.GetKey( 1 ) == "world" );
152 tet_result( TET_FAIL );
154 catch ( DaliException& e )
156 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
162 // deprecated API, only retrieve the string-value pairs
163 int UtcDaliPropertyMapGetPair(void)
169 DALI_TEST_CHECK( map.GetPair( 0 ).first == "hello" );
170 DALI_TEST_CHECK( map.GetPair( 0 ).second.Get< int >() == 1 );
171 DALI_TEST_CHECK( map.GetPair( 1 ).first == "world" );
172 DALI_TEST_CHECK( map.GetPair( 1 ).second.Get< int >() == 2 );
178 tet_result( TET_FAIL );
180 catch ( DaliException& e )
182 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
188 int UtcDaliPropertyMapFind(void)
196 Property::Value* value = NULL;
198 value = map.Find( "hello" );
199 DALI_TEST_CHECK( value );
200 DALI_TEST_CHECK( value->Get<int>() == 1 );
202 const std::string world("world");
203 value = map.Find( world );
204 DALI_TEST_CHECK( value );
205 DALI_TEST_CHECK( value->Get<int>() == 2 );
207 value = map.Find( 100 );
208 DALI_TEST_CHECK( value );
209 DALI_TEST_CHECK( value->Get<int>() == 9 );
211 value = map.Find( 10, Property::STRING );
212 DALI_TEST_CHECK( value );
213 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
215 value = map.Find( 10, Property::INTEGER );
216 DALI_TEST_CHECK( value == NULL );
218 value = map.Find( "invalidKey" );
219 DALI_TEST_CHECK( !value );
225 int UtcDaliPropertyMapOperatorIndex(void)
233 const Property::Map map2 = map;
234 const Property::Value& value10 = map2[10];
235 DALI_TEST_EQUALS( value10.Get<std::string>(), "DALi", TEST_LOCATION );
237 const Property::Value& value100 = map2[100];
238 DALI_TEST_EQUALS( value100.Get<int>(), 9, TEST_LOCATION );
240 const Property::Value& valueHello = map2["hello"];
241 DALI_TEST_EQUALS( valueHello.Get<int>(), 1, TEST_LOCATION );
246 int UtcDaliPropertyMapInsertP(void)
249 DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
250 map.Insert( "foo", "bar");
251 DALI_TEST_EQUALS( 1u, map.Count(), TEST_LOCATION );
252 Property::Value* value = map.Find( "foo" );
253 DALI_TEST_CHECK( value );
254 DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
256 map.Insert( std::string("foo2"), "testing" );
257 DALI_TEST_EQUALS( 2u, map.Count(), TEST_LOCATION );
258 value = map.Find( "foo2" );
259 DALI_TEST_CHECK( value );
260 DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
262 map.Insert( 10, "DALi" );
263 DALI_TEST_EQUALS( 3u, map.Count(), TEST_LOCATION );
264 value = map.Find( 10 );
265 DALI_TEST_CHECK( value );
266 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
268 map.Insert( 100, 9 );
269 DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
270 value = map.Find( 100 );
271 DALI_TEST_CHECK( value );
272 DALI_TEST_CHECK( value->Get<int>() == 9 );
277 int UtcDaliPropertyMapMerge(void)
284 DALI_TEST_CHECK( map.Count() == 3 );
286 // Create another map with the same keys but different values
290 map[ 10 ] = "3DEngine";
292 // Merge map2 into map1, count should still be 2, map values should be from map2
294 DALI_TEST_CHECK( map.Count() == 3 );
295 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
296 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
297 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
299 // Create another map with different keys
304 // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
306 DALI_TEST_CHECK( map.Count() == 5 );
307 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
308 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
309 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
310 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
311 DALI_TEST_CHECK( map[ 100].Get< int >() == 6 );
313 // Create an empty map and attempt to merge, should be successful, nothing should change
315 DALI_TEST_CHECK( map4.Empty() );
317 DALI_TEST_CHECK( map4.Empty() );
318 DALI_TEST_CHECK( map.Count() == 5 );
319 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
320 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
321 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
322 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
323 DALI_TEST_CHECK( map[ 100 ].Get< int >() == 6 );
325 // Merge map into map4, map4 should be the same as map now.
327 DALI_TEST_CHECK( map4.Count() == 5 );
328 DALI_TEST_CHECK( map4[ "hello" ].Get< int >() == 3 );
329 DALI_TEST_CHECK( map4[ "world"].Get< int >() == 4 );
330 DALI_TEST_CHECK( map4[ "foo"].Get< int >() == 5 );
331 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
332 DALI_TEST_CHECK( map4[ 100 ].Get< int >() == 6 );
334 // Attempt to merge into itself, should be successful, nothing should change
336 DALI_TEST_CHECK( map.Count() == 5 );
337 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
338 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
339 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
340 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
341 DALI_TEST_CHECK( map[ 100 ].Get< int >() == 6 );
346 int UtcDaliPropertyMapOstream01(void)
350 map.Insert("duration", 5.0f);
351 map.Insert( 10, "DALi" );
352 map.Insert("delay", 1.0f);
353 map.Insert( 100, 9 );
354 map.Insert("value", 100);
356 std::ostringstream oss;
359 tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
361 // string-value pairs first, then index-value pairs
362 DALI_TEST_EQUALS( oss.str().compare("Map(5) = {duration:5, delay:1, value:100, 10:DALi, 100:9}"), 0, TEST_LOCATION );
368 int UtcDaliPropertyMapOstream02(void)
370 Property::Map map, map2;
372 map2.Insert("duration", 5.0f);
373 map2.Insert("delay", 1.0f);
374 map2.Insert( 10, "DALi" );
375 map.Insert("timePeriod", map2);
376 map.Insert( 100, 9 );
377 map.Insert("value", 100);
379 std::ostringstream oss;
382 tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
384 // string-value pairs first, then index-value pairs
385 DALI_TEST_EQUALS( oss.str().compare("Map(3) = {timePeriod:Map(3) = {duration:5, delay:1, 10:DALi}, value:100, 100:9}"), 0, TEST_LOCATION );