Enable assert always in Dali::Vector
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-CircularQueue.cpp
1 /*
2  * Copyright (c) 2021 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
18 #include <dali-test-suite-utils.h>
19 #include <dali/devel-api/common/circular-queue.h>
20 #include <dali/public-api/dali-core.h>
21 #include <stdlib.h>
22
23 #include <iostream>
24
25 using namespace Dali;
26
27 void utc_dali_circular_queue_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_circular_queue_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 int UtcDaliCircularQueueNew(void)
38 {
39   CircularQueue<int> cQ = CircularQueue<int>(20);
40
41   DALI_TEST_EQUALS(cQ.Count(), 0, TEST_LOCATION);
42   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
43   DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
44
45   END_TEST;
46 }
47
48 int UtcDaliCircularQueuePushBack01(void)
49 {
50   CircularQueue<int> cQ = CircularQueue<int>(20);
51
52   cQ.PushBack(1);
53   DALI_TEST_EQUALS(cQ.Count(), 1, TEST_LOCATION);
54   DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
55   DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
56
57   DALI_TEST_EQUALS(cQ[0], 1, TEST_LOCATION);
58
59   cQ.PushBack(2);
60   DALI_TEST_EQUALS(cQ.Count(), 2, TEST_LOCATION);
61   DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
62   DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
63
64   DALI_TEST_EQUALS(cQ[0], 1, TEST_LOCATION);
65   DALI_TEST_EQUALS(cQ[1], 2, TEST_LOCATION);
66
67   END_TEST;
68 }
69
70 int UtcDaliCircularQueuePushBack02(void)
71 {
72   CircularQueue<int> cQ = CircularQueue<int>(20);
73   for(int i = 0; i < 20; ++i)
74   {
75     cQ.PushBack(i);
76     DALI_TEST_EQUALS(cQ.Count(), i + 1, TEST_LOCATION);
77     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
78     DALI_TEST_EQUALS(cQ.IsFull(), i < 19 ? false : true, TEST_LOCATION);
79   }
80
81   END_TEST;
82 }
83
84 int UtcDaliCircularQueuePushBack03(void)
85 {
86   CircularQueue<int> cQ = CircularQueue<int>(20);
87   for(int i = 0; i < 19; ++i)
88   {
89     cQ.PushBack(i);
90   }
91   cQ.PushBack(19);
92   DALI_TEST_EQUALS(cQ.IsFull(), true, TEST_LOCATION);
93
94   for(int i = 0; i < 10; ++i)
95   {
96     tet_infoline("Test that the end marker wraps around");
97     (void)cQ.PopFront();
98     cQ.PushBack(20 + i);
99     DALI_TEST_EQUALS(cQ.IsFull(), true, TEST_LOCATION);
100     DALI_TEST_EQUALS(cQ[0], 1 + i, TEST_LOCATION);
101     DALI_TEST_EQUALS(cQ[19], 20 + i, TEST_LOCATION);
102   }
103
104   END_TEST;
105 }
106
107 int UtcDaliCircularQueuePushBack04(void)
108 {
109   CircularQueue<int> cQ = CircularQueue<int>(20);
110   for(int i = 0; i < 10; ++i)
111   {
112     cQ.PushBack(i);
113     int v = cQ.PopFront();
114     DALI_TEST_EQUALS(v, i, TEST_LOCATION);
115     DALI_TEST_EQUALS(cQ.Count(), 0, TEST_LOCATION);
116   }
117   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
118
119   // Queue is empty
120
121   cQ.PushBack(10);
122   DALI_TEST_EQUALS(cQ[0], 10, TEST_LOCATION);
123   DALI_TEST_EQUALS(cQ.Count(), 1, TEST_LOCATION);
124   (void)cQ.PopFront();
125   DALI_TEST_EQUALS(cQ.Count(), 0, TEST_LOCATION);
126   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
127
128   // Queue is empty, markers should be in middle
129
130   for(int i = 0; i < 20; ++i)
131   {
132     cQ.PushBack(i);
133     int v = cQ.PopFront();
134     DALI_TEST_EQUALS(v, i, TEST_LOCATION);
135     DALI_TEST_EQUALS(cQ.Count(), 0, TEST_LOCATION);
136   }
137
138   END_TEST;
139 }
140
141 int UtcDaliCircularQueuePushBackN(void)
142 {
143   CircularQueue<int> cQ = CircularQueue<int>(20);
144   for(int i = 0; i < 20; ++i)
145   {
146     cQ.PushBack(i);
147     DALI_TEST_EQUALS(cQ.Count(), i + 1, TEST_LOCATION);
148     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
149     DALI_TEST_EQUALS(cQ.IsFull(), i < 19 ? false : true, TEST_LOCATION);
150   }
151
152   try
153   {
154     cQ.PushBack(20);
155     DALI_TEST_EQUALS(0, 1, TEST_LOCATION); // Failure
156   }
157   catch(DaliException e)
158   {
159     DALI_TEST_EQUALS(1, 1, TEST_LOCATION);
160   }
161
162   END_TEST;
163 }
164
165 int UtcDaliCircularQueueOperatorIndex01(void)
166 {
167   CircularQueue<int> cQ = CircularQueue<int>(20);
168   for(int i = 0; i < 20; ++i)
169   {
170     cQ.PushBack(i);
171     DALI_TEST_EQUALS(cQ.Count(), 1 + i, TEST_LOCATION);
172     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
173     DALI_TEST_EQUALS(cQ.IsFull(), i < 19 ? false : true, TEST_LOCATION);
174   }
175
176   for(int i = 0; i < 20; ++i)
177   {
178     DALI_TEST_EQUALS(cQ[i], i, TEST_LOCATION);
179   }
180
181   END_TEST;
182 }
183
184 int UtcDaliCircularQueueOperatorIndexN01(void)
185 {
186   CircularQueue<int> cQ = CircularQueue<int>(20);
187
188   try
189   {
190     int v = cQ[0];
191     DALI_TEST_EQUALS(v, 1, TEST_LOCATION);
192   }
193   catch(DaliException e)
194   {
195     DALI_TEST_CHECK(true);
196   }
197
198   END_TEST;
199 }
200
201 int UtcDaliCircularQueuePopFront01(void)
202 {
203   CircularQueue<int> cQ = CircularQueue<int>(20);
204   for(int i = 0; i < 20; ++i)
205   {
206     cQ.PushBack(i);
207     DALI_TEST_EQUALS(cQ.Count(), 1 + i, TEST_LOCATION);
208     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
209     DALI_TEST_EQUALS(cQ.IsFull(), i < 19 ? false : true, TEST_LOCATION);
210   }
211
212   for(int i = 0; i < 20; ++i)
213   {
214     int v = cQ.PopFront();
215     DALI_TEST_EQUALS(cQ.Count(), 19 - i, TEST_LOCATION);
216     DALI_TEST_EQUALS(v, i, TEST_LOCATION);
217     DALI_TEST_EQUALS(cQ.IsEmpty(), i < 19 ? false : true, TEST_LOCATION);
218   }
219   END_TEST;
220 }
221
222 int UtcDaliCircularQueuePopFront02(void)
223 {
224   CircularQueue<int> cQ = CircularQueue<int>(20);
225   for(int i = 0; i < 10; ++i)
226   {
227     cQ.PushBack(i);
228     DALI_TEST_EQUALS(cQ[i], i, TEST_LOCATION);
229     DALI_TEST_EQUALS(cQ.Count(), i + 1, TEST_LOCATION);
230   }
231
232   for(int i = 0; i < 10; ++i)
233   {
234     DALI_TEST_EQUALS(cQ.PopFront(), i, TEST_LOCATION);
235   }
236   DALI_TEST_EQUALS(cQ.Count(), 0, TEST_LOCATION);
237
238   END_TEST;
239 }
240
241 int UtcDaliCircularQueuePopFrontN01(void)
242 {
243   tet_infoline("Try popping from an empty queue");
244   CircularQueue<int> cQ = CircularQueue<int>(20);
245
246   try
247   {
248     (void)cQ.PopFront();
249     DALI_TEST_CHECK(false);
250   }
251   catch(DaliException e)
252   {
253     DALI_TEST_CHECK(true);
254   }
255
256   END_TEST;
257 }
258
259 int UtcDaliCircularQueuePopFrontN02(void)
260 {
261   tet_infoline("Try popping from an empty queue");
262
263   CircularQueue<int> cQ = CircularQueue<int>(20);
264
265   for(int i = 0; i < 10; ++i)
266   {
267     cQ.PushBack(i);
268     (void)cQ.PopFront();
269   }
270   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
271
272   try
273   {
274     (void)cQ.PopFront();
275     DALI_TEST_CHECK(false);
276   }
277   catch(DaliException e)
278   {
279     DALI_TEST_CHECK(true);
280   }
281
282   END_TEST;
283 }
284
285 int UtcDaliCircularQueueCount(void)
286 {
287   CircularQueue<int> cQ = CircularQueue<int>(20);
288   DALI_TEST_EQUALS(cQ.Count(), 0, TEST_LOCATION);
289
290   for(int i = 0; i < 20; ++i)
291   {
292     cQ.PushBack(i);
293     DALI_TEST_EQUALS(cQ.Count(), 1 + i, TEST_LOCATION);
294   }
295
296   END_TEST;
297 }
298
299 int UtcDaliCircularQueueIsEmpty(void)
300 {
301   CircularQueue<int> cQ = CircularQueue<int>(20);
302   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
303
304   for(int i = 0; i < 20; ++i)
305   {
306     cQ.PushBack(i);
307     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
308   }
309
310   // Pop off 19 elements
311   for(int i = 0; i < 19; ++i)
312   {
313     (void)cQ.PopFront();
314     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
315   }
316   // pop off last element
317   (void)cQ.PopFront();
318   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
319
320   tet_infoline("Add half into queue, then remove");
321
322   for(int i = 0; i < 10; ++i)
323   {
324     cQ.PushBack(i);
325     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
326   }
327   for(int i = 0; i < 9; ++i)
328   {
329     (void)cQ.PopFront();
330     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
331   }
332   (void)cQ.PopFront();
333   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
334
335   tet_infoline("Markers should now be in the middle of the data structure. Try adding 20 again");
336   for(int i = 0; i < 20; ++i)
337   {
338     cQ.PushBack(i);
339     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
340   }
341
342   for(int i = 0; i < 19; ++i)
343   {
344     (void)cQ.PopFront();
345     DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
346   }
347   (void)cQ.PopFront();
348   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
349   END_TEST;
350 }
351
352 int UtcDaliCircularQueueIsFull(void)
353 {
354   CircularQueue<int> cQ = CircularQueue<int>(20);
355   DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
356
357   for(int i = 0; i < 20; ++i)
358   {
359     cQ.PushBack(i);
360     DALI_TEST_EQUALS(cQ.IsFull(), i < 19 ? false : true, TEST_LOCATION);
361   }
362   DALI_TEST_EQUALS(cQ.IsFull(), true, TEST_LOCATION);
363
364   for(int i = 0; i < 20; ++i)
365   {
366     (void)cQ.PopFront();
367     DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
368   }
369
370   tet_infoline("Add half into queue, then remove");
371
372   for(int i = 0; i < 10; ++i)
373   {
374     cQ.PushBack(i);
375     DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
376   }
377   for(int i = 0; i < 10; ++i)
378   {
379     (void)cQ.PopFront();
380     DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
381   }
382
383   tet_infoline("Markers should now be in the middle of the data structure. Try adding 20 again");
384   for(int i = 0; i < 20; ++i)
385   {
386     cQ.PushBack(i);
387     DALI_TEST_EQUALS(cQ.IsFull(), i < 19 ? false : true, TEST_LOCATION);
388   }
389
390   for(int i = 0; i < 20; ++i)
391   {
392     (void)cQ.PopFront();
393     DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
394   }
395
396   END_TEST;
397 }
398
399 int UtcDaliCircularQueueFront(void)
400 {
401   CircularQueue<int> cQ = CircularQueue<int>(20);
402
403   for(int i = 0; i < 20; ++i)
404   {
405     cQ.PushBack(i);
406     DALI_TEST_EQUALS(cQ.Front(), 0, TEST_LOCATION);
407   }
408
409   for(int i = 0; i < 19; ++i)
410   {
411     (void)cQ.PopFront();
412     DALI_TEST_EQUALS(cQ.Front(), i + 1, TEST_LOCATION);
413   }
414   END_TEST;
415 }
416
417 int UtcDaliCircularQueueBack(void)
418 {
419   CircularQueue<int> cQ = CircularQueue<int>(20);
420
421   for(int i = 0; i < 20; ++i)
422   {
423     cQ.PushBack(i);
424     DALI_TEST_EQUALS(cQ.Back(), i, TEST_LOCATION);
425   }
426
427   for(int i = 0; i < 19; ++i)
428   {
429     (void)cQ.PopFront();
430     DALI_TEST_EQUALS(cQ.Back(), 19, TEST_LOCATION);
431   }
432   END_TEST;
433 }
434
435 int UtcDaliCircularQueueSize1(void)
436 {
437   CircularQueue<int> cQ = CircularQueue<int>(1);
438
439   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
440   DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
441
442   cQ.PushBack(5);
443   DALI_TEST_EQUALS(cQ.IsEmpty(), false, TEST_LOCATION);
444   DALI_TEST_EQUALS(cQ.IsFull(), true, TEST_LOCATION);
445   DALI_TEST_EQUALS(cQ.Front(), 5, TEST_LOCATION);
446   DALI_TEST_EQUALS(cQ.Back(), 5, TEST_LOCATION);
447
448   DALI_TEST_EQUALS(cQ.PopFront(), 5, TEST_LOCATION);
449   DALI_TEST_EQUALS(cQ.IsEmpty(), true, TEST_LOCATION);
450   DALI_TEST_EQUALS(cQ.IsFull(), false, TEST_LOCATION);
451
452   END_TEST;
453 }
454
455 // pushback
456 //  .  => [O]
457 //  se     se
458
459 // [O] => [O] [O]
460 //  se     s   e
461
462 // [O] [O] [O] [O] [O] [ ]  => [O] [O] [O] [O] [O] [O]
463 //  s               e           s                   e
464
465 // [ ] [O] [O] [O] [O] [O]  => [O] [O] [O] [O] [O] [O]
466 //      s               e       e   s
467
468 // [ ] [ ] [O] [ ] [ ] [ ]  => [ ] [ ] [O] [O] [ ] [ ]
469 //          se                          s   e
470
471 // [ ] [ ] [ ] [ ] [ ] [O]  => [O] [ ] [ ] [ ] [ ] [O]
472 //                      se      e                   s
473
474 // [ ] [ ] [ ] [ ] [ ] [ ]  => [ ] [ ] [O] [ ] [ ] [ ]
475 //          se                          se
476
477 // [ ] [ ] [ ] [ ] [ ] [ ]  => [ ] [ ] [ ] [ ] [ ] [0]
478 //                      se                          se
479 // popfront
480 // [O] [O] [O] [O] [O] [O]  => [ ] [O] [O] [O] [O] [O]
481 //  s                   e           s               e
482
483 // [O] [O] [O] [O] [O] [O]  => [O] [O] [O] [O] [ ] [O]
484 //              e   s                       e       s
485
486 // [O] [O] [O] [O] [O] [O]  => [O] [O] [O] [O] [O] [ ]
487 //                  e   s       s               e
488
489 // [ ] [ ] [O] [O] [ ] [ ]  => [ ] [ ] [ ] [O] [ ] [ ]
490 //          s   e                           se
491
492 // [ ] [ ] [ ] [O] [ ] [ ]  => [ ] [ ] [ ] [ ] [ ] [ ]
493 //              se                          se