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() );
42 map[ "world" ] = "world";
43 map[ "world" ] = 3; // same item as line above
44 DALI_TEST_CHECK( !map.Empty() ); // Should no longer be empty
45 DALI_TEST_CHECK( map.Count() == 2 ); // Should only have two items, not three!!
46 DALI_TEST_CHECK( map["hello"].Get<int>() == 1 );
47 DALI_TEST_CHECK( map["world"].Get<int>() == 3 );
50 DALI_TEST_CHECK( map.Empty() );
54 int UtcDaliPropertyMapCopyAndAssignment(void)
60 Property::Map assignedMap;
61 assignedMap[ "foo" ] = 3;
62 DALI_TEST_CHECK( assignedMap.Count() == 1 );
64 DALI_TEST_CHECK( assignedMap.Count() == 2 );
66 Property::Map copiedMap( map );
67 DALI_TEST_CHECK( copiedMap.Count() == 2 );
70 DALI_TEST_CHECK( map.Count() == 2 );
72 DALI_TEST_CHECK( map.Count() == 2 );
77 int UtcDaliPropertyMapConstOperator(void)
82 DALI_TEST_CHECK( map.Count() == 2 );
84 const Property::Map& constMap( map );
85 DALI_TEST_CHECK( constMap[ "world" ].Get<int>() == 2 );
86 DALI_TEST_CHECK( constMap.Count() == 2 ); // Ensure count hasn't gone up
91 constMap[ "invalidKey" ];
92 tet_result( TET_FAIL );
94 catch ( DaliException& e )
96 DALI_TEST_ASSERT( e, "! \"Invalid Key\"", TEST_LOCATION );
102 int UtcDaliPropertyMapGetValue(void)
108 Property::Value& value = map.GetValue( 0 );
109 DALI_TEST_CHECK( value.Get<int>() == 1 );
110 value = 10; // Allows the actual changing of the value as we have a ref
111 DALI_TEST_CHECK( map[ "hello" ].Get<int>() == 10 );
117 tet_result( TET_FAIL );
119 catch ( DaliException& e )
121 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
127 int UtcDaliPropertyMapGetKey(void)
133 DALI_TEST_CHECK( map.GetKey( 0 ) == "hello" );
134 DALI_TEST_CHECK( map.GetKey( 1 ) == "world" );
140 tet_result( TET_FAIL );
142 catch ( DaliException& e )
144 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
150 int UtcDaliPropertyMapGetPair(void)
156 DALI_TEST_CHECK( map.GetPair( 0 ).first == "hello" );
157 DALI_TEST_CHECK( map.GetPair( 0 ).second.Get< int >() == 1 );
158 DALI_TEST_CHECK( map.GetPair( 1 ).first == "world" );
159 DALI_TEST_CHECK( map.GetPair( 1 ).second.Get< int >() == 2 );
165 tet_result( TET_FAIL );
167 catch ( DaliException& e )
169 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
175 int UtcDaliPropertyMapFind(void)
181 Property::Value* value = NULL;
183 value = map.Find( "hello" );
184 DALI_TEST_CHECK( value );
185 DALI_TEST_CHECK( value->Get<int>() == 1 );
187 value = map.Find( "world" );
188 DALI_TEST_CHECK( value );
189 DALI_TEST_CHECK( value->Get<int>() == 2 );
191 value = map.Find( "invalidKey" );
192 DALI_TEST_CHECK( !value );
197 int UtcDaliPropertyMapInsertP(void)
200 DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
201 map.Insert( "foo", "bar");
202 DALI_TEST_EQUALS( 1u, map.Count(), TEST_LOCATION );
203 Property::Value* value = map.Find( "foo" );
204 DALI_TEST_CHECK( value );
205 DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
206 map.Insert( std::string("foo2"), "testing" );
207 DALI_TEST_EQUALS( 2u, map.Count(), TEST_LOCATION );
208 value = map.Find( "foo2" );
209 DALI_TEST_CHECK( value );
210 DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
215 int UtcDaliPropertyMapMerge(void)
221 DALI_TEST_CHECK( map.Count() == 2 );
223 // Create another map with the same keys but different values
228 // Merge map2 into map1, count should still be 2, map values should be from map2
230 DALI_TEST_CHECK( map.Count() == 2 );
231 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
232 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
234 // Create another map with different keys
239 // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
241 DALI_TEST_CHECK( map.Count() == 4 );
242 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
243 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
244 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
245 DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
247 // Create an empty map and attempt to merge, should be successful, nothing should change
249 DALI_TEST_CHECK( map4.Empty() );
251 DALI_TEST_CHECK( map4.Empty() );
252 DALI_TEST_CHECK( map.Count() == 4 );
253 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
254 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
255 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
256 DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
258 // Merge map into map4, map4 should be the same as map now.
260 DALI_TEST_CHECK( map4.Count() == 4 );
261 DALI_TEST_CHECK( map4[ "hello" ].Get< int >() == 3 );
262 DALI_TEST_CHECK( map4[ "world"].Get< int >() == 4 );
263 DALI_TEST_CHECK( map4[ "foo"].Get< int >() == 5 );
264 DALI_TEST_CHECK( map4[ "bar"].Get< int >() == 6 );
266 // Attempt to merge into itself, should be successful, nothing should change
268 DALI_TEST_CHECK( map.Count() == 4 );
269 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
270 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
271 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
272 DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
277 int UtcDaliPropertyMapOstream01(void)
281 map.Insert("duration", 5.0f);
282 map.Insert("delay", 1.0f);
283 map.Insert("value", 100);
285 std::ostringstream oss;
288 tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
290 DALI_TEST_EQUALS( oss.str().compare("Map(3) = {duration:5, delay:1, value:100}"), 0, TEST_LOCATION );
296 int UtcDaliPropertyMapOstream02(void)
298 Property::Map map, map2;
300 map2.Insert("duration", 5.0f);
301 map2.Insert("delay", 1.0f);
302 map.Insert("timePeriod", map2);
303 map.Insert("value", 100);
305 std::ostringstream oss;
308 tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
310 DALI_TEST_EQUALS( oss.str().compare("Map(2) = {timePeriod:Map(2) = {duration:5, delay:1}, value:100}"), 0, TEST_LOCATION );