3 * unit test for GstQueueArray
5 * Copyright (C) <2009> Edward Hervey <bilboed@bilboed.com>
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.
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.
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.
28 #include <gst/check/gstcheck.h>
29 #include <gst/base/gstqueuearray.h>
35 GST_START_TEST (test_array_1)
40 /* Create an array of initial size 10 */
41 array = gst_queue_array_new (10);
43 /* push 5 values in */
44 for (i = 0; i < 5; i++)
45 gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
47 fail_unless_equals_int (gst_queue_array_get_length (array), 5);
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)),
55 fail_unless_equals_int (gst_queue_array_get_length (array), 0);
57 gst_queue_array_free (array);
62 GST_START_TEST (test_array_grow)
67 /* Create an array of initial size 10 */
68 array = gst_queue_array_new (10);
70 /* push 10 values in */
71 for (i = 0; i < 10; i++)
72 gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
74 fail_unless_equals_int (gst_queue_array_get_length (array), 10);
77 /* If we add one value, it will grow */
78 gst_queue_array_push_tail (array, GINT_TO_POINTER (10));
80 fail_unless_equals_int (gst_queue_array_get_length (array), 11);
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)),
88 fail_unless_equals_int (gst_queue_array_get_length (array), 0);
89 gst_queue_array_free (array);
94 GST_START_TEST (test_array_grow_multiple)
99 /* Create an array of initial size 10 */
100 array = gst_queue_array_new (10);
102 /* push 11 values in */
103 for (i = 0; i < 11; i++)
104 gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
106 /* With 11 values, it should have grown once (15) */
107 fail_unless_equals_int (gst_queue_array_get_length (array), 11);
109 for (i = 11; i < 20; i++)
110 gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
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 */
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)),
122 fail_unless_equals_int (gst_queue_array_get_length (array), 0);
123 gst_queue_array_free (array);
128 GST_START_TEST (test_array_grow_middle)
130 GstQueueArray *array;
133 /* Create an array of initial size 10 */
134 array = gst_queue_array_new (10);
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)),
143 /* push 10 values in */
144 for (i = 0; i < 10; i++)
145 gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
147 fail_unless_equals_int (gst_queue_array_get_length (array), 10);
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);
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)),
159 fail_unless_equals_int (gst_queue_array_get_length (array), 0);
160 gst_queue_array_free (array);
165 GST_START_TEST (test_array_grow_end)
167 GstQueueArray *array;
170 /* Create an array of initial size 10 */
171 array = gst_queue_array_new (10);
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)),
180 /* push 10 values in */
181 for (i = 0; i < 10; i++)
182 gst_queue_array_push_tail (array, GINT_TO_POINTER (i));
184 fail_unless_equals_int (gst_queue_array_get_length (array), 10);
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);
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)),
196 fail_unless_equals_int (gst_queue_array_get_length (array), 0);
197 gst_queue_array_free (array);
203 compare_pointer_value (gconstpointer a, gconstpointer b)
205 return (int) ((guintptr) a - (guintptr) b);
208 GST_START_TEST (test_array_drop2)
210 #define NUM_QA_ELEMENTS 674
211 gboolean in_array[NUM_QA_ELEMENTS] = { FALSE, };
212 GstQueueArray *array;
213 guint i, j, count, idx;
215 array = gst_queue_array_new (10);
217 for (i = 0; i < NUM_QA_ELEMENTS; i++) {
218 gpointer element = GUINT_TO_POINTER (i);
220 if (g_random_boolean ()) {
221 gst_queue_array_push_tail (array, element);
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);
230 while (gst_queue_array_get_length (array) > 0) {
231 for (i = 0; i < NUM_QA_ELEMENTS; i++) {
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));
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);
248 gst_queue_array_free (array);
253 GST_START_TEST (test_array_grow_from_prealloc1)
255 GstQueueArray *array;
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);
265 GST_START_TEST (test_array_peek_pop_tail)
267 const guint array_sizes[] = { 0, 1, 2, 5 };
270 for (s = 0; s < G_N_ELEMENTS (array_sizes); ++s) {
271 GstQueueArray *array;
273 GST_INFO ("Testing with initial size %u", array_sizes[s]);
275 array = gst_queue_array_new (array_sizes[s]);
276 fail_unless_equals_int (gst_queue_array_get_length (array), 0);
278 fail_unless (gst_queue_array_peek_tail (array) == NULL);
279 fail_unless (gst_queue_array_pop_tail (array) == NULL);
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);
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);
296 gst_queue_array_push_tail (array, GINT_TO_POINTER (43));
297 gst_queue_array_push_tail (array, GINT_TO_POINTER (44));
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)),
302 fail_unless_equals_int (GPOINTER_TO_INT (gst_queue_array_peek_tail (array)),
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);
311 gst_queue_array_free (array);
318 gst_queue_array_suite (void)
320 Suite *s = suite_create ("GstQueueArray");
321 TCase *tc_chain = tcase_create ("general");
323 suite_add_tcase (s, tc_chain);
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);
338 GST_CHECK_MAIN (gst_queue_array);