d9f0f008bddb5ffd62e82689dc690c5b4da2204e
[platform/upstream/gstreamer.git] / tests / check / libs / queuearray.c
1 /* GStreamer
2  *
3  * unit test for GstQueueArray
4  *
5  * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28 #include <gst/check/gstcheck.h>
29 #include <gst/base/gstqueuearray.h>
30
31 /* Simplest test
32  * Initial size : 10
33  * Add 10, Remove 10
34  */
35 GST_START_TEST (test_array_1)
36 {
37   GstQueueArray *array;
38   guint i;
39
40   /* Create an array of initial size 10 */
41   array = gst_queue_array_new (10);
42
43   /* push 5 values in */
44   for (i = 0; i < 5; i++)
45     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
46
47   fail_unless_equals_int (gst_queue_array_get_length (array), 5);
48
49   /* pull 5 values out */
50   for (i = 0; i < 5; i++) {
51     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
52         i);
53   }
54
55   fail_unless_equals_int (gst_queue_array_get_length (array), 0);
56
57   gst_queue_array_free (array);
58 }
59
60 GST_END_TEST;
61
62 GST_START_TEST (test_array_grow)
63 {
64   GstQueueArray *array;
65   guint i;
66
67   /* Create an array of initial size 10 */
68   array = gst_queue_array_new (10);
69
70   /* push 10 values in */
71   for (i = 0; i < 10; i++)
72     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
73
74   fail_unless_equals_int (gst_queue_array_get_length (array), 10);
75
76
77   /* If we add one value, it will grow */
78   gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
79
80   fail_unless_equals_int (gst_queue_array_get_length (array), 11);
81
82   /* pull the 11 values out */
83   for (i = 0; i < 11; i++) {
84     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
85         i);
86   }
87
88   fail_unless_equals_int (gst_queue_array_get_length (array), 0);
89   gst_queue_array_free (array);
90 }
91
92 GST_END_TEST;
93
94 GST_START_TEST (test_array_grow_multiple)
95 {
96   GstQueueArray *array;
97   guint i;
98
99   /* Create an array of initial size 10 */
100   array = gst_queue_array_new (10);
101
102   /* push 11 values in */
103   for (i = 0; i < 11; i++)
104     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
105
106   /* With 11 values, it should have grown once (15) */
107   fail_unless_equals_int (gst_queue_array_get_length (array), 11);
108
109   for (i = 11; i < 20; i++)
110     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
111
112   /* With 20 total values, it should have grown another time (3 * 15) / 2 = 22) */
113   fail_unless_equals_int (gst_queue_array_get_length (array), 20);
114   /* It did grow beyond initial size */
115
116   /* pull the 20 values out */
117   for (i = 0; i < 20; i++) {
118     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
119         i);
120   }
121
122   fail_unless_equals_int (gst_queue_array_get_length (array), 0);
123   gst_queue_array_free (array);
124 }
125
126 GST_END_TEST;
127
128 GST_START_TEST (test_array_grow_middle)
129 {
130   GstQueueArray *array;
131   guint i;
132
133   /* Create an array of initial size 10 */
134   array = gst_queue_array_new (10);
135
136   /* push/pull 5 values to end up in the middle */
137   for (i = 0; i < 5; i++) {
138     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
139     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
140         i);
141   }
142
143   /* push 10 values in */
144   for (i = 0; i < 10; i++)
145     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
146
147   fail_unless_equals_int (gst_queue_array_get_length (array), 10);
148
149   /* If we add one value, it will grow */
150   gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
151   fail_unless_equals_int (gst_queue_array_get_length (array), 11);
152
153   /* pull the 11 values out */
154   for (i = 0; i < 11; i++) {
155     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
156         i);
157   }
158
159   fail_unless_equals_int (gst_queue_array_get_length (array), 0);
160   gst_queue_array_free (array);
161 }
162
163 GST_END_TEST;
164
165 GST_START_TEST (test_array_grow_end)
166 {
167   GstQueueArray *array;
168   guint i;
169
170   /* Create an array of initial size 10 */
171   array = gst_queue_array_new (10);
172
173   /* push/pull 9 values to end up at the last position */
174   for (i = 0; i < 9; i++) {
175     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
176     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
177         i);
178   }
179
180   /* push 10 values in */
181   for (i = 0; i < 10; i++)
182     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
183
184   fail_unless_equals_int (gst_queue_array_get_length (array), 10);
185
186   /* If we add one value, it will grow */
187   gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
188   fail_unless_equals_int (gst_queue_array_get_length (array), 11);
189
190   /* pull the 11 values out */
191   for (i = 0; i < 11; i++) {
192     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_pop_head (array)),
193         i);
194   }
195
196   fail_unless_equals_int (gst_queue_array_get_length (array), 0);
197   gst_queue_array_free (array);
198 }
199
200 GST_END_TEST;
201
202 static int
203 compare_pointer_value (gconstpointer a, gconstpointer b)
204 {
205   return (int) ((guintptr) a - (guintptr) b);
206 }
207
208 GST_START_TEST (test_array_drop2)
209 {
210 #define NUM_QA_ELEMENTS 674
211   gboolean in_array[NUM_QA_ELEMENTS] = { FALSE, };
212   GstQueueArray *array;
213   guint i, j, count, idx;
214
215   array = gst_queue_array_new (10);
216
217   for (i = 0; i < NUM_QA_ELEMENTS; i++) {
218     gpointer element = GUINT_TO_POINTER (i);
219
220     if (g_random_boolean ()) {
221       gst_queue_array_push_tail (array, element);
222       in_array[i] = TRUE;
223     }
224   }
225
226   for (j = 0, count = 0; j < NUM_QA_ELEMENTS; j++)
227     count += in_array[j] ? 1 : 0;
228   fail_unless_equals_int (gst_queue_array_get_length (array), count);
229
230   while (gst_queue_array_get_length (array) > 0) {
231     for (i = 0; i < NUM_QA_ELEMENTS; i++) {
232       gpointer dropped;
233
234       if (g_random_boolean () && g_random_boolean () && in_array[i]) {
235         idx = gst_queue_array_find (array, compare_pointer_value,
236             GUINT_TO_POINTER (i));
237         dropped = gst_queue_array_drop_element (array, idx);
238         fail_unless_equals_int (i, GPOINTER_TO_INT (dropped));
239         in_array[i] = FALSE;
240       }
241     }
242
243     for (j = 0, count = 0; j < NUM_QA_ELEMENTS; j++)
244       count += in_array[j] ? 1 : 0;
245     fail_unless_equals_int (gst_queue_array_get_length (array), count);
246   }
247
248   gst_queue_array_free (array);
249 }
250
251 GST_END_TEST;
252
253 GST_START_TEST (test_array_grow_from_prealloc1)
254 {
255   GstQueueArray *array;
256
257   array = gst_queue_array_new (1);
258   gst_queue_array_push_tail (array, NULL);
259   gst_queue_array_push_tail (array, NULL);
260   gst_queue_array_free (array);
261 }
262
263 GST_END_TEST;
264
265 GST_START_TEST (test_array_peek_pop_tail)
266 {
267   const guint array_sizes[] = { 0, 1, 2, 5 };
268   guint s;
269
270   for (s = 0; s < G_N_ELEMENTS (array_sizes); ++s) {
271     GstQueueArray *array;
272
273     GST_INFO ("Testing with initial size %u", array_sizes[s]);
274
275     array = gst_queue_array_new (array_sizes[s]);
276     fail_unless_equals_int (gst_queue_array_get_length (array), 0);
277
278     fail_unless (gst_queue_array_peek_tail (array) == NULL);
279     fail_unless (gst_queue_array_pop_tail (array) == NULL);
280
281     gst_queue_array_push_tail (array, GINT_TO_POINTER (42));
282     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
283     fail_unless (gst_queue_array_peek_tail (array) == GINT_TO_POINTER (42));
284     fail_unless (gst_queue_array_peek_head (array) == GINT_TO_POINTER (42));
285     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
286     fail_unless (gst_queue_array_pop_tail (array) == GINT_TO_POINTER (42));
287     fail_unless_equals_int (gst_queue_array_get_length (array), 0);
288
289     gst_queue_array_push_tail (array, GINT_TO_POINTER (42));
290     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
291     fail_unless (gst_queue_array_pop_head (array) == GINT_TO_POINTER (42));
292     fail_unless_equals_int (gst_queue_array_get_length (array), 0);
293     fail_unless (gst_queue_array_peek_tail (array) == NULL);
294     fail_unless (gst_queue_array_pop_tail (array) == NULL);
295
296     gst_queue_array_push_tail (array, GINT_TO_POINTER (43));
297     gst_queue_array_push_tail (array, GINT_TO_POINTER (44));
298
299     fail_unless_equals_int (gst_queue_array_get_length (array), 2);
300     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_head (array)),
301         43);
302     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_tail (array)),
303         44);
304     fail_unless_equals_int (gst_queue_array_get_length (array), 2);
305     fail_unless (gst_queue_array_pop_tail (array) == GINT_TO_POINTER (44));
306     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
307     fail_unless (gst_queue_array_peek_head (array) == GINT_TO_POINTER (43));
308     fail_unless (gst_queue_array_peek_tail (array) == GINT_TO_POINTER (43));
309     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
310
311     gst_queue_array_free (array);
312   }
313 }
314
315 GST_END_TEST;
316
317 static Suite *
318 gst_queue_array_suite (void)
319 {
320   Suite *s = suite_create ("GstQueueArray");
321   TCase *tc_chain = tcase_create ("general");
322
323   suite_add_tcase (s, tc_chain);
324
325   tcase_add_test (tc_chain, test_array_1);
326   tcase_add_test (tc_chain, test_array_grow);
327   tcase_add_test (tc_chain, test_array_grow_multiple);
328   tcase_add_test (tc_chain, test_array_grow_middle);
329   tcase_add_test (tc_chain, test_array_grow_end);
330   tcase_add_test (tc_chain, test_array_drop2);
331   tcase_add_test (tc_chain, test_array_grow_from_prealloc1);
332   tcase_add_test (tc_chain, test_array_peek_pop_tail);
333
334   return s;
335 }
336
337
338 GST_CHECK_MAIN (gst_queue_array);