2 * Copyright (c) 2023 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.
22 #include <dali-test-suite-utils.h>
23 #include <dali/internal/common/ordered-set.h>
25 using namespace Dali::Internal;
27 void utc_dali_internal_owner_set_startup(void)
29 test_return_value = TET_UNDEF;
32 void utc_dali_internal_owner_set_cleanup(void)
34 test_return_value = TET_PASS;
37 ///////////////////////////////////////////////////////////////////////////////
40 /// Takes in a reference to a bool which is set to true when the destructor is called
44 OwnedClass(bool& destructorCalled)
45 : mDestructorCalled(destructorCalled)
47 mDestructorCalled = false;
52 mDestructorCalled = true;
56 bool& mDestructorCalled;
59 /// Just a simple class with a member value and global refcount.
76 int ClassWithId::gRefCount = 0;
80 ///////////////////////////////////////////////////////////////////////////////
82 int UtcDaliOrderedSetEnsureDeletion(void)
84 // Ensure that the object owned by the OrderedSet is deleted.
89 OrderedSet<OwnedClass> set;
90 set.PushBack(new OwnedClass(deleted));
91 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
94 // OrderedSet out-of-scope, object should be deleted.
96 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
101 int UtcDaliOrderedSetFalseEnsureNotDeletionWhen(void)
103 // Ensure that the object owned by the OrderedSET is deleted.
105 bool deleted = false;
110 // Create OrderedSet without ownership. It will not delete automatically.
111 OrderedSet<OwnedClass, false> set;
112 ptr = new OwnedClass(deleted);
114 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
117 // OrderedSet out-of-scope, but, object should not be deleted.
119 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
122 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
127 int UtcDaliOrderedSetDefaultConstructor(void)
129 // Ensure the default constructor is created as expected.
131 OrderedSet<OwnedClass> set;
132 DALI_TEST_CHECK(set.Count() == 0u);
137 int UtcDaliOrderedSetFalseDefaultConstructor(void)
139 // Ensure the default constructor is created as expected.
141 OrderedSet<OwnedClass, false> set;
142 DALI_TEST_CHECK(set.Count() == 0u);
147 int UtcDaliOrderedSetReserve(void)
149 OrderedSet<OwnedClass> set;
152 // Reserve function didn't change the count.
153 DALI_TEST_CHECK(set.Count() == 0u);
155 OrderedSet<OwnedClass> set2;
158 // Reserve function didn't change the count.
159 DALI_TEST_CHECK(set2.Count() == 0u);
164 int UtcDaliOrderedSetMove(void)
166 // Call move constructor and move assignment operator
168 bool deleted = false;
169 OwnedClass* owned = new OwnedClass(deleted);
171 OrderedSet<OwnedClass> first;
172 DALI_TEST_CHECK(first.Find(owned) == first.End());
173 first.PushBack(owned);
174 DALI_TEST_CHECK(first.Find(owned) != first.End());
177 // Move constructor, first should have a nullptr now, no object deletion
178 OrderedSet<OwnedClass> second(std::move(first));
179 DALI_TEST_CHECK(first.Find(owned) == first.End());
180 DALI_TEST_CHECK(second.Find(owned) != second.End());
181 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
183 // Assign second to first, no deletion, second should have a nullptr now
184 first = std::move(second);
185 DALI_TEST_CHECK(first.Find(owned) != first.End());
186 DALI_TEST_CHECK(second.Find(owned) == second.End());
187 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
190 // second is out-of-scope now, no object deletion
191 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
193 // Assign to an empty pointer, owned object should be deleted
194 OrderedSet<OwnedClass> empty;
195 first = std::move(empty);
196 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
197 DALI_TEST_CHECK(first.Find(owned) == first.End());
198 DALI_TEST_CHECK(empty.Find(owned) == empty.End());
203 int UtcDaliOrderedSetFalseMove(void)
205 // Call move constructor and move assignment operator
207 bool deleted = false;
208 OwnedClass* owned = new OwnedClass(deleted);
210 OrderedSet<OwnedClass, false> first;
211 DALI_TEST_CHECK(first.Find(owned) == first.End());
212 first.PushBack(owned);
213 DALI_TEST_CHECK(first.Find(owned) != first.End());
216 // Move constructor, first should have a nullptr now, no object deletion
217 OrderedSet<OwnedClass, false> second(std::move(first));
218 DALI_TEST_CHECK(first.Find(owned) == first.End());
219 DALI_TEST_CHECK(second.Find(owned) != second.End());
220 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
222 // Assign second to first, no deletion, second should have a nullptr now
223 first = std::move(second);
224 DALI_TEST_CHECK(first.Find(owned) != first.End());
225 DALI_TEST_CHECK(second.Find(owned) == second.End());
226 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
229 // second is out-of-scope now, no object deletion
230 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
232 // Assign to an empty pointer, but owned object should not be deleted
233 OrderedSet<OwnedClass, false> empty;
234 first = std::move(empty);
235 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
236 DALI_TEST_CHECK(first.Find(owned) == first.End());
237 DALI_TEST_CHECK(empty.Find(owned) == empty.End());
240 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
245 int UtcDaliOrderedSetErase(void)
247 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
249 bool deleted = false;
250 OwnedClass* owned = new OwnedClass(deleted);
252 OrderedSet<OwnedClass> set;
254 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
255 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
256 set.EraseObject(owned);
257 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
262 int UtcDaliOrderedSetFalseErase(void)
264 // Ensure that calling Reset not deletes the object and sets the owner-pointer to NULL
266 bool deleted = false;
267 OwnedClass* owned = new OwnedClass(deleted);
269 OrderedSet<OwnedClass, false> set;
271 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
272 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
273 set.EraseObject(owned);
274 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
277 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
282 int UtcDaliOrderedSetClear(void)
284 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
286 bool deleted = false;
287 OwnedClass* owned = new OwnedClass(deleted);
289 OrderedSet<OwnedClass> set;
291 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
292 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
295 DALI_TEST_EQUALS(set.Count(), 0u, TEST_LOCATION);
296 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
301 int UtcDaliOrderedSetFalseClear(void)
303 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
305 bool deleted = false;
306 OwnedClass* owned = new OwnedClass(deleted);
308 OrderedSet<OwnedClass, false> set;
310 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
311 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
314 DALI_TEST_EQUALS(set.Count(), 0u, TEST_LOCATION);
315 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
318 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
323 int UtcDaliOrderedSetRelease(void)
325 // Ensure that calling Release does NOT delete the object but still sets the owner-pointer to NULL
327 bool deleted = false;
328 OwnedClass* owned = new OwnedClass(deleted);
330 OrderedSet<OwnedClass> set;
332 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
334 auto iter = set.Find(owned);
335 DALI_TEST_CHECK(iter != set.End());
336 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
338 OwnedClass* released = set.Release(iter);
339 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
340 DALI_TEST_CHECK(set.Find(owned) == set.End());
341 DALI_TEST_CHECK(set.Find(released) == set.End());
342 DALI_TEST_EQUALS(set.Count(), 0u, TEST_LOCATION);
345 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
350 int UtcDaliOrderedSetIteratorOrderCheck(void)
352 // Ensure that order of iterator is equal with Order of Insertion.
354 // Reset refcount of class
355 ClassWithId::gRefCount = 0;
357 // To avoid lucky pass, run this test multiple time.
361 int baseId = tryCnt; // random id
363 int n = 10 + 5 * (tryCnt + 1); // random count
365 OrderedSet<ClassWithId> set;
367 for(int i = 0; i < n; ++i)
369 ClassWithId* object = new ClassWithId(id++);
370 set.PushBack(object);
374 // Check by for iteration
376 for(auto iter = set.Begin(), iterEnd = set.End(); iter != iterEnd; ++iter)
378 DALI_TEST_EQUALS(expectId++, (*iter)->mId, TEST_LOCATION);
380 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
382 // Check by range referenced iteration
384 for(auto&& iter : set)
386 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
387 // Change iter's value.
390 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
392 // Check by range iteration. Note that class value changed.
393 expectId = baseId + 1;
394 for(const auto& iter : set)
396 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
398 DALI_TEST_EQUALS(expectId, id + 1, TEST_LOCATION);
401 // Check whether leak exist.
402 DALI_TEST_EQUALS(ClassWithId::gRefCount, 0, TEST_LOCATION);
407 int UtcDaliOrderedSetFalseIteratorOrderCheck(void)
409 // Ensure that order of iterator is equal with Order of Insertion.
411 // Reset refcount of class
412 ClassWithId::gRefCount = 0;
414 // Container of ownered class, to release memories after test finished.
415 std::vector<ClassWithId*> objectList;
417 // To avoid lucky pass, run this test multiple time.
421 int baseId = tryCnt; // random id
423 int n = 10 + 5 * (tryCnt + 1); // random count
425 OrderedSet<ClassWithId, false> set;
427 for(int i = 0; i < n; ++i)
429 ClassWithId* object = new ClassWithId(id++);
430 objectList.push_back(object);
431 set.PushBack(object);
435 // Check by for iteration
437 for(auto iter = set.Begin(), iterEnd = set.End(); iter != iterEnd; ++iter)
439 DALI_TEST_EQUALS(expectId++, (*iter)->mId, TEST_LOCATION);
441 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
443 // Check by range referenced iteration
445 for(auto&& iter : set)
447 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
448 // Change iter's value.
451 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
453 // Check by range iteration. Note that class value changed.
454 expectId = baseId + 1;
455 for(const auto& iter : set)
457 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
459 DALI_TEST_EQUALS(expectId, id + 1, TEST_LOCATION);
462 // Check whether leak exist.
463 DALI_TEST_EQUALS(ClassWithId::gRefCount, objectList.size(), TEST_LOCATION);
465 // Release memory now.
466 for(auto&& iter : objectList)
471 DALI_TEST_EQUALS(ClassWithId::gRefCount, 0, TEST_LOCATION);