2 * Copyright (c) 2020 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.
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;
23 std::string_view test_keys[20] =
25 "testkey0", "testkey1", "testkey2", "testkey3", "testkey4", "testkey5", "testkey6", "testkey7", "testkey8", "testkey9",
26 "testkey10", "testkey11", "testkey12", "testkey13", "testkey14", "testkey15", "testkey16", "testkey17", "testkey18", "testkey19"
29 std::string_view testKeys[20] =
31 "TestKey0", "TestKey1", "TestKey2", "TestKey3", "TestKey4", "TestKey5", "TestKey6", "TestKey7", "TestKey8", "TestKey9",
32 "TestKey10", "TestKey11", "TestKey12", "TestKey13", "TestKey14", "TestKey15", "TestKey16", "TestKey17", "TestKey18", "TestKey19"
36 int UtcDaliBuilderDictionaryNew(void)
38 Dictionary<int> dictionary;
41 dictionary.GetKeys(keys);
43 DALI_TEST_CHECK( keys.empty() );
44 DALI_TEST_EQUALS( keys.size(), 0, TEST_LOCATION );
49 int UtcDaliBuilderDictionaryAdd1(void)
51 Dictionary<int> dictionary;
53 for(int i=0; i<10; i++)
56 sprintf(buffer, "testkey%d",i);
60 added = dictionary.Add(std::string(buffer), i);
64 added = dictionary.Add(buffer, i);
66 DALI_TEST_EQUALS(added, true, TEST_LOCATION);
70 dictionary.GetKeys(keys);
72 DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
73 for(int i=0; i<10; i++)
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());
84 int UtcDaliBuilderDictionaryAdd2(void)
86 Dictionary<int> dictionary;
88 for(int i=0; i<10; i++)
91 sprintf(buffer, "testkey%d",i);
95 added = dictionary.Add(std::string(buffer), i);
99 added = dictionary.Add(buffer, i);
101 DALI_TEST_EQUALS(added, true, TEST_LOCATION);
105 dictionary.GetKeys(keys);
106 DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
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);
113 DALI_TEST_EQUALS(dictionary.Begin()==dictionary.End(), true, TEST_LOCATION);
114 dictionary.GetKeys(keys);
115 DALI_TEST_EQUALS( keys.size(), 0, TEST_LOCATION );
121 int UtcDaliBuilderDictionaryRemoveP(void)
123 Dictionary<int> dictionary;
125 for(int i=0; i<10; i++)
128 added = dictionary.Add(std::string(testKeys[i]), i);
129 DALI_TEST_EQUALS(added, true, TEST_LOCATION);
133 dictionary.GetKeys(keys);
134 DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
136 for(int i=0; i<10; i++)
140 dictionary.Remove(test_keys[i]); // Should fail (case sensitive)
144 dictionary.Remove(testKeys[i]);
147 dictionary.GetKeys(keys);
148 DALI_TEST_EQUALS( keys.size(), 5, TEST_LOCATION );
151 DALI_TEST_EQUALS(dictionary.Begin()==dictionary.End(), true, TEST_LOCATION);
152 dictionary.GetKeys(keys);
153 DALI_TEST_EQUALS( keys.size(), 0, TEST_LOCATION );
159 int UtcDaliBuilderDictionaryRemoveN(void)
161 Dictionary<int> dictionary;
163 for(int i=0; i<10; i++)
166 added = dictionary.Add(std::string(testKeys[i]), i);
167 DALI_TEST_EQUALS(added, true, TEST_LOCATION);
171 dictionary.GetKeys(keys);
172 DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
174 dictionary.Remove("randomkey");
175 dictionary.GetKeys(keys);
176 DALI_TEST_EQUALS( keys.size(), 10, TEST_LOCATION );
182 int UtcDaliBuilderDictionaryMerge1(void)
184 // Test that "overlapping" dicts merge into 1 successfully
185 Dictionary<int> dictionary1;
187 for(int i=0; i<10; i++)
189 dictionary1.Add(std::string(test_keys[i]), i);
192 Dictionary<int> dictionary2;
193 for(int i=0; i<20; i++)
195 dictionary2.Add(std::string(testKeys[i]), i);
198 dictionary1.Merge(dictionary2);
200 dictionary1.GetKeys(keys);
201 DALI_TEST_EQUALS( keys.size(), 30, TEST_LOCATION ); // Now have 2 case versions of 10 keys :/ - broken by design?
203 for(int i=0; i<20;++i)
205 // Check both cases of keys
206 auto ptr1 = dictionary1.FindConst(test_keys[i]);
207 auto ptr2 = dictionary1.FindConst(testKeys[i]);
209 DALI_TEST_CHECK( nullptr != ptr1 );
210 DALI_TEST_CHECK( nullptr != ptr2 );
216 int UtcDaliBuilderDictionaryMerge2(void)
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
222 dictionary1.Add(std::string(test_keys[i]), i);
225 Dictionary<int> dictionary2;
226 for(int i=10; i<20; i++) // add last 10 from capitalized keys
228 dictionary2.Add(std::string(testKeys[i]), i);
231 dictionary1.Merge(dictionary2);
233 dictionary1.GetKeys(keys);
234 DALI_TEST_EQUALS( keys.size(), 20, TEST_LOCATION ); // check it's an amalgam of both
236 for(int i=0; i<20;++i)
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]));
247 template<typename EntryType>
252 TestElement(std::string name, EntryType entry)
253 : key(std::move(name)),
254 entry(std::move(entry))
259 int UtcDaliBuilderDictionaryFindP(void)
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
265 dictionary.Add(std::string(test_keys[i]), i);
268 // Test that the entries can be directly modified
269 for(int i=0; i<10; i++)
271 auto entryPtr = dictionary.Find(testKeys[i]);
272 DALI_TEST_CHECK( entryPtr != nullptr );
276 for(int i=0; i<10; ++i)
278 auto entryPtr = dictionary.Find(testKeys[i]);
279 DALI_TEST_CHECK( entryPtr != nullptr );
280 DALI_TEST_EQUALS( *entryPtr, i+10, TEST_LOCATION );