• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
GST_START_TEST(test_array_1)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 
GST_START_TEST(test_array_grow)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 
GST_START_TEST(test_array_grow_multiple)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 
GST_START_TEST(test_array_grow_middle)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 
GST_START_TEST(test_array_grow_end)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
compare_pointer_value(gconstpointer a,gconstpointer b)203 compare_pointer_value (gconstpointer a, gconstpointer b)
204 {
205   return (int) ((guintptr) a - (guintptr) b);
206 }
207 
GST_START_TEST(test_array_drop2)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 
GST_START_TEST(test_array_grow_from_prealloc1)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;
GST_START_TEST(test_array_peek_nth)264 GST_START_TEST (test_array_peek_nth)
265 {
266   GstQueueArray *array;
267   guint i;
268 
269   /* Create an array of initial size 10 */
270   array = gst_queue_array_new (10);
271 
272   /* push 10 values in */
273   for (i = 0; i < 10; i++)
274     gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
275 
276   for (i = 0; i < 10; i++)
277     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_nth (array,
278                 i)), i);
279 
280   gst_queue_array_pop_head (array);
281 
282   for (i = 0; i < 9; i++)
283     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_nth (array,
284                 i)), i + 1);
285 
286   gst_queue_array_free (array);
287 }
288 
289 GST_END_TEST;
290 
291 
GST_START_TEST(test_array_peek_pop_tail)292 GST_START_TEST (test_array_peek_pop_tail)
293 {
294   const guint array_sizes[] = { 0, 1, 2, 5 };
295   guint s;
296 
297   for (s = 0; s < G_N_ELEMENTS (array_sizes); ++s) {
298     GstQueueArray *array;
299 
300     GST_INFO ("Testing with initial size %u", array_sizes[s]);
301 
302     array = gst_queue_array_new (array_sizes[s]);
303     fail_unless_equals_int (gst_queue_array_get_length (array), 0);
304 
305     fail_unless (gst_queue_array_peek_tail (array) == NULL);
306     fail_unless (gst_queue_array_pop_tail (array) == NULL);
307 
308     gst_queue_array_push_tail (array, GINT_TO_POINTER (42));
309     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
310     fail_unless (gst_queue_array_peek_tail (array) == GINT_TO_POINTER (42));
311     fail_unless (gst_queue_array_peek_head (array) == GINT_TO_POINTER (42));
312     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
313     fail_unless (gst_queue_array_pop_tail (array) == GINT_TO_POINTER (42));
314     fail_unless_equals_int (gst_queue_array_get_length (array), 0);
315 
316     gst_queue_array_push_tail (array, GINT_TO_POINTER (42));
317     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
318     fail_unless (gst_queue_array_pop_head (array) == GINT_TO_POINTER (42));
319     fail_unless_equals_int (gst_queue_array_get_length (array), 0);
320     fail_unless (gst_queue_array_peek_tail (array) == NULL);
321     fail_unless (gst_queue_array_pop_tail (array) == NULL);
322 
323     gst_queue_array_push_tail (array, GINT_TO_POINTER (43));
324     gst_queue_array_push_tail (array, GINT_TO_POINTER (44));
325 
326     fail_unless_equals_int (gst_queue_array_get_length (array), 2);
327     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_head (array)),
328         43);
329     fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_tail (array)),
330         44);
331     fail_unless_equals_int (gst_queue_array_get_length (array), 2);
332     fail_unless (gst_queue_array_pop_tail (array) == GINT_TO_POINTER (44));
333     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
334     fail_unless (gst_queue_array_peek_head (array) == GINT_TO_POINTER (43));
335     fail_unless (gst_queue_array_peek_tail (array) == GINT_TO_POINTER (43));
336     fail_unless_equals_int (gst_queue_array_get_length (array), 1);
337 
338     gst_queue_array_free (array);
339   }
340 }
341 
342 GST_END_TEST;
343 
344 static Suite *
gst_queue_array_suite(void)345 gst_queue_array_suite (void)
346 {
347   Suite *s = suite_create ("GstQueueArray");
348   TCase *tc_chain = tcase_create ("general");
349 
350   suite_add_tcase (s, tc_chain);
351 
352   tcase_add_test (tc_chain, test_array_1);
353   tcase_add_test (tc_chain, test_array_grow);
354   tcase_add_test (tc_chain, test_array_grow_multiple);
355   tcase_add_test (tc_chain, test_array_grow_middle);
356   tcase_add_test (tc_chain, test_array_grow_end);
357   tcase_add_test (tc_chain, test_array_drop2);
358   tcase_add_test (tc_chain, test_array_grow_from_prealloc1);
359   tcase_add_test (tc_chain, test_array_peek_pop_tail);
360   tcase_add_test (tc_chain, test_array_peek_nth);
361 
362   return s;
363 }
364 
365 
366 GST_CHECK_MAIN (gst_queue_array);
367