2 * Copyright (c) 2022 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.
18 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/dali-core.h>
24 // Internal headers are allowed here
25 #include <dali/internal/common/indexed-const-string-map.h>
28 using Dali::Internal::ConstString;
29 using Dali::Internal::IndexedConstStringMap;
32 std::string RandomString(size_t length)
36 "abcdefghijklmnopqrstuvwxyz"
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
39 thread_local static std::mt19937 rg{std::random_device{}()};
40 thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);
52 // Custom << operator to print debugging log.
53 std::basic_ostream<char>& operator<<(std::basic_ostream<char>& os, const ConstString& constString)
55 std::string convertedString = std::string(constString.GetStringView());
56 os << convertedString;
62 void utc_dali_internal_indexed_conststring_map_startup(void)
64 test_return_value = TET_UNDEF;
67 void utc_dali_internal_indexed_conststring_map_cleanup(void)
69 test_return_value = TET_PASS;
72 int UtcDaliIndexedConstStringMapEmpty(void)
74 IndexedConstStringMap<int> indexedMap;
75 DALI_TEST_EQUALS(indexedMap.Empty(), true, TEST_LOCATION);
76 DALI_TEST_EQUALS(indexedMap.empty(), true, TEST_LOCATION);
78 ConstString str("hello");
80 DALI_TEST_EQUALS(indexedMap.Register(str, value), true, TEST_LOCATION);
82 DALI_TEST_EQUALS(indexedMap.Empty(), false, TEST_LOCATION);
83 DALI_TEST_EQUALS(indexedMap.empty(), false, TEST_LOCATION);
85 DALI_TEST_EQUALS(indexedMap.Count(), 1, TEST_LOCATION);
86 DALI_TEST_EQUALS(indexedMap.size(), 1, TEST_LOCATION);
88 DALI_TEST_EQUALS(indexedMap.Get(str)->first, str, TEST_LOCATION);
89 DALI_TEST_EQUALS(indexedMap.Get(str)->second, value, TEST_LOCATION);
92 DALI_TEST_EQUALS(indexedMap.Empty(), true, TEST_LOCATION);
93 DALI_TEST_EQUALS(indexedMap.empty(), true, TEST_LOCATION);
95 DALI_TEST_CHECK(indexedMap.Get(str) == indexedMap.End());
100 int UtcDaliIndexedConstStringMap(void)
102 IndexedConstStringMap<int> indexedMap;
104 auto IndexedMapGetValueTest = [](const IndexedConstStringMap<int>& indexedMap, ConstString key, bool registered, int element, const char* location) {
105 const auto& iter = indexedMap.Get(key);
106 DALI_TEST_EQUALS(!(iter == indexedMap.end()), registered, location);
109 DALI_TEST_EQUALS(iter->second, element, location);
113 ConstString keyFirst = ConstString("first");
114 ConstString keySecond = ConstString("second");
115 ConstString keyThird = ConstString("third");
116 ConstString keyFourth = ConstString("fourth");
118 // Check. empty state
119 DALI_TEST_EQUALS(0u, indexedMap.Count(), TEST_LOCATION);
120 DALI_TEST_CHECK(indexedMap.Begin() == indexedMap.End());
121 DALI_TEST_CHECK(indexedMap.Empty());
122 DALI_TEST_EQUALS(0u, indexedMap.size(), TEST_LOCATION);
123 DALI_TEST_CHECK(indexedMap.begin() == indexedMap.end());
124 DALI_TEST_CHECK(indexedMap.empty());
126 // phase 1 - Regist two element
127 DALI_TEST_CHECK(indexedMap.Register(keyFirst, 1));
128 DALI_TEST_CHECK(indexedMap.Register(keySecond, 2));
131 DALI_TEST_EQUALS(2u, indexedMap.Count(), TEST_LOCATION);
132 DALI_TEST_CHECK(!indexedMap.Empty());
133 IndexedMapGetValueTest(indexedMap, ConstString("first"), true, 1, TEST_LOCATION);
134 IndexedMapGetValueTest(indexedMap, ConstString("second"), true, 2, TEST_LOCATION);
135 IndexedMapGetValueTest(indexedMap, ConstString("third"), false, 0, TEST_LOCATION);
136 IndexedMapGetValueTest(indexedMap, ConstString("fourth"), false, 0, TEST_LOCATION);
139 DALI_TEST_EQUALS(indexedMap[keyFirst], 1, TEST_LOCATION);
140 DALI_TEST_EQUALS(indexedMap[keySecond], 2, TEST_LOCATION);
141 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(0), 1, TEST_LOCATION);
142 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(1), 2, TEST_LOCATION);
143 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(0), keyFirst, TEST_LOCATION);
144 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(1), keySecond, TEST_LOCATION);
145 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(0) == IndexedConstStringMap<int>::KeyElementPairType(keyFirst, 1));
146 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(1) == IndexedConstStringMap<int>::KeyElementPairType(keySecond, 2));
148 // Const iteration check
149 for(const auto& elem : indexedMap)
151 if(elem.first == keyFirst)
153 DALI_TEST_EQUALS(elem.second, 1, TEST_LOCATION);
155 else if(elem.first == keySecond)
157 DALI_TEST_EQUALS(elem.second, 2, TEST_LOCATION);
161 DALI_TEST_CHECK(false); // Should not get here
165 // Iteration and fix data check
166 for(auto&& elem : indexedMap)
168 if(elem.first == keyFirst)
173 // operator[] fix data check
174 indexedMap[keySecond] += 220;
177 DALI_TEST_EQUALS(2u, indexedMap.Count(), TEST_LOCATION);
178 IndexedMapGetValueTest(indexedMap, keyFirst, true, 111, TEST_LOCATION);
179 IndexedMapGetValueTest(indexedMap, keySecond, true, 222, TEST_LOCATION);
180 IndexedMapGetValueTest(indexedMap, keyThird, false, 0, TEST_LOCATION);
181 IndexedMapGetValueTest(indexedMap, keyFourth, false, 0, TEST_LOCATION);
184 DALI_TEST_EQUALS(indexedMap[keyFirst], 111, TEST_LOCATION);
185 DALI_TEST_EQUALS(indexedMap[keySecond], 222, TEST_LOCATION);
186 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(0), 111, TEST_LOCATION);
187 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(1), 222, TEST_LOCATION);
188 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(0), keyFirst, TEST_LOCATION);
189 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(1), keySecond, TEST_LOCATION);
190 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(0) == IndexedConstStringMap<int>::KeyElementPairType(keyFirst, 111));
191 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(1) == IndexedConstStringMap<int>::KeyElementPairType(keySecond, 222));
193 // Const iteration check
194 for(const auto& elem : indexedMap)
196 if(elem.first == keyFirst)
198 DALI_TEST_EQUALS(elem.second, 111, TEST_LOCATION);
200 else if(elem.first == keySecond)
202 DALI_TEST_EQUALS(elem.second, 222, TEST_LOCATION);
206 DALI_TEST_CHECK(false); // Should not get here
210 // phase 2 - Regist two more element
211 DALI_TEST_CHECK(!indexedMap.Register(keyFirst, 11)); // Register failed when we try to insert data with same key
212 DALI_TEST_CHECK(!indexedMap.Register(keySecond, 22)); // Register failed when we try to insert data with same key
213 DALI_TEST_CHECK(indexedMap.Register(keyThird, 3));
214 DALI_TEST_CHECK(indexedMap.Register(keyFourth, 4));
217 DALI_TEST_EQUALS(4, indexedMap.Count(), TEST_LOCATION);
218 IndexedMapGetValueTest(indexedMap, keyFirst, true, 111, TEST_LOCATION);
219 IndexedMapGetValueTest(indexedMap, keySecond, true, 222, TEST_LOCATION);
220 IndexedMapGetValueTest(indexedMap, keyThird, true, 3, TEST_LOCATION);
221 IndexedMapGetValueTest(indexedMap, keyFourth, true, 4, TEST_LOCATION);
223 // Get elemnt as l-value
225 auto iter = indexedMap.Get(keyFourth);
226 DALI_TEST_CHECK(iter != indexedMap.end())
233 DALI_TEST_EQUALS(indexedMap[keyFirst], 111, TEST_LOCATION);
234 DALI_TEST_EQUALS(indexedMap[keySecond], 222, TEST_LOCATION);
235 DALI_TEST_EQUALS(indexedMap[keyThird], 3, TEST_LOCATION);
236 DALI_TEST_EQUALS(indexedMap[keyFourth], 444, TEST_LOCATION);
237 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(0), 111, TEST_LOCATION);
238 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(1), 222, TEST_LOCATION);
239 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(2), 3, TEST_LOCATION);
240 DALI_TEST_EQUALS(indexedMap.GetElementByIndex(3), 444, TEST_LOCATION);
241 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(0), keyFirst, TEST_LOCATION);
242 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(1), keySecond, TEST_LOCATION);
243 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(2), keyThird, TEST_LOCATION);
244 DALI_TEST_EQUALS(indexedMap.GetKeyByIndex(3), keyFourth, TEST_LOCATION);
245 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(0) == IndexedConstStringMap<int>::KeyElementPairType(keyFirst, 111));
246 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(1) == IndexedConstStringMap<int>::KeyElementPairType(keySecond, 222));
247 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(2) == IndexedConstStringMap<int>::KeyElementPairType(keyThird, 3));
248 DALI_TEST_CHECK(indexedMap.GetKeyElementPairByIndex(3) == IndexedConstStringMap<int>::KeyElementPairType(keyFourth, 444));
251 DALI_TEST_EQUALS(const_cast<const IndexedConstStringMap<int>&>(indexedMap)[keyFirst], 111, TEST_LOCATION);
254 DALI_TEST_CHECK(!indexedMap.Empty());
256 DALI_TEST_CHECK(indexedMap.Empty());
257 DALI_TEST_EQUALS(0u, indexedMap.Count(), TEST_LOCATION);
258 IndexedMapGetValueTest(indexedMap, keyFirst, false, 1, TEST_LOCATION);
259 IndexedMapGetValueTest(indexedMap, keySecond, false, 2, TEST_LOCATION);
260 IndexedMapGetValueTest(indexedMap, keyThird, false, 3, TEST_LOCATION);
261 IndexedMapGetValueTest(indexedMap, keyFourth, false, 4, TEST_LOCATION);
266 int UtcDaliIndexedConstStringMapNegative(void)
268 tet_infoline("Negative test when IndexedConstStringMap access non-exist elements.");
270 IndexedConstStringMap<int> indexedMap;
272 auto IndexedMapAssertTestWithIndex = [](IndexedConstStringMap<int>& indexedMap, int testIndex) {
273 tet_printf("operator[] test");
274 // Assert when try to access as const operator[] input with not registered key.
277 const auto& elem = const_cast<const IndexedConstStringMap<int>&>(indexedMap)[ConstString("333")];
278 if(elem == 0) // Avoid build warning
280 DALI_TEST_CHECK(false); // Should not get here
282 DALI_TEST_CHECK(false); // Should not get here
286 DALI_TEST_CHECK(true); // Asserted
288 // Assert when try to access as operator[] input with not registered key.
291 indexedMap[ConstString("333")] = 0;
292 DALI_TEST_CHECK(false); // Should not get here
296 DALI_TEST_CHECK(true); // Asserted
299 tet_printf("GetElementByIndex test");
300 // Assert when try to access as GetElementByIndex input with not registered index.
303 const auto& elem = indexedMap.GetElementByIndex(testIndex);
304 if(elem == 0) // Avoid build warning
306 DALI_TEST_CHECK(false); // Should not get here
308 DALI_TEST_CHECK(false); // Should not get here
312 DALI_TEST_CHECK(true); // Asserted
315 tet_printf("GetKeyByIndex test");
316 // Assert when try to access as GetKeyByIndex input with not registered index.
319 const auto& elem = indexedMap.GetKeyByIndex(testIndex);
320 if(elem == 0) // Avoid build warning
322 DALI_TEST_CHECK(false); // Should not get here
324 DALI_TEST_CHECK(false); // Should not get here
328 DALI_TEST_CHECK(true); // Asserted
331 tet_printf("GetKeyElementPairByIndex test");
332 // Assert when try to access as GetKeyByIndex input with not registered index.
335 const auto& elem = indexedMap.GetKeyElementPairByIndex(testIndex);
336 if(elem == IndexedConstStringMap<int>::KeyElementPairType(ConstString("zero"), 0)) // Avoid build warning
338 DALI_TEST_CHECK(false); // Should not get here
340 DALI_TEST_CHECK(false); // Should not get here
344 DALI_TEST_CHECK(true); // Asserted
347 // Assert test with empty indexedMap.
348 DALI_TEST_CHECK(indexedMap.Empty());
349 IndexedMapAssertTestWithIndex(indexedMap, 0);
352 DALI_TEST_CHECK(indexedMap.Register(ConstString("first"), 1));
353 DALI_TEST_CHECK(indexedMap.Register(ConstString("second"), 2));
354 DALI_TEST_EQUALS(2u, indexedMap.Count(), TEST_LOCATION);
355 IndexedMapAssertTestWithIndex(indexedMap, 2);
357 // Test with always-invalid index like -1
358 IndexedMapAssertTestWithIndex(indexedMap, -1);
363 int UtcDaliIndexedConstStringMapStressTest(void)
365 // Copy from utc-Dali-Internal-ConstString.cpp
366 static constexpr size_t DB_SIZE = 2000;
368 std::vector<std::string> Database;
369 Database.reserve(DB_SIZE);
371 IndexedConstStringMap<ConstString> constStringDB1;
372 constStringDB1.reserve(DB_SIZE);
374 IndexedConstStringMap<std::string> constStringDB2;
375 constStringDB2.reserve(DB_SIZE);
377 for(auto i = 0u; i < DB_SIZE; i++)
381 Database.push_back(RandomString(10));
385 Database.push_back(RandomString(7));
389 Database.push_back(RandomString(11));
391 std::string randomValue = RandomString(10);
392 DALI_TEST_EQUALS(constStringDB1.Register(ConstString(Database[i]), ConstString(randomValue)), true, TEST_LOCATION);
393 DALI_TEST_EQUALS(constStringDB2.Register(ConstString(Database[i]), randomValue), true, TEST_LOCATION);
396 // Try to extra regist with same key
397 for(auto i = 0u; i < DB_SIZE; i++)
399 std::string randomValue = RandomString(2);
400 DALI_TEST_EQUALS(constStringDB1.Register(ConstString(Database[i]), ConstString(randomValue)), false, TEST_LOCATION);
401 DALI_TEST_EQUALS(constStringDB2.Register(ConstString(Database[i]), randomValue), false, TEST_LOCATION);
404 // check eqality betwwen original string and constString
405 for(auto i = 0u; i < DB_SIZE; i++)
407 DALI_TEST_EQUALS(constStringDB1.GetKeyByIndex(i).GetCString(), Database[i].c_str(), TEST_LOCATION);
408 DALI_TEST_EQUALS(constStringDB2.GetKeyByIndex(i).GetCString(), Database[i].c_str(), TEST_LOCATION);
411 // check pointer eqality betwwen 2 constString
412 for(auto i = 0u; i < DB_SIZE; i++)
414 bool pointerEqual = (constStringDB1[ConstString(Database[i])] == ConstString(constStringDB2[ConstString(Database[i])]));
415 DALI_TEST_EQUALS(pointerEqual, true, TEST_LOCATION);