(Properties) Property::Map class added
[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
20 #include <stdlib.h>
21 #include <boost/bind.hpp>
22 #include <dali/public-api/dali-core.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26
27 void utc_dali_property_map_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_property_map_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 int UtcDaliPropertyMapPopulate(void)
38 {
39   Property::Map map;
40   DALI_TEST_CHECK( map.Empty() );
41
42   map[ "hello" ] = 1;
43   map[ "world" ] = "world";
44   map[ "world" ] = 3; // same item as line above
45   DALI_TEST_CHECK( !map.Empty() ); // Should no longer be empty
46   DALI_TEST_CHECK( map.Count() == 2 ); // Should only have two items, not three!!
47   DALI_TEST_CHECK( map["hello"].Get<int>() == 1 );
48   DALI_TEST_CHECK( map["world"].Get<int>() == 3 );
49
50   map.Clear();
51   DALI_TEST_CHECK( map.Empty() );
52   END_TEST;
53 }
54
55 int UtcDaliPropertyMapCopyAndAssignment(void)
56 {
57   Property::Map map;
58   map[ "hello" ] = 1;
59   map[ "world" ] = 2;
60
61   Property::Map assignedMap;
62   assignedMap[ "foo" ] = 3;
63   DALI_TEST_CHECK( assignedMap.Count() == 1 );
64   assignedMap = map;
65   DALI_TEST_CHECK( assignedMap.Count() == 2 );
66
67   Property::Map copiedMap( map );
68   DALI_TEST_CHECK( copiedMap.Count() == 2 );
69
70   // Self assignment
71   DALI_TEST_CHECK( map.Count() == 2 );
72   map = map;
73   DALI_TEST_CHECK( map.Count() == 2 );
74
75   END_TEST;
76 }
77
78 int UtcDaliPropertyMapConstOperator(void)
79 {
80   Property::Map map;
81   map[ "hello" ] = 1;
82   map[ "world" ] = 2;
83   DALI_TEST_CHECK( map.Count() == 2 );
84
85   const Property::Map& constMap( map );
86   DALI_TEST_CHECK( constMap[ "world" ].Get<int>() == 2 );
87   DALI_TEST_CHECK( constMap.Count() == 2 ); // Ensure count hasn't gone up
88
89   // Invalid Key
90   try
91   {
92     constMap[ "invalid-key" ];
93     tet_result( TET_FAIL );
94   }
95   catch ( DaliException& e )
96   {
97     DALI_TEST_ASSERT( e, "! \"Invalid Key\"", TEST_LOCATION );
98   }
99
100   END_TEST;
101 }
102
103 int UtcDaliPropertyMapGetValue(void)
104 {
105   Property::Map map;
106   map[ "hello" ] = 1;
107   map[ "world" ] = 2;
108
109   Property::Value& value = map.GetValue( 0 );
110   DALI_TEST_CHECK( value.Get<int>() == 1 );
111   value = 10; // Allows the actual changing of the value as we have a ref
112   DALI_TEST_CHECK( map[ "hello" ].Get<int>() == 10 );
113
114   // Out of bounds
115   try
116   {
117     map.GetValue( 2 );
118     tet_result( TET_FAIL );
119   }
120   catch ( DaliException& e )
121   {
122     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
123   }
124
125   END_TEST;
126 }
127
128 int UtcDaliPropertyMapGetKey(void)
129 {
130   Property::Map map;
131   map[ "hello" ] = 1;
132   map[ "world" ] = 2;
133
134   DALI_TEST_CHECK( map.GetKey( 0 ) == "hello" );
135   DALI_TEST_CHECK( map.GetKey( 1 ) == "world" );
136
137   // Out of bounds
138   try
139   {
140     map.GetKey( 2 );
141     tet_result( TET_FAIL );
142   }
143   catch ( DaliException& e )
144   {
145     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
146   }
147
148   END_TEST;
149 }
150
151 int UtcDaliPropertyMapGetPair(void)
152 {
153   Property::Map map;
154   map[ "hello" ] = 1;
155   map[ "world" ] = 2;
156
157   DALI_TEST_CHECK( map.GetPair( 0 ).first == "hello" );
158   DALI_TEST_CHECK( map.GetPair( 0 ).second.Get< int >() == 1 );
159   DALI_TEST_CHECK( map.GetPair( 1 ).first == "world" );
160   DALI_TEST_CHECK( map.GetPair( 1 ).second.Get< int >() == 2 );
161
162   // Out of bounds
163   try
164   {
165     map.GetPair( 2 );
166     tet_result( TET_FAIL );
167   }
168   catch ( DaliException& e )
169   {
170     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
171   }
172
173   END_TEST;
174 }
175
176 int UtcDaliPropertyMapFind(void)
177 {
178   Property::Map map;
179   map[ "hello" ] = 1;
180   map[ "world" ] = 2;
181
182   Property::Value* value = NULL;
183
184   value = map.Find( "hello" );
185   DALI_TEST_CHECK( value );
186   DALI_TEST_CHECK( value->Get<int>() == 1 );
187
188   value = map.Find( "world" );
189   DALI_TEST_CHECK( value );
190   DALI_TEST_CHECK( value->Get<int>() == 2 );
191
192   value = map.Find( "invalid-key" );
193   DALI_TEST_CHECK( !value );
194
195   END_TEST;
196 }
197
198 int UtcDaliPropertyMapMerge(void)
199 {
200   Property::Map map;
201   map[ "hello" ] = 1;
202   map[ "world" ] = 2;
203
204   DALI_TEST_CHECK( map.Count() == 2 );
205
206   // Create another map with the same keys but different values
207   Property::Map map2;
208   map[ "hello" ] = 3;
209   map[ "world" ] = 4;
210
211   // Merge map2 into map1, count should still be 2, map values should be from map2
212   map.Merge( map2 );
213   DALI_TEST_CHECK( map.Count() == 2 );
214   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
215   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
216
217   // Create another map with different keys
218   Property::Map map3;
219   map3[ "foo" ] = 5;
220   map3[ "bar" ] = 6;
221
222   // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
223   map.Merge( map3 );
224   DALI_TEST_CHECK( map.Count() == 4 );
225   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
226   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
227   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
228   DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
229
230   // Create an empty map and attempt to merge, should be successful, nothing should change
231   Property::Map map4;
232   DALI_TEST_CHECK( map4.Empty() );
233   map.Merge( map4 );
234   DALI_TEST_CHECK( map4.Empty() );
235   DALI_TEST_CHECK( map.Count() == 4 );
236   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
237   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
238   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
239   DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
240
241   // Merge map into map4, map4 should be the same as map now.
242   map4.Merge( map );
243   DALI_TEST_CHECK( map4.Count() == 4 );
244   DALI_TEST_CHECK( map4[ "hello" ].Get< int >() == 3 );
245   DALI_TEST_CHECK( map4[ "world"].Get< int >() == 4 );
246   DALI_TEST_CHECK( map4[ "foo"].Get< int >() == 5 );
247   DALI_TEST_CHECK( map4[ "bar"].Get< int >() == 6 );
248
249   // Attempt to merge into itself, should be successful, nothing should change
250   map.Merge( map );
251   DALI_TEST_CHECK( map.Count() == 4 );
252   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
253   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
254   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
255   DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
256
257   END_TEST;
258 }