Make NPatchData receive LoadComplete call from TextureManager.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Dictionary.cpp
1 /*
2  * Copyright (c) 2020 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 #include <dali-toolkit-test-suite-utils.h>
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/internal/builder/dictionary.h>
20 using namespace Dali::Toolkit::Internal;
21
22
23 std::string_view test_keys[20] =
24 {
25   "testkey0", "testkey1", "testkey2", "testkey3", "testkey4", "testkey5", "testkey6", "testkey7", "testkey8", "testkey9",
26   "testkey10", "testkey11", "testkey12", "testkey13", "testkey14", "testkey15", "testkey16", "testkey17", "testkey18", "testkey19"
27 };
28
29 std::string_view testKeys[20] =
30 {
31   "TestKey0", "TestKey1", "TestKey2", "TestKey3", "TestKey4", "TestKey5", "TestKey6", "TestKey7", "TestKey8", "TestKey9",
32   "TestKey10", "TestKey11", "TestKey12", "TestKey13", "TestKey14", "TestKey15", "TestKey16", "TestKey17", "TestKey18", "TestKey19"
33 };
34
35
36 int UtcDaliBuilderDictionaryNew(void)
37 {
38   Dictionary<int> dictionary;
39
40   DictionaryKeys keys;
41   dictionary.GetKeys(keys);
42
43   DALI_TEST_CHECK( keys.empty() );
44   DALI_TEST_EQUALS( keys.size(), 0, TEST_LOCATION );
45   END_TEST;
46 }
47
48
49 int UtcDaliBuilderDictionaryAdd1(void)
50 {
51   Dictionary<int> dictionary;
52
53   for(int i=0; i<10; i++)
54   {
55     char buffer[16];
56     sprintf(buffer, "testkey%d",i);
57     bool added=false;
58     if(i%2 == 0)
59     {
60       added = dictionary.Add(std::string(buffer), i);
61     }
62     else
63     {
64       added = dictionary.Add(buffer, i);
65     }
66     DALI_TEST_EQUALS(added, true, TEST_LOCATION);
67   }
68
69   DictionaryKeys keys;
70   dictionary.GetKeys(keys);
71
72   DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
73   for(int i=0; i<10; i++)
74   {
75     char buffer[16];
76     sprintf(buffer, "testkey%d",i);
77     auto iter = std::find(keys.begin(), keys.end(), std::string(buffer));
78     DALI_TEST_CHECK(iter != keys.end());
79   }
80
81   END_TEST;
82 }
83
84 int UtcDaliBuilderDictionaryAdd2(void)
85 {
86   Dictionary<int> dictionary;
87
88   for(int i=0; i<10; i++)
89   {
90     char buffer[16];
91     sprintf(buffer, "testkey%d",i);
92     bool added=false;
93     if(i%2 == 0)
94     {
95       added = dictionary.Add(std::string(buffer), i);
96     }
97     else
98     {
99       added = dictionary.Add(buffer, i);
100     }
101     DALI_TEST_EQUALS(added, true, TEST_LOCATION);
102   }
103
104   DictionaryKeys keys;
105   dictionary.GetKeys(keys);
106   DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
107
108   bool added = dictionary.Add("testkey5", 1);
109   DALI_TEST_EQUALS(added, false, TEST_LOCATION);
110   DALI_TEST_EQUALS(*dictionary.Find("testkey5"), 5, TEST_LOCATION);
111
112   dictionary.Clear();
113   DALI_TEST_EQUALS(dictionary.Begin()==dictionary.End(), true, TEST_LOCATION);
114   dictionary.GetKeys(keys);
115   DALI_TEST_EQUALS( keys.size(), 0, TEST_LOCATION );
116
117   END_TEST;
118 }
119
120
121 int UtcDaliBuilderDictionaryRemoveP(void)
122 {
123   Dictionary<int> dictionary;
124
125   for(int i=0; i<10; i++)
126   {
127     bool added=false;
128     added = dictionary.Add(std::string(testKeys[i]), i);
129     DALI_TEST_EQUALS(added, true, TEST_LOCATION);
130   }
131
132   DictionaryKeys keys;
133   dictionary.GetKeys(keys);
134   DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
135
136   for(int i=0; i<10; i++)
137   {
138     if(i%2==0)
139     {
140       dictionary.Remove(test_keys[i]); // Should fail (case sensitive)
141     }
142     else
143     {
144       dictionary.Remove(testKeys[i]);
145     }
146   }
147   dictionary.GetKeys(keys);
148   DALI_TEST_EQUALS( keys.size(), 5, TEST_LOCATION );
149
150   dictionary.Clear();
151   DALI_TEST_EQUALS(dictionary.Begin()==dictionary.End(), true, TEST_LOCATION);
152   dictionary.GetKeys(keys);
153   DALI_TEST_EQUALS( keys.size(), 0, TEST_LOCATION );
154
155   END_TEST;
156 }
157
158
159 int UtcDaliBuilderDictionaryRemoveN(void)
160 {
161   Dictionary<int> dictionary;
162
163   for(int i=0; i<10; i++)
164   {
165     bool added=false;
166     added = dictionary.Add(std::string(testKeys[i]), i);
167     DALI_TEST_EQUALS(added, true, TEST_LOCATION);
168   }
169
170   DictionaryKeys keys;
171   dictionary.GetKeys(keys);
172   DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
173
174   dictionary.Remove("randomkey");
175   dictionary.GetKeys(keys);
176   DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
177
178   END_TEST;
179 }
180
181
182 int UtcDaliBuilderDictionaryMerge1(void)
183 {
184   // Test that "overlapping" dicts merge into 1 successfully
185   Dictionary<int> dictionary1;
186
187   for(int i=0; i<10; i++)
188   {
189     dictionary1.Add(std::string(test_keys[i]), i);
190   }
191
192   Dictionary<int> dictionary2;
193   for(int i=0; i<20; i++)
194   {
195     dictionary2.Add(std::string(testKeys[i]), i);
196   }
197
198   dictionary1.Merge(dictionary2);
199   DictionaryKeys keys;
200   dictionary1.GetKeys(keys);
201   DALI_TEST_EQUALS( keys.size(), 30, TEST_LOCATION ); // Now have 2 case versions of 10 keys :/ - broken by design?
202
203   for(int i=0; i<20;++i)
204   {
205     // Check both cases of keys
206     auto ptr1 = dictionary1.FindConst(test_keys[i]);
207     auto ptr2 = dictionary1.FindConst(testKeys[i]);
208
209     DALI_TEST_CHECK( nullptr != ptr1 );
210     DALI_TEST_CHECK( nullptr != ptr2 );
211   }
212
213   END_TEST;
214 }
215
216 int UtcDaliBuilderDictionaryMerge2(void)
217 {
218   // Test that non-overlapping dicts merge successfully
219   Dictionary<int> dictionary1;
220   for(int i=0; i<10; i++) // Add first 10 from lowercase keys
221   {
222     dictionary1.Add(std::string(test_keys[i]), i);
223   }
224
225   Dictionary<int> dictionary2;
226   for(int i=10; i<20; i++) // add last 10 from capitalized keys
227   {
228     dictionary2.Add(std::string(testKeys[i]), i);
229   }
230
231   dictionary1.Merge(dictionary2);
232   DictionaryKeys keys;
233   dictionary1.GetKeys(keys);
234   DALI_TEST_EQUALS( keys.size(), 20, TEST_LOCATION ); // check it's an amalgam of both
235
236   for(int i=0; i<20;++i)
237   {
238     // Check both cases of keys
239     DALI_TEST_CHECK( nullptr != dictionary1.FindConst(test_keys[i]));
240     DALI_TEST_CHECK( nullptr != dictionary1.FindConst(testKeys[i]));
241   }
242
243   END_TEST;
244 }
245
246
247 template<typename EntryType>
248 struct TestElement
249 {
250   std::string key;
251   EntryType entry;
252   TestElement(std::string name, EntryType entry)
253   : key(std::move(name)),
254     entry(std::move(entry))
255   {
256   }
257 };
258
259 int UtcDaliBuilderDictionaryFindP(void)
260 {
261   // Test that non-overlapping dicts merge successfully
262   Dictionary<int> dictionary;
263   for(int i=0; i<10; i++) // Add first 10 from lowercase keys
264   {
265     dictionary.Add(std::string(test_keys[i]), i);
266   }
267
268   // Test that the entries can be directly modified
269   for(int i=0; i<10; i++)
270   {
271     auto entryPtr = dictionary.Find(testKeys[i]);
272     DALI_TEST_CHECK( entryPtr != nullptr );
273     *entryPtr = i+10;
274   }
275
276   for(int i=0; i<10; ++i)
277   {
278     auto entryPtr = dictionary.Find(testKeys[i]);
279     DALI_TEST_CHECK( entryPtr != nullptr );
280     DALI_TEST_EQUALS( *entryPtr, i+10, TEST_LOCATION );
281   }
282
283   END_TEST;
284 }