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