[dali_1.1.28] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyMap.cpp
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 <string>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_property_map_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_property_map_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 int UtcDaliPropertyMapPopulate(void)
37 {
38   Property::Map map;
39   DALI_TEST_CHECK( map.Empty() );
40
41   map[ "hello" ] = 1;
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 );
48
49   map.Clear();
50   DALI_TEST_CHECK( map.Empty() );
51   END_TEST;
52 }
53
54 int UtcDaliPropertyMapCopyAndAssignment(void)
55 {
56   Property::Map map;
57   map[ "hello" ] = 1;
58   map[ "world" ] = 2;
59
60   Property::Map assignedMap;
61   assignedMap[ "foo" ] = 3;
62   DALI_TEST_CHECK( assignedMap.Count() == 1 );
63   assignedMap = map;
64   DALI_TEST_CHECK( assignedMap.Count() == 2 );
65
66   Property::Map copiedMap( map );
67   DALI_TEST_CHECK( copiedMap.Count() == 2 );
68
69   // Self assignment
70   DALI_TEST_CHECK( map.Count() == 2 );
71   map = map;
72   DALI_TEST_CHECK( map.Count() == 2 );
73
74   END_TEST;
75 }
76
77 int UtcDaliPropertyMapConstOperator(void)
78 {
79   Property::Map map;
80   map[ "hello" ] = 1;
81   map[ "world" ] = 2;
82   DALI_TEST_CHECK( map.Count() == 2 );
83
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
87
88   // Invalid Key
89   try
90   {
91     constMap[ "invalidKey" ];
92     tet_result( TET_FAIL );
93   }
94   catch ( DaliException& e )
95   {
96     DALI_TEST_ASSERT( e, "! \"Invalid Key\"", TEST_LOCATION );
97   }
98
99   END_TEST;
100 }
101
102 int UtcDaliPropertyMapGetValue(void)
103 {
104   Property::Map map;
105   map[ "hello" ] = 1;
106   map[ "world" ] = 2;
107
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 );
112
113   // Out of bounds
114   try
115   {
116     map.GetValue( 2 );
117     tet_result( TET_FAIL );
118   }
119   catch ( DaliException& e )
120   {
121     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
122   }
123
124   END_TEST;
125 }
126
127 int UtcDaliPropertyMapGetKey(void)
128 {
129   Property::Map map;
130   map[ "hello" ] = 1;
131   map[ "world" ] = 2;
132
133   DALI_TEST_CHECK( map.GetKey( 0 ) == "hello" );
134   DALI_TEST_CHECK( map.GetKey( 1 ) == "world" );
135
136   // Out of bounds
137   try
138   {
139     map.GetKey( 2 );
140     tet_result( TET_FAIL );
141   }
142   catch ( DaliException& e )
143   {
144     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
145   }
146
147   END_TEST;
148 }
149
150 int UtcDaliPropertyMapGetPair(void)
151 {
152   Property::Map map;
153   map[ "hello" ] = 1;
154   map[ "world" ] = 2;
155
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 );
160
161   // Out of bounds
162   try
163   {
164     map.GetPair( 2 );
165     tet_result( TET_FAIL );
166   }
167   catch ( DaliException& e )
168   {
169     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
170   }
171
172   END_TEST;
173 }
174
175 int UtcDaliPropertyMapFind(void)
176 {
177   Property::Map map;
178   map[ "hello" ] = 1;
179   map[ "world" ] = 2;
180
181   Property::Value* value = NULL;
182
183   value = map.Find( "hello" );
184   DALI_TEST_CHECK( value );
185   DALI_TEST_CHECK( value->Get<int>() == 1 );
186
187   value = map.Find( "world" );
188   DALI_TEST_CHECK( value );
189   DALI_TEST_CHECK( value->Get<int>() == 2 );
190
191   value = map.Find( "invalidKey" );
192   DALI_TEST_CHECK( !value );
193
194   END_TEST;
195 }
196
197 int UtcDaliPropertyMapInsertP(void)
198 {
199   Property::Map map;
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 );
211
212   END_TEST;
213 }
214
215 int UtcDaliPropertyMapMerge(void)
216 {
217   Property::Map map;
218   map[ "hello" ] = 1;
219   map[ "world" ] = 2;
220
221   DALI_TEST_CHECK( map.Count() == 2 );
222
223   // Create another map with the same keys but different values
224   Property::Map map2;
225   map[ "hello" ] = 3;
226   map[ "world" ] = 4;
227
228   // Merge map2 into map1, count should still be 2, map values should be from map2
229   map.Merge( 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 );
233
234   // Create another map with different keys
235   Property::Map map3;
236   map3[ "foo" ] = 5;
237   map3[ "bar" ] = 6;
238
239   // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
240   map.Merge( 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 );
246
247   // Create an empty map and attempt to merge, should be successful, nothing should change
248   Property::Map map4;
249   DALI_TEST_CHECK( map4.Empty() );
250   map.Merge( map4 );
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 );
257
258   // Merge map into map4, map4 should be the same as map now.
259   map4.Merge( map );
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 );
265
266   // Attempt to merge into itself, should be successful, nothing should change
267   map.Merge( map );
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 );
273
274   END_TEST;
275 }
276
277 int UtcDaliPropertyMapOstream01(void)
278 {
279   Property::Map map;
280
281   map.Insert("duration", 5.0f);
282   map.Insert("delay", 1.0f);
283   map.Insert("value", 100);
284
285   std::ostringstream oss;
286   oss << map;
287
288   tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
289
290   DALI_TEST_EQUALS( oss.str().compare("Map(3) = {duration:5, delay:1, value:100}"), 0, TEST_LOCATION );
291
292   END_TEST;
293 }
294
295
296 int UtcDaliPropertyMapOstream02(void)
297 {
298   Property::Map map, map2;
299
300   map2.Insert("duration", 5.0f);
301   map2.Insert("delay", 1.0f);
302   map.Insert("timePeriod", map2);
303   map.Insert("value", 100);
304
305   std::ostringstream oss;
306   oss << map;
307
308   tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
309
310   DALI_TEST_EQUALS( oss.str().compare("Map(2) = {timePeriod:Map(2) = {duration:5, delay:1}, value:100}"), 0, TEST_LOCATION );
311
312   END_TEST;
313 }