2 * Copyright (c) 2024 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/integration-api/ordered-set.h>
26 using namespace Dali::Integration;
28 void utc_dali_internal_ordered_set_startup(void)
30 test_return_value = TET_UNDEF;
33 void utc_dali_internal_ordered_set_cleanup(void)
35 test_return_value = TET_PASS;
38 ///////////////////////////////////////////////////////////////////////////////
41 /// Takes in a reference to a bool which is set to true when the destructor is called
45 OwnedClass(bool& destructorCalled)
46 : mDestructorCalled(destructorCalled)
48 mDestructorCalled = false;
53 mDestructorCalled = true;
57 bool& mDestructorCalled;
60 /// Just a simple class with a member value and global refcount.
77 int ClassWithId::gRefCount = 0;
81 ///////////////////////////////////////////////////////////////////////////////
83 int UtcDaliOrderedSetEnsureDeletion(void)
85 // Ensure that the object owned by the OrderedSet is deleted.
90 OrderedSet<OwnedClass> set;
91 set.PushBack(new OwnedClass(deleted));
92 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
95 // OrderedSet out-of-scope, object should be deleted.
97 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
102 int UtcDaliOrderedSetFalseEnsureNotDeletionWhen(void)
104 // Ensure that the object owned by the OrderedSET is deleted.
106 bool deleted = false;
111 // Create OrderedSet without ownership. It will not delete automatically.
112 OrderedSet<OwnedClass, false> set;
113 ptr = new OwnedClass(deleted);
115 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
118 // OrderedSet out-of-scope, but, object should not be deleted.
120 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
123 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
128 int UtcDaliOrderedSetDefaultConstructor(void)
130 // Ensure the default constructor is created as expected.
132 OrderedSet<OwnedClass> set;
133 DALI_TEST_CHECK(set.Count() == 0u);
138 int UtcDaliOrderedSetFalseDefaultConstructor(void)
140 // Ensure the default constructor is created as expected.
142 OrderedSet<OwnedClass, false> set;
143 DALI_TEST_CHECK(set.Count() == 0u);
148 int UtcDaliOrderedSetReserve(void)
150 OrderedSet<OwnedClass> set;
153 // Reserve function didn't change the count.
154 DALI_TEST_CHECK(set.Count() == 0u);
156 OrderedSet<OwnedClass> set2;
159 // Reserve function didn't change the count.
160 DALI_TEST_CHECK(set2.Count() == 0u);
165 int UtcDaliOrderedSetMove(void)
167 // Call move constructor and move assignment operator
169 bool deleted = false;
170 OwnedClass* owned = new OwnedClass(deleted);
172 OrderedSet<OwnedClass> first;
173 DALI_TEST_CHECK(first.Find(owned) == first.End());
174 first.PushBack(owned);
175 DALI_TEST_CHECK(first.Find(owned) != first.End());
178 // Move constructor, first should have a nullptr now, no object deletion
179 OrderedSet<OwnedClass> second(std::move(first));
180 DALI_TEST_CHECK(first.Find(owned) == first.End());
181 DALI_TEST_CHECK(second.Find(owned) != second.End());
182 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
184 // Assign second to first, no deletion, second should have a nullptr now
185 first = std::move(second);
186 DALI_TEST_CHECK(first.Find(owned) != first.End());
187 DALI_TEST_CHECK(second.Find(owned) == second.End());
188 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
191 // second is out-of-scope now, no object deletion
192 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
194 // Assign to an empty pointer, owned object should be deleted
195 OrderedSet<OwnedClass> empty;
196 first = std::move(empty);
197 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
198 DALI_TEST_CHECK(first.Find(owned) == first.End());
199 DALI_TEST_CHECK(empty.Find(owned) == empty.End());
204 int UtcDaliOrderedSetFalseMove(void)
206 // Call move constructor and move assignment operator
208 bool deleted = false;
209 OwnedClass* owned = new OwnedClass(deleted);
211 OrderedSet<OwnedClass, false> first;
212 DALI_TEST_CHECK(first.Find(owned) == first.End());
213 first.PushBack(owned);
214 DALI_TEST_CHECK(first.Find(owned) != first.End());
217 // Move constructor, first should have a nullptr now, no object deletion
218 OrderedSet<OwnedClass, false> second(std::move(first));
219 DALI_TEST_CHECK(first.Find(owned) == first.End());
220 DALI_TEST_CHECK(second.Find(owned) != second.End());
221 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
223 // Assign second to first, no deletion, second should have a nullptr now
224 first = std::move(second);
225 DALI_TEST_CHECK(first.Find(owned) != first.End());
226 DALI_TEST_CHECK(second.Find(owned) == second.End());
227 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
230 // second is out-of-scope now, no object deletion
231 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
233 // Assign to an empty pointer, but owned object should not be deleted
234 OrderedSet<OwnedClass, false> empty;
235 first = std::move(empty);
236 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
237 DALI_TEST_CHECK(first.Find(owned) == first.End());
238 DALI_TEST_CHECK(empty.Find(owned) == empty.End());
241 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
246 int UtcDaliOrderedSetErase(void)
248 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
250 bool deleted = false;
251 OwnedClass* owned = new OwnedClass(deleted);
253 OrderedSet<OwnedClass> set;
255 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
256 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
257 set.EraseObject(owned);
258 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
263 int UtcDaliOrderedSetFalseErase(void)
265 // Ensure that calling Reset not deletes the object and sets the owner-pointer to NULL
267 bool deleted = false;
268 OwnedClass* owned = new OwnedClass(deleted);
270 OrderedSet<OwnedClass, false> set;
272 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
273 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
274 set.EraseObject(owned);
275 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
278 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
283 int UtcDaliOrderedSetClear(void)
285 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
287 bool deleted = false;
288 OwnedClass* owned = new OwnedClass(deleted);
290 OrderedSet<OwnedClass> set;
292 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
293 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
296 DALI_TEST_EQUALS(set.Count(), 0u, TEST_LOCATION);
297 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
302 int UtcDaliOrderedSetFalseClear(void)
304 // Ensure that calling Reset deletes the object and sets the owner-pointer to NULL
306 bool deleted = false;
307 OwnedClass* owned = new OwnedClass(deleted);
309 OrderedSet<OwnedClass, false> set;
311 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
312 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
315 DALI_TEST_EQUALS(set.Count(), 0u, TEST_LOCATION);
316 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
319 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
324 int UtcDaliOrderedSetRelease(void)
326 // Ensure that calling Release does NOT delete the object but still sets the owner-pointer to NULL
328 bool deleted = false;
329 OwnedClass* owned = new OwnedClass(deleted);
331 OrderedSet<OwnedClass> set;
333 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
335 auto iter = set.Find(owned);
336 DALI_TEST_CHECK(iter != set.End());
337 DALI_TEST_EQUALS(set.Count(), 1u, TEST_LOCATION);
339 OwnedClass* released = set.Release(iter);
340 DALI_TEST_EQUALS(deleted, false, TEST_LOCATION);
341 DALI_TEST_CHECK(set.Find(owned) == set.End());
342 DALI_TEST_CHECK(set.Find(released) == set.End());
343 DALI_TEST_EQUALS(set.Count(), 0u, TEST_LOCATION);
346 DALI_TEST_EQUALS(deleted, true, TEST_LOCATION);
351 int UtcDaliOrderedSetIteratorOrderCheck(void)
353 // Ensure that order of iterator is equal with Order of Insertion.
355 // Reset refcount of class
356 ClassWithId::gRefCount = 0;
358 // To avoid lucky pass, run this test multiple time.
362 int baseId = tryCnt; // random id
364 int n = 10 + 5 * (tryCnt + 1); // random count
366 OrderedSet<ClassWithId> set;
368 for(int i = 0; i < n; ++i)
370 ClassWithId* object = new ClassWithId(id++);
371 set.PushBack(object);
375 // Check by for iteration
377 for(auto iter = set.Begin(), iterEnd = set.End(); iter != iterEnd; ++iter)
379 DALI_TEST_EQUALS(expectId++, (*iter)->mId, TEST_LOCATION);
381 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
383 // Check by range referenced iteration
385 for(auto&& iter : set)
387 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
388 // Change iter's value.
391 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
393 // Check by range iteration. Note that class value changed.
394 expectId = baseId + 1;
395 for(const auto& iter : set)
397 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
399 DALI_TEST_EQUALS(expectId, id + 1, TEST_LOCATION);
402 // Check whether leak exist.
403 DALI_TEST_EQUALS(ClassWithId::gRefCount, 0, TEST_LOCATION);
408 int UtcDaliOrderedSetFalseIteratorOrderCheck(void)
410 // Ensure that order of iterator is equal with Order of Insertion.
412 // Reset refcount of class
413 ClassWithId::gRefCount = 0;
415 // Container of ownered class, to release memories after test finished.
416 std::vector<ClassWithId*> objectList;
418 // To avoid lucky pass, run this test multiple time.
422 int baseId = tryCnt; // random id
424 int n = 10 + 5 * (tryCnt + 1); // random count
426 OrderedSet<ClassWithId, false> set;
428 for(int i = 0; i < n; ++i)
430 ClassWithId* object = new ClassWithId(id++);
431 objectList.push_back(object);
432 set.PushBack(object);
436 // Check by for iteration
438 for(auto iter = set.Begin(), iterEnd = set.End(); iter != iterEnd; ++iter)
440 DALI_TEST_EQUALS(expectId++, (*iter)->mId, TEST_LOCATION);
442 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
444 // Check by range referenced iteration
446 for(auto&& iter : set)
448 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
449 // Change iter's value.
452 DALI_TEST_EQUALS(expectId, id, TEST_LOCATION);
454 // Check by range iteration. Note that class value changed.
455 expectId = baseId + 1;
456 for(const auto& iter : set)
458 DALI_TEST_EQUALS(expectId++, iter->mId, TEST_LOCATION);
460 DALI_TEST_EQUALS(expectId, id + 1, TEST_LOCATION);
463 // Check whether leak exist.
464 DALI_TEST_EQUALS(ClassWithId::gRefCount, objectList.size(), TEST_LOCATION);
466 // Release memory now.
467 for(auto&& iter : objectList)
472 DALI_TEST_EQUALS(ClassWithId::gRefCount, 0, TEST_LOCATION);