[dali_1.0.36] Merge branch 'tizen'
[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 <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[ "invalid-key" ];
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( "invalid-key" );
192   DALI_TEST_CHECK( !value );
193
194   END_TEST;
195 }
196
197 int UtcDaliPropertyMapMerge(void)
198 {
199   Property::Map map;
200   map[ "hello" ] = 1;
201   map[ "world" ] = 2;
202
203   DALI_TEST_CHECK( map.Count() == 2 );
204
205   // Create another map with the same keys but different values
206   Property::Map map2;
207   map[ "hello" ] = 3;
208   map[ "world" ] = 4;
209
210   // Merge map2 into map1, count should still be 2, map values should be from map2
211   map.Merge( map2 );
212   DALI_TEST_CHECK( map.Count() == 2 );
213   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
214   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
215
216   // Create another map with different keys
217   Property::Map map3;
218   map3[ "foo" ] = 5;
219   map3[ "bar" ] = 6;
220
221   // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
222   map.Merge( map3 );
223   DALI_TEST_CHECK( map.Count() == 4 );
224   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
225   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
226   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
227   DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
228
229   // Create an empty map and attempt to merge, should be successful, nothing should change
230   Property::Map map4;
231   DALI_TEST_CHECK( map4.Empty() );
232   map.Merge( map4 );
233   DALI_TEST_CHECK( map4.Empty() );
234   DALI_TEST_CHECK( map.Count() == 4 );
235   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
236   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
237   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
238   DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
239
240   // Merge map into map4, map4 should be the same as map now.
241   map4.Merge( map );
242   DALI_TEST_CHECK( map4.Count() == 4 );
243   DALI_TEST_CHECK( map4[ "hello" ].Get< int >() == 3 );
244   DALI_TEST_CHECK( map4[ "world"].Get< int >() == 4 );
245   DALI_TEST_CHECK( map4[ "foo"].Get< int >() == 5 );
246   DALI_TEST_CHECK( map4[ "bar"].Get< int >() == 6 );
247
248   // Attempt to merge into itself, should be successful, nothing should change
249   map.Merge( map );
250   DALI_TEST_CHECK( map.Count() == 4 );
251   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
252   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
253   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
254   DALI_TEST_CHECK( map[ "bar"].Get< int >() == 6 );
255
256   END_TEST;
257 }